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 | |
| 43 | #define DFL_FILENAME "keyfile.key" |
| 44 | #define DFL_DEBUG_LEVEL 0 |
| 45 | #define DFL_OUTPUT_FILENAME "cert.req" |
| 46 | #define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL" |
| 47 | |
| 48 | /* |
| 49 | * global options |
| 50 | */ |
| 51 | struct options |
| 52 | { |
| 53 | char *filename; /* filename of the key file */ |
| 54 | int debug_level; /* level of debugging */ |
| 55 | char *output_file; /* where to store the constructed key file */ |
| 56 | char *subject_name; /* subject name for certificate request */ |
| 57 | } opt; |
| 58 | |
| 59 | void my_debug( void *ctx, int level, const char *str ) |
| 60 | { |
| 61 | if( level < opt.debug_level ) |
| 62 | { |
| 63 | fprintf( (FILE *) ctx, "%s", str ); |
| 64 | fflush( (FILE *) ctx ); |
| 65 | } |
| 66 | } |
| 67 | |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 68 | int write_certificate_request( x509_cert_req *req, char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 69 | { |
| 70 | FILE *f; |
| 71 | unsigned char output_buf[4096]; |
| 72 | unsigned char base_buf[4096]; |
| 73 | unsigned char *c; |
| 74 | int ret; |
| 75 | size_t len = 0, olen = 4096; |
| 76 | |
| 77 | memset(output_buf, 0, 4096); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 78 | ret = x509_write_cert_req( req, output_buf, 4096 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 79 | |
| 80 | if( ret < 0 ) |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 81 | return( ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 82 | |
| 83 | len = ret; |
| 84 | c = output_buf + 4095 - len; |
| 85 | |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 86 | if( ( ret = base64_encode( base_buf, &olen, c, len ) ) != 0 ) |
| 87 | return( ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 88 | |
| 89 | c = base_buf; |
| 90 | |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 91 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 92 | return( -1 ); |
| 93 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 94 | fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n"); |
| 95 | while (olen) |
| 96 | { |
| 97 | int use_len = olen; |
| 98 | if (use_len > 64) use_len = 64; |
| 99 | fwrite( c, 1, use_len, f ); |
| 100 | olen -= use_len; |
| 101 | c += use_len; |
| 102 | fprintf(f, "\n"); |
| 103 | } |
| 104 | fprintf(f, "-----END CERTIFICATE REQUEST-----\n"); |
| 105 | fclose(f); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 106 | |
| 107 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | #define USAGE \ |
| 111 | "\n usage: key_app param=<>...\n" \ |
| 112 | "\n acceptable parameters:\n" \ |
| 113 | " filename=%%s default: keyfile.key\n" \ |
| 114 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 115 | " output_file=%%s default: cert.req\n" \ |
| 116 | " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \ |
| 117 | "\n" |
| 118 | |
| 119 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 120 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
| 121 | int main( int argc, char *argv[] ) |
| 122 | { |
| 123 | ((void) argc); |
| 124 | ((void) argv); |
| 125 | |
| 126 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 127 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 128 | return( 0 ); |
| 129 | } |
| 130 | #else |
| 131 | int main( int argc, char *argv[] ) |
| 132 | { |
| 133 | int ret = 0; |
| 134 | rsa_context rsa; |
| 135 | char buf[1024]; |
| 136 | int i, j, n; |
| 137 | char *p, *q; |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 138 | x509_cert_req req; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Set to sane values |
| 142 | */ |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 143 | x509cert_req_init( &req ); |
| 144 | x509cert_req_set_md_alg( &req, POLARSSL_MD_SHA1 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 145 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 146 | memset( buf, 0, 1024 ); |
| 147 | |
| 148 | if( argc == 0 ) |
| 149 | { |
| 150 | usage: |
| 151 | printf( USAGE ); |
| 152 | goto exit; |
| 153 | } |
| 154 | |
| 155 | opt.filename = DFL_FILENAME; |
| 156 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 157 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 158 | opt.subject_name = DFL_SUBJECT_NAME; |
| 159 | |
| 160 | for( i = 1; i < argc; i++ ) |
| 161 | { |
| 162 | |
| 163 | p = argv[i]; |
| 164 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 165 | goto usage; |
| 166 | *q++ = '\0'; |
| 167 | |
| 168 | n = strlen( p ); |
| 169 | for( j = 0; j < n; j++ ) |
| 170 | { |
| 171 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 172 | argv[i][j] |= 0x20; |
| 173 | } |
| 174 | |
| 175 | if( strcmp( p, "filename" ) == 0 ) |
| 176 | opt.filename = q; |
| 177 | else if( strcmp( p, "output_file" ) == 0 ) |
| 178 | opt.output_file = q; |
| 179 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 180 | { |
| 181 | opt.debug_level = atoi( q ); |
| 182 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 183 | goto usage; |
| 184 | } |
| 185 | else if( strcmp( p, "subject_name" ) == 0 ) |
| 186 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 187 | opt.subject_name = q; |
| 188 | } |
| 189 | else |
| 190 | goto usage; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * 1.0. Check the subject name for validity |
| 195 | */ |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 196 | if( ( ret = x509cert_req_set_subject_name( &req, opt.subject_name ) ) != 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 197 | { |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 198 | #ifdef POLARSSL_ERROR_C |
| 199 | error_strerror( ret, buf, 1024 ); |
| 200 | #endif |
| 201 | printf( " failed\n ! x509cert_req_set_subject_name returned %d - %s\n\n", ret, buf ); |
| 202 | goto exit; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * 1.1. Load the key |
| 207 | */ |
| 208 | printf( "\n . Loading the private key ..." ); |
| 209 | fflush( stdout ); |
| 210 | |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 211 | ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 212 | |
| 213 | if( ret != 0 ) |
| 214 | { |
| 215 | #ifdef POLARSSL_ERROR_C |
| 216 | error_strerror( ret, buf, 1024 ); |
| 217 | #endif |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 218 | 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^] | 219 | goto exit; |
| 220 | } |
| 221 | |
| 222 | x509cert_req_set_rsa_key( &req, &rsa ); |
| 223 | |
| 224 | printf( " ok\n" ); |
| 225 | |
| 226 | /* |
| 227 | * 1.2. Writing the request |
| 228 | */ |
| 229 | printf( " . Writing the certificate request ..." ); |
| 230 | fflush( stdout ); |
| 231 | |
| 232 | if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 ) |
| 233 | { |
| 234 | #ifdef POLARSSL_ERROR_C |
| 235 | error_strerror( ret, buf, 1024 ); |
| 236 | #endif |
| 237 | printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | printf( " ok\n" ); |
| 242 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 243 | exit: |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame^] | 244 | x509cert_req_free( &req ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 245 | rsa_free( &rsa ); |
| 246 | |
| 247 | #if defined(_WIN32) |
| 248 | printf( " + Press Enter to exit this program.\n" ); |
| 249 | fflush( stdout ); getchar(); |
| 250 | #endif |
| 251 | |
| 252 | return( ret ); |
| 253 | } |
| 254 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
| 255 | POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |