blob: a2f26569726b293971bdc1bece6f94b91ccc5099 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
33 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020034 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020035 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020036int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041 printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020042 "POLARSSL_FS_IO and/or "
43 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
44 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020045 return( 0 );
46}
47#else
48
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020049#include "polarssl/x509_crt.h"
50#include "polarssl/x509_csr.h"
51#include "polarssl/entropy.h"
52#include "polarssl/ctr_drbg.h"
53#include "polarssl/error.h"
54
Paul Bakker1014e952013-09-09 13:59:42 +020055#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020056#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020057#define DFL_SUBJECT_KEY "subject.key"
58#define DFL_ISSUER_KEY "ca.key"
59#define DFL_SUBJECT_PWD ""
60#define DFL_ISSUER_PWD ""
61#define DFL_OUTPUT_FILENAME "cert.crt"
62#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
63#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
64#define DFL_NOT_BEFORE "20010101000000"
65#define DFL_NOT_AFTER "20301231235959"
66#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020067#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020068#define DFL_IS_CA 0
69#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020070#define DFL_KEY_USAGE 0
71#define DFL_NS_CERT_TYPE 0
72
73/*
74 * global options
75 */
76struct options
77{
Paul Bakker8fc30b12013-11-25 13:29:43 +010078 const char *issuer_crt; /* filename of the issuer certificate */
79 const char *request_file; /* filename of the certificate request */
80 const char *subject_key; /* filename of the subject key file */
81 const char *issuer_key; /* filename of the issuer key file */
82 const char *subject_pwd; /* password for the subject key file */
83 const char *issuer_pwd; /* password for the issuer key file */
84 const char *output_file; /* where to store the constructed key file */
85 const char *subject_name; /* subject name for certificate */
86 const char *issuer_name; /* issuer name for certificate */
87 const char *not_before; /* validity period not before */
88 const char *not_after; /* validity period not after */
89 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020090 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020091 int is_ca; /* is a CA certificate */
92 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020093 unsigned char key_usage; /* key usage flags */
94 unsigned char ns_cert_type; /* NS cert type */
95} opt;
96
Paul Bakker8fc30b12013-11-25 13:29:43 +010097int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020098 int (*f_rng)(void *, unsigned char *, size_t),
99 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200100{
101 int ret;
102 FILE *f;
103 unsigned char output_buf[4096];
104 size_t len = 0;
105
106 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200107 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200108 return( ret );
109
110 len = strlen( (char *) output_buf );
111
112 if( ( f = fopen( output_file, "w" ) ) == NULL )
113 return( -1 );
114
115 if( fwrite( output_buf, 1, len, f ) != len )
116 return( -1 );
117
118 fclose(f);
119
120 return( 0 );
121}
122
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200123#if defined(POLARSSL_X509_CSR_PARSE_C)
124#define USAGE_CSR \
125 " request_file=%%s default: (empty)\n" \
126 " If request_file is specified, subject_key,\n" \
127 " subject_pwd and subject_name are ignored!\n"
128#else
129#define USAGE_CSR ""
130#endif /* POLARSSL_X509_CSR_PARSE_C */
131
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132#define USAGE \
133 "\n usage: cert_write param=<>...\n" \
134 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200135 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200136 " subject_key=%%s default: subject.key\n" \
137 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200138 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
139 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200140 " issuer_crt=%%s default: (empty)\n" \
141 " If issuer_crt is specified, issuer_name is\n" \
142 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200143 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
144 "\n" \
145 " selfsign=%%d default: 0 (false)\n" \
146 " If selfsign is enabled, issuer_name and\n" \
147 " issuer_key are required (issuer_crt and\n" \
148 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200149 " issuer_key=%%s default: ca.key\n" \
150 " issuer_pwd=%%s default: (empty)\n" \
151 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152 " serial=%%s default: 1\n" \
153 " not_before=%%s default: 20010101000000\n"\
154 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200155 " is_ca=%%d default: 0 (disabled)\n" \
156 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200157 " key_usage=%%s default: (empty)\n" \
158 " Comma-separated-list of values:\n" \
159 " digital_signature\n" \
160 " non_repudiation\n" \
161 " key_encipherment\n" \
162 " data_encipherment\n" \
163 " key_agreement\n" \
164 " key_certificate_sign\n" \
165 " crl_sign\n" \
166 " ns_cert_type=%%s default: (empty)\n" \
167 " Comma-separated-list of values:\n" \
168 " ssl_client\n" \
169 " ssl_server\n" \
170 " email\n" \
171 " object_signing\n" \
172 " ssl_ca\n" \
173 " email_ca\n" \
174 " object_signing_ca\n" \
175 "\n"
176
177int main( int argc, char *argv[] )
178{
179 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200180 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200181 pk_context loaded_issuer_key, loaded_subject_key;
182 pk_context *issuer_key = &loaded_issuer_key,
183 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200185 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100186 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200188#if defined(POLARSSL_X509_CSR_PARSE_C)
189 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200190 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200191#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 x509write_cert crt;
193 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200194 entropy_context entropy;
195 ctr_drbg_context ctr_drbg;
196 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197
198 /*
199 * Set to sane values
200 */
201 x509write_crt_init( &crt );
202 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200203 pk_init( &loaded_issuer_key );
204 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200206#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200207 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200208#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200209 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210 memset( buf, 0, 1024 );
211
212 if( argc == 0 )
213 {
214 usage:
215 printf( USAGE );
216 ret = 1;
217 goto exit;
218 }
219
Paul Bakker1014e952013-09-09 13:59:42 +0200220 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200221 opt.request_file = DFL_REQUEST_FILE;
222 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 opt.subject_key = DFL_SUBJECT_KEY;
224 opt.issuer_key = DFL_ISSUER_KEY;
225 opt.subject_pwd = DFL_SUBJECT_PWD;
226 opt.issuer_pwd = DFL_ISSUER_PWD;
227 opt.output_file = DFL_OUTPUT_FILENAME;
228 opt.subject_name = DFL_SUBJECT_NAME;
229 opt.issuer_name = DFL_ISSUER_NAME;
230 opt.not_before = DFL_NOT_BEFORE;
231 opt.not_after = DFL_NOT_AFTER;
232 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200233 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200234 opt.is_ca = DFL_IS_CA;
235 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236 opt.key_usage = DFL_KEY_USAGE;
237 opt.ns_cert_type = DFL_NS_CERT_TYPE;
238
239 for( i = 1; i < argc; i++ )
240 {
241
242 p = argv[i];
243 if( ( q = strchr( p, '=' ) ) == NULL )
244 goto usage;
245 *q++ = '\0';
246
Paul Bakkere2673fb2013-09-09 15:52:07 +0200247 if( strcmp( p, "request_file" ) == 0 )
248 opt.request_file = q;
249 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200250 opt.subject_key = q;
251 else if( strcmp( p, "issuer_key" ) == 0 )
252 opt.issuer_key = q;
253 else if( strcmp( p, "subject_pwd" ) == 0 )
254 opt.subject_pwd = q;
255 else if( strcmp( p, "issuer_pwd" ) == 0 )
256 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200257 else if( strcmp( p, "issuer_crt" ) == 0 )
258 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200259 else if( strcmp( p, "output_file" ) == 0 )
260 opt.output_file = q;
261 else if( strcmp( p, "subject_name" ) == 0 )
262 {
263 opt.subject_name = q;
264 }
265 else if( strcmp( p, "issuer_name" ) == 0 )
266 {
267 opt.issuer_name = q;
268 }
269 else if( strcmp( p, "not_before" ) == 0 )
270 {
271 opt.not_before = q;
272 }
273 else if( strcmp( p, "not_after" ) == 0 )
274 {
275 opt.not_after = q;
276 }
277 else if( strcmp( p, "serial" ) == 0 )
278 {
279 opt.serial = q;
280 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200281 else if( strcmp( p, "selfsign" ) == 0 )
282 {
283 opt.selfsign = atoi( q );
284 if( opt.selfsign < 0 || opt.selfsign > 1 )
285 goto usage;
286 }
Paul Bakker15162a02013-09-06 19:27:21 +0200287 else if( strcmp( p, "is_ca" ) == 0 )
288 {
289 opt.is_ca = atoi( q );
290 if( opt.is_ca < 0 || opt.is_ca > 1 )
291 goto usage;
292 }
293 else if( strcmp( p, "max_pathlen" ) == 0 )
294 {
295 opt.max_pathlen = atoi( q );
296 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
297 goto usage;
298 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 else if( strcmp( p, "key_usage" ) == 0 )
300 {
301 while( q != NULL )
302 {
303 if( ( r = strchr( q, ',' ) ) != NULL )
304 *r++ = '\0';
305
306 if( strcmp( q, "digital_signature" ) == 0 )
307 opt.key_usage |= KU_DIGITAL_SIGNATURE;
308 else if( strcmp( q, "non_repudiation" ) == 0 )
309 opt.key_usage |= KU_NON_REPUDIATION;
310 else if( strcmp( q, "key_encipherment" ) == 0 )
311 opt.key_usage |= KU_KEY_ENCIPHERMENT;
312 else if( strcmp( q, "data_encipherment" ) == 0 )
313 opt.key_usage |= KU_DATA_ENCIPHERMENT;
314 else if( strcmp( q, "key_agreement" ) == 0 )
315 opt.key_usage |= KU_KEY_AGREEMENT;
316 else if( strcmp( q, "key_cert_sign" ) == 0 )
317 opt.key_usage |= KU_KEY_CERT_SIGN;
318 else if( strcmp( q, "crl_sign" ) == 0 )
319 opt.key_usage |= KU_CRL_SIGN;
320 else
321 goto usage;
322
323 q = r;
324 }
325 }
326 else if( strcmp( p, "ns_cert_type" ) == 0 )
327 {
328 while( q != NULL )
329 {
330 if( ( r = strchr( q, ',' ) ) != NULL )
331 *r++ = '\0';
332
333 if( strcmp( q, "ssl_client" ) == 0 )
334 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
335 else if( strcmp( q, "ssl_server" ) == 0 )
336 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
337 else if( strcmp( q, "email" ) == 0 )
338 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
339 else if( strcmp( q, "object_signing" ) == 0 )
340 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
341 else if( strcmp( q, "ssl_ca" ) == 0 )
342 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
343 else if( strcmp( q, "email_ca" ) == 0 )
344 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
345 else if( strcmp( q, "object_signing_ca" ) == 0 )
346 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
347 else
348 goto usage;
349
350 q = r;
351 }
352 }
353 else
354 goto usage;
355 }
356
Paul Bakker1014e952013-09-09 13:59:42 +0200357 printf("\n");
358
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200359 /*
360 * 0. Seed the PRNG
361 */
362 printf( " . Seeding the random number generator..." );
363 fflush( stdout );
364
365 entropy_init( &entropy );
366 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
367 (const unsigned char *) pers,
368 strlen( pers ) ) ) != 0 )
369 {
370 error_strerror( ret, buf, 1024 );
371 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
372 goto exit;
373 }
374
375 printf( " ok\n" );
376
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377 // Parse serial to MPI
378 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200379 printf( " . Reading serial number..." );
380 fflush( stdout );
381
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
383 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
386 goto exit;
387 }
388
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200389 printf( " ok\n" );
390
Paul Bakker1014e952013-09-09 13:59:42 +0200391 // Parse issuer certificate if present
392 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200393 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200394 {
395 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200396 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200397 */
398 printf( " . Loading the issuer certificate ..." );
399 fflush( stdout );
400
Paul Bakkerddf26b42013-09-18 13:46:23 +0200401 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200402 {
Paul Bakker1014e952013-09-09 13:59:42 +0200403 error_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200404 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200405 goto exit;
406 }
407
Paul Bakker86d0c192013-09-18 11:11:02 +0200408 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200409 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200410 if( ret < 0 )
411 {
Paul Bakker1014e952013-09-09 13:59:42 +0200412 error_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200413 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200414 goto exit;
415 }
416
Paul Bakkere2673fb2013-09-09 15:52:07 +0200417 opt.issuer_name = issuer_name;
418
419 printf( " ok\n" );
420 }
421
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200422#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200423 // Parse certificate request if present
424 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200425 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200426 {
427 /*
428 * 1.0.b. Load the CSR
429 */
430 printf( " . Loading the certificate request ..." );
431 fflush( stdout );
432
Paul Bakkerddf26b42013-09-18 13:46:23 +0200433 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200435 error_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200436 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 goto exit;
438 }
439
Paul Bakker86d0c192013-09-18 11:11:02 +0200440 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200441 &csr.subject );
442 if( ret < 0 )
443 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 error_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200445 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 goto exit;
447 }
448
449 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200450 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200451
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200452 printf( " ok\n" );
453 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200454#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200455
456 /*
457 * 1.1. Load the keys
458 */
459 if( !opt.selfsign && !strlen( opt.request_file ) )
460 {
461 printf( " . Loading the subject key ..." );
462 fflush( stdout );
463
Paul Bakker1a7550a2013-09-15 13:01:22 +0200464 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200465 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200466 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200467 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200468 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200469 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200470 goto exit;
471 }
Paul Bakker1014e952013-09-09 13:59:42 +0200472
473 printf( " ok\n" );
474 }
475
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200476 printf( " . Loading the issuer key ..." );
477 fflush( stdout );
478
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
480 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200481 if( ret != 0 )
482 {
483 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200485 goto exit;
486 }
487
488 // Check if key and issuer certificate match
489 //
490 if( strlen( opt.issuer_crt ) )
491 {
492 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200493 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
494 &pk_rsa( *issuer_key )->N ) != 0 ||
495 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
496 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200497 {
498 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
499 ret = -1;
500 goto exit;
501 }
502 }
503
504 printf( " ok\n" );
505
506 if( opt.selfsign )
507 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100508 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200509 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200510 }
511
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200512 x509write_crt_set_subject_key( &crt, subject_key );
513 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514
Paul Bakker9397dcb2013-09-06 09:55:26 +0200515 /*
516 * 1.0. Check the names for validity
517 */
518 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
519 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200520 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200521 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
522 goto exit;
523 }
524
525 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
526 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200528 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
529 goto exit;
530 }
531
Paul Bakker9397dcb2013-09-06 09:55:26 +0200532 printf( " . Setting certificate values ..." );
533 fflush( stdout );
534
535 ret = x509write_crt_set_serial( &crt, &serial );
536 if( ret != 0 )
537 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200538 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200539 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
540 goto exit;
541 }
542
543 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
544 if( ret != 0 )
545 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200546 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200547 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
548 goto exit;
549 }
550
551 printf( " ok\n" );
552
Paul Bakker15162a02013-09-06 19:27:21 +0200553 printf( " . Adding the Basic Constraints extension ..." );
554 fflush( stdout );
555
556 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
557 opt.max_pathlen );
558 if( ret != 0 )
559 {
Paul Bakker15162a02013-09-06 19:27:21 +0200560 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200561 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
562 goto exit;
563 }
564
565 printf( " ok\n" );
566
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100567#if defined(POLARSSL_SHA1_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200568 printf( " . Adding the Subject Key Identifier ..." );
569 fflush( stdout );
570
571 ret = x509write_crt_set_subject_key_identifier( &crt );
572 if( ret != 0 )
573 {
Paul Bakker15162a02013-09-06 19:27:21 +0200574 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200575 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
576 goto exit;
577 }
578
579 printf( " ok\n" );
580
581 printf( " . Adding the Authority Key Identifier ..." );
582 fflush( stdout );
583
584 ret = x509write_crt_set_authority_key_identifier( &crt );
585 if( ret != 0 )
586 {
Paul Bakker15162a02013-09-06 19:27:21 +0200587 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200588 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
589 goto exit;
590 }
591
592 printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100593#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200594
Paul Bakker52be08c2013-09-09 12:37:54 +0200595 if( opt.key_usage )
596 {
597 printf( " . Adding the Key Usage extension ..." );
598 fflush( stdout );
599
600 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
601 if( ret != 0 )
602 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200603 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200604 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
605 goto exit;
606 }
607
608 printf( " ok\n" );
609 }
610
611 if( opt.ns_cert_type )
612 {
613 printf( " . Adding the NS Cert Type extension ..." );
614 fflush( stdout );
615
616 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
617 if( ret != 0 )
618 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200619 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200620 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
621 goto exit;
622 }
623
624 printf( " ok\n" );
625 }
626
Paul Bakker9397dcb2013-09-06 09:55:26 +0200627 /*
628 * 1.2. Writing the request
629 */
630 printf( " . Writing the certificate..." );
631 fflush( stdout );
632
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200633 if( ( ret = write_certificate( &crt, opt.output_file,
634 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200637 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
638 goto exit;
639 }
640
641 printf( " ok\n" );
642
643exit:
644 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200645 pk_free( &loaded_subject_key );
646 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200647 mpi_free( &serial );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200648 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200649
650#if defined(_WIN32)
651 printf( " + Press Enter to exit this program.\n" );
652 fflush( stdout ); getchar();
653#endif
654
655 return( ret );
656}
Paul Bakker36713e82013-09-17 13:25:29 +0200657#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
658 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200659 POLARSSL_ERROR_C */