req_app.c 3.7 KB

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