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