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