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