blob: ee3448e2a48de50459c9dc78b5c4e2e3b3f8581a [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate writing
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49: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/*
23 * References:
24 * - certificates: RFC 5280, updated by RFC 6818
25 * - CSRs: PKCS#10 v1.7 aka RFC 2986
26 * - attributes: PKCS#9 v2.0 aka RFC 2985
27 */
28
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
35#if defined(POLARSSL_X509_CRT_WRITE_C)
36
37#include "polarssl/x509_crt.h"
38#include "polarssl/oid.h"
39#include "polarssl/asn1write.h"
40#include "polarssl/sha1.h"
41
42#if defined(POLARSSL_PEM_WRITE_C)
43#include "polarssl/pem.h"
44#endif /* POLARSSL_PEM_WRITE_C */
45
Paul Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
47static void polarssl_zeroize( void *v, size_t n ) {
48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051void x509write_crt_init( x509write_cert *ctx )
52{
53 memset( ctx, 0, sizeof(x509write_cert) );
54
55 mpi_init( &ctx->serial );
56 ctx->version = X509_CRT_VERSION_3;
57}
58
59void x509write_crt_free( x509write_cert *ctx )
60{
61 mpi_free( &ctx->serial );
62
63 asn1_free_named_data_list( &ctx->subject );
64 asn1_free_named_data_list( &ctx->issuer );
65 asn1_free_named_data_list( &ctx->extensions );
66
Paul Bakker34617722014-06-13 17:20:13 +020067 polarssl_zeroize( ctx, sizeof(x509write_cert) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068}
69
Paul Bakker5191e922013-10-11 10:54:28 +020070void x509write_crt_set_version( x509write_cert *ctx, int version )
71{
72 ctx->version = version;
73}
74
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
76{
77 ctx->md_alg = md_alg;
78}
79
80void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
81{
82 ctx->subject_key = key;
83}
84
85void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
86{
87 ctx->issuer_key = key;
88}
89
Paul Bakker50dc8502013-10-28 21:19:10 +010090int x509write_crt_set_subject_name( x509write_cert *ctx,
91 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092{
Paul Bakker86d0c192013-09-18 11:11:02 +020093 return x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094}
95
Paul Bakker50dc8502013-10-28 21:19:10 +010096int x509write_crt_set_issuer_name( x509write_cert *ctx,
97 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098{
Paul Bakker86d0c192013-09-18 11:11:02 +020099 return x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100}
101
102int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
103{
104 int ret;
105
106 if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
107 return( ret );
108
109 return( 0 );
110}
111
Paul Bakker50dc8502013-10-28 21:19:10 +0100112int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
113 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114{
Paul Bakker66d5d072014-06-17 16:39:18 +0200115 if( strlen( not_before ) != X509_RFC5280_UTC_TIME_LEN - 1 ||
116 strlen( not_after ) != X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 {
Paul Bakker51876562013-09-17 14:36:05 +0200118 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 }
120 strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
121 strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
122 ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
123 ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
124
125 return( 0 );
126}
127
128int x509write_crt_set_extension( x509write_cert *ctx,
129 const char *oid, size_t oid_len,
130 int critical,
131 const unsigned char *val, size_t val_len )
132{
133 return x509_set_extension( &ctx->extensions, oid, oid_len,
134 critical, val, val_len );
135}
136
137int x509write_crt_set_basic_constraints( x509write_cert *ctx,
138 int is_ca, int max_pathlen )
139{
140 int ret;
141 unsigned char buf[9];
142 unsigned char *c = buf + sizeof(buf);
143 size_t len = 0;
144
145 memset( buf, 0, sizeof(buf) );
146
147 if( is_ca && max_pathlen > 127 )
Paul Bakker51876562013-09-17 14:36:05 +0200148 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149
150 if( is_ca )
151 {
152 if( max_pathlen >= 0 )
153 {
154 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
155 }
156 ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
157 }
158
159 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200160 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
161 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
163 return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
164 OID_SIZE( OID_BASIC_CONSTRAINTS ),
165 0, buf + sizeof(buf) - len, len );
166}
167
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100168#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
170{
171 int ret;
172 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
173 unsigned char *c = buf + sizeof(buf);
174 size_t len = 0;
175
Paul Bakker66d5d072014-06-17 16:39:18 +0200176 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
178
179 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
180 c = buf + sizeof(buf) - 20;
181 len = 20;
182
183 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
184 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
185
186 return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
187 OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
188 0, buf + sizeof(buf) - len, len );
189}
190
191int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
192{
193 int ret;
194 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
195 unsigned char *c = buf + sizeof(buf);
196 size_t len = 0;
197
Paul Bakker66d5d072014-06-17 16:39:18 +0200198 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
200
201 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
202 c = buf + sizeof(buf) - 20;
203 len = 20;
204
205 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
206 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
207
208 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200209 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
210 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
212 return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
213 OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
214 0, buf + sizeof(buf) - len, len );
215}
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100216#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217
218int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
219{
220 unsigned char buf[4];
221 unsigned char *c;
222 int ret;
223
224 c = buf + 4;
225
226 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
227 return( ret );
228
229 ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
230 OID_SIZE( OID_KEY_USAGE ),
231 1, buf, 4 );
232 if( ret != 0 )
233 return( ret );
234
235 return( 0 );
236}
237
238int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
239 unsigned char ns_cert_type )
240{
241 unsigned char buf[4];
242 unsigned char *c;
243 int ret;
244
245 c = buf + 4;
246
247 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
248 return( ret );
249
250 ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
251 OID_SIZE( OID_NS_CERT_TYPE ),
252 0, buf, 4 );
253 if( ret != 0 )
254 return( ret );
255
256 return( 0 );
257}
258
259static int x509_write_time( unsigned char **p, unsigned char *start,
260 const char *time, size_t size )
261{
262 int ret;
263 size_t len = 0;
264
265 /*
266 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
267 */
268 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
269 {
270 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
271 (const unsigned char *) time + 2,
272 size - 2 ) );
273 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
274 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
275 }
276 else
277 {
278 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
279 (const unsigned char *) time,
280 size ) );
281 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
282 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
283 }
284
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286}
287
288int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
289 int (*f_rng)(void *, unsigned char *, size_t),
290 void *p_rng )
291{
292 int ret;
293 const char *sig_oid;
294 size_t sig_oid_len = 0;
295 unsigned char *c, *c2;
296 unsigned char hash[64];
297 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
298 unsigned char tmp_buf[2048];
299 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
300 size_t len = 0;
301 pk_type_t pk_alg;
302
303 /*
304 * Prepare data to be signed in tmp_buf
305 */
306 c = tmp_buf + sizeof( tmp_buf );
307
308 /* Signature algorithm needed in TBS, and later for actual signature */
309 pk_alg = pk_get_type( ctx->issuer_key );
310 if( pk_alg == POLARSSL_PK_ECKEY )
311 pk_alg = POLARSSL_PK_ECDSA;
312
313 if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
314 &sig_oid, &sig_oid_len ) ) != 0 )
315 {
316 return( ret );
317 }
318
319 /*
320 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
321 */
322 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
323 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200324 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
325 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200327 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
328 ASN1_CONSTRUCTED | 3 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330 /*
331 * SubjectPublicKeyInfo
332 */
333 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
334 tmp_buf, c - tmp_buf ) );
335 c -= pub_len;
336 len += pub_len;
337
338 /*
339 * Subject ::= Name
340 */
341 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
342
343 /*
344 * Validity ::= SEQUENCE {
345 * notBefore Time,
346 * notAfter Time }
347 */
348 sub_len = 0;
349
350 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
351 X509_RFC5280_UTC_TIME_LEN ) );
352
353 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
354 X509_RFC5280_UTC_TIME_LEN ) );
355
356 len += sub_len;
357 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200358 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
359 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 /*
362 * Issuer ::= Name
363 */
364 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
365
366 /*
367 * Signature ::= AlgorithmIdentifier
368 */
369 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
370 sig_oid, strlen( sig_oid ), 0 ) );
371
372 /*
373 * Serial ::= INTEGER
374 */
375 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
376
377 /*
378 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
379 */
380 sub_len = 0;
381 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
382 len += sub_len;
383 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200384 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
385 ASN1_CONSTRUCTED | 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200388 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
389 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390
391 /*
392 * Make signature
393 */
394 md( md_info_from_type( ctx->md_alg ), c, len, hash );
395
396 if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
397 f_rng, p_rng ) ) != 0 )
398 {
399 return( ret );
400 }
401
402 /*
403 * Write data to output buffer
404 */
405 c2 = buf + size;
406 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
407 sig_oid, sig_oid_len, sig, sig_len ) );
408
409 c2 -= len;
410 memcpy( c2, c, len );
411
412 len += sig_and_oid_len;
413 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200414 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
415 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200417 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418}
419
420#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
421#define PEM_END_CRT "-----END CERTIFICATE-----\n"
422
423#if defined(POLARSSL_PEM_WRITE_C)
424int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
425 int (*f_rng)(void *, unsigned char *, size_t),
426 void *p_rng )
427{
428 int ret;
429 unsigned char output_buf[4096];
430 size_t olen = 0;
431
432 if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
433 f_rng, p_rng ) ) < 0 )
434 {
435 return( ret );
436 }
437
438 if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
439 output_buf + sizeof(output_buf) - ret,
440 ret, buf, size, &olen ) ) != 0 )
441 {
442 return( ret );
443 }
444
445 return( 0 );
446}
447#endif /* POLARSSL_PEM_WRITE_C */
448
449#endif /* POLARSSL_X509_CRT_WRITE_C */