Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Certificate request generation |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | #include "polarssl/x509_csr.h" |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 34 | #include "polarssl/entropy.h" |
| 35 | #include "polarssl/ctr_drbg.h" |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 36 | #include "polarssl/error.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 37 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 38 | #if !defined(POLARSSL_X509_CSR_WRITE_C) || !defined(POLARSSL_FS_IO) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 39 | !defined(POLARSSL_PK_PARSE_C) || \ |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 40 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 41 | int main( int argc, char *argv[] ) |
| 42 | { |
| 43 | ((void) argc); |
| 44 | ((void) argv); |
| 45 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 46 | printf( "POLARSSL_X509_CSR_WRITE_C and/or POLARSSL_FS_IO and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 47 | "POLARSSL_PK_PARSE_C and/or " |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 48 | "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C " |
| 49 | "not defined.\n"); |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 54 | #define DFL_FILENAME "keyfile.key" |
| 55 | #define DFL_DEBUG_LEVEL 0 |
| 56 | #define DFL_OUTPUT_FILENAME "cert.req" |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 57 | #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 58 | #define DFL_KEY_USAGE 0 |
| 59 | #define DFL_NS_CERT_TYPE 0 |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * global options |
| 63 | */ |
| 64 | struct options |
| 65 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 66 | const char *filename; /* filename of the key file */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 67 | int debug_level; /* level of debugging */ |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 68 | const char *output_file; /* where to store the constructed key file */ |
| 69 | const char *subject_name; /* subject name for certificate request */ |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 70 | unsigned char key_usage; /* key usage flags */ |
| 71 | unsigned char ns_cert_type; /* NS cert type */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 72 | } opt; |
| 73 | |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 74 | int write_certificate_request( x509write_csr *req, const char *output_file, |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 75 | int (*f_rng)(void *, unsigned char *, size_t), |
| 76 | void *p_rng ) |
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 ); |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 84 | if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 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 ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 93 | { |
| 94 | fclose( f ); |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 95 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 96 | } |
Paul Bakker | 135f1e9 | 2013-08-26 16:54:13 +0200 | [diff] [blame] | 97 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 98 | fclose( f ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 99 | |
| 100 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #define USAGE \ |
Paul Bakker | 8693274 | 2013-09-09 14:09:42 +0200 | [diff] [blame] | 104 | "\n usage: cert_req param=<>...\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 105 | "\n acceptable parameters:\n" \ |
| 106 | " filename=%%s default: keyfile.key\n" \ |
| 107 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 108 | " output_file=%%s default: cert.req\n" \ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 109 | " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \ |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 110 | " key_usage=%%s default: (empty)\n" \ |
| 111 | " Comma-separated-list of values:\n" \ |
| 112 | " digital_signature\n" \ |
| 113 | " non_repudiation\n" \ |
| 114 | " key_encipherment\n" \ |
| 115 | " data_encipherment\n" \ |
| 116 | " key_agreement\n" \ |
| 117 | " key_certificate_sign\n" \ |
| 118 | " crl_sign\n" \ |
| 119 | " ns_cert_type=%%s default: (empty)\n" \ |
| 120 | " Comma-separated-list of values:\n" \ |
| 121 | " ssl_client\n" \ |
| 122 | " ssl_server\n" \ |
| 123 | " email\n" \ |
| 124 | " object_signing\n" \ |
| 125 | " ssl_ca\n" \ |
| 126 | " email_ca\n" \ |
| 127 | " object_signing_ca\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 128 | "\n" |
| 129 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 130 | int main( int argc, char *argv[] ) |
| 131 | { |
| 132 | int ret = 0; |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 133 | pk_context key; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 134 | char buf[1024]; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 135 | int i; |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 136 | char *p, *q, *r; |
Paul Bakker | cd35803 | 2013-09-09 12:08:11 +0200 | [diff] [blame] | 137 | x509write_csr req; |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 138 | entropy_context entropy; |
| 139 | ctr_drbg_context ctr_drbg; |
| 140 | const char *pers = "csr example app"; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Set to sane values |
| 144 | */ |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 145 | x509write_csr_init( &req ); |
| 146 | x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 ); |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 147 | pk_init( &key ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 148 | memset( buf, 0, sizeof( buf ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 149 | |
| 150 | if( argc == 0 ) |
| 151 | { |
| 152 | usage: |
| 153 | printf( USAGE ); |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 154 | ret = 1; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 155 | goto exit; |
| 156 | } |
| 157 | |
| 158 | opt.filename = DFL_FILENAME; |
| 159 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 160 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 161 | opt.subject_name = DFL_SUBJECT_NAME; |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 162 | opt.key_usage = DFL_KEY_USAGE; |
| 163 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 164 | |
| 165 | for( i = 1; i < argc; i++ ) |
| 166 | { |
| 167 | |
| 168 | p = argv[i]; |
| 169 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 170 | goto usage; |
| 171 | *q++ = '\0'; |
| 172 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 173 | if( strcmp( p, "filename" ) == 0 ) |
| 174 | opt.filename = q; |
| 175 | else if( strcmp( p, "output_file" ) == 0 ) |
| 176 | opt.output_file = q; |
| 177 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 178 | { |
| 179 | opt.debug_level = atoi( q ); |
| 180 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 181 | goto usage; |
| 182 | } |
| 183 | else if( strcmp( p, "subject_name" ) == 0 ) |
| 184 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 185 | opt.subject_name = q; |
| 186 | } |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 187 | else if( strcmp( p, "key_usage" ) == 0 ) |
| 188 | { |
| 189 | while( q != NULL ) |
| 190 | { |
| 191 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 192 | *r++ = '\0'; |
| 193 | |
| 194 | if( strcmp( q, "digital_signature" ) == 0 ) |
| 195 | opt.key_usage |= KU_DIGITAL_SIGNATURE; |
| 196 | else if( strcmp( q, "non_repudiation" ) == 0 ) |
| 197 | opt.key_usage |= KU_NON_REPUDIATION; |
| 198 | else if( strcmp( q, "key_encipherment" ) == 0 ) |
| 199 | opt.key_usage |= KU_KEY_ENCIPHERMENT; |
| 200 | else if( strcmp( q, "data_encipherment" ) == 0 ) |
| 201 | opt.key_usage |= KU_DATA_ENCIPHERMENT; |
| 202 | else if( strcmp( q, "key_agreement" ) == 0 ) |
| 203 | opt.key_usage |= KU_KEY_AGREEMENT; |
| 204 | else if( strcmp( q, "key_cert_sign" ) == 0 ) |
| 205 | opt.key_usage |= KU_KEY_CERT_SIGN; |
| 206 | else if( strcmp( q, "crl_sign" ) == 0 ) |
| 207 | opt.key_usage |= KU_CRL_SIGN; |
| 208 | else |
| 209 | goto usage; |
| 210 | |
| 211 | q = r; |
| 212 | } |
| 213 | } |
| 214 | else if( strcmp( p, "ns_cert_type" ) == 0 ) |
| 215 | { |
| 216 | while( q != NULL ) |
| 217 | { |
| 218 | if( ( r = strchr( q, ',' ) ) != NULL ) |
| 219 | *r++ = '\0'; |
| 220 | |
| 221 | if( strcmp( q, "ssl_client" ) == 0 ) |
| 222 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT; |
| 223 | else if( strcmp( q, "ssl_server" ) == 0 ) |
| 224 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER; |
| 225 | else if( strcmp( q, "email" ) == 0 ) |
| 226 | opt.ns_cert_type |= NS_CERT_TYPE_EMAIL; |
| 227 | else if( strcmp( q, "object_signing" ) == 0 ) |
| 228 | opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING; |
| 229 | else if( strcmp( q, "ssl_ca" ) == 0 ) |
| 230 | opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA; |
| 231 | else if( strcmp( q, "email_ca" ) == 0 ) |
| 232 | opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA; |
| 233 | else if( strcmp( q, "object_signing_ca" ) == 0 ) |
| 234 | opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA; |
| 235 | else |
| 236 | goto usage; |
| 237 | |
| 238 | q = r; |
| 239 | } |
| 240 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 241 | else |
| 242 | goto usage; |
| 243 | } |
| 244 | |
Paul Bakker | 57be6e2 | 2013-08-26 14:13:14 +0200 | [diff] [blame] | 245 | if( opt.key_usage ) |
| 246 | x509write_csr_set_key_usage( &req, opt.key_usage ); |
| 247 | |
| 248 | if( opt.ns_cert_type ) |
| 249 | x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type ); |
| 250 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 251 | /* |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 252 | * 0. Seed the PRNG |
| 253 | */ |
| 254 | printf( " . Seeding the random number generator..." ); |
| 255 | fflush( stdout ); |
| 256 | |
| 257 | entropy_init( &entropy ); |
| 258 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 259 | (const unsigned char *) pers, |
| 260 | strlen( pers ) ) ) != 0 ) |
| 261 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 262 | printf( " failed\n ! ctr_drbg_init returned %d", ret ); |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 263 | goto exit; |
| 264 | } |
| 265 | |
| 266 | printf( " ok\n" ); |
| 267 | |
| 268 | /* |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 269 | * 1.0. Check the subject name for validity |
| 270 | */ |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 271 | printf( " . Checking subjet name..." ); |
| 272 | fflush( stdout ); |
| 273 | |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 274 | if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 275 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 276 | printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 277 | goto exit; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 280 | printf( " ok\n" ); |
| 281 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 282 | /* |
| 283 | * 1.1. Load the key |
| 284 | */ |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 285 | printf( " . Loading the private key ..." ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 286 | fflush( stdout ); |
| 287 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 288 | ret = pk_parse_keyfile( &key, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 289 | |
| 290 | if( ret != 0 ) |
| 291 | { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 292 | printf( " failed\n ! pk_parse_keyfile returned %d", ret ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 293 | goto exit; |
| 294 | } |
| 295 | |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 296 | x509write_csr_set_key( &req, &key ); |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 297 | |
| 298 | printf( " ok\n" ); |
| 299 | |
| 300 | /* |
| 301 | * 1.2. Writing the request |
| 302 | */ |
| 303 | printf( " . Writing the certificate request ..." ); |
| 304 | fflush( stdout ); |
| 305 | |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 306 | if( ( ret = write_certificate_request( &req, opt.output_file, |
| 307 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 308 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 309 | printf( " failed\n ! write_certifcate_request %d", ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 310 | goto exit; |
| 311 | } |
| 312 | |
| 313 | printf( " ok\n" ); |
| 314 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 315 | exit: |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 316 | |
| 317 | if( ret != 0 && ret != 1) |
| 318 | { |
| 319 | #ifdef POLARSSL_ERROR_C |
| 320 | polarssl_strerror( ret, buf, sizeof( buf ) ); |
| 321 | printf( " - %s\n", buf ); |
| 322 | #else |
| 323 | printf("\n"); |
| 324 | #endif |
| 325 | } |
| 326 | |
Paul Bakker | 82e2945 | 2013-08-25 11:01:31 +0200 | [diff] [blame] | 327 | x509write_csr_free( &req ); |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 328 | pk_free( &key ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 329 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 330 | entropy_free( &entropy ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 331 | |
| 332 | #if defined(_WIN32) |
| 333 | printf( " + Press Enter to exit this program.\n" ); |
| 334 | fflush( stdout ); getchar(); |
| 335 | #endif |
| 336 | |
| 337 | return( ret ); |
| 338 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 339 | #endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO && |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 340 | POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */ |