Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate generation and signing |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #endif |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define mbedtls_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if !defined(MBEDTLS_X509_CRT_WRITE_C) || \ |
| 37 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 38 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 39 | !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_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 " |
| 43 | "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and_or " |
| 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" |
| 54 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 55 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 56 | #include <stdio.h> |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 61 | #define USAGE_CSR \ |
| 62 | " request_file=%%s default: (empty)\n" \ |
| 63 | " If request_file is specified, subject_key,\n" \ |
| 64 | " subject_pwd and subject_name are ignored!\n" |
| 65 | #else |
| 66 | #define USAGE_CSR "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 68 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 69 | #define DFL_ISSUER_CRT "" |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 70 | #define DFL_REQUEST_FILE "" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 71 | #define DFL_SUBJECT_KEY "subject.key" |
| 72 | #define DFL_ISSUER_KEY "ca.key" |
| 73 | #define DFL_SUBJECT_PWD "" |
| 74 | #define DFL_ISSUER_PWD "" |
| 75 | #define DFL_OUTPUT_FILENAME "cert.crt" |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 76 | #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" |
| 77 | #define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 78 | #define DFL_NOT_BEFORE "20010101000000" |
| 79 | #define DFL_NOT_AFTER "20301231235959" |
| 80 | #define DFL_SERIAL "1" |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 81 | #define DFL_SELFSIGN 0 |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 82 | #define DFL_IS_CA 0 |
| 83 | #define DFL_MAX_PATHLEN -1 |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 84 | #define DFL_KEY_USAGE 0 |
| 85 | #define DFL_NS_CERT_TYPE 0 |
| 86 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 87 | #define USAGE \ |
| 88 | "\n usage: cert_write param=<>...\n" \ |
| 89 | "\n acceptable parameters:\n" \ |
| 90 | USAGE_CSR \ |
| 91 | " subject_key=%%s default: subject.key\n" \ |
| 92 | " subject_pwd=%%s default: (empty)\n" \ |
| 93 | " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \ |
| 94 | "\n" \ |
| 95 | " issuer_crt=%%s default: (empty)\n" \ |
| 96 | " If issuer_crt is specified, issuer_name is\n" \ |
| 97 | " ignored!\n" \ |
| 98 | " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \ |
| 99 | "\n" \ |
| 100 | " selfsign=%%d default: 0 (false)\n" \ |
| 101 | " If selfsign is enabled, issuer_name and\n" \ |
| 102 | " issuer_key are required (issuer_crt and\n" \ |
| 103 | " subject_* are ignored\n" \ |
| 104 | " issuer_key=%%s default: ca.key\n" \ |
| 105 | " issuer_pwd=%%s default: (empty)\n" \ |
| 106 | " output_file=%%s default: cert.crt\n" \ |
| 107 | " serial=%%s default: 1\n" \ |
| 108 | " not_before=%%s default: 20010101000000\n"\ |
| 109 | " not_after=%%s default: 20301231235959\n"\ |
| 110 | " is_ca=%%d default: 0 (disabled)\n" \ |
| 111 | " max_pathlen=%%d default: -1 (none)\n" \ |
| 112 | " key_usage=%%s default: (empty)\n" \ |
| 113 | " Comma-separated-list of values:\n" \ |
| 114 | " digital_signature\n" \ |
| 115 | " non_repudiation\n" \ |
| 116 | " key_encipherment\n" \ |
| 117 | " data_encipherment\n" \ |
| 118 | " key_agreement\n" \ |
| 119 | " key_certificate_sign\n" \ |
| 120 | " crl_sign\n" \ |
| 121 | " ns_cert_type=%%s default: (empty)\n" \ |
| 122 | " Comma-separated-list of values:\n" \ |
| 123 | " ssl_client\n" \ |
| 124 | " ssl_server\n" \ |
| 125 | " email\n" \ |
| 126 | " object_signing\n" \ |
| 127 | " ssl_ca\n" \ |
| 128 | " email_ca\n" \ |
| 129 | " object_signing_ca\n" \ |
| 130 | "\n" |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 131 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 132 | /* |
| 133 | * global options |
| 134 | */ |
| 135 | struct options |
| 136 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 137 | const char *issuer_crt; /* filename of the issuer certificate */ |
| 138 | const char *request_file; /* filename of the certificate request */ |
| 139 | const char *subject_key; /* filename of the subject key file */ |
| 140 | const char *issuer_key; /* filename of the issuer key file */ |
| 141 | const char *subject_pwd; /* password for the subject key file */ |
| 142 | const char *issuer_pwd; /* password for the issuer key file */ |
| 143 | const char *output_file; /* where to store the constructed key file */ |
| 144 | const char *subject_name; /* subject name for certificate */ |
| 145 | const char *issuer_name; /* issuer name for certificate */ |
| 146 | const char *not_before; /* validity period not before */ |
| 147 | const char *not_after; /* validity period not after */ |
| 148 | const char *serial; /* serial number string */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 149 | int selfsign; /* selfsign the certificate */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 150 | int is_ca; /* is a CA certificate */ |
| 151 | int max_pathlen; /* maximum CA path length */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 152 | unsigned char key_usage; /* key usage flags */ |
| 153 | unsigned char ns_cert_type; /* NS cert type */ |
| 154 | } opt; |
| 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | 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] | 157 | int (*f_rng)(void *, unsigned char *, size_t), |
| 158 | void *p_rng ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 159 | { |
| 160 | int ret; |
| 161 | FILE *f; |
| 162 | unsigned char output_buf[4096]; |
| 163 | size_t len = 0; |
| 164 | |
| 165 | memset( output_buf, 0, 4096 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 167 | return( ret ); |
| 168 | |
| 169 | len = strlen( (char *) output_buf ); |
| 170 | |
| 171 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 172 | return( -1 ); |
| 173 | |
| 174 | if( fwrite( output_buf, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 175 | { |
| 176 | fclose( f ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 177 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 178 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 179 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 180 | fclose( f ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 181 | |
| 182 | return( 0 ); |
| 183 | } |
| 184 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 185 | int main( int argc, char *argv[] ) |
| 186 | { |
| 187 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_x509_crt issuer_crt; |
| 189 | mbedtls_pk_context loaded_issuer_key, loaded_subject_key; |
| 190 | mbedtls_pk_context *issuer_key = &loaded_issuer_key, |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 191 | *subject_key = &loaded_subject_key; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 192 | char buf[1024]; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 193 | char issuer_name[128]; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 194 | int i; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 195 | char *p, *q, *r; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 197 | char subject_name[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | mbedtls_x509_csr csr; |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 199 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | mbedtls_x509write_cert crt; |
| 201 | mbedtls_mpi serial; |
| 202 | mbedtls_entropy_context entropy; |
| 203 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 204 | const char *pers = "crt example app"; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * Set to sane values |
| 208 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_x509write_crt_init( &crt ); |
| 210 | mbedtls_x509write_crt_set_md_alg( &crt, MBEDTLS_MD_SHA256 ); |
| 211 | mbedtls_pk_init( &loaded_issuer_key ); |
| 212 | mbedtls_pk_init( &loaded_subject_key ); |
| 213 | mbedtls_mpi_init( &serial ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame^] | 214 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
| 216 | mbedtls_x509_csr_init( &csr ); |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 217 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | mbedtls_x509_crt_init( &issuer_crt ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 219 | memset( buf, 0, 1024 ); |
| 220 | |
| 221 | if( argc == 0 ) |
| 222 | { |
| 223 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | mbedtls_printf( USAGE ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 225 | ret = 1; |
| 226 | goto exit; |
| 227 | } |
| 228 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 229 | opt.issuer_crt = DFL_ISSUER_CRT; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 230 | opt.request_file = DFL_REQUEST_FILE; |
| 231 | opt.request_file = DFL_REQUEST_FILE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 232 | opt.subject_key = DFL_SUBJECT_KEY; |
| 233 | opt.issuer_key = DFL_ISSUER_KEY; |
| 234 | opt.subject_pwd = DFL_SUBJECT_PWD; |
| 235 | opt.issuer_pwd = DFL_ISSUER_PWD; |
| 236 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 237 | opt.subject_name = DFL_SUBJECT_NAME; |
| 238 | opt.issuer_name = DFL_ISSUER_NAME; |
| 239 | opt.not_before = DFL_NOT_BEFORE; |
| 240 | opt.not_after = DFL_NOT_AFTER; |
| 241 | opt.serial = DFL_SERIAL; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 242 | opt.selfsign = DFL_SELFSIGN; |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 243 | opt.is_ca = DFL_IS_CA; |
| 244 | opt.max_pathlen = DFL_MAX_PATHLEN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 245 | opt.key_usage = DFL_KEY_USAGE; |
| 246 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
| 247 | |
| 248 | for( i = 1; i < argc; i++ ) |
| 249 | { |
| 250 | |
| 251 | p = argv[i]; |
| 252 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 253 | goto usage; |
| 254 | *q++ = '\0'; |
| 255 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 256 | if( strcmp( p, "request_file" ) == 0 ) |
| 257 | opt.request_file = q; |
| 258 | else if( strcmp( p, "subject_key" ) == 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 259 | opt.subject_key = q; |
| 260 | else if( strcmp( p, "issuer_key" ) == 0 ) |
| 261 | opt.issuer_key = q; |
| 262 | else if( strcmp( p, "subject_pwd" ) == 0 ) |
| 263 | opt.subject_pwd = q; |
| 264 | else if( strcmp( p, "issuer_pwd" ) == 0 ) |
| 265 | opt.issuer_pwd = q; |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 266 | else if( strcmp( p, "issuer_crt" ) == 0 ) |
| 267 | opt.issuer_crt = q; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 268 | else if( strcmp( p, "output_file" ) == 0 ) |
| 269 | opt.output_file = q; |
| 270 | else if( strcmp( p, "subject_name" ) == 0 ) |
| 271 | { |
| 272 | opt.subject_name = q; |
| 273 | } |
| 274 | else if( strcmp( p, "issuer_name" ) == 0 ) |
| 275 | { |
| 276 | opt.issuer_name = q; |
| 277 | } |
| 278 | else if( strcmp( p, "not_before" ) == 0 ) |
| 279 | { |
| 280 | opt.not_before = q; |
| 281 | } |
| 282 | else if( strcmp( p, "not_after" ) == 0 ) |
| 283 | { |
| 284 | opt.not_after = q; |
| 285 | } |
| 286 | else if( strcmp( p, "serial" ) == 0 ) |
| 287 | { |
| 288 | opt.serial = q; |
| 289 | } |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 290 | else if( strcmp( p, "selfsign" ) == 0 ) |
| 291 | { |
| 292 | opt.selfsign = atoi( q ); |
| 293 | if( opt.selfsign < 0 || opt.selfsign > 1 ) |
| 294 | goto usage; |
| 295 | } |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 296 | else if( strcmp( p, "is_ca" ) == 0 ) |
| 297 | { |
| 298 | opt.is_ca = atoi( q ); |
| 299 | if( opt.is_ca < 0 || opt.is_ca > 1 ) |
| 300 | goto usage; |
| 301 | } |
| 302 | else if( strcmp( p, "max_pathlen" ) == 0 ) |
| 303 | { |
| 304 | opt.max_pathlen = atoi( q ); |
| 305 | if( opt.max_pathlen < -1 || opt.max_pathlen > 127 ) |
| 306 | goto usage; |
| 307 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 308 | else if( strcmp( p, "key_usage" ) == 0 ) |
| 309 | { |
| 310 | while( q != NULL ) |
| 311 | { |
| 312 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 313 | *r++ = '\0'; |
| 314 | |
| 315 | if( strcmp( q, "digital_signature" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 317 | else if( strcmp( q, "non_repudiation" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 319 | else if( strcmp( q, "key_encipherment" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 320 | opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 321 | else if( strcmp( q, "data_encipherment" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 322 | opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 323 | else if( strcmp( q, "key_agreement" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 324 | opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 325 | else if( strcmp( q, "key_cert_sign" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 327 | else if( strcmp( q, "crl_sign" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 329 | else |
| 330 | goto usage; |
| 331 | |
| 332 | q = r; |
| 333 | } |
| 334 | } |
| 335 | else if( strcmp( p, "ns_cert_type" ) == 0 ) |
| 336 | { |
| 337 | while( q != NULL ) |
| 338 | { |
| 339 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 340 | *r++ = '\0'; |
| 341 | |
| 342 | if( strcmp( q, "ssl_client" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 344 | else if( strcmp( q, "ssl_server" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 345 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 346 | else if( strcmp( q, "email" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 348 | else if( strcmp( q, "object_signing" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 350 | else if( strcmp( q, "ssl_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 351 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 352 | else if( strcmp( q, "email_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 353 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 354 | else if( strcmp( q, "object_signing_ca" ) == 0 ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 355 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 356 | else |
| 357 | goto usage; |
| 358 | |
| 359 | q = r; |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | goto usage; |
| 364 | } |
| 365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | mbedtls_printf("\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 367 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 368 | /* |
| 369 | * 0. Seed the PRNG |
| 370 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | mbedtls_printf( " . Seeding the random number generator..." ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 372 | fflush( stdout ); |
| 373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame^] | 375 | 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] | 376 | (const unsigned char *) pers, |
| 377 | strlen( pers ) ) ) != 0 ) |
| 378 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | mbedtls_strerror( ret, buf, 1024 ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame^] | 380 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", ret, buf ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 381 | goto exit; |
| 382 | } |
| 383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 385 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 386 | // Parse serial to MPI |
| 387 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | mbedtls_printf( " . Reading serial number..." ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 389 | fflush( stdout ); |
| 390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | mbedtls_strerror( ret, buf, 1024 ); |
| 394 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 395 | goto exit; |
| 396 | } |
| 397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 399 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 400 | // Parse issuer certificate if present |
| 401 | // |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 402 | if( !opt.selfsign && strlen( opt.issuer_crt ) ) |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 403 | { |
| 404 | /* |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 405 | * 1.0.a. Load the certificates |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 406 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | mbedtls_printf( " . Loading the issuer certificate ..." ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 408 | fflush( stdout ); |
| 409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | 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] | 411 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | mbedtls_strerror( ret, buf, 1024 ); |
| 413 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 414 | goto exit; |
| 415 | } |
| 416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name), |
Paul Bakker | fdba468 | 2014-04-25 11:48:35 +0200 | [diff] [blame] | 418 | &issuer_crt.subject ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 419 | if( ret < 0 ) |
| 420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | mbedtls_strerror( ret, buf, 1024 ); |
| 422 | mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 423 | goto exit; |
| 424 | } |
| 425 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 426 | opt.issuer_name = issuer_name; |
| 427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | mbedtls_printf( " ok\n" ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 432 | // Parse certificate request if present |
| 433 | // |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 434 | if( !opt.selfsign && strlen( opt.request_file ) ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 435 | { |
| 436 | /* |
| 437 | * 1.0.b. Load the CSR |
| 438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | mbedtls_printf( " . Loading the certificate request ..." ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 440 | fflush( stdout ); |
| 441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 443 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | mbedtls_strerror( ret, buf, 1024 ); |
| 445 | mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 446 | goto exit; |
| 447 | } |
| 448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name), |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 450 | &csr.subject ); |
| 451 | if( ret < 0 ) |
| 452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | mbedtls_strerror( ret, buf, 1024 ); |
| 454 | mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 455 | goto exit; |
| 456 | } |
| 457 | |
| 458 | opt.subject_name = subject_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 459 | subject_key = &csr.pk; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 461 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 462 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * 1.1. Load the keys |
| 467 | */ |
| 468 | if( !opt.selfsign && !strlen( opt.request_file ) ) |
| 469 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | mbedtls_printf( " . Loading the subject key ..." ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 471 | fflush( stdout ); |
| 472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | 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] | 474 | opt.subject_pwd ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 475 | if( ret != 0 ) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 476 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | mbedtls_strerror( ret, buf, 1024 ); |
| 478 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 479 | goto exit; |
| 480 | } |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 485 | mbedtls_printf( " . Loading the issuer key ..." ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 486 | fflush( stdout ); |
| 487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 489 | opt.issuer_pwd ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 490 | if( ret != 0 ) |
| 491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | mbedtls_strerror( ret, buf, 1024 ); |
| 493 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 494 | goto exit; |
| 495 | } |
| 496 | |
| 497 | // Check if key and issuer certificate match |
| 498 | // |
| 499 | if( strlen( opt.issuer_crt ) ) |
| 500 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) || |
| 502 | mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N, |
| 503 | &mbedtls_pk_rsa( *issuer_key )->N ) != 0 || |
| 504 | mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E, |
| 505 | &mbedtls_pk_rsa( *issuer_key )->E ) != 0 ) |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 506 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 508 | ret = -1; |
| 509 | goto exit; |
| 510 | } |
| 511 | } |
| 512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | mbedtls_printf( " ok\n" ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 514 | |
| 515 | if( opt.selfsign ) |
| 516 | { |
Paul Bakker | 93c6aa4 | 2013-10-28 22:28:09 +0100 | [diff] [blame] | 517 | opt.subject_name = opt.issuer_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 518 | subject_key = issuer_key; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | mbedtls_x509write_crt_set_subject_key( &crt, subject_key ); |
| 522 | mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key ); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 523 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 524 | /* |
| 525 | * 1.0. Check the names for validity |
| 526 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | 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] | 528 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | mbedtls_strerror( ret, buf, 1024 ); |
| 530 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 531 | goto exit; |
| 532 | } |
| 533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | 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] | 535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | mbedtls_strerror( ret, buf, 1024 ); |
| 537 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 538 | goto exit; |
| 539 | } |
| 540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | mbedtls_printf( " . Setting certificate values ..." ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 542 | fflush( stdout ); |
| 543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | ret = mbedtls_x509write_crt_set_serial( &crt, &serial ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 545 | if( ret != 0 ) |
| 546 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 547 | mbedtls_strerror( ret, buf, 1024 ); |
| 548 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 549 | goto exit; |
| 550 | } |
| 551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | 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] | 553 | if( ret != 0 ) |
| 554 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | mbedtls_strerror( ret, buf, 1024 ); |
| 556 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 557 | goto exit; |
| 558 | } |
| 559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | mbedtls_printf( " . Adding the Basic Constraints extension ..." ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 563 | fflush( stdout ); |
| 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca, |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 566 | opt.max_pathlen ); |
| 567 | if( ret != 0 ) |
| 568 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | mbedtls_strerror( ret, buf, 1024 ); |
| 570 | mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 571 | goto exit; |
| 572 | } |
| 573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | #if defined(MBEDTLS_SHA1_C) |
| 577 | mbedtls_printf( " . Adding the Subject Key Identifier ..." ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 578 | fflush( stdout ); |
| 579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 581 | if( ret != 0 ) |
| 582 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | mbedtls_strerror( ret, buf, 1024 ); |
| 584 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 585 | goto exit; |
| 586 | } |
| 587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | mbedtls_printf( " . Adding the Authority Key Identifier ..." ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 591 | fflush( stdout ); |
| 592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 594 | if( ret != 0 ) |
| 595 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | mbedtls_strerror( ret, buf, 1024 ); |
| 597 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 598 | goto exit; |
| 599 | } |
| 600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | mbedtls_printf( " ok\n" ); |
| 602 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 603 | |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 604 | if( opt.key_usage ) |
| 605 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | mbedtls_printf( " . Adding the Key Usage extension ..." ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 607 | fflush( stdout ); |
| 608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 609 | ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 610 | if( ret != 0 ) |
| 611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | mbedtls_strerror( ret, buf, 1024 ); |
| 613 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 614 | goto exit; |
| 615 | } |
| 616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | if( opt.ns_cert_type ) |
| 621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 622 | mbedtls_printf( " . Adding the NS Cert Type extension ..." ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 623 | fflush( stdout ); |
| 624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | 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] | 626 | if( ret != 0 ) |
| 627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | mbedtls_strerror( ret, buf, 1024 ); |
| 629 | mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 630 | goto exit; |
| 631 | } |
| 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 636 | /* |
| 637 | * 1.2. Writing the request |
| 638 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | mbedtls_printf( " . Writing the certificate..." ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 640 | fflush( stdout ); |
| 641 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 642 | if( ( ret = write_certificate( &crt, opt.output_file, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 644 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | mbedtls_strerror( ret, buf, 1024 ); |
| 646 | mbedtls_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 647 | goto exit; |
| 648 | } |
| 649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 651 | |
| 652 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | mbedtls_x509write_crt_free( &crt ); |
| 654 | mbedtls_pk_free( &loaded_subject_key ); |
| 655 | mbedtls_pk_free( &loaded_issuer_key ); |
| 656 | mbedtls_mpi_free( &serial ); |
| 657 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 658 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 659 | |
| 660 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 662 | fflush( stdout ); getchar(); |
| 663 | #endif |
| 664 | |
| 665 | return( ret ); |
| 666 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && |
| 668 | MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
| 669 | MBEDTLS_ERROR_C */ |