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