req_app.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define mbedtls_printf printf
  31. #endif
  32. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  33. !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
  34. int main( void )
  35. {
  36. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  37. "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
  38. return( 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 = 0;
  62. unsigned char buf[100000];
  63. mbedtls_x509_csr csr;
  64. int i;
  65. char *p, *q;
  66. /*
  67. * Set to sane values
  68. */
  69. mbedtls_x509_csr_init( &csr );
  70. if( argc == 0 )
  71. {
  72. usage:
  73. mbedtls_printf( USAGE );
  74. goto exit;
  75. }
  76. opt.filename = DFL_FILENAME;
  77. for( i = 1; i < argc; i++ )
  78. {
  79. p = argv[i];
  80. if( ( q = strchr( p, '=' ) ) == NULL )
  81. goto usage;
  82. *q++ = '\0';
  83. if( strcmp( p, "filename" ) == 0 )
  84. opt.filename = q;
  85. else
  86. goto usage;
  87. }
  88. /*
  89. * 1.1. Load the CSR
  90. */
  91. mbedtls_printf( "\n . Loading the CSR ..." );
  92. fflush( stdout );
  93. ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
  94. if( ret != 0 )
  95. {
  96. mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
  97. mbedtls_x509_csr_free( &csr );
  98. goto exit;
  99. }
  100. mbedtls_printf( " ok\n" );
  101. /*
  102. * 1.2 Print the CSR
  103. */
  104. mbedtls_printf( " . CSR information ...\n" );
  105. ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
  106. if( ret == -1 )
  107. {
  108. mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
  109. mbedtls_x509_csr_free( &csr );
  110. goto exit;
  111. }
  112. mbedtls_printf( "%s\n", buf );
  113. exit:
  114. mbedtls_x509_csr_free( &csr );
  115. #if defined(_WIN32)
  116. mbedtls_printf( " + Press Enter to exit this program.\n" );
  117. fflush( stdout ); getchar();
  118. #endif
  119. return( ret );
  120. }
  121. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
  122. MBEDTLS_FS_IO */