blob: b4fc4500ff867446281f61ab2083ebe1ddeab776 [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 )
Paul Bakker0c226102014-04-17 16:02:36 +0200116 {
117 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200118 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200119 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200120
Paul Bakker0c226102014-04-17 16:02:36 +0200121 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200122
123 return( 0 );
124}
125
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200126#if defined(POLARSSL_X509_CSR_PARSE_C)
127#define USAGE_CSR \
128 " request_file=%%s default: (empty)\n" \
129 " If request_file is specified, subject_key,\n" \
130 " subject_pwd and subject_name are ignored!\n"
131#else
132#define USAGE_CSR ""
133#endif /* POLARSSL_X509_CSR_PARSE_C */
134
Paul Bakker9397dcb2013-09-06 09:55:26 +0200135#define USAGE \
136 "\n usage: cert_write param=<>...\n" \
137 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200138 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200139 " subject_key=%%s default: subject.key\n" \
140 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200141 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
142 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200143 " issuer_crt=%%s default: (empty)\n" \
144 " If issuer_crt is specified, issuer_name is\n" \
145 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200146 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
147 "\n" \
148 " selfsign=%%d default: 0 (false)\n" \
149 " If selfsign is enabled, issuer_name and\n" \
150 " issuer_key are required (issuer_crt and\n" \
151 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152 " issuer_key=%%s default: ca.key\n" \
153 " issuer_pwd=%%s default: (empty)\n" \
154 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200155 " serial=%%s default: 1\n" \
156 " not_before=%%s default: 20010101000000\n"\
157 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200158 " is_ca=%%d default: 0 (disabled)\n" \
159 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200160 " key_usage=%%s default: (empty)\n" \
161 " Comma-separated-list of values:\n" \
162 " digital_signature\n" \
163 " non_repudiation\n" \
164 " key_encipherment\n" \
165 " data_encipherment\n" \
166 " key_agreement\n" \
167 " key_certificate_sign\n" \
168 " crl_sign\n" \
169 " ns_cert_type=%%s default: (empty)\n" \
170 " Comma-separated-list of values:\n" \
171 " ssl_client\n" \
172 " ssl_server\n" \
173 " email\n" \
174 " object_signing\n" \
175 " ssl_ca\n" \
176 " email_ca\n" \
177 " object_signing_ca\n" \
178 "\n"
179
180int main( int argc, char *argv[] )
181{
182 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200183 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200184 pk_context loaded_issuer_key, loaded_subject_key;
185 pk_context *issuer_key = &loaded_issuer_key,
186 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200188 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100189 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200190 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200191#if defined(POLARSSL_X509_CSR_PARSE_C)
192 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200193 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200194#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 x509write_cert crt;
196 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200197 entropy_context entropy;
198 ctr_drbg_context ctr_drbg;
199 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200
201 /*
202 * Set to sane values
203 */
204 x509write_crt_init( &crt );
205 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200206 pk_init( &loaded_issuer_key );
207 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200209#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200210 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200211#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200212 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213 memset( buf, 0, 1024 );
214
215 if( argc == 0 )
216 {
217 usage:
218 printf( USAGE );
219 ret = 1;
220 goto exit;
221 }
222
Paul Bakker1014e952013-09-09 13:59:42 +0200223 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200224 opt.request_file = DFL_REQUEST_FILE;
225 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200226 opt.subject_key = DFL_SUBJECT_KEY;
227 opt.issuer_key = DFL_ISSUER_KEY;
228 opt.subject_pwd = DFL_SUBJECT_PWD;
229 opt.issuer_pwd = DFL_ISSUER_PWD;
230 opt.output_file = DFL_OUTPUT_FILENAME;
231 opt.subject_name = DFL_SUBJECT_NAME;
232 opt.issuer_name = DFL_ISSUER_NAME;
233 opt.not_before = DFL_NOT_BEFORE;
234 opt.not_after = DFL_NOT_AFTER;
235 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200236 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200237 opt.is_ca = DFL_IS_CA;
238 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239 opt.key_usage = DFL_KEY_USAGE;
240 opt.ns_cert_type = DFL_NS_CERT_TYPE;
241
242 for( i = 1; i < argc; i++ )
243 {
244
245 p = argv[i];
246 if( ( q = strchr( p, '=' ) ) == NULL )
247 goto usage;
248 *q++ = '\0';
249
Paul Bakkere2673fb2013-09-09 15:52:07 +0200250 if( strcmp( p, "request_file" ) == 0 )
251 opt.request_file = q;
252 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253 opt.subject_key = q;
254 else if( strcmp( p, "issuer_key" ) == 0 )
255 opt.issuer_key = q;
256 else if( strcmp( p, "subject_pwd" ) == 0 )
257 opt.subject_pwd = q;
258 else if( strcmp( p, "issuer_pwd" ) == 0 )
259 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200260 else if( strcmp( p, "issuer_crt" ) == 0 )
261 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 else if( strcmp( p, "output_file" ) == 0 )
263 opt.output_file = q;
264 else if( strcmp( p, "subject_name" ) == 0 )
265 {
266 opt.subject_name = q;
267 }
268 else if( strcmp( p, "issuer_name" ) == 0 )
269 {
270 opt.issuer_name = q;
271 }
272 else if( strcmp( p, "not_before" ) == 0 )
273 {
274 opt.not_before = q;
275 }
276 else if( strcmp( p, "not_after" ) == 0 )
277 {
278 opt.not_after = q;
279 }
280 else if( strcmp( p, "serial" ) == 0 )
281 {
282 opt.serial = q;
283 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200284 else if( strcmp( p, "selfsign" ) == 0 )
285 {
286 opt.selfsign = atoi( q );
287 if( opt.selfsign < 0 || opt.selfsign > 1 )
288 goto usage;
289 }
Paul Bakker15162a02013-09-06 19:27:21 +0200290 else if( strcmp( p, "is_ca" ) == 0 )
291 {
292 opt.is_ca = atoi( q );
293 if( opt.is_ca < 0 || opt.is_ca > 1 )
294 goto usage;
295 }
296 else if( strcmp( p, "max_pathlen" ) == 0 )
297 {
298 opt.max_pathlen = atoi( q );
299 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
300 goto usage;
301 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200302 else if( strcmp( p, "key_usage" ) == 0 )
303 {
304 while( q != NULL )
305 {
306 if( ( r = strchr( q, ',' ) ) != NULL )
307 *r++ = '\0';
308
309 if( strcmp( q, "digital_signature" ) == 0 )
310 opt.key_usage |= KU_DIGITAL_SIGNATURE;
311 else if( strcmp( q, "non_repudiation" ) == 0 )
312 opt.key_usage |= KU_NON_REPUDIATION;
313 else if( strcmp( q, "key_encipherment" ) == 0 )
314 opt.key_usage |= KU_KEY_ENCIPHERMENT;
315 else if( strcmp( q, "data_encipherment" ) == 0 )
316 opt.key_usage |= KU_DATA_ENCIPHERMENT;
317 else if( strcmp( q, "key_agreement" ) == 0 )
318 opt.key_usage |= KU_KEY_AGREEMENT;
319 else if( strcmp( q, "key_cert_sign" ) == 0 )
320 opt.key_usage |= KU_KEY_CERT_SIGN;
321 else if( strcmp( q, "crl_sign" ) == 0 )
322 opt.key_usage |= KU_CRL_SIGN;
323 else
324 goto usage;
325
326 q = r;
327 }
328 }
329 else if( strcmp( p, "ns_cert_type" ) == 0 )
330 {
331 while( q != NULL )
332 {
333 if( ( r = strchr( q, ',' ) ) != NULL )
334 *r++ = '\0';
335
336 if( strcmp( q, "ssl_client" ) == 0 )
337 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
338 else if( strcmp( q, "ssl_server" ) == 0 )
339 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
340 else if( strcmp( q, "email" ) == 0 )
341 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
342 else if( strcmp( q, "object_signing" ) == 0 )
343 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
344 else if( strcmp( q, "ssl_ca" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
346 else if( strcmp( q, "email_ca" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
348 else if( strcmp( q, "object_signing_ca" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
350 else
351 goto usage;
352
353 q = r;
354 }
355 }
356 else
357 goto usage;
358 }
359
Paul Bakker1014e952013-09-09 13:59:42 +0200360 printf("\n");
361
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200362 /*
363 * 0. Seed the PRNG
364 */
365 printf( " . Seeding the random number generator..." );
366 fflush( stdout );
367
368 entropy_init( &entropy );
369 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
370 (const unsigned char *) pers,
371 strlen( pers ) ) ) != 0 )
372 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700373 polarssl_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200374 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
375 goto exit;
376 }
377
378 printf( " ok\n" );
379
Paul Bakker9397dcb2013-09-06 09:55:26 +0200380 // Parse serial to MPI
381 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200382 printf( " . Reading serial number..." );
383 fflush( stdout );
384
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
386 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700387 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
389 goto exit;
390 }
391
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200392 printf( " ok\n" );
393
Paul Bakker1014e952013-09-09 13:59:42 +0200394 // Parse issuer certificate if present
395 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200396 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200397 {
398 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200399 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200400 */
401 printf( " . Loading the issuer certificate ..." );
402 fflush( stdout );
403
Paul Bakkerddf26b42013-09-18 13:46:23 +0200404 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200405 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700406 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200407 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200408 goto exit;
409 }
410
Paul Bakker86d0c192013-09-18 11:11:02 +0200411 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200412 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200413 if( ret < 0 )
414 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700415 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200416 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200417 goto exit;
418 }
419
Paul Bakkere2673fb2013-09-09 15:52:07 +0200420 opt.issuer_name = issuer_name;
421
422 printf( " ok\n" );
423 }
424
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200425#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200426 // Parse certificate request if present
427 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200428 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200429 {
430 /*
431 * 1.0.b. Load the CSR
432 */
433 printf( " . Loading the certificate request ..." );
434 fflush( stdout );
435
Paul Bakkerddf26b42013-09-18 13:46:23 +0200436 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700438 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200439 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200440 goto exit;
441 }
442
Paul Bakker86d0c192013-09-18 11:11:02 +0200443 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 &csr.subject );
445 if( ret < 0 )
446 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700447 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200448 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449 goto exit;
450 }
451
452 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200453 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200454
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200455 printf( " ok\n" );
456 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200457#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200458
459 /*
460 * 1.1. Load the keys
461 */
462 if( !opt.selfsign && !strlen( opt.request_file ) )
463 {
464 printf( " . Loading the subject key ..." );
465 fflush( stdout );
466
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200468 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200469 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200470 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700471 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200472 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200473 goto exit;
474 }
Paul Bakker1014e952013-09-09 13:59:42 +0200475
476 printf( " ok\n" );
477 }
478
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200479 printf( " . Loading the issuer key ..." );
480 fflush( stdout );
481
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
483 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200484 if( ret != 0 )
485 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700486 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200488 goto exit;
489 }
490
491 // Check if key and issuer certificate match
492 //
493 if( strlen( opt.issuer_crt ) )
494 {
495 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200496 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
497 &pk_rsa( *issuer_key )->N ) != 0 ||
498 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
499 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200500 {
501 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
502 ret = -1;
503 goto exit;
504 }
505 }
506
507 printf( " ok\n" );
508
509 if( opt.selfsign )
510 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100511 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200512 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200513 }
514
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200515 x509write_crt_set_subject_key( &crt, subject_key );
516 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200517
Paul Bakker9397dcb2013-09-06 09:55:26 +0200518 /*
519 * 1.0. Check the names for validity
520 */
521 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
522 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700523 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200524 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
525 goto exit;
526 }
527
528 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
529 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700530 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200531 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
532 goto exit;
533 }
534
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 printf( " . Setting certificate values ..." );
536 fflush( stdout );
537
538 ret = x509write_crt_set_serial( &crt, &serial );
539 if( ret != 0 )
540 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700541 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200542 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
543 goto exit;
544 }
545
546 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
547 if( ret != 0 )
548 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700549 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200550 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
551 goto exit;
552 }
553
554 printf( " ok\n" );
555
Paul Bakker15162a02013-09-06 19:27:21 +0200556 printf( " . Adding the Basic Constraints extension ..." );
557 fflush( stdout );
558
559 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
560 opt.max_pathlen );
561 if( ret != 0 )
562 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700563 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200564 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
565 goto exit;
566 }
567
568 printf( " ok\n" );
569
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100570#if defined(POLARSSL_SHA1_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200571 printf( " . Adding the Subject Key Identifier ..." );
572 fflush( stdout );
573
574 ret = x509write_crt_set_subject_key_identifier( &crt );
575 if( ret != 0 )
576 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700577 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200578 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
579 goto exit;
580 }
581
582 printf( " ok\n" );
583
584 printf( " . Adding the Authority Key Identifier ..." );
585 fflush( stdout );
586
587 ret = x509write_crt_set_authority_key_identifier( &crt );
588 if( ret != 0 )
589 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700590 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200591 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
592 goto exit;
593 }
594
595 printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100596#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200597
Paul Bakker52be08c2013-09-09 12:37:54 +0200598 if( opt.key_usage )
599 {
600 printf( " . Adding the Key Usage extension ..." );
601 fflush( stdout );
602
603 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
604 if( ret != 0 )
605 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700606 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200607 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
608 goto exit;
609 }
610
611 printf( " ok\n" );
612 }
613
614 if( opt.ns_cert_type )
615 {
616 printf( " . Adding the NS Cert Type extension ..." );
617 fflush( stdout );
618
619 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
620 if( ret != 0 )
621 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700622 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200623 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
624 goto exit;
625 }
626
627 printf( " ok\n" );
628 }
629
Paul Bakker9397dcb2013-09-06 09:55:26 +0200630 /*
631 * 1.2. Writing the request
632 */
633 printf( " . Writing the certificate..." );
634 fflush( stdout );
635
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200636 if( ( ret = write_certificate( &crt, opt.output_file,
637 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200638 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700639 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
641 goto exit;
642 }
643
644 printf( " ok\n" );
645
646exit:
647 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200648 pk_free( &loaded_subject_key );
649 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650 mpi_free( &serial );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200651 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200652
653#if defined(_WIN32)
654 printf( " + Press Enter to exit this program.\n" );
655 fflush( stdout ); getchar();
656#endif
657
658 return( ret );
659}
Paul Bakker36713e82013-09-17 13:25:29 +0200660#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
661 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200662 POLARSSL_ERROR_C */