blob: 45fd059b0868508ba5fd9690df8c3815d4aac3b6 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
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 Bakker9397dcb2013-09-06 09:55:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +020020 */
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 Bakker9397dcb2013-09-06 09:55:26 +020027
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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Simon Butcher203a6932016-10-07 15:00:17 +010035#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
36 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
37 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
38 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
39 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020040int main( void )
41{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnardd7389652015-08-06 18:22:26 +020043 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
45 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/x509_crt.h"
51#include "mbedtls/x509_csr.h"
52#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010054#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020056
Rich Evans18b78c72015-02-11 14:06:19 +000057#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000062#define USAGE_CSR \
63 " request_file=%%s default: (empty)\n" \
64 " If request_file is specified, subject_key,\n" \
65 " subject_pwd and subject_name are ignored!\n"
66#else
67#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000069
Paul Bakker1014e952013-09-09 13:59:42 +020070#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020071#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020072#define DFL_SUBJECT_KEY "subject.key"
73#define DFL_ISSUER_KEY "ca.key"
74#define DFL_SUBJECT_PWD ""
75#define DFL_ISSUER_PWD ""
76#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000077#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
78#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020079#define DFL_NOT_BEFORE "20010101000000"
80#define DFL_NOT_AFTER "20301231235959"
81#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020082#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020083#define DFL_IS_CA 0
84#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020085#define DFL_KEY_USAGE 0
86#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010087#define DFL_VERSION 3
88#define DFL_AUTH_IDENT 1
89#define DFL_SUBJ_IDENT 1
90#define DFL_CONSTRAINTS 1
91#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020092
Rich Evans18b78c72015-02-11 14:06:19 +000093#define USAGE \
94 "\n usage: cert_write param=<>...\n" \
95 "\n acceptable parameters:\n" \
96 USAGE_CSR \
97 " subject_key=%%s default: subject.key\n" \
98 " subject_pwd=%%s default: (empty)\n" \
99 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
100 "\n" \
101 " issuer_crt=%%s default: (empty)\n" \
102 " If issuer_crt is specified, issuer_name is\n" \
103 " ignored!\n" \
104 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
105 "\n" \
106 " selfsign=%%d default: 0 (false)\n" \
107 " If selfsign is enabled, issuer_name and\n" \
108 " issuer_key are required (issuer_crt and\n" \
109 " subject_* are ignored\n" \
110 " issuer_key=%%s default: ca.key\n" \
111 " issuer_pwd=%%s default: (empty)\n" \
112 " output_file=%%s default: cert.crt\n" \
113 " serial=%%s default: 1\n" \
114 " not_before=%%s default: 20010101000000\n"\
115 " not_after=%%s default: 20301231235959\n"\
116 " is_ca=%%d default: 0 (disabled)\n" \
117 " max_pathlen=%%d default: -1 (none)\n" \
Hanno Becker6c13d372017-09-13 12:49:22 +0100118 " md=%%s default: SHA256\n" \
119 " Supported values:\n" \
120 " MD5, SHA1, SHA256, SHA512\n"\
121 " version=%%d default: 3\n" \
122 " Possible values: 1, 2, 3\n"\
123 " subject_identifier default: 1\n" \
124 " Possible values: 0, 1\n" \
125 " (Considered for v3 only)\n"\
126 " authority_identifier default: 1\n" \
127 " Possible values: 0, 1\n" \
128 " (Considered for v3 only)\n"\
129 " basic_constraints default: 1\n" \
130 " Possible values: 0, 1\n" \
131 " (Considered for v3 only)\n"\
Rich Evans18b78c72015-02-11 14:06:19 +0000132 " key_usage=%%s default: (empty)\n" \
133 " Comma-separated-list of values:\n" \
134 " digital_signature\n" \
135 " non_repudiation\n" \
136 " key_encipherment\n" \
137 " data_encipherment\n" \
138 " key_agreement\n" \
Jonathan Leroy81962c32015-10-10 21:42:29 +0200139 " key_cert_sign\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000140 " crl_sign\n" \
Hanno Becker6c13d372017-09-13 12:49:22 +0100141 " (Considered for v3 only)\n"\
Rich Evans18b78c72015-02-11 14:06:19 +0000142 " ns_cert_type=%%s default: (empty)\n" \
143 " Comma-separated-list of values:\n" \
144 " ssl_client\n" \
145 " ssl_server\n" \
146 " email\n" \
147 " object_signing\n" \
148 " ssl_ca\n" \
149 " email_ca\n" \
150 " object_signing_ca\n" \
151 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000152
Paul Bakker9397dcb2013-09-06 09:55:26 +0200153/*
154 * global options
155 */
156struct options
157{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100158 const char *issuer_crt; /* filename of the issuer certificate */
159 const char *request_file; /* filename of the certificate request */
160 const char *subject_key; /* filename of the subject key file */
161 const char *issuer_key; /* filename of the issuer key file */
162 const char *subject_pwd; /* password for the subject key file */
163 const char *issuer_pwd; /* password for the issuer key file */
164 const char *output_file; /* where to store the constructed key file */
165 const char *subject_name; /* subject name for certificate */
166 const char *issuer_name; /* issuer name for certificate */
167 const char *not_before; /* validity period not before */
168 const char *not_after; /* validity period not after */
169 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200170 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200171 int is_ca; /* is a CA certificate */
172 int max_pathlen; /* maximum CA path length */
Hanno Becker6c13d372017-09-13 12:49:22 +0100173 int authority_identifier; /* add authority identifier id to CRT */
174 int subject_identifier; /* add subject identifier id to CRT */
175 int basic_constraints; /* add basic constraints ext to CRT */
176 int version; /* CRT version */
177 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178 unsigned char key_usage; /* key usage flags */
179 unsigned char ns_cert_type; /* NS cert type */
180} opt;
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200183 int (*f_rng)(void *, unsigned char *, size_t),
184 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185{
186 int ret;
187 FILE *f;
188 unsigned char output_buf[4096];
189 size_t len = 0;
190
191 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200193 return( ret );
194
195 len = strlen( (char *) output_buf );
196
197 if( ( f = fopen( output_file, "w" ) ) == NULL )
198 return( -1 );
199
200 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200201 {
202 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200204 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205
Paul Bakker0c226102014-04-17 16:02:36 +0200206 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207
208 return( 0 );
209}
210
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211int main( int argc, char *argv[] )
212{
213 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_x509_crt issuer_crt;
215 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
216 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200217 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200219 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100220 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200223 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_x509write_cert crt;
227 mbedtls_mpi serial;
228 mbedtls_entropy_context entropy;
229 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200230 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231
232 /*
233 * Set to sane values
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_pk_init( &loaded_issuer_key );
237 mbedtls_pk_init( &loaded_subject_key );
238 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200239 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_X509_CSR_PARSE_C)
241 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 memset( buf, 0, 1024 );
245
246 if( argc == 0 )
247 {
248 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200250 ret = 1;
251 goto exit;
252 }
253
Paul Bakker1014e952013-09-09 13:59:42 +0200254 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200255 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200256 opt.subject_key = DFL_SUBJECT_KEY;
257 opt.issuer_key = DFL_ISSUER_KEY;
258 opt.subject_pwd = DFL_SUBJECT_PWD;
259 opt.issuer_pwd = DFL_ISSUER_PWD;
260 opt.output_file = DFL_OUTPUT_FILENAME;
261 opt.subject_name = DFL_SUBJECT_NAME;
262 opt.issuer_name = DFL_ISSUER_NAME;
263 opt.not_before = DFL_NOT_BEFORE;
264 opt.not_after = DFL_NOT_AFTER;
265 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200266 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200267 opt.is_ca = DFL_IS_CA;
268 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269 opt.key_usage = DFL_KEY_USAGE;
270 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker6c13d372017-09-13 12:49:22 +0100271 opt.version = DFL_VERSION;
272 opt.md = DFL_DIGEST;
273 opt.subject_identifier = DFL_SUBJ_IDENT;
274 opt.authority_identifier = DFL_AUTH_IDENT;
275 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276
277 for( i = 1; i < argc; i++ )
278 {
279
280 p = argv[i];
281 if( ( q = strchr( p, '=' ) ) == NULL )
282 goto usage;
283 *q++ = '\0';
284
Paul Bakkere2673fb2013-09-09 15:52:07 +0200285 if( strcmp( p, "request_file" ) == 0 )
286 opt.request_file = q;
287 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200288 opt.subject_key = q;
289 else if( strcmp( p, "issuer_key" ) == 0 )
290 opt.issuer_key = q;
291 else if( strcmp( p, "subject_pwd" ) == 0 )
292 opt.subject_pwd = q;
293 else if( strcmp( p, "issuer_pwd" ) == 0 )
294 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200295 else if( strcmp( p, "issuer_crt" ) == 0 )
296 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297 else if( strcmp( p, "output_file" ) == 0 )
298 opt.output_file = q;
299 else if( strcmp( p, "subject_name" ) == 0 )
300 {
301 opt.subject_name = q;
302 }
303 else if( strcmp( p, "issuer_name" ) == 0 )
304 {
305 opt.issuer_name = q;
306 }
307 else if( strcmp( p, "not_before" ) == 0 )
308 {
309 opt.not_before = q;
310 }
311 else if( strcmp( p, "not_after" ) == 0 )
312 {
313 opt.not_after = q;
314 }
315 else if( strcmp( p, "serial" ) == 0 )
316 {
317 opt.serial = q;
318 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100319 else if( strcmp( p, "authority_identifier" ) == 0 )
320 {
321 opt.authority_identifier = atoi( q );
322 if( opt.authority_identifier != 0 &&
323 opt.authority_identifier != 1 )
324 {
325 goto usage;
326 }
327 }
328 else if( strcmp( p, "subject_identifier" ) == 0 )
329 {
330 opt.subject_identifier = atoi( q );
331 if( opt.subject_identifier != 0 &&
332 opt.subject_identifier != 1 )
333 {
334 goto usage;
335 }
336 }
337 else if( strcmp( p, "basic_constraints" ) == 0 )
338 {
339 opt.basic_constraints = atoi( q );
340 if( opt.basic_constraints != 0 &&
341 opt.basic_constraints != 1 )
342 {
343 goto usage;
344 }
345 }
346 else if( strcmp( p, "md" ) == 0 )
347 {
348 if( strcmp( q, "SHA1" ) == 0 )
349 opt.md = MBEDTLS_MD_SHA1;
350 else if( strcmp( q, "SHA256" ) == 0 )
351 opt.md = MBEDTLS_MD_SHA256;
352 else if( strcmp( q, "SHA512" ) == 0 )
353 opt.md = MBEDTLS_MD_SHA512;
354 else if( strcmp( q, "MD5" ) == 0 )
355 opt.md = MBEDTLS_MD_MD5;
356 else
357 goto usage;
358 }
359 else if( strcmp( p, "version" ) == 0 )
360 {
361 opt.version = atoi( q );
362 if( opt.version < 1 || opt.version > 3 )
363 goto usage;
364 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200365 else if( strcmp( p, "selfsign" ) == 0 )
366 {
367 opt.selfsign = atoi( q );
368 if( opt.selfsign < 0 || opt.selfsign > 1 )
369 goto usage;
370 }
Paul Bakker15162a02013-09-06 19:27:21 +0200371 else if( strcmp( p, "is_ca" ) == 0 )
372 {
373 opt.is_ca = atoi( q );
374 if( opt.is_ca < 0 || opt.is_ca > 1 )
375 goto usage;
376 }
377 else if( strcmp( p, "max_pathlen" ) == 0 )
378 {
379 opt.max_pathlen = atoi( q );
380 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
381 goto usage;
382 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200383 else if( strcmp( p, "key_usage" ) == 0 )
384 {
385 while( q != NULL )
386 {
387 if( ( r = strchr( q, ',' ) ) != NULL )
388 *r++ = '\0';
389
390 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100395 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200396 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100397 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200398 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100399 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200400 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200402 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200404 else
405 goto usage;
406
407 q = r;
408 }
409 }
410 else if( strcmp( p, "ns_cert_type" ) == 0 )
411 {
412 while( q != NULL )
413 {
414 if( ( r = strchr( q, ',' ) ) != NULL )
415 *r++ = '\0';
416
417 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100420 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100426 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100428 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100430 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else
432 goto usage;
433
434 q = r;
435 }
436 }
437 else
438 goto usage;
439 }
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200442
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200443 /*
444 * 0. Seed the PRNG
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200447 fflush( stdout );
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200450 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200451 (const unsigned char *) pers,
452 strlen( pers ) ) ) != 0 )
453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200455 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200456 goto exit;
457 }
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200460
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 // Parse serial to MPI
462 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200464 fflush( stdout );
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_strerror( ret, buf, 1024 );
469 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200470 goto exit;
471 }
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200474
Paul Bakker1014e952013-09-09 13:59:42 +0200475 // Parse issuer certificate if present
476 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200477 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200478 {
479 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200480 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200483 fflush( stdout );
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_strerror( ret, buf, 1024 );
488 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200489 goto exit;
490 }
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200493 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200494 if( ret < 0 )
495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_strerror( ret, buf, 1024 );
497 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200498 goto exit;
499 }
500
Paul Bakkere2673fb2013-09-09 15:52:07 +0200501 opt.issuer_name = issuer_name;
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200504 }
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200507 // Parse certificate request if present
508 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200509 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200510 {
511 /*
512 * 1.0.b. Load the CSR
513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200515 fflush( stdout );
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_strerror( ret, buf, 1024 );
520 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200521 goto exit;
522 }
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200525 &csr.subject );
526 if( ret < 0 )
527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_strerror( ret, buf, 1024 );
529 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200530 goto exit;
531 }
532
533 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200534 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200539
540 /*
541 * 1.1. Load the keys
542 */
543 if( !opt.selfsign && !strlen( opt.request_file ) )
544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200546 fflush( stdout );
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200549 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200550 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_strerror( ret, buf, 1024 );
553 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200554 goto exit;
555 }
Paul Bakker1014e952013-09-09 13:59:42 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200558 }
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200561 fflush( stdout );
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200565 if( ret != 0 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_strerror( ret, buf, 1024 );
568 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200569 goto exit;
570 }
571
572 // Check if key and issuer certificate match
573 //
574 if( strlen( opt.issuer_crt ) )
575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
577 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
578 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
579 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
580 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200583 ret = -1;
584 goto exit;
585 }
586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200589
590 if( opt.selfsign )
591 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100592 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200593 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
597 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200598
Paul Bakker9397dcb2013-09-06 09:55:26 +0200599 /*
600 * 1.0. Check the names for validity
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_strerror( ret, buf, 1024 );
605 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200606 goto exit;
607 }
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_strerror( ret, buf, 1024 );
612 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200613 goto exit;
614 }
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200617 fflush( stdout );
618
Hanno Becker6c13d372017-09-13 12:49:22 +0100619 mbedtls_x509write_crt_set_version( &crt, opt.version - 1 );
620 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200623 if( ret != 0 )
624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_strerror( ret, buf, 1024 );
626 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200627 goto exit;
628 }
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200631 if( ret != 0 )
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_strerror( ret, buf, 1024 );
634 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 goto exit;
636 }
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639
Hanno Becker6c13d372017-09-13 12:49:22 +0100640 if( opt.version == 3 && opt.basic_constraints )
Paul Bakker15162a02013-09-06 19:27:21 +0200641 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100642 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
643 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200644
Hanno Becker6c13d372017-09-13 12:49:22 +0100645 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
646 opt.max_pathlen );
647 if( ret != 0 )
648 {
649 mbedtls_strerror( ret, buf, 1024 );
650 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
651 "returned -0x%02x - %s\n\n", -ret, buf );
652 goto exit;
653 }
654
655 mbedtls_printf( " ok\n" );
656 }
Paul Bakker15162a02013-09-06 19:27:21 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SHA1_C)
Hanno Becker6c13d372017-09-13 12:49:22 +0100659 if( opt.version == 3 && opt.subject_identifier )
Paul Bakker15162a02013-09-06 19:27:21 +0200660 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100661 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
662 fflush( stdout );
663
664 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
665 if( ret != 0 )
666 {
667 mbedtls_strerror( ret, buf, 1024 );
668 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
669 "_key_identifier returned -0x%02x - %s\n\n",
670 -ret, buf );
671 goto exit;
672 }
673
674 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200675 }
676
Hanno Becker6c13d372017-09-13 12:49:22 +0100677 if( opt.version == 3 && opt.authority_identifier )
Paul Bakker15162a02013-09-06 19:27:21 +0200678 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100679 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
680 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200681
Hanno Becker6c13d372017-09-13 12:49:22 +0100682 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
683 if( ret != 0 )
684 {
685 mbedtls_strerror( ret, buf, 1024 );
686 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
687 "key_identifier returned -0x%02x - %s\n\n",
688 -ret, buf );
689 goto exit;
690 }
691
692 mbedtls_printf( " ok\n" );
693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200695
Hanno Becker6c13d372017-09-13 12:49:22 +0100696 if( opt.version == 3 && opt.key_usage )
Paul Bakker52be08c2013-09-09 12:37:54 +0200697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200699 fflush( stdout );
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200702 if( ret != 0 )
703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_strerror( ret, buf, 1024 );
705 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200706 goto exit;
707 }
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200710 }
711
Hanno Becker6c13d372017-09-13 12:49:22 +0100712 if( opt.version == 3 && opt.ns_cert_type )
Paul Bakker52be08c2013-09-09 12:37:54 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200715 fflush( stdout );
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200718 if( ret != 0 )
719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_strerror( ret, buf, 1024 );
721 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200722 goto exit;
723 }
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200726 }
727
Paul Bakker9397dcb2013-09-06 09:55:26 +0200728 /*
729 * 1.2. Writing the request
730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200732 fflush( stdout );
733
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200734 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_strerror( ret, buf, 1024 );
738 mbedtls_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200739 goto exit;
740 }
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200743
744exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_x509write_crt_free( &crt );
746 mbedtls_pk_free( &loaded_subject_key );
747 mbedtls_pk_free( &loaded_issuer_key );
748 mbedtls_mpi_free( &serial );
749 mbedtls_ctr_drbg_free( &ctr_drbg );
750 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200751
752#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200754 fflush( stdout ); getchar();
755#endif
756
757 return( ret );
758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
760 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100761 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */