blob: 1d9c144895dea5e23dd622919e95b53604d0d15e [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
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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
37 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020038 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020039 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020040int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020046 "POLARSSL_FS_IO and/or "
47 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
48 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020049 return( 0 );
50}
51#else
52
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020053#include "polarssl/x509_crt.h"
54#include "polarssl/x509_csr.h"
55#include "polarssl/entropy.h"
56#include "polarssl/ctr_drbg.h"
57#include "polarssl/error.h"
58
Paul Bakker1014e952013-09-09 13:59:42 +020059#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020060#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020061#define DFL_SUBJECT_KEY "subject.key"
62#define DFL_ISSUER_KEY "ca.key"
63#define DFL_SUBJECT_PWD ""
64#define DFL_ISSUER_PWD ""
65#define DFL_OUTPUT_FILENAME "cert.crt"
66#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
67#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
68#define DFL_NOT_BEFORE "20010101000000"
69#define DFL_NOT_AFTER "20301231235959"
70#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020071#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020072#define DFL_IS_CA 0
73#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_KEY_USAGE 0
75#define DFL_NS_CERT_TYPE 0
76
77/*
78 * global options
79 */
80struct options
81{
Paul Bakker1014e952013-09-09 13:59:42 +020082 char *issuer_crt; /* filename of the issuer certificate */
Paul Bakkere2673fb2013-09-09 15:52:07 +020083 char *request_file; /* filename of the certificate request */
Paul Bakker9397dcb2013-09-06 09:55:26 +020084 char *subject_key; /* filename of the subject key file */
85 char *issuer_key; /* filename of the issuer key file */
86 char *subject_pwd; /* password for the subject key file */
87 char *issuer_pwd; /* password for the issuer key file */
88 char *output_file; /* where to store the constructed key file */
89 char *subject_name; /* subject name for certificate */
90 char *issuer_name; /* issuer name for certificate */
91 char *not_before; /* validity period not before */
92 char *not_after; /* validity period not after */
93 char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020094 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020095 int is_ca; /* is a CA certificate */
96 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020097 unsigned char key_usage; /* key usage flags */
98 unsigned char ns_cert_type; /* NS cert type */
99} opt;
100
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200101int write_certificate( x509write_cert *crt, char *output_file,
102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200104{
105 int ret;
106 FILE *f;
107 unsigned char output_buf[4096];
108 size_t len = 0;
109
110 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200111 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200112 return( ret );
113
114 len = strlen( (char *) output_buf );
115
116 if( ( f = fopen( output_file, "w" ) ) == NULL )
117 return( -1 );
118
119 if( fwrite( output_buf, 1, len, f ) != len )
120 return( -1 );
121
122 fclose(f);
123
124 return( 0 );
125}
126
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200127#if defined(POLARSSL_X509_CSR_PARSE_C)
128#define USAGE_CSR \
129 " request_file=%%s default: (empty)\n" \
130 " If request_file is specified, subject_key,\n" \
131 " subject_pwd and subject_name are ignored!\n"
132#else
133#define USAGE_CSR ""
134#endif /* POLARSSL_X509_CSR_PARSE_C */
135
Paul Bakker9397dcb2013-09-06 09:55:26 +0200136#define USAGE \
137 "\n usage: cert_write param=<>...\n" \
138 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200139 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200140 " subject_key=%%s default: subject.key\n" \
141 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200142 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
143 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200144 " issuer_crt=%%s default: (empty)\n" \
145 " If issuer_crt is specified, issuer_name is\n" \
146 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200147 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
148 "\n" \
149 " selfsign=%%d default: 0 (false)\n" \
150 " If selfsign is enabled, issuer_name and\n" \
151 " issuer_key are required (issuer_crt and\n" \
152 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200153 " issuer_key=%%s default: ca.key\n" \
154 " issuer_pwd=%%s default: (empty)\n" \
155 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200156 " serial=%%s default: 1\n" \
157 " not_before=%%s default: 20010101000000\n"\
158 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200159 " is_ca=%%d default: 0 (disabled)\n" \
160 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200161 " key_usage=%%s default: (empty)\n" \
162 " Comma-separated-list of values:\n" \
163 " digital_signature\n" \
164 " non_repudiation\n" \
165 " key_encipherment\n" \
166 " data_encipherment\n" \
167 " key_agreement\n" \
168 " key_certificate_sign\n" \
169 " crl_sign\n" \
170 " ns_cert_type=%%s default: (empty)\n" \
171 " Comma-separated-list of values:\n" \
172 " ssl_client\n" \
173 " ssl_server\n" \
174 " email\n" \
175 " object_signing\n" \
176 " ssl_ca\n" \
177 " email_ca\n" \
178 " object_signing_ca\n" \
179 "\n"
180
181int main( int argc, char *argv[] )
182{
183 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200184 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200185 pk_context loaded_issuer_key, loaded_subject_key;
186 pk_context *issuer_key = &loaded_issuer_key,
187 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200188 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200189 char issuer_name[128];
Paul Bakker9397dcb2013-09-06 09:55:26 +0200190 int i, j, n;
191 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200192#if defined(POLARSSL_X509_CSR_PARSE_C)
193 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200194 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200195#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196 x509write_cert crt;
197 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200198 entropy_context entropy;
199 ctr_drbg_context ctr_drbg;
200 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201
202 /*
203 * Set to sane values
204 */
205 x509write_crt_init( &crt );
206 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200207 pk_init( &loaded_issuer_key );
208 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200210#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200211 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200212#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200213 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200214 memset( buf, 0, 1024 );
215
216 if( argc == 0 )
217 {
218 usage:
219 printf( USAGE );
220 ret = 1;
221 goto exit;
222 }
223
Paul Bakker1014e952013-09-09 13:59:42 +0200224 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200225 opt.request_file = DFL_REQUEST_FILE;
226 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 opt.subject_key = DFL_SUBJECT_KEY;
228 opt.issuer_key = DFL_ISSUER_KEY;
229 opt.subject_pwd = DFL_SUBJECT_PWD;
230 opt.issuer_pwd = DFL_ISSUER_PWD;
231 opt.output_file = DFL_OUTPUT_FILENAME;
232 opt.subject_name = DFL_SUBJECT_NAME;
233 opt.issuer_name = DFL_ISSUER_NAME;
234 opt.not_before = DFL_NOT_BEFORE;
235 opt.not_after = DFL_NOT_AFTER;
236 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200237 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200238 opt.is_ca = DFL_IS_CA;
239 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200240 opt.key_usage = DFL_KEY_USAGE;
241 opt.ns_cert_type = DFL_NS_CERT_TYPE;
242
243 for( i = 1; i < argc; i++ )
244 {
245
246 p = argv[i];
247 if( ( q = strchr( p, '=' ) ) == NULL )
248 goto usage;
249 *q++ = '\0';
250
251 n = strlen( p );
252 for( j = 0; j < n; j++ )
253 {
254 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
255 argv[i][j] |= 0x20;
256 }
257
Paul Bakkere2673fb2013-09-09 15:52:07 +0200258 if( strcmp( p, "request_file" ) == 0 )
259 opt.request_file = q;
260 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200261 opt.subject_key = q;
262 else if( strcmp( p, "issuer_key" ) == 0 )
263 opt.issuer_key = q;
264 else if( strcmp( p, "subject_pwd" ) == 0 )
265 opt.subject_pwd = q;
266 else if( strcmp( p, "issuer_pwd" ) == 0 )
267 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200268 else if( strcmp( p, "issuer_crt" ) == 0 )
269 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200270 else if( strcmp( p, "output_file" ) == 0 )
271 opt.output_file = q;
272 else if( strcmp( p, "subject_name" ) == 0 )
273 {
274 opt.subject_name = q;
275 }
276 else if( strcmp( p, "issuer_name" ) == 0 )
277 {
278 opt.issuer_name = q;
279 }
280 else if( strcmp( p, "not_before" ) == 0 )
281 {
282 opt.not_before = q;
283 }
284 else if( strcmp( p, "not_after" ) == 0 )
285 {
286 opt.not_after = q;
287 }
288 else if( strcmp( p, "serial" ) == 0 )
289 {
290 opt.serial = q;
291 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200292 else if( strcmp( p, "selfsign" ) == 0 )
293 {
294 opt.selfsign = atoi( q );
295 if( opt.selfsign < 0 || opt.selfsign > 1 )
296 goto usage;
297 }
Paul Bakker15162a02013-09-06 19:27:21 +0200298 else if( strcmp( p, "is_ca" ) == 0 )
299 {
300 opt.is_ca = atoi( q );
301 if( opt.is_ca < 0 || opt.is_ca > 1 )
302 goto usage;
303 }
304 else if( strcmp( p, "max_pathlen" ) == 0 )
305 {
306 opt.max_pathlen = atoi( q );
307 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
308 goto usage;
309 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200310 else if( strcmp( p, "key_usage" ) == 0 )
311 {
312 while( q != NULL )
313 {
314 if( ( r = strchr( q, ',' ) ) != NULL )
315 *r++ = '\0';
316
317 if( strcmp( q, "digital_signature" ) == 0 )
318 opt.key_usage |= KU_DIGITAL_SIGNATURE;
319 else if( strcmp( q, "non_repudiation" ) == 0 )
320 opt.key_usage |= KU_NON_REPUDIATION;
321 else if( strcmp( q, "key_encipherment" ) == 0 )
322 opt.key_usage |= KU_KEY_ENCIPHERMENT;
323 else if( strcmp( q, "data_encipherment" ) == 0 )
324 opt.key_usage |= KU_DATA_ENCIPHERMENT;
325 else if( strcmp( q, "key_agreement" ) == 0 )
326 opt.key_usage |= KU_KEY_AGREEMENT;
327 else if( strcmp( q, "key_cert_sign" ) == 0 )
328 opt.key_usage |= KU_KEY_CERT_SIGN;
329 else if( strcmp( q, "crl_sign" ) == 0 )
330 opt.key_usage |= KU_CRL_SIGN;
331 else
332 goto usage;
333
334 q = r;
335 }
336 }
337 else if( strcmp( p, "ns_cert_type" ) == 0 )
338 {
339 while( q != NULL )
340 {
341 if( ( r = strchr( q, ',' ) ) != NULL )
342 *r++ = '\0';
343
344 if( strcmp( q, "ssl_client" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
346 else if( strcmp( q, "ssl_server" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
348 else if( strcmp( q, "email" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
350 else if( strcmp( q, "object_signing" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
352 else if( strcmp( q, "ssl_ca" ) == 0 )
353 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
354 else if( strcmp( q, "email_ca" ) == 0 )
355 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
356 else if( strcmp( q, "object_signing_ca" ) == 0 )
357 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
358 else
359 goto usage;
360
361 q = r;
362 }
363 }
364 else
365 goto usage;
366 }
367
Paul Bakker1014e952013-09-09 13:59:42 +0200368 printf("\n");
369
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200370 /*
371 * 0. Seed the PRNG
372 */
373 printf( " . Seeding the random number generator..." );
374 fflush( stdout );
375
376 entropy_init( &entropy );
377 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
378 (const unsigned char *) pers,
379 strlen( pers ) ) ) != 0 )
380 {
381 error_strerror( ret, buf, 1024 );
382 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
383 goto exit;
384 }
385
386 printf( " ok\n" );
387
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388 // Parse serial to MPI
389 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200390 printf( " . Reading serial number..." );
391 fflush( stdout );
392
Paul Bakker9397dcb2013-09-06 09:55:26 +0200393 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
394 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200395 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200396 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
397 goto exit;
398 }
399
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200400 printf( " ok\n" );
401
Paul Bakker1014e952013-09-09 13:59:42 +0200402 // Parse issuer certificate if present
403 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200404 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200405 {
406 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200407 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200408 */
409 printf( " . Loading the issuer certificate ..." );
410 fflush( stdout );
411
Paul Bakkerddf26b42013-09-18 13:46:23 +0200412 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200413 {
Paul Bakker1014e952013-09-09 13:59:42 +0200414 error_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200415 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200416 goto exit;
417 }
418
Paul Bakker86d0c192013-09-18 11:11:02 +0200419 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200420 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200421 if( ret < 0 )
422 {
Paul Bakker1014e952013-09-09 13:59:42 +0200423 error_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200424 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200425 goto exit;
426 }
427
Paul Bakkere2673fb2013-09-09 15:52:07 +0200428 opt.issuer_name = issuer_name;
429
430 printf( " ok\n" );
431 }
432
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200433#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 // Parse certificate request if present
435 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200436 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 {
438 /*
439 * 1.0.b. Load the CSR
440 */
441 printf( " . Loading the certificate request ..." );
442 fflush( stdout );
443
Paul Bakkerddf26b42013-09-18 13:46:23 +0200444 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200445 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 error_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200447 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200448 goto exit;
449 }
450
Paul Bakker86d0c192013-09-18 11:11:02 +0200451 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200452 &csr.subject );
453 if( ret < 0 )
454 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200455 error_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200456 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200457 goto exit;
458 }
459
460 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200461 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200462
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200463 printf( " ok\n" );
464 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200465#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200466
467 /*
468 * 1.1. Load the keys
469 */
470 if( !opt.selfsign && !strlen( opt.request_file ) )
471 {
472 printf( " . Loading the subject key ..." );
473 fflush( stdout );
474
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200476 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200477 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200478 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200479 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200481 goto exit;
482 }
Paul Bakker1014e952013-09-09 13:59:42 +0200483
484 printf( " ok\n" );
485 }
486
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200487 printf( " . Loading the issuer key ..." );
488 fflush( stdout );
489
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
491 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200492 if( ret != 0 )
493 {
494 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200495 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200496 goto exit;
497 }
498
499 // Check if key and issuer certificate match
500 //
501 if( strlen( opt.issuer_crt ) )
502 {
503 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200504 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
505 &pk_rsa( *issuer_key )->N ) != 0 ||
506 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
507 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 {
509 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
510 ret = -1;
511 goto exit;
512 }
513 }
514
515 printf( " ok\n" );
516
517 if( opt.selfsign )
518 {
519 opt.issuer_name = opt.subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200520 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200521 }
522
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200523 x509write_crt_set_subject_key( &crt, subject_key );
524 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200525
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 /*
527 * 1.0. Check the names for validity
528 */
529 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
530 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200531 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200532 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
533 goto exit;
534 }
535
536 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 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_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
540 goto exit;
541 }
542
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 printf( " . Setting certificate values ..." );
544 fflush( stdout );
545
546 ret = x509write_crt_set_serial( &crt, &serial );
547 if( ret != 0 )
548 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200549 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200550 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
551 goto exit;
552 }
553
554 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
555 if( ret != 0 )
556 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200557 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200558 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
559 goto exit;
560 }
561
562 printf( " ok\n" );
563
Paul Bakker15162a02013-09-06 19:27:21 +0200564 printf( " . Adding the Basic Constraints extension ..." );
565 fflush( stdout );
566
567 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
568 opt.max_pathlen );
569 if( ret != 0 )
570 {
Paul Bakker15162a02013-09-06 19:27:21 +0200571 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200572 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
573 goto exit;
574 }
575
576 printf( " ok\n" );
577
578 printf( " . Adding the Subject Key Identifier ..." );
579 fflush( stdout );
580
581 ret = x509write_crt_set_subject_key_identifier( &crt );
582 if( ret != 0 )
583 {
Paul Bakker15162a02013-09-06 19:27:21 +0200584 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200585 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
586 goto exit;
587 }
588
589 printf( " ok\n" );
590
591 printf( " . Adding the Authority Key Identifier ..." );
592 fflush( stdout );
593
594 ret = x509write_crt_set_authority_key_identifier( &crt );
595 if( ret != 0 )
596 {
Paul Bakker15162a02013-09-06 19:27:21 +0200597 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200598 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
599 goto exit;
600 }
601
602 printf( " ok\n" );
603
Paul Bakker52be08c2013-09-09 12:37:54 +0200604 if( opt.key_usage )
605 {
606 printf( " . Adding the Key Usage extension ..." );
607 fflush( stdout );
608
609 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
610 if( ret != 0 )
611 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200612 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200613 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
614 goto exit;
615 }
616
617 printf( " ok\n" );
618 }
619
620 if( opt.ns_cert_type )
621 {
622 printf( " . Adding the NS Cert Type extension ..." );
623 fflush( stdout );
624
625 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
626 if( ret != 0 )
627 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200628 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200629 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
630 goto exit;
631 }
632
633 printf( " ok\n" );
634 }
635
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 /*
637 * 1.2. Writing the request
638 */
639 printf( " . Writing the certificate..." );
640 fflush( stdout );
641
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200642 if( ( ret = write_certificate( &crt, opt.output_file,
643 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
647 goto exit;
648 }
649
650 printf( " ok\n" );
651
652exit:
653 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200654 pk_free( &loaded_subject_key );
655 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200656 mpi_free( &serial );
657
658#if defined(_WIN32)
659 printf( " + Press Enter to exit this program.\n" );
660 fflush( stdout ); getchar();
661#endif
662
663 return( ret );
664}
Paul Bakker36713e82013-09-17 13:25:29 +0200665#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
666 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200667 POLARSSL_ERROR_C */