Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Certificate request generation |
| 3 | * |
| 4 | * Copyright (C) 2006-2011, 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" |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 41 | #include "polarssl/oid.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame^] | 43 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 44 | !defined(POLARSSL_X509_PARSE_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_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 52 | return( 0 ); |
| 53 | } |
| 54 | #else |
| 55 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 56 | #define DFL_FILENAME "keyfile.key" |
| 57 | #define DFL_DEBUG_LEVEL 0 |
| 58 | #define DFL_OUTPUT_FILENAME "cert.req" |
| 59 | #define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL" |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 60 | #define DFL_KEY_USAGE 0 |
| 61 | #define DFL_NS_CERT_TYPE 0 |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * global options |
| 65 | */ |
| 66 | struct options |
| 67 | { |
| 68 | char *filename; /* filename of the key file */ |
| 69 | int debug_level; /* level of debugging */ |
| 70 | char *output_file; /* where to store the constructed key file */ |
| 71 | char *subject_name; /* subject name for certificate request */ |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 72 | unsigned char key_usage; /* key usage flags */ |
| 73 | unsigned char ns_cert_type; /* NS cert type */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 74 | } opt; |
| 75 | |
Paul Bakker | cd35803 | 2013-09-09 12:08:11 +0200 | [diff] [blame] | 76 | int write_certificate_request( x509write_csr *req, char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 77 | { |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 78 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 79 | FILE *f; |
| 80 | unsigned char output_buf[4096]; |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 81 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 82 | |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 83 | memset( output_buf, 0, 4096 ); |
| 84 | if( ( ret = x509write_csr_pem( req, output_buf, 4096 ) ) < 0 ) |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 85 | return( ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 86 | |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 87 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 88 | |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 89 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 90 | return( -1 ); |
| 91 | |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 92 | if( fwrite( output_buf, 1, len, f ) != len ) |
| 93 | return( -1 ); |
| 94 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 95 | fclose(f); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 96 | |
| 97 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #define USAGE \ |
Paul Bakker | 8693274 | 2013-09-09 14:09:42 +0200 | [diff] [blame] | 101 | "\n usage: cert_req param=<>...\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 102 | "\n acceptable parameters:\n" \ |
| 103 | " filename=%%s default: keyfile.key\n" \ |
| 104 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 105 | " output_file=%%s default: cert.req\n" \ |
| 106 | " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \ |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 107 | " key_usage=%%s default: (empty)\n" \ |
| 108 | " Comma-separated-list of values:\n" \ |
| 109 | " digital_signature\n" \ |
| 110 | " non_repudiation\n" \ |
| 111 | " key_encipherment\n" \ |
| 112 | " data_encipherment\n" \ |
| 113 | " key_agreement\n" \ |
| 114 | " key_certificate_sign\n" \ |
| 115 | " crl_sign\n" \ |
| 116 | " ns_cert_type=%%s default: (empty)\n" \ |
| 117 | " Comma-separated-list of values:\n" \ |
| 118 | " ssl_client\n" \ |
| 119 | " ssl_server\n" \ |
| 120 | " email\n" \ |
| 121 | " object_signing\n" \ |
| 122 | " ssl_ca\n" \ |
| 123 | " email_ca\n" \ |
| 124 | " object_signing_ca\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 125 | "\n" |
| 126 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 127 | int main( int argc, char *argv[] ) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | rsa_context rsa; |
| 131 | char buf[1024]; |
| 132 | int i, j, n; |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 133 | char *p, *q, *r; |
Paul Bakker | cd35803 | 2013-09-09 12:08:11 +0200 | [diff] [blame] | 134 | x509write_csr req; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Set to sane values |
| 138 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 139 | x509write_csr_init( &req ); |
| 140 | x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 141 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 142 | memset( buf, 0, 1024 ); |
| 143 | |
| 144 | if( argc == 0 ) |
| 145 | { |
| 146 | usage: |
| 147 | printf( USAGE ); |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 148 | ret = 1; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 149 | goto exit; |
| 150 | } |
| 151 | |
| 152 | opt.filename = DFL_FILENAME; |
| 153 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 154 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 155 | opt.subject_name = DFL_SUBJECT_NAME; |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 156 | opt.key_usage = DFL_KEY_USAGE; |
| 157 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 158 | |
| 159 | for( i = 1; i < argc; i++ ) |
| 160 | { |
| 161 | |
| 162 | p = argv[i]; |
| 163 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 164 | goto usage; |
| 165 | *q++ = '\0'; |
| 166 | |
| 167 | n = strlen( p ); |
| 168 | for( j = 0; j < n; j++ ) |
| 169 | { |
| 170 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 171 | argv[i][j] |= 0x20; |
| 172 | } |
| 173 | |
| 174 | if( strcmp( p, "filename" ) == 0 ) |
| 175 | opt.filename = q; |
| 176 | else if( strcmp( p, "output_file" ) == 0 ) |
| 177 | opt.output_file = q; |
| 178 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 179 | { |
| 180 | opt.debug_level = atoi( q ); |
| 181 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 182 | goto usage; |
| 183 | } |
| 184 | else if( strcmp( p, "subject_name" ) == 0 ) |
| 185 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 186 | opt.subject_name = q; |
| 187 | } |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 188 | else if( strcmp( p, "key_usage" ) == 0 ) |
| 189 | { |
| 190 | while( q != NULL ) |
| 191 | { |
| 192 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 193 | *r++ = '\0'; |
| 194 | |
| 195 | if( strcmp( q, "digital_signature" ) == 0 ) |
| 196 | opt.key_usage |= KU_DIGITAL_SIGNATURE; |
| 197 | else if( strcmp( q, "non_repudiation" ) == 0 ) |
| 198 | opt.key_usage |= KU_NON_REPUDIATION; |
| 199 | else if( strcmp( q, "key_encipherment" ) == 0 ) |
| 200 | opt.key_usage |= KU_KEY_ENCIPHERMENT; |
| 201 | else if( strcmp( q, "data_encipherment" ) == 0 ) |
| 202 | opt.key_usage |= KU_DATA_ENCIPHERMENT; |
| 203 | else if( strcmp( q, "key_agreement" ) == 0 ) |
| 204 | opt.key_usage |= KU_KEY_AGREEMENT; |
| 205 | else if( strcmp( q, "key_cert_sign" ) == 0 ) |
| 206 | opt.key_usage |= KU_KEY_CERT_SIGN; |
| 207 | else if( strcmp( q, "crl_sign" ) == 0 ) |
| 208 | opt.key_usage |= KU_CRL_SIGN; |
| 209 | else |
| 210 | goto usage; |
| 211 | |
| 212 | q = r; |
| 213 | } |
| 214 | } |
| 215 | else if( strcmp( p, "ns_cert_type" ) == 0 ) |
| 216 | { |
| 217 | while( q != NULL ) |
| 218 | { |
| 219 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 220 | *r++ = '\0'; |
| 221 | |
| 222 | if( strcmp( q, "ssl_client" ) == 0 ) |
| 223 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT; |
| 224 | else if( strcmp( q, "ssl_server" ) == 0 ) |
| 225 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER; |
| 226 | else if( strcmp( q, "email" ) == 0 ) |
| 227 | opt.ns_cert_type |= NS_CERT_TYPE_EMAIL; |
| 228 | else if( strcmp( q, "object_signing" ) == 0 ) |
| 229 | opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING; |
| 230 | else if( strcmp( q, "ssl_ca" ) == 0 ) |
| 231 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA; |
| 232 | else if( strcmp( q, "email_ca" ) == 0 ) |
| 233 | opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA; |
| 234 | else if( strcmp( q, "object_signing_ca" ) == 0 ) |
| 235 | opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA; |
| 236 | else |
| 237 | goto usage; |
| 238 | |
| 239 | q = r; |
| 240 | } |
| 241 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 242 | else |
| 243 | goto usage; |
| 244 | } |
| 245 | |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 246 | if( opt.key_usage ) |
| 247 | x509write_csr_set_key_usage( &req, opt.key_usage ); |
| 248 | |
| 249 | if( opt.ns_cert_type ) |
| 250 | x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type ); |
| 251 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 252 | /* |
| 253 | * 1.0. Check the subject name for validity |
| 254 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 255 | if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 256 | { |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 257 | #ifdef POLARSSL_ERROR_C |
| 258 | error_strerror( ret, buf, 1024 ); |
| 259 | #endif |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 260 | printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 261 | goto exit; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * 1.1. Load the key |
| 266 | */ |
| 267 | printf( "\n . Loading the private key ..." ); |
| 268 | fflush( stdout ); |
| 269 | |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 270 | ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 271 | |
| 272 | if( ret != 0 ) |
| 273 | { |
| 274 | #ifdef POLARSSL_ERROR_C |
| 275 | error_strerror( ret, buf, 1024 ); |
| 276 | #endif |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 277 | printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 278 | goto exit; |
| 279 | } |
| 280 | |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 281 | x509write_csr_set_rsa_key( &req, &rsa ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 282 | |
| 283 | printf( " ok\n" ); |
| 284 | |
| 285 | /* |
| 286 | * 1.2. Writing the request |
| 287 | */ |
| 288 | printf( " . Writing the certificate request ..." ); |
| 289 | fflush( stdout ); |
| 290 | |
| 291 | if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 ) |
| 292 | { |
| 293 | #ifdef POLARSSL_ERROR_C |
| 294 | error_strerror( ret, buf, 1024 ); |
| 295 | #endif |
| 296 | printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 297 | goto exit; |
| 298 | } |
| 299 | |
| 300 | printf( " ok\n" ); |
| 301 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 302 | exit: |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 303 | x509write_csr_free( &req ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 304 | rsa_free( &rsa ); |
| 305 | |
| 306 | #if defined(_WIN32) |
| 307 | printf( " + Press Enter to exit this program.\n" ); |
| 308 | fflush( stdout ); getchar(); |
| 309 | #endif |
| 310 | |
| 311 | return( ret ); |
| 312 | } |
| 313 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
| 314 | POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |