1 /*
2 * Certificate request reading application
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf printf
59 #define mbedtls_exit exit
60 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
61 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62 #endif /* MBEDTLS_PLATFORM_C */
63
64 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
65 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)66 int main( void )
67 {
68 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
69 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
70 mbedtls_exit( 0 );
71 }
72 #else
73
74 #include "mbedtls/x509_csr.h"
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79
80 #define DFL_FILENAME "cert.req"
81 #define DFL_DEBUG_LEVEL 0
82
83 #define USAGE \
84 "\n usage: req_app param=<>...\n" \
85 "\n acceptable parameters:\n" \
86 " filename=%%s default: cert.req\n" \
87 "\n"
88
89
90 /*
91 * global options
92 */
93 struct options
94 {
95 const char *filename; /* filename of the certificate request */
96 } opt;
97
main(int argc,char * argv[])98 int main( int argc, char *argv[] )
99 {
100 int ret = 1;
101 int exit_code = MBEDTLS_EXIT_FAILURE;
102 unsigned char buf[100000];
103 mbedtls_x509_csr csr;
104 int i;
105 char *p, *q;
106
107 /*
108 * Set to sane values
109 */
110 mbedtls_x509_csr_init( &csr );
111
112 if( argc == 0 )
113 {
114 usage:
115 mbedtls_printf( USAGE );
116 goto exit;
117 }
118
119 opt.filename = DFL_FILENAME;
120
121 for( i = 1; i < argc; i++ )
122 {
123 p = argv[i];
124 if( ( q = strchr( p, '=' ) ) == NULL )
125 goto usage;
126 *q++ = '\0';
127
128 if( strcmp( p, "filename" ) == 0 )
129 opt.filename = q;
130 else
131 goto usage;
132 }
133
134 /*
135 * 1.1. Load the CSR
136 */
137 mbedtls_printf( "\n . Loading the CSR ..." );
138 fflush( stdout );
139
140 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
141
142 if( ret != 0 )
143 {
144 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
145 mbedtls_x509_csr_free( &csr );
146 goto exit;
147 }
148
149 mbedtls_printf( " ok\n" );
150
151 /*
152 * 1.2 Print the CSR
153 */
154 mbedtls_printf( " . CSR information ...\n" );
155 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
156 if( ret == -1 )
157 {
158 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
159 mbedtls_x509_csr_free( &csr );
160 goto exit;
161 }
162
163 mbedtls_printf( "%s\n", buf );
164
165 exit_code = MBEDTLS_EXIT_SUCCESS;
166
167 exit:
168 mbedtls_x509_csr_free( &csr );
169
170 #if defined(_WIN32)
171 mbedtls_printf( " + Press Enter to exit this program.\n" );
172 fflush( stdout ); getchar();
173 #endif
174
175 mbedtls_exit( exit_code );
176 }
177 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
178 MBEDTLS_FS_IO */
179