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