blob: 8f0616c83a8616b8be82618a384b0177719b9804 [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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.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 Bakker8fc30b12013-11-25 13:29:43 +010082 const char *issuer_crt; /* filename of the issuer certificate */
83 const char *request_file; /* filename of the certificate request */
84 const char *subject_key; /* filename of the subject key file */
85 const char *issuer_key; /* filename of the issuer key file */
86 const char *subject_pwd; /* password for the subject key file */
87 const char *issuer_pwd; /* password for the issuer key file */
88 const char *output_file; /* where to store the constructed key file */
89 const char *subject_name; /* subject name for certificate */
90 const char *issuer_name; /* issuer name for certificate */
91 const char *not_before; /* validity period not before */
92 const char *not_after; /* validity period not after */
93 const 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
Paul Bakker8fc30b12013-11-25 13:29:43 +0100101int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200102 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 )
Paul Bakker0c226102014-04-17 16:02:36 +0200120 {
121 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200122 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200123 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200124
Paul Bakker0c226102014-04-17 16:02:36 +0200125 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200126
127 return( 0 );
128}
129
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200130#if defined(POLARSSL_X509_CSR_PARSE_C)
131#define USAGE_CSR \
132 " request_file=%%s default: (empty)\n" \
133 " If request_file is specified, subject_key,\n" \
134 " subject_pwd and subject_name are ignored!\n"
135#else
136#define USAGE_CSR ""
137#endif /* POLARSSL_X509_CSR_PARSE_C */
138
Paul Bakker9397dcb2013-09-06 09:55:26 +0200139#define USAGE \
140 "\n usage: cert_write param=<>...\n" \
141 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200142 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200143 " subject_key=%%s default: subject.key\n" \
144 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200145 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
146 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200147 " issuer_crt=%%s default: (empty)\n" \
148 " If issuer_crt is specified, issuer_name is\n" \
149 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200150 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
151 "\n" \
152 " selfsign=%%d default: 0 (false)\n" \
153 " If selfsign is enabled, issuer_name and\n" \
154 " issuer_key are required (issuer_crt and\n" \
155 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200156 " issuer_key=%%s default: ca.key\n" \
157 " issuer_pwd=%%s default: (empty)\n" \
158 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200159 " serial=%%s default: 1\n" \
160 " not_before=%%s default: 20010101000000\n"\
161 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200162 " is_ca=%%d default: 0 (disabled)\n" \
163 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200164 " key_usage=%%s default: (empty)\n" \
165 " Comma-separated-list of values:\n" \
166 " digital_signature\n" \
167 " non_repudiation\n" \
168 " key_encipherment\n" \
169 " data_encipherment\n" \
170 " key_agreement\n" \
171 " key_certificate_sign\n" \
172 " crl_sign\n" \
173 " ns_cert_type=%%s default: (empty)\n" \
174 " Comma-separated-list of values:\n" \
175 " ssl_client\n" \
176 " ssl_server\n" \
177 " email\n" \
178 " object_signing\n" \
179 " ssl_ca\n" \
180 " email_ca\n" \
181 " object_signing_ca\n" \
182 "\n"
183
184int main( int argc, char *argv[] )
185{
186 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200187 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200188 pk_context loaded_issuer_key, loaded_subject_key;
189 pk_context *issuer_key = &loaded_issuer_key,
190 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200192 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100193 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200195#if defined(POLARSSL_X509_CSR_PARSE_C)
196 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200197 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200198#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 x509write_cert crt;
200 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200201 entropy_context entropy;
202 ctr_drbg_context ctr_drbg;
203 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204
205 /*
206 * Set to sane values
207 */
208 x509write_crt_init( &crt );
209 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200210 pk_init( &loaded_issuer_key );
211 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200213#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200214 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200215#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200216 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217 memset( buf, 0, 1024 );
218
219 if( argc == 0 )
220 {
221 usage:
222 printf( USAGE );
223 ret = 1;
224 goto exit;
225 }
226
Paul Bakker1014e952013-09-09 13:59:42 +0200227 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200228 opt.request_file = DFL_REQUEST_FILE;
229 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200230 opt.subject_key = DFL_SUBJECT_KEY;
231 opt.issuer_key = DFL_ISSUER_KEY;
232 opt.subject_pwd = DFL_SUBJECT_PWD;
233 opt.issuer_pwd = DFL_ISSUER_PWD;
234 opt.output_file = DFL_OUTPUT_FILENAME;
235 opt.subject_name = DFL_SUBJECT_NAME;
236 opt.issuer_name = DFL_ISSUER_NAME;
237 opt.not_before = DFL_NOT_BEFORE;
238 opt.not_after = DFL_NOT_AFTER;
239 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200240 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200241 opt.is_ca = DFL_IS_CA;
242 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243 opt.key_usage = DFL_KEY_USAGE;
244 opt.ns_cert_type = DFL_NS_CERT_TYPE;
245
246 for( i = 1; i < argc; i++ )
247 {
248
249 p = argv[i];
250 if( ( q = strchr( p, '=' ) ) == NULL )
251 goto usage;
252 *q++ = '\0';
253
Paul Bakkere2673fb2013-09-09 15:52:07 +0200254 if( strcmp( p, "request_file" ) == 0 )
255 opt.request_file = q;
256 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200257 opt.subject_key = q;
258 else if( strcmp( p, "issuer_key" ) == 0 )
259 opt.issuer_key = q;
260 else if( strcmp( p, "subject_pwd" ) == 0 )
261 opt.subject_pwd = q;
262 else if( strcmp( p, "issuer_pwd" ) == 0 )
263 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200264 else if( strcmp( p, "issuer_crt" ) == 0 )
265 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266 else if( strcmp( p, "output_file" ) == 0 )
267 opt.output_file = q;
268 else if( strcmp( p, "subject_name" ) == 0 )
269 {
270 opt.subject_name = q;
271 }
272 else if( strcmp( p, "issuer_name" ) == 0 )
273 {
274 opt.issuer_name = q;
275 }
276 else if( strcmp( p, "not_before" ) == 0 )
277 {
278 opt.not_before = q;
279 }
280 else if( strcmp( p, "not_after" ) == 0 )
281 {
282 opt.not_after = q;
283 }
284 else if( strcmp( p, "serial" ) == 0 )
285 {
286 opt.serial = q;
287 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200288 else if( strcmp( p, "selfsign" ) == 0 )
289 {
290 opt.selfsign = atoi( q );
291 if( opt.selfsign < 0 || opt.selfsign > 1 )
292 goto usage;
293 }
Paul Bakker15162a02013-09-06 19:27:21 +0200294 else if( strcmp( p, "is_ca" ) == 0 )
295 {
296 opt.is_ca = atoi( q );
297 if( opt.is_ca < 0 || opt.is_ca > 1 )
298 goto usage;
299 }
300 else if( strcmp( p, "max_pathlen" ) == 0 )
301 {
302 opt.max_pathlen = atoi( q );
303 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
304 goto usage;
305 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306 else if( strcmp( p, "key_usage" ) == 0 )
307 {
308 while( q != NULL )
309 {
310 if( ( r = strchr( q, ',' ) ) != NULL )
311 *r++ = '\0';
312
313 if( strcmp( q, "digital_signature" ) == 0 )
314 opt.key_usage |= KU_DIGITAL_SIGNATURE;
315 else if( strcmp( q, "non_repudiation" ) == 0 )
316 opt.key_usage |= KU_NON_REPUDIATION;
317 else if( strcmp( q, "key_encipherment" ) == 0 )
318 opt.key_usage |= KU_KEY_ENCIPHERMENT;
319 else if( strcmp( q, "data_encipherment" ) == 0 )
320 opt.key_usage |= KU_DATA_ENCIPHERMENT;
321 else if( strcmp( q, "key_agreement" ) == 0 )
322 opt.key_usage |= KU_KEY_AGREEMENT;
323 else if( strcmp( q, "key_cert_sign" ) == 0 )
324 opt.key_usage |= KU_KEY_CERT_SIGN;
325 else if( strcmp( q, "crl_sign" ) == 0 )
326 opt.key_usage |= KU_CRL_SIGN;
327 else
328 goto usage;
329
330 q = r;
331 }
332 }
333 else if( strcmp( p, "ns_cert_type" ) == 0 )
334 {
335 while( q != NULL )
336 {
337 if( ( r = strchr( q, ',' ) ) != NULL )
338 *r++ = '\0';
339
340 if( strcmp( q, "ssl_client" ) == 0 )
341 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
342 else if( strcmp( q, "ssl_server" ) == 0 )
343 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
344 else if( strcmp( q, "email" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
346 else if( strcmp( q, "object_signing" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
348 else if( strcmp( q, "ssl_ca" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
350 else if( strcmp( q, "email_ca" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
352 else if( strcmp( q, "object_signing_ca" ) == 0 )
353 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
354 else
355 goto usage;
356
357 q = r;
358 }
359 }
360 else
361 goto usage;
362 }
363
Paul Bakker1014e952013-09-09 13:59:42 +0200364 printf("\n");
365
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200366 /*
367 * 0. Seed the PRNG
368 */
369 printf( " . Seeding the random number generator..." );
370 fflush( stdout );
371
372 entropy_init( &entropy );
373 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
374 (const unsigned char *) pers,
375 strlen( pers ) ) ) != 0 )
376 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700377 polarssl_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200378 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
379 goto exit;
380 }
381
382 printf( " ok\n" );
383
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 // Parse serial to MPI
385 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200386 printf( " . Reading serial number..." );
387 fflush( stdout );
388
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
390 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700391 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
393 goto exit;
394 }
395
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200396 printf( " ok\n" );
397
Paul Bakker1014e952013-09-09 13:59:42 +0200398 // Parse issuer certificate if present
399 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200400 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200401 {
402 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200403 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200404 */
405 printf( " . Loading the issuer certificate ..." );
406 fflush( stdout );
407
Paul Bakkerddf26b42013-09-18 13:46:23 +0200408 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200409 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700410 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200411 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200412 goto exit;
413 }
414
Paul Bakker86d0c192013-09-18 11:11:02 +0200415 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200416 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200417 if( ret < 0 )
418 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700419 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200420 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200421 goto exit;
422 }
423
Paul Bakkere2673fb2013-09-09 15:52:07 +0200424 opt.issuer_name = issuer_name;
425
426 printf( " ok\n" );
427 }
428
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200429#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200430 // Parse certificate request if present
431 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200432 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200433 {
434 /*
435 * 1.0.b. Load the CSR
436 */
437 printf( " . Loading the certificate request ..." );
438 fflush( stdout );
439
Paul Bakkerddf26b42013-09-18 13:46:23 +0200440 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200441 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700442 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200443 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 goto exit;
445 }
446
Paul Bakker86d0c192013-09-18 11:11:02 +0200447 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200448 &csr.subject );
449 if( ret < 0 )
450 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700451 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200452 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200453 goto exit;
454 }
455
456 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200457 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200458
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200459 printf( " ok\n" );
460 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200461#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200462
463 /*
464 * 1.1. Load the keys
465 */
466 if( !opt.selfsign && !strlen( opt.request_file ) )
467 {
468 printf( " . Loading the subject key ..." );
469 fflush( stdout );
470
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200472 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200473 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200474 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700475 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200477 goto exit;
478 }
Paul Bakker1014e952013-09-09 13:59:42 +0200479
480 printf( " ok\n" );
481 }
482
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200483 printf( " . Loading the issuer key ..." );
484 fflush( stdout );
485
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
487 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200488 if( ret != 0 )
489 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700490 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200492 goto exit;
493 }
494
495 // Check if key and issuer certificate match
496 //
497 if( strlen( opt.issuer_crt ) )
498 {
499 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200500 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
501 &pk_rsa( *issuer_key )->N ) != 0 ||
502 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
503 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200504 {
505 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
506 ret = -1;
507 goto exit;
508 }
509 }
510
511 printf( " ok\n" );
512
513 if( opt.selfsign )
514 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100515 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200516 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200517 }
518
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200519 x509write_crt_set_subject_key( &crt, subject_key );
520 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200521
Paul Bakker9397dcb2013-09-06 09:55:26 +0200522 /*
523 * 1.0. Check the names for validity
524 */
525 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
526 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700527 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200528 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
529 goto exit;
530 }
531
532 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
533 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700534 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
536 goto exit;
537 }
538
Paul Bakker9397dcb2013-09-06 09:55:26 +0200539 printf( " . Setting certificate values ..." );
540 fflush( stdout );
541
542 ret = x509write_crt_set_serial( &crt, &serial );
543 if( ret != 0 )
544 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700545 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200546 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
547 goto exit;
548 }
549
550 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
551 if( ret != 0 )
552 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700553 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200554 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
555 goto exit;
556 }
557
558 printf( " ok\n" );
559
Paul Bakker15162a02013-09-06 19:27:21 +0200560 printf( " . Adding the Basic Constraints extension ..." );
561 fflush( stdout );
562
563 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
564 opt.max_pathlen );
565 if( ret != 0 )
566 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700567 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200568 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
569 goto exit;
570 }
571
572 printf( " ok\n" );
573
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100574#if defined(POLARSSL_SHA1_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200575 printf( " . Adding the Subject Key Identifier ..." );
576 fflush( stdout );
577
578 ret = x509write_crt_set_subject_key_identifier( &crt );
579 if( ret != 0 )
580 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700581 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200582 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
583 goto exit;
584 }
585
586 printf( " ok\n" );
587
588 printf( " . Adding the Authority Key Identifier ..." );
589 fflush( stdout );
590
591 ret = x509write_crt_set_authority_key_identifier( &crt );
592 if( ret != 0 )
593 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700594 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200595 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
596 goto exit;
597 }
598
599 printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100600#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200601
Paul Bakker52be08c2013-09-09 12:37:54 +0200602 if( opt.key_usage )
603 {
604 printf( " . Adding the Key Usage extension ..." );
605 fflush( stdout );
606
607 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
608 if( ret != 0 )
609 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700610 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200611 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
612 goto exit;
613 }
614
615 printf( " ok\n" );
616 }
617
618 if( opt.ns_cert_type )
619 {
620 printf( " . Adding the NS Cert Type extension ..." );
621 fflush( stdout );
622
623 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
624 if( ret != 0 )
625 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700626 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200627 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
628 goto exit;
629 }
630
631 printf( " ok\n" );
632 }
633
Paul Bakker9397dcb2013-09-06 09:55:26 +0200634 /*
635 * 1.2. Writing the request
636 */
637 printf( " . Writing the certificate..." );
638 fflush( stdout );
639
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200640 if( ( ret = write_certificate( &crt, opt.output_file,
641 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700643 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
645 goto exit;
646 }
647
648 printf( " ok\n" );
649
650exit:
651 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200652 pk_free( &loaded_subject_key );
653 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200655 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200656 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200657
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 */