blob: 89a29882868bcdd03c3cdddf9cbc84e2d1d1dd7b [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/x509_crt.h"
38#include "mbedtls/oid.h"
39#include "mbedtls/asn1write.h"
40#include "mbedtls/sha1.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#if defined(POLARSSL_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046#endif /* POLARSSL_PEM_WRITE_C */
47
Paul Bakker34617722014-06-13 17:20:13 +020048/* Implementation that should never be optimized out by the compiler */
49static void polarssl_zeroize( void *v, size_t n ) {
50 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053void x509write_crt_init( x509write_cert *ctx )
54{
55 memset( ctx, 0, sizeof(x509write_cert) );
56
57 mpi_init( &ctx->serial );
58 ctx->version = X509_CRT_VERSION_3;
59}
60
61void x509write_crt_free( x509write_cert *ctx )
62{
63 mpi_free( &ctx->serial );
64
65 asn1_free_named_data_list( &ctx->subject );
66 asn1_free_named_data_list( &ctx->issuer );
67 asn1_free_named_data_list( &ctx->extensions );
68
Paul Bakker34617722014-06-13 17:20:13 +020069 polarssl_zeroize( ctx, sizeof(x509write_cert) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070}
71
Paul Bakker5191e922013-10-11 10:54:28 +020072void x509write_crt_set_version( x509write_cert *ctx, int version )
73{
74 ctx->version = version;
75}
76
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
78{
79 ctx->md_alg = md_alg;
80}
81
82void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
83{
84 ctx->subject_key = key;
85}
86
87void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
88{
89 ctx->issuer_key = key;
90}
91
Paul Bakker50dc8502013-10-28 21:19:10 +010092int x509write_crt_set_subject_name( x509write_cert *ctx,
93 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094{
Paul Bakker86d0c192013-09-18 11:11:02 +020095 return x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096}
97
Paul Bakker50dc8502013-10-28 21:19:10 +010098int x509write_crt_set_issuer_name( x509write_cert *ctx,
99 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100{
Paul Bakker86d0c192013-09-18 11:11:02 +0200101 return x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102}
103
104int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
105{
106 int ret;
107
108 if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
109 return( ret );
110
111 return( 0 );
112}
113
Paul Bakker50dc8502013-10-28 21:19:10 +0100114int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
115 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116{
Paul Bakker66d5d072014-06-17 16:39:18 +0200117 if( strlen( not_before ) != X509_RFC5280_UTC_TIME_LEN - 1 ||
118 strlen( not_after ) != X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 {
Paul Bakker51876562013-09-17 14:36:05 +0200120 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121 }
122 strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
123 strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
124 ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
125 ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
126
127 return( 0 );
128}
129
130int x509write_crt_set_extension( x509write_cert *ctx,
131 const char *oid, size_t oid_len,
132 int critical,
133 const unsigned char *val, size_t val_len )
134{
135 return x509_set_extension( &ctx->extensions, oid, oid_len,
136 critical, val, val_len );
137}
138
139int x509write_crt_set_basic_constraints( x509write_cert *ctx,
140 int is_ca, int max_pathlen )
141{
142 int ret;
143 unsigned char buf[9];
144 unsigned char *c = buf + sizeof(buf);
145 size_t len = 0;
146
147 memset( buf, 0, sizeof(buf) );
148
149 if( is_ca && max_pathlen > 127 )
Paul Bakker51876562013-09-17 14:36:05 +0200150 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151
152 if( is_ca )
153 {
154 if( max_pathlen >= 0 )
155 {
156 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
157 }
158 ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
159 }
160
161 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200162 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
163 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164
165 return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
166 OID_SIZE( OID_BASIC_CONSTRAINTS ),
167 0, buf + sizeof(buf) - len, len );
168}
169
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100170#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
172{
173 int ret;
174 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
175 unsigned char *c = buf + sizeof(buf);
176 size_t len = 0;
177
Paul Bakker66d5d072014-06-17 16:39:18 +0200178 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
180
181 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
182 c = buf + sizeof(buf) - 20;
183 len = 20;
184
185 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
186 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
187
188 return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
189 OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
190 0, buf + sizeof(buf) - len, len );
191}
192
193int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
194{
195 int ret;
196 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
197 unsigned char *c = buf + sizeof(buf);
198 size_t len = 0;
199
Paul Bakker66d5d072014-06-17 16:39:18 +0200200 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
202
203 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
204 c = buf + sizeof(buf) - 20;
205 len = 20;
206
207 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
208 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
209
210 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200211 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
212 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214 return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
215 OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
216 0, buf + sizeof(buf) - len, len );
217}
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100218#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
221{
222 unsigned char buf[4];
223 unsigned char *c;
224 int ret;
225
226 c = buf + 4;
227
228 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
229 return( ret );
230
231 ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
232 OID_SIZE( OID_KEY_USAGE ),
233 1, buf, 4 );
234 if( ret != 0 )
235 return( ret );
236
237 return( 0 );
238}
239
240int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
241 unsigned char ns_cert_type )
242{
243 unsigned char buf[4];
244 unsigned char *c;
245 int ret;
246
247 c = buf + 4;
248
249 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
250 return( ret );
251
252 ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
253 OID_SIZE( OID_NS_CERT_TYPE ),
254 0, buf, 4 );
255 if( ret != 0 )
256 return( ret );
257
258 return( 0 );
259}
260
261static int x509_write_time( unsigned char **p, unsigned char *start,
262 const char *time, size_t size )
263{
264 int ret;
265 size_t len = 0;
266
267 /*
268 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
269 */
270 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
271 {
272 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
273 (const unsigned char *) time + 2,
274 size - 2 ) );
275 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
276 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
277 }
278 else
279 {
280 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
281 (const unsigned char *) time,
282 size ) );
283 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
284 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
285 }
286
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200287 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288}
289
290int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
291 int (*f_rng)(void *, unsigned char *, size_t),
292 void *p_rng )
293{
294 int ret;
295 const char *sig_oid;
296 size_t sig_oid_len = 0;
297 unsigned char *c, *c2;
298 unsigned char hash[64];
299 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
300 unsigned char tmp_buf[2048];
301 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
302 size_t len = 0;
303 pk_type_t pk_alg;
304
305 /*
306 * Prepare data to be signed in tmp_buf
307 */
308 c = tmp_buf + sizeof( tmp_buf );
309
310 /* Signature algorithm needed in TBS, and later for actual signature */
311 pk_alg = pk_get_type( ctx->issuer_key );
312 if( pk_alg == POLARSSL_PK_ECKEY )
313 pk_alg = POLARSSL_PK_ECDSA;
314
315 if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
316 &sig_oid, &sig_oid_len ) ) != 0 )
317 {
318 return( ret );
319 }
320
321 /*
322 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
323 */
324 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
325 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200326 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
327 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200329 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
330 ASN1_CONSTRUCTED | 3 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331
332 /*
333 * SubjectPublicKeyInfo
334 */
335 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
336 tmp_buf, c - tmp_buf ) );
337 c -= pub_len;
338 len += pub_len;
339
340 /*
341 * Subject ::= Name
342 */
343 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
344
345 /*
346 * Validity ::= SEQUENCE {
347 * notBefore Time,
348 * notAfter Time }
349 */
350 sub_len = 0;
351
352 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
353 X509_RFC5280_UTC_TIME_LEN ) );
354
355 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
356 X509_RFC5280_UTC_TIME_LEN ) );
357
358 len += sub_len;
359 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200360 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
361 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 /*
364 * Issuer ::= Name
365 */
366 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
367
368 /*
369 * Signature ::= AlgorithmIdentifier
370 */
371 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
372 sig_oid, strlen( sig_oid ), 0 ) );
373
374 /*
375 * Serial ::= INTEGER
376 */
377 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
378
379 /*
380 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
381 */
382 sub_len = 0;
383 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
384 len += sub_len;
385 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200386 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
387 ASN1_CONSTRUCTED | 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200390 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
391 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393 /*
394 * Make signature
395 */
396 md( md_info_from_type( ctx->md_alg ), c, len, hash );
397
398 if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
399 f_rng, p_rng ) ) != 0 )
400 {
401 return( ret );
402 }
403
404 /*
405 * Write data to output buffer
406 */
407 c2 = buf + size;
408 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
409 sig_oid, sig_oid_len, sig, sig_len ) );
410
411 c2 -= len;
412 memcpy( c2, c, len );
413
414 len += sig_and_oid_len;
415 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200416 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
417 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420}
421
422#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
423#define PEM_END_CRT "-----END CERTIFICATE-----\n"
424
425#if defined(POLARSSL_PEM_WRITE_C)
426int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
427 int (*f_rng)(void *, unsigned char *, size_t),
428 void *p_rng )
429{
430 int ret;
431 unsigned char output_buf[4096];
432 size_t olen = 0;
433
434 if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
435 f_rng, p_rng ) ) < 0 )
436 {
437 return( ret );
438 }
439
440 if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
441 output_buf + sizeof(output_buf) - ret,
442 ret, buf, size, &olen ) ) != 0 )
443 {
444 return( ret );
445 }
446
447 return( 0 );
448}
449#endif /* POLARSSL_PEM_WRITE_C */
450
451#endif /* POLARSSL_X509_CRT_WRITE_C */