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