req_app.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "mbedtls/build_info.h"
  20. #if defined(MBEDTLS_PLATFORM_C)
  21. #include "mbedtls/platform.h"
  22. #else
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define mbedtls_printf printf
  26. #define mbedtls_exit exit
  27. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  28. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  29. #endif /* MBEDTLS_PLATFORM_C */
  30. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  31. !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  32. defined(MBEDTLS_X509_REMOVE_INFO)
  33. int main( void )
  34. {
  35. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  36. "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
  37. "MBEDTLS_X509_REMOVE_INFO defined.\n");
  38. mbedtls_exit( 0 );
  39. }
  40. #else
  41. #include "mbedtls/x509_csr.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #define DFL_FILENAME "cert.req"
  46. #define DFL_DEBUG_LEVEL 0
  47. #define USAGE \
  48. "\n usage: req_app param=<>...\n" \
  49. "\n acceptable parameters:\n" \
  50. " filename=%%s default: cert.req\n" \
  51. "\n"
  52. /*
  53. * global options
  54. */
  55. struct options
  56. {
  57. const char *filename; /* filename of the certificate request */
  58. } opt;
  59. int main( int argc, char *argv[] )
  60. {
  61. int ret = 1;
  62. int exit_code = MBEDTLS_EXIT_FAILURE;
  63. unsigned char buf[100000];
  64. mbedtls_x509_csr csr;
  65. int i;
  66. char *p, *q;
  67. /*
  68. * Set to sane values
  69. */
  70. mbedtls_x509_csr_init( &csr );
  71. if( argc == 0 )
  72. {
  73. usage:
  74. mbedtls_printf( USAGE );
  75. goto exit;
  76. }
  77. opt.filename = DFL_FILENAME;
  78. for( i = 1; i < argc; i++ )
  79. {
  80. p = argv[i];
  81. if( ( q = strchr( p, '=' ) ) == NULL )
  82. goto usage;
  83. *q++ = '\0';
  84. if( strcmp( p, "filename" ) == 0 )
  85. opt.filename = q;
  86. else
  87. goto usage;
  88. }
  89. /*
  90. * 1.1. Load the CSR
  91. */
  92. mbedtls_printf( "\n . Loading the CSR ..." );
  93. fflush( stdout );
  94. ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
  95. if( ret != 0 )
  96. {
  97. mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
  98. mbedtls_x509_csr_free( &csr );
  99. goto exit;
  100. }
  101. mbedtls_printf( " ok\n" );
  102. /*
  103. * 1.2 Print the CSR
  104. */
  105. mbedtls_printf( " . CSR information ...\n" );
  106. ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
  107. if( ret == -1 )
  108. {
  109. mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
  110. mbedtls_x509_csr_free( &csr );
  111. goto exit;
  112. }
  113. mbedtls_printf( "%s\n", buf );
  114. exit_code = MBEDTLS_EXIT_SUCCESS;
  115. exit:
  116. mbedtls_x509_csr_free( &csr );
  117. #if defined(_WIN32)
  118. mbedtls_printf( " + Press Enter to exit this program.\n" );
  119. fflush( stdout ); getchar();
  120. #endif
  121. mbedtls_exit( exit_code );
  122. }
  123. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
  124. MBEDTLS_FS_IO */