blob: b4bfe765bcd997b952c9b0be0f354af952761e4d [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker9397dcb2013-09-06 09:55:26 +02005 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +02007 *
Paul Bakker9397dcb2013-09-06 09:55:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020028
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
34 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020035 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020036 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020037int main( int argc, char *argv[] )
38{
39 ((void) argc);
40 ((void) argv);
41
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042 printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020043 "POLARSSL_FS_IO and/or "
44 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
45 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020046 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020050#include "polarssl/x509_crt.h"
51#include "polarssl/x509_csr.h"
52#include "polarssl/entropy.h"
53#include "polarssl/ctr_drbg.h"
54#include "polarssl/error.h"
55
Paul Bakker1014e952013-09-09 13:59:42 +020056#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020057#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020058#define DFL_SUBJECT_KEY "subject.key"
59#define DFL_ISSUER_KEY "ca.key"
60#define DFL_SUBJECT_PWD ""
61#define DFL_ISSUER_PWD ""
62#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000063#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
64#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020065#define DFL_NOT_BEFORE "20010101000000"
66#define DFL_NOT_AFTER "20301231235959"
67#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020068#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020069#define DFL_IS_CA 0
70#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_KEY_USAGE 0
72#define DFL_NS_CERT_TYPE 0
73
74/*
75 * global options
76 */
77struct options
78{
Paul Bakker8fc30b12013-11-25 13:29:43 +010079 const char *issuer_crt; /* filename of the issuer certificate */
80 const char *request_file; /* filename of the certificate request */
81 const char *subject_key; /* filename of the subject key file */
82 const char *issuer_key; /* filename of the issuer key file */
83 const char *subject_pwd; /* password for the subject key file */
84 const char *issuer_pwd; /* password for the issuer key file */
85 const char *output_file; /* where to store the constructed key file */
86 const char *subject_name; /* subject name for certificate */
87 const char *issuer_name; /* issuer name for certificate */
88 const char *not_before; /* validity period not before */
89 const char *not_after; /* validity period not after */
90 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020091 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020092 int is_ca; /* is a CA certificate */
93 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020094 unsigned char key_usage; /* key usage flags */
95 unsigned char ns_cert_type; /* NS cert type */
96} opt;
97
Paul Bakker8fc30b12013-11-25 13:29:43 +010098int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020099 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200101{
102 int ret;
103 FILE *f;
104 unsigned char output_buf[4096];
105 size_t len = 0;
106
107 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200108 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200109 return( ret );
110
111 len = strlen( (char *) output_buf );
112
113 if( ( f = fopen( output_file, "w" ) ) == NULL )
114 return( -1 );
115
116 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200117 {
118 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200119 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200120 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200121
Paul Bakker0c226102014-04-17 16:02:36 +0200122 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200123
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" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000142 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200143 "\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" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000147 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200148 "\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 Bakkerc97f9f62013-11-30 15:13:02 +0100190 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191 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
Paul Bakkere2673fb2013-09-09 15:52:07 +0200251 if( strcmp( p, "request_file" ) == 0 )
252 opt.request_file = q;
253 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200254 opt.subject_key = q;
255 else if( strcmp( p, "issuer_key" ) == 0 )
256 opt.issuer_key = q;
257 else if( strcmp( p, "subject_pwd" ) == 0 )
258 opt.subject_pwd = q;
259 else if( strcmp( p, "issuer_pwd" ) == 0 )
260 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200261 else if( strcmp( p, "issuer_crt" ) == 0 )
262 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200263 else if( strcmp( p, "output_file" ) == 0 )
264 opt.output_file = q;
265 else if( strcmp( p, "subject_name" ) == 0 )
266 {
267 opt.subject_name = q;
268 }
269 else if( strcmp( p, "issuer_name" ) == 0 )
270 {
271 opt.issuer_name = q;
272 }
273 else if( strcmp( p, "not_before" ) == 0 )
274 {
275 opt.not_before = q;
276 }
277 else if( strcmp( p, "not_after" ) == 0 )
278 {
279 opt.not_after = q;
280 }
281 else if( strcmp( p, "serial" ) == 0 )
282 {
283 opt.serial = q;
284 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200285 else if( strcmp( p, "selfsign" ) == 0 )
286 {
287 opt.selfsign = atoi( q );
288 if( opt.selfsign < 0 || opt.selfsign > 1 )
289 goto usage;
290 }
Paul Bakker15162a02013-09-06 19:27:21 +0200291 else if( strcmp( p, "is_ca" ) == 0 )
292 {
293 opt.is_ca = atoi( q );
294 if( opt.is_ca < 0 || opt.is_ca > 1 )
295 goto usage;
296 }
297 else if( strcmp( p, "max_pathlen" ) == 0 )
298 {
299 opt.max_pathlen = atoi( q );
300 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
301 goto usage;
302 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 else if( strcmp( p, "key_usage" ) == 0 )
304 {
305 while( q != NULL )
306 {
307 if( ( r = strchr( q, ',' ) ) != NULL )
308 *r++ = '\0';
309
310 if( strcmp( q, "digital_signature" ) == 0 )
311 opt.key_usage |= KU_DIGITAL_SIGNATURE;
312 else if( strcmp( q, "non_repudiation" ) == 0 )
313 opt.key_usage |= KU_NON_REPUDIATION;
314 else if( strcmp( q, "key_encipherment" ) == 0 )
315 opt.key_usage |= KU_KEY_ENCIPHERMENT;
316 else if( strcmp( q, "data_encipherment" ) == 0 )
317 opt.key_usage |= KU_DATA_ENCIPHERMENT;
318 else if( strcmp( q, "key_agreement" ) == 0 )
319 opt.key_usage |= KU_KEY_AGREEMENT;
320 else if( strcmp( q, "key_cert_sign" ) == 0 )
321 opt.key_usage |= KU_KEY_CERT_SIGN;
322 else if( strcmp( q, "crl_sign" ) == 0 )
323 opt.key_usage |= KU_CRL_SIGN;
324 else
325 goto usage;
326
327 q = r;
328 }
329 }
330 else if( strcmp( p, "ns_cert_type" ) == 0 )
331 {
332 while( q != NULL )
333 {
334 if( ( r = strchr( q, ',' ) ) != NULL )
335 *r++ = '\0';
336
337 if( strcmp( q, "ssl_client" ) == 0 )
338 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
339 else if( strcmp( q, "ssl_server" ) == 0 )
340 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
341 else if( strcmp( q, "email" ) == 0 )
342 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
343 else if( strcmp( q, "object_signing" ) == 0 )
344 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
345 else if( strcmp( q, "ssl_ca" ) == 0 )
346 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
347 else if( strcmp( q, "email_ca" ) == 0 )
348 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
349 else if( strcmp( q, "object_signing_ca" ) == 0 )
350 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
351 else
352 goto usage;
353
354 q = r;
355 }
356 }
357 else
358 goto usage;
359 }
360
Paul Bakker1014e952013-09-09 13:59:42 +0200361 printf("\n");
362
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200363 /*
364 * 0. Seed the PRNG
365 */
366 printf( " . Seeding the random number generator..." );
367 fflush( stdout );
368
369 entropy_init( &entropy );
370 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
371 (const unsigned char *) pers,
372 strlen( pers ) ) ) != 0 )
373 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700374 polarssl_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200375 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
376 goto exit;
377 }
378
379 printf( " ok\n" );
380
Paul Bakker9397dcb2013-09-06 09:55:26 +0200381 // Parse serial to MPI
382 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200383 printf( " . Reading serial number..." );
384 fflush( stdout );
385
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
387 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700388 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
390 goto exit;
391 }
392
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200393 printf( " ok\n" );
394
Paul Bakker1014e952013-09-09 13:59:42 +0200395 // Parse issuer certificate if present
396 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200397 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200398 {
399 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200400 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200401 */
402 printf( " . Loading the issuer certificate ..." );
403 fflush( stdout );
404
Paul Bakkerddf26b42013-09-18 13:46:23 +0200405 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200406 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700407 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200408 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200409 goto exit;
410 }
411
Paul Bakker86d0c192013-09-18 11:11:02 +0200412 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200413 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200414 if( ret < 0 )
415 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700416 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200417 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200418 goto exit;
419 }
420
Paul Bakkere2673fb2013-09-09 15:52:07 +0200421 opt.issuer_name = issuer_name;
422
423 printf( " ok\n" );
424 }
425
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200426#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200427 // Parse certificate request if present
428 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200429 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200430 {
431 /*
432 * 1.0.b. Load the CSR
433 */
434 printf( " . Loading the certificate request ..." );
435 fflush( stdout );
436
Paul Bakkerddf26b42013-09-18 13:46:23 +0200437 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200438 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700439 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200440 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200441 goto exit;
442 }
443
Paul Bakker86d0c192013-09-18 11:11:02 +0200444 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200445 &csr.subject );
446 if( ret < 0 )
447 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700448 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200449 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200450 goto exit;
451 }
452
453 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200454 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200455
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200456 printf( " ok\n" );
457 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200458#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200459
460 /*
461 * 1.1. Load the keys
462 */
463 if( !opt.selfsign && !strlen( opt.request_file ) )
464 {
465 printf( " . Loading the subject key ..." );
466 fflush( stdout );
467
Paul Bakker1a7550a2013-09-15 13:01:22 +0200468 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200469 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200470 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200471 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700472 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200473 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200474 goto exit;
475 }
Paul Bakker1014e952013-09-09 13:59:42 +0200476
477 printf( " ok\n" );
478 }
479
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200480 printf( " . Loading the issuer key ..." );
481 fflush( stdout );
482
Paul Bakker1a7550a2013-09-15 13:01:22 +0200483 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
484 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200485 if( ret != 0 )
486 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700487 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200489 goto exit;
490 }
491
492 // Check if key and issuer certificate match
493 //
494 if( strlen( opt.issuer_crt ) )
495 {
496 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200497 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
498 &pk_rsa( *issuer_key )->N ) != 0 ||
499 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
500 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200501 {
502 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
503 ret = -1;
504 goto exit;
505 }
506 }
507
508 printf( " ok\n" );
509
510 if( opt.selfsign )
511 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100512 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200513 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514 }
515
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200516 x509write_crt_set_subject_key( &crt, subject_key );
517 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200518
Paul Bakker9397dcb2013-09-06 09:55:26 +0200519 /*
520 * 1.0. Check the names for validity
521 */
522 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
523 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700524 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200525 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
526 goto exit;
527 }
528
529 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
530 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700531 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200532 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
533 goto exit;
534 }
535
Paul Bakker9397dcb2013-09-06 09:55:26 +0200536 printf( " . Setting certificate values ..." );
537 fflush( stdout );
538
539 ret = x509write_crt_set_serial( &crt, &serial );
540 if( ret != 0 )
541 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700542 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
544 goto exit;
545 }
546
547 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
548 if( ret != 0 )
549 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700550 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200551 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
552 goto exit;
553 }
554
555 printf( " ok\n" );
556
Paul Bakker15162a02013-09-06 19:27:21 +0200557 printf( " . Adding the Basic Constraints extension ..." );
558 fflush( stdout );
559
560 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
561 opt.max_pathlen );
562 if( ret != 0 )
563 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700564 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200565 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
566 goto exit;
567 }
568
569 printf( " ok\n" );
570
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100571#if defined(POLARSSL_SHA1_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200572 printf( " . Adding the Subject Key Identifier ..." );
573 fflush( stdout );
574
575 ret = x509write_crt_set_subject_key_identifier( &crt );
576 if( ret != 0 )
577 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700578 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200579 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
580 goto exit;
581 }
582
583 printf( " ok\n" );
584
585 printf( " . Adding the Authority Key Identifier ..." );
586 fflush( stdout );
587
588 ret = x509write_crt_set_authority_key_identifier( &crt );
589 if( ret != 0 )
590 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700591 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200592 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
593 goto exit;
594 }
595
596 printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100597#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200598
Paul Bakker52be08c2013-09-09 12:37:54 +0200599 if( opt.key_usage )
600 {
601 printf( " . Adding the Key Usage extension ..." );
602 fflush( stdout );
603
604 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
605 if( ret != 0 )
606 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700607 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200608 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
609 goto exit;
610 }
611
612 printf( " ok\n" );
613 }
614
615 if( opt.ns_cert_type )
616 {
617 printf( " . Adding the NS Cert Type extension ..." );
618 fflush( stdout );
619
620 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
621 if( ret != 0 )
622 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700623 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200624 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
625 goto exit;
626 }
627
628 printf( " ok\n" );
629 }
630
Paul Bakker9397dcb2013-09-06 09:55:26 +0200631 /*
632 * 1.2. Writing the request
633 */
634 printf( " . Writing the certificate..." );
635 fflush( stdout );
636
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200637 if( ( ret = write_certificate( &crt, opt.output_file,
638 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700640 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200641 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
642 goto exit;
643 }
644
645 printf( " ok\n" );
646
647exit:
648 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200649 pk_free( &loaded_subject_key );
650 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200651 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200652 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200653 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654
655#if defined(_WIN32)
656 printf( " + Press Enter to exit this program.\n" );
657 fflush( stdout ); getchar();
658#endif
659
660 return( ret );
661}
Paul Bakker36713e82013-09-17 13:25:29 +0200662#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
663 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200664 POLARSSL_ERROR_C */