blob: 94e88d4831e88e4dc1415dcd45ece09280d8035c [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"
Azim Khan26a455c2018-02-07 11:11:17 +000056#include "mbedtls/pk_info.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020057
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
Azim Khan26a455c2018-02-07 11:11:17 +000061#include <Windows.h>
Rich Evans18b78c72015-02-11 14:06:19 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000064#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010065 " request_file=%%s default: (empty)\n" \
66 " If request_file is specified, subject_key,\n" \
67 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000068#else
69#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000071
Paul Bakker1014e952013-09-09 13:59:42 +020072#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020073#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_SUBJECT_KEY "subject.key"
75#define DFL_ISSUER_KEY "ca.key"
76#define DFL_SUBJECT_PWD ""
77#define DFL_ISSUER_PWD ""
78#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000079#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
80#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020081#define DFL_NOT_BEFORE "20010101000000"
82#define DFL_NOT_AFTER "20301231235959"
83#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020084#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020085#define DFL_IS_CA 0
86#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
88#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010089#define DFL_VERSION 3
90#define DFL_AUTH_IDENT 1
91#define DFL_SUBJ_IDENT 1
92#define DFL_CONSTRAINTS 1
93#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020094
Azim Khan26a455c2018-02-07 11:11:17 +000095typedef struct
96{
97 const char * serial_port;
98 unsigned char key_idx;
99}remote_serial_pk_context;
100
101int is_remote_key( const char * remote_info );
102int load_pubkey_from_remote( const char * remote_info, mbedtls_pk_context * ctx );
103int setup_opaque_privkey( const char * remote_info, mbedtls_pk_context * ctx );
104void mbedtls_pk_remote_free( mbedtls_pk_context * ctx );
105int serial_xfer( const char * serial_port, const unsigned char * tx_buf,
106 size_t tx_buf_len, unsigned char * rx_buf, size_t rx_buf_len,
107 size_t * rx_len );
108#define UNUSED(x) ((void)(x))
109
Rich Evans18b78c72015-02-11 14:06:19 +0000110#define USAGE \
111 "\n usage: cert_write param=<>...\n" \
112 "\n acceptable parameters:\n" \
113 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100114 " subject_key=%%s default: subject.key\n" \
115 " subject_pwd=%%s default: (empty)\n" \
116 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000117 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100118 " issuer_crt=%%s default: (empty)\n" \
119 " If issuer_crt is specified, issuer_name is\n" \
120 " ignored!\n" \
121 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000122 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100123 " selfsign=%%d default: 0 (false)\n" \
124 " If selfsign is enabled, issuer_name and\n" \
125 " issuer_key are required (issuer_crt and\n" \
126 " subject_* are ignored\n" \
127 " issuer_key=%%s default: ca.key\n" \
128 " issuer_pwd=%%s default: (empty)\n" \
129 " output_file=%%s default: cert.crt\n" \
130 " serial=%%s default: 1\n" \
131 " not_before=%%s default: 20010101000000\n"\
132 " not_after=%%s default: 20301231235959\n"\
133 " is_ca=%%d default: 0 (disabled)\n" \
134 " max_pathlen=%%d default: -1 (none)\n" \
135 " md=%%s default: SHA256\n" \
136 " Supported values:\n" \
137 " MD5, SHA1, SHA256, SHA512\n"\
138 " version=%%d default: 3\n" \
139 " Possible values: 1, 2, 3\n"\
140 " subject_identifier=%%s default: 1\n" \
141 " Possible values: 0, 1\n" \
142 " (Considered for v3 only)\n"\
143 " authority_identifier=%%s default: 1\n" \
144 " Possible values: 0, 1\n" \
145 " (Considered for v3 only)\n"\
146 " basic_constraints=%%d default: 1\n" \
147 " Possible values: 0, 1\n" \
148 " (Considered for v3 only)\n"\
149 " key_usage=%%s default: (empty)\n" \
150 " Comma-separated-list of values:\n" \
151 " digital_signature\n" \
152 " non_repudiation\n" \
153 " key_encipherment\n" \
154 " data_encipherment\n" \
155 " key_agreement\n" \
156 " key_cert_sign\n" \
157 " crl_sign\n" \
158 " (Considered for v3 only)\n"\
159 " ns_cert_type=%%s default: (empty)\n" \
160 " Comma-separated-list of values:\n" \
161 " ssl_client\n" \
162 " ssl_server\n" \
163 " email\n" \
164 " object_signing\n" \
165 " ssl_ca\n" \
166 " email_ca\n" \
167 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000168 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000169
Paul Bakker9397dcb2013-09-06 09:55:26 +0200170/*
171 * global options
172 */
173struct options
174{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100175 const char *issuer_crt; /* filename of the issuer certificate */
176 const char *request_file; /* filename of the certificate request */
177 const char *subject_key; /* filename of the subject key file */
178 const char *issuer_key; /* filename of the issuer key file */
179 const char *subject_pwd; /* password for the subject key file */
180 const char *issuer_pwd; /* password for the issuer key file */
181 const char *output_file; /* where to store the constructed key file */
182 const char *subject_name; /* subject name for certificate */
183 const char *issuer_name; /* issuer name for certificate */
184 const char *not_before; /* validity period not before */
185 const char *not_after; /* validity period not after */
186 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200187 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200188 int is_ca; /* is a CA certificate */
189 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100190 int authority_identifier; /* add authority identifier to CRT */
191 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100192 int basic_constraints; /* add basic constraints ext to CRT */
193 int version; /* CRT version */
194 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 unsigned char key_usage; /* key usage flags */
196 unsigned char ns_cert_type; /* NS cert type */
197} opt;
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200200 int (*f_rng)(void *, unsigned char *, size_t),
201 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200202{
203 int ret;
204 FILE *f;
205 unsigned char output_buf[4096];
206 size_t len = 0;
207
208 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100209 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
210 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211 return( ret );
212
213 len = strlen( (char *) output_buf );
214
215 if( ( f = fopen( output_file, "w" ) ) == NULL )
216 return( -1 );
217
218 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200219 {
220 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200222 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
Paul Bakker0c226102014-04-17 16:02:36 +0200224 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225
226 return( 0 );
227}
228
Paul Bakker9397dcb2013-09-06 09:55:26 +0200229int main( int argc, char *argv[] )
230{
231 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509_crt issuer_crt;
233 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
234 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200235 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200237 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100238 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200241 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509write_cert crt;
245 mbedtls_mpi serial;
246 mbedtls_entropy_context entropy;
247 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200248 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
250 /*
251 * Set to sane values
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pk_init( &loaded_issuer_key );
255 mbedtls_pk_init( &loaded_subject_key );
256 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200257 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_X509_CSR_PARSE_C)
259 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200260#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 memset( buf, 0, 1024 );
263
264 if( argc == 0 )
265 {
266 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200268 ret = 1;
269 goto exit;
270 }
271
Paul Bakker1014e952013-09-09 13:59:42 +0200272 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200273 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.subject_key = DFL_SUBJECT_KEY;
275 opt.issuer_key = DFL_ISSUER_KEY;
276 opt.subject_pwd = DFL_SUBJECT_PWD;
277 opt.issuer_pwd = DFL_ISSUER_PWD;
278 opt.output_file = DFL_OUTPUT_FILENAME;
279 opt.subject_name = DFL_SUBJECT_NAME;
280 opt.issuer_name = DFL_ISSUER_NAME;
281 opt.not_before = DFL_NOT_BEFORE;
282 opt.not_after = DFL_NOT_AFTER;
283 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200284 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200285 opt.is_ca = DFL_IS_CA;
286 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.key_usage = DFL_KEY_USAGE;
288 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100289 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100290 opt.md = DFL_DIGEST;
291 opt.subject_identifier = DFL_SUBJ_IDENT;
292 opt.authority_identifier = DFL_AUTH_IDENT;
293 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294
295 for( i = 1; i < argc; i++ )
296 {
297
298 p = argv[i];
299 if( ( q = strchr( p, '=' ) ) == NULL )
300 goto usage;
301 *q++ = '\0';
302
Paul Bakkere2673fb2013-09-09 15:52:07 +0200303 if( strcmp( p, "request_file" ) == 0 )
304 opt.request_file = q;
305 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306 opt.subject_key = q;
307 else if( strcmp( p, "issuer_key" ) == 0 )
308 opt.issuer_key = q;
309 else if( strcmp( p, "subject_pwd" ) == 0 )
310 opt.subject_pwd = q;
311 else if( strcmp( p, "issuer_pwd" ) == 0 )
312 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200313 else if( strcmp( p, "issuer_crt" ) == 0 )
314 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200315 else if( strcmp( p, "output_file" ) == 0 )
316 opt.output_file = q;
317 else if( strcmp( p, "subject_name" ) == 0 )
318 {
319 opt.subject_name = q;
320 }
321 else if( strcmp( p, "issuer_name" ) == 0 )
322 {
323 opt.issuer_name = q;
324 }
325 else if( strcmp( p, "not_before" ) == 0 )
326 {
327 opt.not_before = q;
328 }
329 else if( strcmp( p, "not_after" ) == 0 )
330 {
331 opt.not_after = q;
332 }
333 else if( strcmp( p, "serial" ) == 0 )
334 {
335 opt.serial = q;
336 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100337 else if( strcmp( p, "authority_identifier" ) == 0 )
338 {
339 opt.authority_identifier = atoi( q );
340 if( opt.authority_identifier != 0 &&
341 opt.authority_identifier != 1 )
342 {
Hanno Becker17c32762017-10-03 14:56:04 +0100343 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100344 goto usage;
345 }
346 }
347 else if( strcmp( p, "subject_identifier" ) == 0 )
348 {
349 opt.subject_identifier = atoi( q );
350 if( opt.subject_identifier != 0 &&
351 opt.subject_identifier != 1 )
352 {
Hanno Becker17c32762017-10-03 14:56:04 +0100353 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100354 goto usage;
355 }
356 }
357 else if( strcmp( p, "basic_constraints" ) == 0 )
358 {
359 opt.basic_constraints = atoi( q );
360 if( opt.basic_constraints != 0 &&
361 opt.basic_constraints != 1 )
362 {
Hanno Becker17c32762017-10-03 14:56:04 +0100363 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100364 goto usage;
365 }
366 }
367 else if( strcmp( p, "md" ) == 0 )
368 {
369 if( strcmp( q, "SHA1" ) == 0 )
370 opt.md = MBEDTLS_MD_SHA1;
371 else if( strcmp( q, "SHA256" ) == 0 )
372 opt.md = MBEDTLS_MD_SHA256;
373 else if( strcmp( q, "SHA512" ) == 0 )
374 opt.md = MBEDTLS_MD_SHA512;
375 else if( strcmp( q, "MD5" ) == 0 )
376 opt.md = MBEDTLS_MD_MD5;
377 else
Hanno Becker17c32762017-10-03 14:56:04 +0100378 {
379 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100380 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100381 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100382 }
383 else if( strcmp( p, "version" ) == 0 )
384 {
385 opt.version = atoi( q );
386 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100387 {
388 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100389 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100390 }
Hanno Becker38eff432017-09-22 15:38:20 +0100391 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100392 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200393 else if( strcmp( p, "selfsign" ) == 0 )
394 {
395 opt.selfsign = atoi( q );
396 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100397 {
398 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200399 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100400 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200401 }
Paul Bakker15162a02013-09-06 19:27:21 +0200402 else if( strcmp( p, "is_ca" ) == 0 )
403 {
404 opt.is_ca = atoi( q );
405 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100406 {
407 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200408 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100409 }
Paul Bakker15162a02013-09-06 19:27:21 +0200410 }
411 else if( strcmp( p, "max_pathlen" ) == 0 )
412 {
413 opt.max_pathlen = atoi( q );
414 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100415 {
416 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200417 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100418 }
Paul Bakker15162a02013-09-06 19:27:21 +0200419 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200420 else if( strcmp( p, "key_usage" ) == 0 )
421 {
422 while( q != NULL )
423 {
424 if( ( r = strchr( q, ',' ) ) != NULL )
425 *r++ = '\0';
426
427 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100432 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100434 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100436 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441 else
Hanno Becker17c32762017-10-03 14:56:04 +0100442 {
443 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100445 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446
447 q = r;
448 }
449 }
450 else if( strcmp( p, "ns_cert_type" ) == 0 )
451 {
452 while( q != NULL )
453 {
454 if( ( r = strchr( q, ',' ) ) != NULL )
455 *r++ = '\0';
456
457 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200459 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100460 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200465 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100466 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200467 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100468 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200469 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100470 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200471 else
Hanno Becker17c32762017-10-03 14:56:04 +0100472 {
473 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200474 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100475 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200476
477 q = r;
478 }
479 }
480 else
481 goto usage;
482 }
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200485
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200486 /*
487 * 0. Seed the PRNG
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200490 fflush( stdout );
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200493 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200494 (const unsigned char *) pers,
495 strlen( pers ) ) ) != 0 )
496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100498 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
499 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200500 goto exit;
501 }
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200504
Paul Bakker9397dcb2013-09-06 09:55:26 +0200505 // Parse serial to MPI
506 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200508 fflush( stdout );
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100513 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100514 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200515 goto exit;
516 }
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200519
Paul Bakker1014e952013-09-09 13:59:42 +0200520 // Parse issuer certificate if present
521 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200522 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200523 {
524 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200525 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200528 fflush( stdout );
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100533 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100534 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200535 goto exit;
536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200539 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200540 if( ret < 0 )
541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100543 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100544 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200545 goto exit;
546 }
547
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 opt.issuer_name = issuer_name;
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 }
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200554 // Parse certificate request if present
555 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200556 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200557 {
558 /*
559 * 1.0.b. Load the CSR
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200562 fflush( stdout );
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100567 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100568 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200569 goto exit;
570 }
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200573 &csr.subject );
574 if( ret < 0 )
575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100577 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100578 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200579 goto exit;
580 }
581
582 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200583 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200588
589 /*
590 * 1.1. Load the keys
591 */
592 if( !opt.selfsign && !strlen( opt.request_file ) )
593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200595 fflush( stdout );
596
Azim Khan26a455c2018-02-07 11:11:17 +0000597 if ( is_remote_key( opt.subject_key ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200598 {
Azim Khan26a455c2018-02-07 11:11:17 +0000599 ret = load_pubkey_from_remote( opt.subject_key, &loaded_subject_key );
600 if ( ret != 0 )
601 goto exit;
602 }
603 else
604 {
605 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
606 opt.subject_pwd );
607 if( ret != 0 )
608 {
609 mbedtls_strerror( ret, buf, 1024 );
610 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
611 "returned -0x%04x - %s\n\n", -ret, buf );
612 goto exit;
613 }
Paul Bakkere2673fb2013-09-09 15:52:07 +0200614 }
Paul Bakker1014e952013-09-09 13:59:42 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200617 }
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200620 fflush( stdout );
621
Azim Khan26a455c2018-02-07 11:11:17 +0000622 if ( is_remote_key( opt.issuer_key ) )
623 {
624 ret = setup_opaque_privkey( opt.issuer_key, &loaded_issuer_key );
625 if ( ret != 0 )
626 goto exit;
627 }
628 else
629 {
630 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
631 opt.issuer_pwd );
632 }
633
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200634 if( ret != 0 )
635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100637 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
638 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200639 goto exit;
640 }
641
642 // Check if key and issuer certificate match
643 //
644 if( strlen( opt.issuer_crt ) )
645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
647 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
648 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
649 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
650 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200651 {
Hanno Becker81535d02017-09-13 15:39:59 +0100652 mbedtls_printf( " failed\n ! issuer_key does not match "
653 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200654 ret = -1;
655 goto exit;
656 }
657 }
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200660
661 if( opt.selfsign )
662 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100663 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200664 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200665 }
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
668 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200669
Paul Bakker9397dcb2013-09-06 09:55:26 +0200670 /*
671 * 1.0. Check the names for validity
672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100676 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100677 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200678 goto exit;
679 }
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100684 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100685 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200686 goto exit;
687 }
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200690 fflush( stdout );
691
Hanno Becker38eff432017-09-22 15:38:20 +0100692 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100693 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200696 if( ret != 0 )
697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100699 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100700 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200701 goto exit;
702 }
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705 if( ret != 0 )
706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100708 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100709 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200710 goto exit;
711 }
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200714
Hanno Becker38eff432017-09-22 15:38:20 +0100715 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
716 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200717 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100718 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
719 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200720
Hanno Becker6c13d372017-09-13 12:49:22 +0100721 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
722 opt.max_pathlen );
723 if( ret != 0 )
724 {
725 mbedtls_strerror( ret, buf, 1024 );
726 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100727 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100728 goto exit;
729 }
730
731 mbedtls_printf( " ok\n" );
732 }
Paul Bakker15162a02013-09-06 19:27:21 +0200733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100735 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
736 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200737 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100738 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
739 fflush( stdout );
740
741 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
742 if( ret != 0 )
743 {
744 mbedtls_strerror( ret, buf, 1024 );
745 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100746 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100747 -ret, buf );
748 goto exit;
749 }
750
751 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200752 }
753
Hanno Becker38eff432017-09-22 15:38:20 +0100754 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
755 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200756 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100757 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
758 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200759
Hanno Becker6c13d372017-09-13 12:49:22 +0100760 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
761 if( ret != 0 )
762 {
763 mbedtls_strerror( ret, buf, 1024 );
764 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100765 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100766 -ret, buf );
767 goto exit;
768 }
769
770 mbedtls_printf( " ok\n" );
771 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200773
Hanno Becker38eff432017-09-22 15:38:20 +0100774 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
775 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200778 fflush( stdout );
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200781 if( ret != 0 )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100784 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100785 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200786 goto exit;
787 }
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200790 }
791
Hanno Becker38eff432017-09-22 15:38:20 +0100792 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
793 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200796 fflush( stdout );
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200799 if( ret != 0 )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100802 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100803 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200804 goto exit;
805 }
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200808 }
809
Paul Bakker9397dcb2013-09-06 09:55:26 +0200810 /*
811 * 1.2. Writing the request
812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200814 fflush( stdout );
815
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200816 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100820 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100821 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200822 goto exit;
823 }
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200826
827exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_x509write_crt_free( &crt );
829 mbedtls_pk_free( &loaded_subject_key );
Azim Khan26a455c2018-02-07 11:11:17 +0000830 if ( is_remote_key( opt.issuer_key ) )
831 mbedtls_pk_remote_free( &loaded_issuer_key );
832 else
833 mbedtls_pk_free( &loaded_issuer_key );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_mpi_free( &serial );
835 mbedtls_ctr_drbg_free( &ctr_drbg );
836 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200837
838#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200840 fflush( stdout ); getchar();
841#endif
842
843 return( ret );
844}
Azim Khan26a455c2018-02-07 11:11:17 +0000845
846/** Below magic pattern is used with ATCAECC508A demo application (or similar)
847 * running on target to differentiate between user input and cert_write.exe.
848 */
849#define REMOTE_KEY_CMD_TAG "remote"
850#define REMOTE_KEY_MAGIC_PATTERN "rEmOtEkEy"
851#define REMOTE_KEY_ID_MIN 0
852#define REMOTE_KEY_ID_MAX 7
853#define REMOTE_KEY_SERIAL_BAUD CBR_9600
854
855#define REMOTE_KEY_FUNC_GET_PUBKEY 0xA
856#define REMOTE_KEY_FUNC_SIGN 0xB
857
858extern mbedtls_pk_info_t mbedtls_eckey_info;
859
860int is_remote_key( const char * remote_info )
861{
862 size_t tag_len = strlen( REMOTE_KEY_CMD_TAG );
863 printf ("is_remote_key %s\n", remote_info);
864 if ( strlen( remote_info ) > tag_len &&
865 strncmp( remote_info, REMOTE_KEY_CMD_TAG, tag_len ) == 0 )
866 return 1;
867 return 0;
868}
869
870/** Load a transparent public key context with public key from remote device
871 * over serial.
872 * This function sends:
873 * rEmOtEkEy<char encoded function code=GetPubKey><char encoded private key ID>
874 * Receives:
875 * <4 bytes length indicator in network order><concatenated public key>
876 */
877int load_pubkey_from_remote( const char * remote_info, mbedtls_pk_context * ctx )
878{
879 int key_idx = 0, offset = 0, ret = 0;
880 const char * serial_port = NULL;
881 unsigned char func_buffer[10];
882 unsigned char pub_key_buf[100];
883 size_t rx_len = 0;
884 static mbedtls_ecp_keypair ecp_key;
885
886 offset = strlen( REMOTE_KEY_CMD_TAG );
887 key_idx = (int)remote_info[offset++];
888 key_idx = key_idx - 48; // ascii to decimal
889
890 if ( key_idx < REMOTE_KEY_ID_MIN || key_idx > REMOTE_KEY_ID_MAX )
891 {
892 mbedtls_printf( " failed\n ! Invalid remote key index %d\n\n", key_idx );
893 return( -1 );
894 }
895 serial_port = remote_info + offset;
896
897 /* Prepare command */
898 offset = 0;
899 func_buffer[offset++] = REMOTE_KEY_FUNC_GET_PUBKEY;
900 func_buffer[offset++] = key_idx;
901
902 if ( serial_xfer( serial_port, func_buffer, offset, pub_key_buf, sizeof( pub_key_buf ), &rx_len ) != 0 )
903 {
904 mbedtls_printf( " failed\n ! Serial error trying to get pulic key\n\n" );
905 return( -1 );
906 }
907
908 /* Import public key from received binary */
909 mbedtls_ecp_keypair_init(&ecp_key);
910 ret = mbedtls_ecp_group_load(&ecp_key.grp, MBEDTLS_ECP_DP_SECP256R1);
911 if ( ret != 0 )
912 return( -1 );
913 ret = mbedtls_ecp_point_read_binary(&ecp_key.grp, &ecp_key.Q, pub_key_buf, rx_len );
914 if ( ret != 0 )
915 {
916 mbedtls_printf( " failed\n ! Failed to read ecp key from binary\n\n" );
917 return( -1 );
918 }
919 ctx->pk_info = &mbedtls_eckey_info;
920 ctx->pk_ctx = &ecp_key;
921 return( 0 );
922}
923
924/**
925 * @brief Tell if can do the operation given by type
926 *
927 * @param type Target type
928 *
929 * @return 0 if context can't do the operations,
930 * 1 otherwise.
931 */
932static int remote_can_do_func(const void *ctx, mbedtls_pk_type_t type)
933{
934 UNUSED(ctx);
935 /* At the moment on ECDSA is supported */
936 return (MBEDTLS_PK_ECDSA == type);
937}
938
939/**
940 * @brief Use STSAFE private key for signature.
941 *
942 * @param ctx ECDSA context
943 * @param md_alg Algorithm that was used to hash the message
944 * @param hash Message hash
945 * @param hash_len Length of hash
946 * @param sig Buffer that will hold the signature
947 * @param sig_len Length of the signature written
948 * @param f_rng RNG function
949 * @param p_rng RNG parameter
950 *
951 * @retval 0 if successful, or 1.
952 */
953static int remote_sign_func(void *ctx, mbedtls_md_type_t md_alg,
954 const unsigned char *hash, size_t hash_len,
955 unsigned char *sig, size_t *sig_len,
956 int (*f_rng)(void *, unsigned char *, size_t),
957 void *p_rng)
958{
959 remote_serial_pk_context * remote_ctx = (remote_serial_pk_context *)ctx;
960 unsigned char func_buffer[1024];
961 size_t offset = 0;
962
963 UNUSED( f_rng );
964 UNUSED( p_rng );
965
966 if ( md_alg != MBEDTLS_MD_SHA256 )
967 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
968
969 func_buffer[offset++] = REMOTE_KEY_FUNC_SIGN;
970 func_buffer[offset++] = remote_ctx->key_idx;
971 func_buffer[offset++] = hash_len >> 24;
972 func_buffer[offset++] = hash_len >> 16;
973 func_buffer[offset++] = hash_len >> 8;
974 func_buffer[offset++] = hash_len & 0xff;
975
976 memcpy( func_buffer + offset, hash, hash_len );
977 offset += hash_len;
978
979 if ( serial_xfer( remote_ctx->serial_port, func_buffer, offset, sig, 100/* FIXME */, sig_len ) != 0 )
980 {
981 mbedtls_printf( " failed\n ! Serial error in signing\n\n" );
982 return( -1 );
983 }
984
985 return( 0 );
986}
987
988int mbedtls_pk_remote_setup( mbedtls_pk_context * ctx, const char * serial_port, unsigned char key_idx )
989{
990 // allocate remote serial context
991 static remote_serial_pk_context remote;
992 /* Opaque private key */
993 static const mbedtls_pk_info_t remote_pk_info =
994 {
995 /* MBEDTLS_PK_ECKEY, */
996 MBEDTLS_PK_OPAQUE,
997 "RemoteSerial",
998 NULL,
999 remote_can_do_func,
1000 NULL,
1001 NULL,
1002 remote_sign_func,
1003 NULL,
1004 NULL,
1005 NULL,
1006 NULL,
1007 NULL,
1008 NULL
1009 };
1010
1011
1012 if ( ctx == NULL )
1013 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1014
1015 remote.serial_port = serial_port;
1016 remote.key_idx = key_idx;
1017 ctx->pk_ctx = (void *)&remote;
1018 ctx->pk_info = &remote_pk_info;
1019
1020 return( 0 );
1021}
1022
1023void mbedtls_pk_remote_free( mbedtls_pk_context * ctx )
1024{
1025 /* Nothing to free since remote context is statically allocated.
1026 * Within this app there is no need to scrub the memory.
1027 */
1028 UNUSED( ctx );
1029}
1030
1031int setup_opaque_privkey( const char * remote_info, mbedtls_pk_context * ctx )
1032{
1033 int key_idx = 0, offset = 0, ret = 0;
1034 const char * serial_port = NULL;
1035
1036 offset = strlen( REMOTE_KEY_CMD_TAG );
1037 key_idx = (int)remote_info[offset++];
1038 key_idx = key_idx - 48; // ascii to decimal
1039
1040 if ( key_idx < REMOTE_KEY_ID_MIN || key_idx > REMOTE_KEY_ID_MAX )
1041 {
1042 mbedtls_printf( " failed\n ! Invalid remote key index %d\n\n", key_idx );
1043 return( -1 );
1044 }
1045 serial_port = remote_info + offset;
1046 ret = mbedtls_pk_remote_setup( ctx, serial_port, key_idx );
1047 if ( ret != 0 )
1048 {
1049 mbedtls_printf( " failed\n ! remote pk setup failure \n\n" );
1050 return( -1 );
1051 }
1052
1053 return( 0 );
1054}
1055
1056int serial_xfer( const char * serial_port, const unsigned char * tx_buf,
1057 size_t tx_buf_len, unsigned char * rx_buf, size_t rx_buf_len,
1058 size_t * rx_len )
1059{
Azim Khan506849b2018-02-17 16:00:06 +00001060 char c, comm_name[20];
Azim Khan26a455c2018-02-07 11:11:17 +00001061 HANDLE hComm;
1062 DCB dcbConfig;
1063 COMMTIMEOUTS commTimeout;
1064 DWORD xfer_len;
1065 unsigned char len_buf[sizeof(size_t)];
1066 int ret = -1;
1067 size_t len = 0, sync_pattern_idx = 0;
1068
1069 do
1070 {
1071 sprintf( comm_name, "\\\\.\\%s", serial_port );
1072
1073 // Open port
1074 hComm = CreateFile( comm_name, GENERIC_READ | GENERIC_WRITE, 0, 0,
1075 OPEN_EXISTING, 0, 0 );
1076 if ( hComm == INVALID_HANDLE_VALUE )
1077 {
1078 mbedtls_printf( " failed\n ! failed to open port %s %lu\n\n", serial_port, GetLastError() );
1079 break;
1080 }
1081
1082 if( GetCommState( hComm, &dcbConfig ) )
1083 {
Azim Khan26a455c2018-02-07 11:11:17 +00001084 dcbConfig.BaudRate = REMOTE_KEY_SERIAL_BAUD;
1085 dcbConfig.Parity = NOPARITY;
1086 dcbConfig.ByteSize = 8;
1087 dcbConfig.StopBits = ONESTOPBIT;
1088 dcbConfig.fOutxCtsFlow = FALSE; // No CTS output flow control
1089 dcbConfig.fOutxDsrFlow = FALSE; // No DSR output flow control
1090 dcbConfig.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type
1091 dcbConfig.fDsrSensitivity = FALSE; // DSR sensitivity
1092 dcbConfig.fTXContinueOnXoff = TRUE; // XOFF continues Tx
1093 dcbConfig.fOutX = FALSE; // No XON/XOFF out flow control
1094 dcbConfig.fInX = FALSE; // No XON/XOFF in flow control
1095 dcbConfig.fErrorChar = FALSE; // Disable error replacement
1096 dcbConfig.fNull = FALSE; // Disable null stripping
1097 dcbConfig.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control
1098 dcbConfig.fAbortOnError = FALSE; // Do not abort reads/writes on error
1099 }
1100 else
1101 break;
1102
1103 if( !SetCommState( hComm, &dcbConfig ) )
1104 break;
1105
1106 if( GetCommTimeouts( hComm, &commTimeout ) )
1107 {
1108 commTimeout.ReadIntervalTimeout = 1000;
1109 commTimeout.ReadTotalTimeoutMultiplier = 10;
1110 commTimeout.ReadTotalTimeoutConstant = 1000;
1111 commTimeout.WriteTotalTimeoutConstant = 1000;
1112 commTimeout.WriteTotalTimeoutMultiplier = 10;
1113 }
1114 else
1115 break;
1116
1117 if( !SetCommTimeouts( hComm, &commTimeout ) )
1118 break;
1119
1120
Azim Khan506849b2018-02-17 16:00:06 +00001121 /* Flush data on serial before sending sync pattern */
1122 while( ReadFile( hComm, &c, sizeof(c), &xfer_len, NULL ) && xfer_len != 0 );
Azim Khan26a455c2018-02-07 11:11:17 +00001123 /* Sync with peer */
1124 if( !WriteFile( hComm, REMOTE_KEY_MAGIC_PATTERN, strlen(REMOTE_KEY_MAGIC_PATTERN),
1125 &xfer_len, NULL ) )
1126 break;
1127
1128 while( sync_pattern_idx != strlen(REMOTE_KEY_MAGIC_PATTERN) )
1129 {
Azim Khan26a455c2018-02-07 11:11:17 +00001130 if( !ReadFile( hComm, &c, sizeof(c), &xfer_len, NULL ) )
1131 break;
1132 if ( c == REMOTE_KEY_MAGIC_PATTERN[sync_pattern_idx] )
1133 sync_pattern_idx++;
1134 else
1135 sync_pattern_idx = 0;
1136 }
1137
1138 /* Exit if there was a read error */
1139 if ( sync_pattern_idx != strlen(REMOTE_KEY_MAGIC_PATTERN) )
1140 {
1141 printf("Failedi to sync!");
1142 break;
1143 }
1144
1145 {
1146 size_t i;
1147 printf("Tx: ");
1148 for (i = 0; i < tx_buf_len; i++)
1149 printf ("0x%02x ", (tx_buf)[i]);
1150 printf("\n");
1151 }
1152 if( !WriteFile( hComm, tx_buf, tx_buf_len,
1153 &xfer_len, NULL ) )
1154 break;
1155
1156 /* Read length indicator */
1157 if( !ReadFile( hComm, len_buf, sizeof(len_buf), &xfer_len, NULL ) )
1158 break;
1159
1160 *rx_len = ( len_buf[0] << 24 ) | ( len_buf[1] << 16 ) | ( len_buf[2] << 8 ) | len_buf[3];
1161 if ( *rx_len > rx_buf_len )
1162 return( -1 );
1163 /* Read payload */
1164 while( len < *rx_len )
1165 {
1166 if( !ReadFile( hComm, rx_buf + len, *rx_len - len, &xfer_len, NULL ) )
1167 break;
1168 len += xfer_len;
1169 }
1170 printf("Received LI 0x%02x 0x%02x 0x%02x 0x%02x \n", len_buf[0], len_buf[1], len_buf[2], len_buf[3]);
1171 {
1172 size_t i;
1173 printf("Rx: ");
1174 for (i = 0; i < *rx_len; i++)
1175 printf ("0x%02x ", (rx_buf)[i]);
1176 printf("\n");
1177 }
1178
1179 ret = 0;
1180 } while( 0 );
1181
1182 if( hComm != INVALID_HANDLE_VALUE )
1183 {
1184 CloseHandle( hComm );
1185 hComm = INVALID_HANDLE_VALUE;
1186 }
1187
1188 return( ret );
1189}
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
1192 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +01001193 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */