blob: 50ba62274bf61875ea42f70fb752f73a47cdcc65 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23/*
24 * References:
25 * - certificates: RFC 5280, updated by RFC 6818
26 * - CSRs: PKCS#10 v1.7 aka RFC 2986
27 * - attributes: PKCS#9 v2.0 aka RFC 2985
28 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36#if defined(POLARSSL_X509_CRT_WRITE_C)
37
38#include "polarssl/x509_crt.h"
39#include "polarssl/oid.h"
40#include "polarssl/asn1write.h"
41#include "polarssl/sha1.h"
42
43#if defined(POLARSSL_PEM_WRITE_C)
44#include "polarssl/pem.h"
45#endif /* POLARSSL_PEM_WRITE_C */
46
Paul Bakker34617722014-06-13 17:20:13 +020047/* Implementation that should never be optimized out by the compiler */
48static void polarssl_zeroize( void *v, size_t n ) {
49 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
50}
51
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052void x509write_crt_init( x509write_cert *ctx )
53{
54 memset( ctx, 0, sizeof(x509write_cert) );
55
56 mpi_init( &ctx->serial );
57 ctx->version = X509_CRT_VERSION_3;
58}
59
60void x509write_crt_free( x509write_cert *ctx )
61{
62 mpi_free( &ctx->serial );
63
64 asn1_free_named_data_list( &ctx->subject );
65 asn1_free_named_data_list( &ctx->issuer );
66 asn1_free_named_data_list( &ctx->extensions );
67
Paul Bakker34617722014-06-13 17:20:13 +020068 polarssl_zeroize( ctx, sizeof(x509write_cert) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069}
70
Paul Bakker5191e922013-10-11 10:54:28 +020071void x509write_crt_set_version( x509write_cert *ctx, int version )
72{
73 ctx->version = version;
74}
75
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
77{
78 ctx->md_alg = md_alg;
79}
80
81void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
82{
83 ctx->subject_key = key;
84}
85
86void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
87{
88 ctx->issuer_key = key;
89}
90
Paul Bakker50dc8502013-10-28 21:19:10 +010091int x509write_crt_set_subject_name( x509write_cert *ctx,
92 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093{
Paul Bakker86d0c192013-09-18 11:11:02 +020094 return x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
Paul Bakker50dc8502013-10-28 21:19:10 +010097int x509write_crt_set_issuer_name( x509write_cert *ctx,
98 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099{
Paul Bakker86d0c192013-09-18 11:11:02 +0200100 return x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101}
102
103int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
104{
105 int ret;
106
107 if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
108 return( ret );
109
110 return( 0 );
111}
112
Paul Bakker50dc8502013-10-28 21:19:10 +0100113int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
114 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115{
Paul Bakker66d5d072014-06-17 16:39:18 +0200116 if( strlen( not_before ) != X509_RFC5280_UTC_TIME_LEN - 1 ||
117 strlen( not_after ) != X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118 {
Paul Bakker51876562013-09-17 14:36:05 +0200119 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120 }
121 strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
122 strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
123 ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
124 ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
125
126 return( 0 );
127}
128
129int x509write_crt_set_extension( x509write_cert *ctx,
130 const char *oid, size_t oid_len,
131 int critical,
132 const unsigned char *val, size_t val_len )
133{
134 return x509_set_extension( &ctx->extensions, oid, oid_len,
135 critical, val, val_len );
136}
137
138int x509write_crt_set_basic_constraints( x509write_cert *ctx,
139 int is_ca, int max_pathlen )
140{
141 int ret;
142 unsigned char buf[9];
143 unsigned char *c = buf + sizeof(buf);
144 size_t len = 0;
145
146 memset( buf, 0, sizeof(buf) );
147
148 if( is_ca && max_pathlen > 127 )
Paul Bakker51876562013-09-17 14:36:05 +0200149 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
151 if( is_ca )
152 {
153 if( max_pathlen >= 0 )
154 {
155 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
156 }
157 ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
158 }
159
160 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200161 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
162 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163
164 return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
165 OID_SIZE( OID_BASIC_CONSTRAINTS ),
166 0, buf + sizeof(buf) - len, len );
167}
168
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100169#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
171{
172 int ret;
173 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
174 unsigned char *c = buf + sizeof(buf);
175 size_t len = 0;
176
Paul Bakker66d5d072014-06-17 16:39:18 +0200177 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
179
180 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
181 c = buf + sizeof(buf) - 20;
182 len = 20;
183
184 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
185 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
186
187 return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
188 OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
189 0, buf + sizeof(buf) - len, len );
190}
191
192int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
193{
194 int ret;
195 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
196 unsigned char *c = buf + sizeof(buf);
197 size_t len = 0;
198
Paul Bakker66d5d072014-06-17 16:39:18 +0200199 memset( buf, 0, sizeof(buf) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
201
202 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
203 c = buf + sizeof(buf) - 20;
204 len = 20;
205
206 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
207 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
208
209 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200210 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
211 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212
213 return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
214 OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
215 0, buf + sizeof(buf) - len, len );
216}
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100217#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
219int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
220{
221 unsigned char buf[4];
222 unsigned char *c;
223 int ret;
224
225 c = buf + 4;
226
227 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
228 return( ret );
229
230 ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
231 OID_SIZE( OID_KEY_USAGE ),
232 1, buf, 4 );
233 if( ret != 0 )
234 return( ret );
235
236 return( 0 );
237}
238
239int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
240 unsigned char ns_cert_type )
241{
242 unsigned char buf[4];
243 unsigned char *c;
244 int ret;
245
246 c = buf + 4;
247
248 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
249 return( ret );
250
251 ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
252 OID_SIZE( OID_NS_CERT_TYPE ),
253 0, buf, 4 );
254 if( ret != 0 )
255 return( ret );
256
257 return( 0 );
258}
259
260static int x509_write_time( unsigned char **p, unsigned char *start,
261 const char *time, size_t size )
262{
263 int ret;
264 size_t len = 0;
265
266 /*
267 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
268 */
269 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
270 {
271 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
272 (const unsigned char *) time + 2,
273 size - 2 ) );
274 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
275 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
276 }
277 else
278 {
279 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
280 (const unsigned char *) time,
281 size ) );
282 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
283 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
284 }
285
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287}
288
289int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
290 int (*f_rng)(void *, unsigned char *, size_t),
291 void *p_rng )
292{
293 int ret;
294 const char *sig_oid;
295 size_t sig_oid_len = 0;
296 unsigned char *c, *c2;
297 unsigned char hash[64];
298 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
299 unsigned char tmp_buf[2048];
300 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
301 size_t len = 0;
302 pk_type_t pk_alg;
303
304 /*
305 * Prepare data to be signed in tmp_buf
306 */
307 c = tmp_buf + sizeof( tmp_buf );
308
309 /* Signature algorithm needed in TBS, and later for actual signature */
310 pk_alg = pk_get_type( ctx->issuer_key );
311 if( pk_alg == POLARSSL_PK_ECKEY )
312 pk_alg = POLARSSL_PK_ECDSA;
313
314 if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
315 &sig_oid, &sig_oid_len ) ) != 0 )
316 {
317 return( ret );
318 }
319
320 /*
321 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
322 */
323 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
324 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200325 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
326 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200328 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
329 ASN1_CONSTRUCTED | 3 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331 /*
332 * SubjectPublicKeyInfo
333 */
334 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
335 tmp_buf, c - tmp_buf ) );
336 c -= pub_len;
337 len += pub_len;
338
339 /*
340 * Subject ::= Name
341 */
342 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
343
344 /*
345 * Validity ::= SEQUENCE {
346 * notBefore Time,
347 * notAfter Time }
348 */
349 sub_len = 0;
350
351 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
352 X509_RFC5280_UTC_TIME_LEN ) );
353
354 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
355 X509_RFC5280_UTC_TIME_LEN ) );
356
357 len += sub_len;
358 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200359 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
360 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362 /*
363 * Issuer ::= Name
364 */
365 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
366
367 /*
368 * Signature ::= AlgorithmIdentifier
369 */
370 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
371 sig_oid, strlen( sig_oid ), 0 ) );
372
373 /*
374 * Serial ::= INTEGER
375 */
376 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
377
378 /*
379 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
380 */
381 sub_len = 0;
382 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
383 len += sub_len;
384 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200385 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
386 ASN1_CONSTRUCTED | 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
388 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200389 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
390 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392 /*
393 * Make signature
394 */
395 md( md_info_from_type( ctx->md_alg ), c, len, hash );
396
397 if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
398 f_rng, p_rng ) ) != 0 )
399 {
400 return( ret );
401 }
402
403 /*
404 * Write data to output buffer
405 */
406 c2 = buf + size;
407 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
408 sig_oid, sig_oid_len, sig, sig_len ) );
409
410 c2 -= len;
411 memcpy( c2, c, len );
412
413 len += sig_and_oid_len;
414 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200415 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
416 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419}
420
421#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
422#define PEM_END_CRT "-----END CERTIFICATE-----\n"
423
424#if defined(POLARSSL_PEM_WRITE_C)
425int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
426 int (*f_rng)(void *, unsigned char *, size_t),
427 void *p_rng )
428{
429 int ret;
430 unsigned char output_buf[4096];
431 size_t olen = 0;
432
433 if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
434 f_rng, p_rng ) ) < 0 )
435 {
436 return( ret );
437 }
438
439 if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
440 output_buf + sizeof(output_buf) - ret,
441 ret, buf, size, &olen ) ) != 0 )
442 {
443 return( ret );
444 }
445
446 return( 0 );
447}
448#endif /* POLARSSL_PEM_WRITE_C */
449
450#endif /* POLARSSL_X509_CRT_WRITE_C */