blob: 66e5f1dabbf23332f746f5bef6c8a2f178a287f6 [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"
54#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000061#define USAGE_CSR \
62 " request_file=%%s default: (empty)\n" \
63 " If request_file is specified, subject_key,\n" \
64 " subject_pwd and subject_name are ignored!\n"
65#else
66#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000068
Paul Bakker1014e952013-09-09 13:59:42 +020069#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020070#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_SUBJECT_KEY "subject.key"
72#define DFL_ISSUER_KEY "ca.key"
73#define DFL_SUBJECT_PWD ""
74#define DFL_ISSUER_PWD ""
75#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000076#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_NOT_BEFORE "20010101000000"
79#define DFL_NOT_AFTER "20301231235959"
80#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020081#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020082#define DFL_IS_CA 0
83#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020084#define DFL_KEY_USAGE 0
85#define DFL_NS_CERT_TYPE 0
86
Rich Evans18b78c72015-02-11 14:06:19 +000087#define USAGE \
88 "\n usage: cert_write param=<>...\n" \
89 "\n acceptable parameters:\n" \
90 USAGE_CSR \
91 " subject_key=%%s default: subject.key\n" \
92 " subject_pwd=%%s default: (empty)\n" \
93 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
94 "\n" \
95 " issuer_crt=%%s default: (empty)\n" \
96 " If issuer_crt is specified, issuer_name is\n" \
97 " ignored!\n" \
98 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
99 "\n" \
100 " selfsign=%%d default: 0 (false)\n" \
101 " If selfsign is enabled, issuer_name and\n" \
102 " issuer_key are required (issuer_crt and\n" \
103 " subject_* are ignored\n" \
104 " issuer_key=%%s default: ca.key\n" \
105 " issuer_pwd=%%s default: (empty)\n" \
106 " output_file=%%s default: cert.crt\n" \
107 " serial=%%s default: 1\n" \
108 " not_before=%%s default: 20010101000000\n"\
109 " not_after=%%s default: 20301231235959\n"\
110 " is_ca=%%d default: 0 (disabled)\n" \
111 " max_pathlen=%%d default: -1 (none)\n" \
112 " key_usage=%%s default: (empty)\n" \
113 " Comma-separated-list of values:\n" \
114 " digital_signature\n" \
115 " non_repudiation\n" \
116 " key_encipherment\n" \
117 " data_encipherment\n" \
118 " key_agreement\n" \
Jonathan Leroy81962c32015-10-10 21:42:29 +0200119 " key_cert_sign\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000120 " crl_sign\n" \
121 " ns_cert_type=%%s default: (empty)\n" \
122 " Comma-separated-list of values:\n" \
123 " ssl_client\n" \
124 " ssl_server\n" \
125 " email\n" \
126 " object_signing\n" \
127 " ssl_ca\n" \
128 " email_ca\n" \
129 " object_signing_ca\n" \
130 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000131
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132/*
133 * global options
134 */
135struct options
136{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100137 const char *issuer_crt; /* filename of the issuer certificate */
138 const char *request_file; /* filename of the certificate request */
139 const char *subject_key; /* filename of the subject key file */
140 const char *issuer_key; /* filename of the issuer key file */
141 const char *subject_pwd; /* password for the subject key file */
142 const char *issuer_pwd; /* password for the issuer key file */
143 const char *output_file; /* where to store the constructed key file */
144 const char *subject_name; /* subject name for certificate */
145 const char *issuer_name; /* issuer name for certificate */
146 const char *not_before; /* validity period not before */
147 const char *not_after; /* validity period not after */
148 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200149 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200150 int is_ca; /* is a CA certificate */
151 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152 unsigned char key_usage; /* key usage flags */
153 unsigned char ns_cert_type; /* NS cert type */
154} opt;
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200157 int (*f_rng)(void *, unsigned char *, size_t),
158 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200159{
160 int ret;
161 FILE *f;
162 unsigned char output_buf[4096];
163 size_t len = 0;
164
165 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167 return( ret );
168
169 len = strlen( (char *) output_buf );
170
171 if( ( f = fopen( output_file, "w" ) ) == NULL )
172 return( -1 );
173
174 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200175 {
176 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200177 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200178 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200179
Paul Bakker0c226102014-04-17 16:02:36 +0200180 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181
182 return( 0 );
183}
184
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185int main( int argc, char *argv[] )
186{
187 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_x509_crt issuer_crt;
189 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
190 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200191 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200193 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100194 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200197 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_x509write_cert crt;
201 mbedtls_mpi serial;
202 mbedtls_entropy_context entropy;
203 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200204 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205
206 /*
207 * Set to sane values
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509write_crt_init( &crt );
210 mbedtls_x509write_crt_set_md_alg( &crt, MBEDTLS_MD_SHA256 );
211 mbedtls_pk_init( &loaded_issuer_key );
212 mbedtls_pk_init( &loaded_subject_key );
213 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200214 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_X509_CSR_PARSE_C)
216 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200219 memset( buf, 0, 1024 );
220
221 if( argc == 0 )
222 {
223 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225 ret = 1;
226 goto exit;
227 }
228
Paul Bakker1014e952013-09-09 13:59:42 +0200229 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200230 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231 opt.subject_key = DFL_SUBJECT_KEY;
232 opt.issuer_key = DFL_ISSUER_KEY;
233 opt.subject_pwd = DFL_SUBJECT_PWD;
234 opt.issuer_pwd = DFL_ISSUER_PWD;
235 opt.output_file = DFL_OUTPUT_FILENAME;
236 opt.subject_name = DFL_SUBJECT_NAME;
237 opt.issuer_name = DFL_ISSUER_NAME;
238 opt.not_before = DFL_NOT_BEFORE;
239 opt.not_after = DFL_NOT_AFTER;
240 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200241 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200242 opt.is_ca = DFL_IS_CA;
243 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 opt.key_usage = DFL_KEY_USAGE;
245 opt.ns_cert_type = DFL_NS_CERT_TYPE;
246
247 for( i = 1; i < argc; i++ )
248 {
249
250 p = argv[i];
251 if( ( q = strchr( p, '=' ) ) == NULL )
252 goto usage;
253 *q++ = '\0';
254
Paul Bakkere2673fb2013-09-09 15:52:07 +0200255 if( strcmp( p, "request_file" ) == 0 )
256 opt.request_file = q;
257 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 opt.subject_key = q;
259 else if( strcmp( p, "issuer_key" ) == 0 )
260 opt.issuer_key = q;
261 else if( strcmp( p, "subject_pwd" ) == 0 )
262 opt.subject_pwd = q;
263 else if( strcmp( p, "issuer_pwd" ) == 0 )
264 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200265 else if( strcmp( p, "issuer_crt" ) == 0 )
266 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200267 else if( strcmp( p, "output_file" ) == 0 )
268 opt.output_file = q;
269 else if( strcmp( p, "subject_name" ) == 0 )
270 {
271 opt.subject_name = q;
272 }
273 else if( strcmp( p, "issuer_name" ) == 0 )
274 {
275 opt.issuer_name = q;
276 }
277 else if( strcmp( p, "not_before" ) == 0 )
278 {
279 opt.not_before = q;
280 }
281 else if( strcmp( p, "not_after" ) == 0 )
282 {
283 opt.not_after = q;
284 }
285 else if( strcmp( p, "serial" ) == 0 )
286 {
287 opt.serial = q;
288 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200289 else if( strcmp( p, "selfsign" ) == 0 )
290 {
291 opt.selfsign = atoi( q );
292 if( opt.selfsign < 0 || opt.selfsign > 1 )
293 goto usage;
294 }
Paul Bakker15162a02013-09-06 19:27:21 +0200295 else if( strcmp( p, "is_ca" ) == 0 )
296 {
297 opt.is_ca = atoi( q );
298 if( opt.is_ca < 0 || opt.is_ca > 1 )
299 goto usage;
300 }
301 else if( strcmp( p, "max_pathlen" ) == 0 )
302 {
303 opt.max_pathlen = atoi( q );
304 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
305 goto usage;
306 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 else if( strcmp( p, "key_usage" ) == 0 )
308 {
309 while( q != NULL )
310 {
311 if( ( r = strchr( q, ',' ) ) != NULL )
312 *r++ = '\0';
313
314 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200318 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100319 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200320 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100321 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200322 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100323 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200324 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200326 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200328 else
329 goto usage;
330
331 q = r;
332 }
333 }
334 else if( strcmp( p, "ns_cert_type" ) == 0 )
335 {
336 while( q != NULL )
337 {
338 if( ( r = strchr( q, ',' ) ) != NULL )
339 *r++ = '\0';
340
341 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200343 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100344 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200345 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200347 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200349 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100350 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200351 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100352 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200353 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100354 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200355 else
356 goto usage;
357
358 q = r;
359 }
360 }
361 else
362 goto usage;
363 }
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200366
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200367 /*
368 * 0. Seed the PRNG
369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200371 fflush( stdout );
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200374 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200375 (const unsigned char *) pers,
376 strlen( pers ) ) ) != 0 )
377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200379 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200380 goto exit;
381 }
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200384
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 // Parse serial to MPI
386 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200388 fflush( stdout );
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_strerror( ret, buf, 1024 );
393 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 goto exit;
395 }
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200398
Paul Bakker1014e952013-09-09 13:59:42 +0200399 // Parse issuer certificate if present
400 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200401 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200402 {
403 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200404 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200407 fflush( stdout );
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_strerror( ret, buf, 1024 );
412 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200413 goto exit;
414 }
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200417 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200418 if( ret < 0 )
419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_strerror( ret, buf, 1024 );
421 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200422 goto exit;
423 }
424
Paul Bakkere2673fb2013-09-09 15:52:07 +0200425 opt.issuer_name = issuer_name;
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200428 }
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200431 // Parse certificate request if present
432 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200433 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 {
435 /*
436 * 1.0.b. Load the CSR
437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200439 fflush( stdout );
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_strerror( ret, buf, 1024 );
444 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200445 goto exit;
446 }
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449 &csr.subject );
450 if( ret < 0 )
451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_strerror( ret, buf, 1024 );
453 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200454 goto exit;
455 }
456
457 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200458 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200463
464 /*
465 * 1.1. Load the keys
466 */
467 if( !opt.selfsign && !strlen( opt.request_file ) )
468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200470 fflush( stdout );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200473 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200474 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_strerror( ret, buf, 1024 );
477 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200478 goto exit;
479 }
Paul Bakker1014e952013-09-09 13:59:42 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200482 }
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200485 fflush( stdout );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200489 if( ret != 0 )
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_strerror( ret, buf, 1024 );
492 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200493 goto exit;
494 }
495
496 // Check if key and issuer certificate match
497 //
498 if( strlen( opt.issuer_crt ) )
499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
501 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
502 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
503 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
504 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200507 ret = -1;
508 goto exit;
509 }
510 }
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200513
514 if( opt.selfsign )
515 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100516 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200517 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200518 }
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
521 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200522
Paul Bakker9397dcb2013-09-06 09:55:26 +0200523 /*
524 * 1.0. Check the names for validity
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_strerror( ret, buf, 1024 );
529 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 +0200530 goto exit;
531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_strerror( ret, buf, 1024 );
536 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 +0200537 goto exit;
538 }
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200541 fflush( stdout );
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 if( ret != 0 )
545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_strerror( ret, buf, 1024 );
547 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200548 goto exit;
549 }
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200552 if( ret != 0 )
553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_strerror( ret, buf, 1024 );
555 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200556 goto exit;
557 }
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200562 fflush( stdout );
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
Paul Bakker15162a02013-09-06 19:27:21 +0200565 opt.max_pathlen );
566 if( ret != 0 )
567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_strerror( ret, buf, 1024 );
569 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200570 goto exit;
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_SHA1_C)
576 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200577 fflush( stdout );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200580 if( ret != 0 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_strerror( ret, buf, 1024 );
583 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200584 goto exit;
585 }
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200590 fflush( stdout );
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200593 if( ret != 0 )
594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_strerror( ret, buf, 1024 );
596 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200597 goto exit;
598 }
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_printf( " ok\n" );
601#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200602
Paul Bakker52be08c2013-09-09 12:37:54 +0200603 if( opt.key_usage )
604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200606 fflush( stdout );
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200609 if( ret != 0 )
610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_strerror( ret, buf, 1024 );
612 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 +0200613 goto exit;
614 }
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200617 }
618
619 if( opt.ns_cert_type )
620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200622 fflush( stdout );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200625 if( ret != 0 )
626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_strerror( ret, buf, 1024 );
628 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 +0200629 goto exit;
630 }
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200633 }
634
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 /*
636 * 1.2. Writing the request
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639 fflush( stdout );
640
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200641 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_strerror( ret, buf, 1024 );
645 mbedtls_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 goto exit;
647 }
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650
651exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_x509write_crt_free( &crt );
653 mbedtls_pk_free( &loaded_subject_key );
654 mbedtls_pk_free( &loaded_issuer_key );
655 mbedtls_mpi_free( &serial );
656 mbedtls_ctr_drbg_free( &ctr_drbg );
657 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200658
659#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200661 fflush( stdout ); getchar();
662#endif
663
664 return( ret );
665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
667 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100668 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */