Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate generation and signing |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #define mbedtls_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 35 | #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é-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 40 | int main( void ) |
| 41 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or " |
Manuel Pégourié-Gonnard | d738965 | 2015-08-06 18:22:26 +0200 | [diff] [blame] | 43 | "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or " |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 45 | "MBEDTLS_ERROR_C not defined.\n"); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 46 | return( 0 ); |
| 47 | } |
| 48 | #else |
| 49 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/x509_crt.h" |
| 51 | #include "mbedtls/x509_csr.h" |
| 52 | #include "mbedtls/entropy.h" |
| 53 | #include "mbedtls/ctr_drbg.h" |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 54 | #include "mbedtls/md.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 56 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 57 | #include <stdio.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 62 | #define USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 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" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 66 | #else |
| 67 | #define USAGE_CSR "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 69 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 70 | #define DFL_ISSUER_CRT "" |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 71 | #define DFL_REQUEST_FILE "" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 72 | #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é-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 77 | #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" |
| 78 | #define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 79 | #define DFL_NOT_BEFORE "20010101000000" |
| 80 | #define DFL_NOT_AFTER "20301231235959" |
| 81 | #define DFL_SERIAL "1" |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 82 | #define DFL_SELFSIGN 0 |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 83 | #define DFL_IS_CA 0 |
| 84 | #define DFL_MAX_PATHLEN -1 |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 85 | #define DFL_KEY_USAGE 0 |
| 86 | #define DFL_NS_CERT_TYPE 0 |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 87 | #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 Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 92 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 93 | #define USAGE \ |
| 94 | "\n usage: cert_write param=<>...\n" \ |
| 95 | "\n acceptable parameters:\n" \ |
| 96 | USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 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" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 100 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 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" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 105 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 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" \ |
| 118 | " 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=%%s default: 1\n" \ |
| 124 | " Possible values: 0, 1\n" \ |
| 125 | " (Considered for v3 only)\n"\ |
| 126 | " authority_identifier=%%s default: 1\n" \ |
| 127 | " Possible values: 0, 1\n" \ |
| 128 | " (Considered for v3 only)\n"\ |
| 129 | " basic_constraints=%%d default: 1\n" \ |
| 130 | " Possible values: 0, 1\n" \ |
| 131 | " (Considered for v3 only)\n"\ |
| 132 | " 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" \ |
| 139 | " key_cert_sign\n" \ |
| 140 | " crl_sign\n" \ |
| 141 | " (Considered for v3 only)\n"\ |
| 142 | " 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" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 151 | "\n" |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 152 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 153 | /* |
| 154 | * global options |
| 155 | */ |
| 156 | struct options |
| 157 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 158 | 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 Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 170 | int selfsign; /* selfsign the certificate */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 171 | int is_ca; /* is a CA certificate */ |
| 172 | int max_pathlen; /* maximum CA path length */ |
Hanno Becker | e1b1d0a | 2017-09-22 15:35:16 +0100 | [diff] [blame] | 173 | int authority_identifier; /* add authority identifier to CRT */ |
| 174 | int subject_identifier; /* add subject identifier to CRT */ |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 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 Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 178 | unsigned char key_usage; /* key usage flags */ |
| 179 | unsigned char ns_cert_type; /* NS cert type */ |
| 180 | } opt; |
| 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | int write_certificate( mbedtls_x509write_cert *crt, const char *output_file, |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 183 | int (*f_rng)(void *, unsigned char *, size_t), |
| 184 | void *p_rng ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 185 | { |
| 186 | int ret; |
| 187 | FILE *f; |
| 188 | unsigned char output_buf[4096]; |
| 189 | size_t len = 0; |
| 190 | |
| 191 | memset( output_buf, 0, 4096 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 192 | if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, |
| 193 | f_rng, p_rng ) ) < 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 194 | return( ret ); |
| 195 | |
| 196 | len = strlen( (char *) output_buf ); |
| 197 | |
| 198 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 199 | return( -1 ); |
| 200 | |
| 201 | if( fwrite( output_buf, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 202 | { |
| 203 | fclose( f ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 204 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 205 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 206 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 207 | fclose( f ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 208 | |
| 209 | return( 0 ); |
| 210 | } |
| 211 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 212 | int main( int argc, char *argv[] ) |
| 213 | { |
| 214 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | mbedtls_x509_crt issuer_crt; |
| 216 | mbedtls_pk_context loaded_issuer_key, loaded_subject_key; |
| 217 | mbedtls_pk_context *issuer_key = &loaded_issuer_key, |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 218 | *subject_key = &loaded_subject_key; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 219 | char buf[1024]; |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 220 | char issuer_name[256]; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 221 | int i; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 222 | char *p, *q, *r; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 224 | char subject_name[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | mbedtls_x509_csr csr; |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 226 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | mbedtls_x509write_cert crt; |
| 228 | mbedtls_mpi serial; |
| 229 | mbedtls_entropy_context entropy; |
| 230 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 231 | const char *pers = "crt example app"; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * Set to sane values |
| 235 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_x509write_crt_init( &crt ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | mbedtls_pk_init( &loaded_issuer_key ); |
| 238 | mbedtls_pk_init( &loaded_subject_key ); |
| 239 | mbedtls_mpi_init( &serial ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 240 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
| 242 | mbedtls_x509_csr_init( &csr ); |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 243 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_x509_crt_init( &issuer_crt ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 245 | memset( buf, 0, 1024 ); |
| 246 | |
| 247 | if( argc == 0 ) |
| 248 | { |
| 249 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_printf( USAGE ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 251 | ret = 1; |
| 252 | goto exit; |
| 253 | } |
| 254 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 255 | opt.issuer_crt = DFL_ISSUER_CRT; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 256 | opt.request_file = DFL_REQUEST_FILE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 257 | opt.subject_key = DFL_SUBJECT_KEY; |
| 258 | opt.issuer_key = DFL_ISSUER_KEY; |
| 259 | opt.subject_pwd = DFL_SUBJECT_PWD; |
| 260 | opt.issuer_pwd = DFL_ISSUER_PWD; |
| 261 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 262 | opt.subject_name = DFL_SUBJECT_NAME; |
| 263 | opt.issuer_name = DFL_ISSUER_NAME; |
| 264 | opt.not_before = DFL_NOT_BEFORE; |
| 265 | opt.not_after = DFL_NOT_AFTER; |
| 266 | opt.serial = DFL_SERIAL; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 267 | opt.selfsign = DFL_SELFSIGN; |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 268 | opt.is_ca = DFL_IS_CA; |
| 269 | opt.max_pathlen = DFL_MAX_PATHLEN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 270 | opt.key_usage = DFL_KEY_USAGE; |
| 271 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 272 | opt.version = DFL_VERSION - 1; |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 273 | opt.md = DFL_DIGEST; |
| 274 | opt.subject_identifier = DFL_SUBJ_IDENT; |
| 275 | opt.authority_identifier = DFL_AUTH_IDENT; |
| 276 | opt.basic_constraints = DFL_CONSTRAINTS; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 277 | |
| 278 | for( i = 1; i < argc; i++ ) |
| 279 | { |
| 280 | |
| 281 | p = argv[i]; |
| 282 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 283 | goto usage; |
| 284 | *q++ = '\0'; |
| 285 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 286 | if( strcmp( p, "request_file" ) == 0 ) |
| 287 | opt.request_file = q; |
| 288 | else if( strcmp( p, "subject_key" ) == 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 289 | opt.subject_key = q; |
| 290 | else if( strcmp( p, "issuer_key" ) == 0 ) |
| 291 | opt.issuer_key = q; |
| 292 | else if( strcmp( p, "subject_pwd" ) == 0 ) |
| 293 | opt.subject_pwd = q; |
| 294 | else if( strcmp( p, "issuer_pwd" ) == 0 ) |
| 295 | opt.issuer_pwd = q; |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 296 | else if( strcmp( p, "issuer_crt" ) == 0 ) |
| 297 | opt.issuer_crt = q; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 298 | else if( strcmp( p, "output_file" ) == 0 ) |
| 299 | opt.output_file = q; |
| 300 | else if( strcmp( p, "subject_name" ) == 0 ) |
| 301 | { |
| 302 | opt.subject_name = q; |
| 303 | } |
| 304 | else if( strcmp( p, "issuer_name" ) == 0 ) |
| 305 | { |
| 306 | opt.issuer_name = q; |
| 307 | } |
| 308 | else if( strcmp( p, "not_before" ) == 0 ) |
| 309 | { |
| 310 | opt.not_before = q; |
| 311 | } |
| 312 | else if( strcmp( p, "not_after" ) == 0 ) |
| 313 | { |
| 314 | opt.not_after = q; |
| 315 | } |
| 316 | else if( strcmp( p, "serial" ) == 0 ) |
| 317 | { |
| 318 | opt.serial = q; |
| 319 | } |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 320 | else if( strcmp( p, "authority_identifier" ) == 0 ) |
| 321 | { |
| 322 | opt.authority_identifier = atoi( q ); |
| 323 | if( opt.authority_identifier != 0 && |
| 324 | opt.authority_identifier != 1 ) |
| 325 | { |
| 326 | goto usage; |
| 327 | } |
| 328 | } |
| 329 | else if( strcmp( p, "subject_identifier" ) == 0 ) |
| 330 | { |
| 331 | opt.subject_identifier = atoi( q ); |
| 332 | if( opt.subject_identifier != 0 && |
| 333 | opt.subject_identifier != 1 ) |
| 334 | { |
| 335 | goto usage; |
| 336 | } |
| 337 | } |
| 338 | else if( strcmp( p, "basic_constraints" ) == 0 ) |
| 339 | { |
| 340 | opt.basic_constraints = atoi( q ); |
| 341 | if( opt.basic_constraints != 0 && |
| 342 | opt.basic_constraints != 1 ) |
| 343 | { |
| 344 | goto usage; |
| 345 | } |
| 346 | } |
| 347 | else if( strcmp( p, "md" ) == 0 ) |
| 348 | { |
| 349 | if( strcmp( q, "SHA1" ) == 0 ) |
| 350 | opt.md = MBEDTLS_MD_SHA1; |
| 351 | else if( strcmp( q, "SHA256" ) == 0 ) |
| 352 | opt.md = MBEDTLS_MD_SHA256; |
| 353 | else if( strcmp( q, "SHA512" ) == 0 ) |
| 354 | opt.md = MBEDTLS_MD_SHA512; |
| 355 | else if( strcmp( q, "MD5" ) == 0 ) |
| 356 | opt.md = MBEDTLS_MD_MD5; |
| 357 | else |
| 358 | goto usage; |
| 359 | } |
| 360 | else if( strcmp( p, "version" ) == 0 ) |
| 361 | { |
| 362 | opt.version = atoi( q ); |
| 363 | if( opt.version < 1 || opt.version > 3 ) |
| 364 | goto usage; |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 365 | opt.version--; |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 366 | } |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 367 | else if( strcmp( p, "selfsign" ) == 0 ) |
| 368 | { |
| 369 | opt.selfsign = atoi( q ); |
| 370 | if( opt.selfsign < 0 || opt.selfsign > 1 ) |
| 371 | goto usage; |
| 372 | } |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 373 | else if( strcmp( p, "is_ca" ) == 0 ) |
| 374 | { |
| 375 | opt.is_ca = atoi( q ); |
| 376 | if( opt.is_ca < 0 || opt.is_ca > 1 ) |
| 377 | goto usage; |
| 378 | } |
| 379 | else if( strcmp( p, "max_pathlen" ) == 0 ) |
| 380 | { |
| 381 | opt.max_pathlen = atoi( q ); |
| 382 | if( opt.max_pathlen < -1 || opt.max_pathlen > 127 ) |
| 383 | goto usage; |
| 384 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 385 | else if( strcmp( p, "key_usage" ) == 0 ) |
| 386 | { |
| 387 | while( q != NULL ) |
| 388 | { |
| 389 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 390 | *r++ = '\0'; |
| 391 | |
| 392 | if( strcmp( q, "digital_signature" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 394 | else if( strcmp( q, "non_repudiation" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 396 | else if( strcmp( q, "key_encipherment" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 397 | opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 398 | else if( strcmp( q, "data_encipherment" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 399 | opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 400 | else if( strcmp( q, "key_agreement" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 401 | opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 402 | else if( strcmp( q, "key_cert_sign" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 403 | opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 404 | else if( strcmp( q, "crl_sign" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 406 | else |
| 407 | goto usage; |
| 408 | |
| 409 | q = r; |
| 410 | } |
| 411 | } |
| 412 | else if( strcmp( p, "ns_cert_type" ) == 0 ) |
| 413 | { |
| 414 | while( q != NULL ) |
| 415 | { |
| 416 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 417 | *r++ = '\0'; |
| 418 | |
| 419 | if( strcmp( q, "ssl_client" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 421 | else if( strcmp( q, "ssl_server" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 422 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 423 | else if( strcmp( q, "email" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 425 | else if( strcmp( q, "object_signing" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 427 | else if( strcmp( q, "ssl_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 428 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 429 | else if( strcmp( q, "email_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 430 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 431 | else if( strcmp( q, "object_signing_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 432 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 433 | else |
| 434 | goto usage; |
| 435 | |
| 436 | q = r; |
| 437 | } |
| 438 | } |
| 439 | else |
| 440 | goto usage; |
| 441 | } |
| 442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | mbedtls_printf("\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 444 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 445 | /* |
| 446 | * 0. Seed the PRNG |
| 447 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | mbedtls_printf( " . Seeding the random number generator..." ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 449 | fflush( stdout ); |
| 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 452 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 453 | (const unsigned char *) pers, |
| 454 | strlen( pers ) ) ) != 0 ) |
| 455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 457 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", |
| 458 | ret, buf ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 459 | goto exit; |
| 460 | } |
| 461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 462 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 463 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 464 | // Parse serial to MPI |
| 465 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | mbedtls_printf( " . Reading serial number..." ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 467 | fflush( stdout ); |
| 468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 470 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 471 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 472 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_string " |
| 473 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 474 | goto exit; |
| 475 | } |
| 476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 478 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 479 | // Parse issuer certificate if present |
| 480 | // |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 481 | if( !opt.selfsign && strlen( opt.issuer_crt ) ) |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 482 | { |
| 483 | /* |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 484 | * 1.0.a. Load the certificates |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 485 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | mbedtls_printf( " . Loading the issuer certificate ..." ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 487 | fflush( stdout ); |
| 488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 ) |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 490 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 492 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file " |
| 493 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 494 | goto exit; |
| 495 | } |
| 496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 497 | ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name), |
Paul Bakker | fdba468 | 2014-04-25 11:48:35 +0200 | [diff] [blame] | 498 | &issuer_crt.subject ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 499 | if( ret < 0 ) |
| 500 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 502 | mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets " |
| 503 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 504 | goto exit; |
| 505 | } |
| 506 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 507 | opt.issuer_name = issuer_name; |
| 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | mbedtls_printf( " ok\n" ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 513 | // Parse certificate request if present |
| 514 | // |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 515 | if( !opt.selfsign && strlen( opt.request_file ) ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 516 | { |
| 517 | /* |
| 518 | * 1.0.b. Load the CSR |
| 519 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | mbedtls_printf( " . Loading the certificate request ..." ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 521 | fflush( stdout ); |
| 522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 526 | mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file " |
| 527 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 528 | goto exit; |
| 529 | } |
| 530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name), |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 532 | &csr.subject ); |
| 533 | if( ret < 0 ) |
| 534 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 536 | mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets " |
| 537 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 538 | goto exit; |
| 539 | } |
| 540 | |
| 541 | opt.subject_name = subject_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 542 | subject_key = &csr.pk; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 545 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * 1.1. Load the keys |
| 550 | */ |
| 551 | if( !opt.selfsign && !strlen( opt.request_file ) ) |
| 552 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | mbedtls_printf( " . Loading the subject key ..." ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 554 | fflush( stdout ); |
| 555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key, |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 557 | opt.subject_pwd ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 558 | if( ret != 0 ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 561 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile " |
| 562 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 563 | goto exit; |
| 564 | } |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | mbedtls_printf( " . Loading the issuer key ..." ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 570 | fflush( stdout ); |
| 571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 573 | opt.issuer_pwd ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 574 | if( ret != 0 ) |
| 575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 577 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile " |
| 578 | "returned -x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 579 | goto exit; |
| 580 | } |
| 581 | |
| 582 | // Check if key and issuer certificate match |
| 583 | // |
| 584 | if( strlen( opt.issuer_crt ) ) |
| 585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) || |
| 587 | mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N, |
| 588 | &mbedtls_pk_rsa( *issuer_key )->N ) != 0 || |
| 589 | mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E, |
| 590 | &mbedtls_pk_rsa( *issuer_key )->E ) != 0 ) |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 591 | { |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 592 | mbedtls_printf( " failed\n ! issuer_key does not match " |
| 593 | "issuer certificate\n\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 594 | ret = -1; |
| 595 | goto exit; |
| 596 | } |
| 597 | } |
| 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 600 | |
| 601 | if( opt.selfsign ) |
| 602 | { |
Paul Bakker | 93c6aa4 | 2013-10-28 22:28:09 +0100 | [diff] [blame] | 603 | opt.subject_name = opt.issuer_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 604 | subject_key = issuer_key; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | mbedtls_x509write_crt_set_subject_key( &crt, subject_key ); |
| 608 | mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 609 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 610 | /* |
| 611 | * 1.0. Check the names for validity |
| 612 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 616 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name " |
| 617 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 618 | goto exit; |
| 619 | } |
| 620 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 622 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 624 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name " |
| 625 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 626 | goto exit; |
| 627 | } |
| 628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_printf( " . Setting certificate values ..." ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 630 | fflush( stdout ); |
| 631 | |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 632 | mbedtls_x509write_crt_set_version( &crt, opt.version ); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 633 | mbedtls_x509write_crt_set_md_alg( &crt, opt.md ); |
| 634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | ret = mbedtls_x509write_crt_set_serial( &crt, &serial ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 636 | if( ret != 0 ) |
| 637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 639 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial " |
| 640 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 641 | goto exit; |
| 642 | } |
| 643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 645 | if( ret != 0 ) |
| 646 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 648 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity " |
| 649 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 650 | goto exit; |
| 651 | } |
| 652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 654 | |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 655 | if( opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 656 | opt.basic_constraints != 0 ) |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 657 | { |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 658 | mbedtls_printf( " . Adding the Basic Constraints extension ..." ); |
| 659 | fflush( stdout ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 660 | |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 661 | ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca, |
| 662 | opt.max_pathlen ); |
| 663 | if( ret != 0 ) |
| 664 | { |
| 665 | mbedtls_strerror( ret, buf, 1024 ); |
| 666 | mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints " |
| 667 | "returned -0x%02x - %s\n\n", -ret, buf ); |
| 668 | goto exit; |
| 669 | } |
| 670 | |
| 671 | mbedtls_printf( " ok\n" ); |
| 672 | } |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | #if defined(MBEDTLS_SHA1_C) |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 675 | if( opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 676 | opt.subject_identifier != 0 ) |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 677 | { |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 678 | mbedtls_printf( " . Adding the Subject Key Identifier ..." ); |
| 679 | fflush( stdout ); |
| 680 | |
| 681 | ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt ); |
| 682 | if( ret != 0 ) |
| 683 | { |
| 684 | mbedtls_strerror( ret, buf, 1024 ); |
| 685 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject" |
| 686 | "_key_identifier returned -0x%02x - %s\n\n", |
| 687 | -ret, buf ); |
| 688 | goto exit; |
| 689 | } |
| 690 | |
| 691 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 694 | if( opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 695 | opt.authority_identifier != 0 ) |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 696 | { |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 697 | mbedtls_printf( " . Adding the Authority Key Identifier ..." ); |
| 698 | fflush( stdout ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 699 | |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 700 | ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt ); |
| 701 | if( ret != 0 ) |
| 702 | { |
| 703 | mbedtls_strerror( ret, buf, 1024 ); |
| 704 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_" |
| 705 | "key_identifier returned -0x%02x - %s\n\n", |
| 706 | -ret, buf ); |
| 707 | goto exit; |
| 708 | } |
| 709 | |
| 710 | mbedtls_printf( " ok\n" ); |
| 711 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 713 | |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 714 | if( opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 715 | opt.key_usage != 0 ) |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 716 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | mbedtls_printf( " . Adding the Key Usage extension ..." ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 718 | fflush( stdout ); |
| 719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 721 | if( ret != 0 ) |
| 722 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 724 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage " |
| 725 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 726 | goto exit; |
| 727 | } |
| 728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 729 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame^] | 732 | if( opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 733 | opt.ns_cert_type != 0 ) |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 734 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | mbedtls_printf( " . Adding the NS Cert Type extension ..." ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 736 | fflush( stdout ); |
| 737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 739 | if( ret != 0 ) |
| 740 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 742 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type " |
| 743 | "returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 744 | goto exit; |
| 745 | } |
| 746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 747 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 750 | /* |
| 751 | * 1.2. Writing the request |
| 752 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | mbedtls_printf( " . Writing the certificate..." ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 754 | fflush( stdout ); |
| 755 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 756 | if( ( ret = write_certificate( &crt, opt.output_file, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 758 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | mbedtls_strerror( ret, buf, 1024 ); |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 760 | mbedtls_printf( " failed\n ! write_certificate -0x%02x - %s\n\n", |
| 761 | -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 762 | goto exit; |
| 763 | } |
| 764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 766 | |
| 767 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | mbedtls_x509write_crt_free( &crt ); |
| 769 | mbedtls_pk_free( &loaded_subject_key ); |
| 770 | mbedtls_pk_free( &loaded_issuer_key ); |
| 771 | mbedtls_mpi_free( &serial ); |
| 772 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 773 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 774 | |
| 775 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 776 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 777 | fflush( stdout ); getchar(); |
| 778 | #endif |
| 779 | |
| 780 | return( ret ); |
| 781 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && |
| 783 | MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 784 | MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */ |