req_app.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Certificate request reading application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_exit exit
  31. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  32. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  33. #endif /* MBEDTLS_PLATFORM_C */
  34. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  35. !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
  36. int main( void )
  37. {
  38. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  39. "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
  40. mbedtls_exit( 0 );
  41. }
  42. #else
  43. #include "mbedtls/x509_csr.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #define DFL_FILENAME "cert.req"
  48. #define DFL_DEBUG_LEVEL 0
  49. #define USAGE \
  50. "\n usage: req_app param=<>...\n" \
  51. "\n acceptable parameters:\n" \
  52. " filename=%%s default: cert.req\n" \
  53. "\n"
  54. /*
  55. * global options
  56. */
  57. struct options
  58. {
  59. const char *filename; /* filename of the certificate request */
  60. } opt;
  61. int main( int argc, char *argv[] )
  62. {
  63. int ret = 1;
  64. int exit_code = MBEDTLS_EXIT_FAILURE;
  65. unsigned char buf[100000];
  66. mbedtls_x509_csr csr;
  67. int i;
  68. char *p, *q;
  69. /*
  70. * Set to sane values
  71. */
  72. mbedtls_x509_csr_init( &csr );
  73. if( argc == 0 )
  74. {
  75. usage:
  76. mbedtls_printf( USAGE );
  77. goto exit;
  78. }
  79. opt.filename = DFL_FILENAME;
  80. for( i = 1; i < argc; i++ )
  81. {
  82. p = argv[i];
  83. if( ( q = strchr( p, '=' ) ) == NULL )
  84. goto usage;
  85. *q++ = '\0';
  86. if( strcmp( p, "filename" ) == 0 )
  87. opt.filename = q;
  88. else
  89. goto usage;
  90. }
  91. /*
  92. * 1.1. Load the CSR
  93. */
  94. mbedtls_printf( "\n . Loading the CSR ..." );
  95. fflush( stdout );
  96. ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
  97. if( ret != 0 )
  98. {
  99. mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
  100. mbedtls_x509_csr_free( &csr );
  101. goto exit;
  102. }
  103. mbedtls_printf( " ok\n" );
  104. /*
  105. * 1.2 Print the CSR
  106. */
  107. mbedtls_printf( " . CSR information ...\n" );
  108. ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
  109. if( ret == -1 )
  110. {
  111. mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
  112. mbedtls_x509_csr_free( &csr );
  113. goto exit;
  114. }
  115. mbedtls_printf( "%s\n", buf );
  116. exit_code = MBEDTLS_EXIT_SUCCESS;
  117. exit:
  118. mbedtls_x509_csr_free( &csr );
  119. #if defined(_WIN32)
  120. mbedtls_printf( " + Press Enter to exit this program.\n" );
  121. fflush( stdout ); getchar();
  122. #endif
  123. mbedtls_exit( exit_code );
  124. }
  125. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
  126. MBEDTLS_FS_IO */