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