blob: 027050c07f225e0f9e3c38b87a138cb847035d7b [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Certificate request generation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
Simon Butcher203a6932016-10-07 15:00:17 +010041 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
42 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020043int main( void )
44{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_printf( "MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
47 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020048 "not defined.\n");
49 return( 0 );
50}
51#else
52
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/x509_csr.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000057
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000061
62#define DFL_FILENAME "keyfile.key"
Hanno Becker56e84632018-11-01 14:10:23 +000063#define DFL_PASSWORD NULL
Rich Evans18b78c72015-02-11 14:06:19 +000064#define DFL_DEBUG_LEVEL 0
65#define DFL_OUTPUT_FILENAME "cert.req"
66#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
67#define DFL_KEY_USAGE 0
68#define DFL_NS_CERT_TYPE 0
Hanno Beckerf7457332018-10-08 17:14:42 +010069#define DFL_MD_ALG MBEDTLS_MD_SHA256
Rich Evans18b78c72015-02-11 14:06:19 +000070
71#define USAGE \
72 "\n usage: cert_req param=<>...\n" \
73 "\n acceptable parameters:\n" \
74 " filename=%%s default: keyfile.key\n" \
Hanno Becker56e84632018-11-01 14:10:23 +000075 " password=%%s default: NULL\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000076 " debug_level=%%d default: 0 (disabled)\n" \
77 " output_file=%%s default: cert.req\n" \
78 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
79 " key_usage=%%s default: (empty)\n" \
80 " Comma-separated-list of values:\n" \
81 " digital_signature\n" \
82 " non_repudiation\n" \
83 " key_encipherment\n" \
84 " data_encipherment\n" \
85 " key_agreement\n" \
Jonathan Leroy81962c32015-10-10 21:42:29 +020086 " key_cert_sign\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000087 " crl_sign\n" \
88 " ns_cert_type=%%s default: (empty)\n" \
89 " Comma-separated-list of values:\n" \
90 " ssl_client\n" \
91 " ssl_server\n" \
92 " email\n" \
93 " object_signing\n" \
94 " ssl_ca\n" \
95 " email_ca\n" \
96 " object_signing_ca\n" \
Hanno Beckerf7457332018-10-08 17:14:42 +010097 " md=%%s default: SHA256\n" \
98 " possible values:\n" \
99 " MD4, MD5, SHA1\n" \
100 " SHA224, SHA256\n" \
101 " SHA384, SHA512\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000102 "\n"
103
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100104#if defined(MBEDTLS_CHECK_PARAMS)
105void mbedtls_param_failed( const char *failure_condition,
106 const char *file,
107 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000108{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100109 mbedtls_printf( "%s:%i: Input param failed - %s\n",
110 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000111 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
112}
113#endif
114
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115/*
116 * global options
117 */
118struct options
119{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100120 const char *filename; /* filename of the key file */
Hanno Becker56e84632018-11-01 14:10:23 +0000121 const char *password; /* password for the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122 int debug_level; /* level of debugging */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100123 const char *output_file; /* where to store the constructed key file */
124 const char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +0200125 unsigned char key_usage; /* key usage flags */
126 unsigned char ns_cert_type; /* NS cert type */
Hanno Beckerf7457332018-10-08 17:14:42 +0100127 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128} opt;
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200131 int (*f_rng)(void *, unsigned char *, size_t),
132 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133{
Paul Bakker135f1e92013-08-26 16:54:13 +0200134 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135 FILE *f;
136 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +0200137 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138
Paul Bakker135f1e92013-08-26 16:54:13 +0200139 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200141 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142
Paul Bakker135f1e92013-08-26 16:54:13 +0200143 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
Paul Bakker8eabfc12013-08-25 10:18:25 +0200145 if( ( f = fopen( output_file, "w" ) ) == NULL )
146 return( -1 );
147
Paul Bakker135f1e92013-08-26 16:54:13 +0200148 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200149 {
150 fclose( f );
Paul Bakker135f1e92013-08-26 16:54:13 +0200151 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200152 }
Paul Bakker135f1e92013-08-26 16:54:13 +0200153
Paul Bakker0c226102014-04-17 16:02:36 +0200154 fclose( f );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200155
156 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157}
158
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159int main( int argc, char *argv[] )
160{
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100161 int ret = 1;
162 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100165 int i;
Paul Bakker57be6e22013-08-26 14:13:14 +0200166 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_x509write_csr req;
168 mbedtls_entropy_context entropy;
169 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200170 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171
172 /*
173 * Set to sane values
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_x509write_csr_init( &req );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200177 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200178 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
180 if( argc == 0 )
181 {
182 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 goto exit;
185 }
186
187 opt.filename = DFL_FILENAME;
Hanno Becker56e84632018-11-01 14:10:23 +0000188 opt.password = DFL_PASSWORD;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189 opt.debug_level = DFL_DEBUG_LEVEL;
190 opt.output_file = DFL_OUTPUT_FILENAME;
191 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200192 opt.key_usage = DFL_KEY_USAGE;
193 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Beckerf7457332018-10-08 17:14:42 +0100194 opt.md_alg = DFL_MD_ALG;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195
196 for( i = 1; i < argc; i++ )
197 {
198
199 p = argv[i];
200 if( ( q = strchr( p, '=' ) ) == NULL )
201 goto usage;
202 *q++ = '\0';
203
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204 if( strcmp( p, "filename" ) == 0 )
205 opt.filename = q;
Hanno Becker56e84632018-11-01 14:10:23 +0000206 else if( strcmp( p, "password" ) == 0 )
207 opt.password = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208 else if( strcmp( p, "output_file" ) == 0 )
209 opt.output_file = q;
210 else if( strcmp( p, "debug_level" ) == 0 )
211 {
212 opt.debug_level = atoi( q );
213 if( opt.debug_level < 0 || opt.debug_level > 65535 )
214 goto usage;
215 }
216 else if( strcmp( p, "subject_name" ) == 0 )
217 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218 opt.subject_name = q;
219 }
Hanno Beckerf7457332018-10-08 17:14:42 +0100220 else if( strcmp( p, "md" ) == 0 )
221 {
222 if( strcmp( q, "SHA256" ) == 0 )
223 {
224 opt.md_alg = MBEDTLS_MD_SHA256;
225 }
226 else if( strcmp( q, "SHA224" ) == 0 )
227 {
228 opt.md_alg = MBEDTLS_MD_SHA224;
229 }
230 else
231#if defined(MBEDTLS_MD5_C)
232 if( strcmp( q, "MD5" ) == 0 )
233 {
234 opt.md_alg = MBEDTLS_MD_MD5;
235 }
236 else
237#endif /* MBEDTLS_MD5_C */
238#if defined(MBEDTLS_MD4_C)
239 if( strcmp( q, "MD4" ) == 0 )
240 {
241 opt.md_alg = MBEDTLS_MD_MD4;
242 }
243 else
244#endif /* MBEDTLS_MD5_C */
245#if defined(MBEDTLS_SHA1_C)
246 if( strcmp( q, "SHA1" ) == 0 )
247 {
248 opt.md_alg = MBEDTLS_MD_SHA1;
249 }
250 else
251#endif /* MBEDTLS_SHA1_C */
252#if defined(MBEDTLS_SHA512_C)
253 if( strcmp( q, "SHA384" ) == 0 )
254 {
255 opt.md_alg = MBEDTLS_MD_SHA384;
256 }
257 else
258 if( strcmp( q, "SHA512" ) == 0 )
259 {
260 opt.md_alg = MBEDTLS_MD_SHA512;
261 }
262 else
263#endif /* MBEDTLS_SHA512_C */
264 {
265 goto usage;
266 }
267 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200268 else if( strcmp( p, "key_usage" ) == 0 )
269 {
270 while( q != NULL )
271 {
272 if( ( r = strchr( q, ',' ) ) != NULL )
273 *r++ = '\0';
274
275 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker57be6e22013-08-26 14:13:14 +0200277 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker57be6e22013-08-26 14:13:14 +0200279 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100280 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker57be6e22013-08-26 14:13:14 +0200281 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100282 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker57be6e22013-08-26 14:13:14 +0200283 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100284 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker57be6e22013-08-26 14:13:14 +0200285 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker57be6e22013-08-26 14:13:14 +0200287 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker57be6e22013-08-26 14:13:14 +0200289 else
290 goto usage;
291
292 q = r;
293 }
294 }
295 else if( strcmp( p, "ns_cert_type" ) == 0 )
296 {
297 while( q != NULL )
298 {
299 if( ( r = strchr( q, ',' ) ) != NULL )
300 *r++ = '\0';
301
302 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker57be6e22013-08-26 14:13:14 +0200304 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100305 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker57be6e22013-08-26 14:13:14 +0200306 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker57be6e22013-08-26 14:13:14 +0200308 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker57be6e22013-08-26 14:13:14 +0200310 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100311 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker57be6e22013-08-26 14:13:14 +0200312 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100313 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker57be6e22013-08-26 14:13:14 +0200314 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100315 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker57be6e22013-08-26 14:13:14 +0200316 else
317 goto usage;
318
319 q = r;
320 }
321 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000322 else
323 goto usage;
324 }
325
Hanno Beckerf7457332018-10-08 17:14:42 +0100326 mbedtls_x509write_csr_set_md_alg( &req, opt.md_alg );
327
Paul Bakker57be6e22013-08-26 14:13:14 +0200328 if( opt.key_usage )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
Paul Bakker57be6e22013-08-26 14:13:14 +0200330
331 if( opt.ns_cert_type )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
Paul Bakker57be6e22013-08-26 14:13:14 +0200333
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000334 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200335 * 0. Seed the PRNG
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200338 fflush( stdout );
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200341 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200342 (const unsigned char *) pers,
343 strlen( pers ) ) ) != 0 )
344 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200345 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200346 goto exit;
347 }
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200350
351 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000352 * 1.0. Check the subject name for validity
353 */
Manuel Pégourié-Gonnardd7389652015-08-06 18:22:26 +0200354 mbedtls_printf( " . Checking subject name..." );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200355 fflush( stdout );
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_printf( " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200360 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000361 }
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200364
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000365 /*
366 * 1.1. Load the key
367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000369 fflush( stdout );
370
Hanno Becker56e84632018-11-01 14:10:23 +0000371 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, opt.password );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000372
373 if( ret != 0 )
374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200376 goto exit;
377 }
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_printf( " ok\n" );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200382
383 /*
384 * 1.2. Writing the request
385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_printf( " . Writing the certificate request ..." );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200387 fflush( stdout );
388
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200389 if( ( ret = write_certificate_request( &req, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000393 goto exit;
394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000397
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100398 exit_code = MBEDTLS_EXIT_SUCCESS;
399
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000400exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200401
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100402 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#ifdef MBEDTLS_ERROR_C
405 mbedtls_strerror( ret, buf, sizeof( buf ) );
406 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200407#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200409#endif
410 }
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509write_csr_free( &req );
413 mbedtls_pk_free( &key );
414 mbedtls_ctr_drbg_free( &ctr_drbg );
415 mbedtls_entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000416
417#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000419 fflush( stdout ); getchar();
420#endif
421
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100422 return( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Simon Butcher203a6932016-10-07 15:00:17 +0100425 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */