blob: b64499593e53f8dfb2625d3ed2c4d2b2d2439648 [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)
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
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)
45#include "polarssl/pem.h"
46#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,
Hanno Beckerab3fbc22017-04-26 15:01:23 +0100262 const char *t, size_t size )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263{
264 int ret;
265 size_t len = 0;
266
267 /*
268 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
269 */
Hanno Beckerab3fbc22017-04-26 15:01:23 +0100270 if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 {
272 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
Hanno Beckerab3fbc22017-04-26 15:01:23 +0100273 (const unsigned char *) t + 2,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 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,
Hanno Beckerab3fbc22017-04-26 15:01:23 +0100281 (const unsigned char *) t,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 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 */
Hanno Becker2bc85eb2017-09-28 14:43:04 +0100311
312 /* There's no direct way of extracting a signature algorithm
313 * (represented as an element of pk_type_t) from a PK instance. */
314 if( pk_can_do( ctx->issuer_key, POLARSSL_PK_RSA ) )
315 pk_alg = POLARSSL_PK_RSA;
316 else if( pk_can_do( ctx->issuer_key, POLARSSL_PK_ECDSA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317 pk_alg = POLARSSL_PK_ECDSA;
Hanno Becker2bc85eb2017-09-28 14:43:04 +0100318 else
319 return( POLARSSL_ERR_X509_INVALID_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
321 if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
322 &sig_oid, &sig_oid_len ) ) != 0 )
323 {
324 return( ret );
325 }
326
327 /*
328 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
329 */
330 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
331 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200332 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
333 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200335 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
336 ASN1_CONSTRUCTED | 3 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
338 /*
339 * SubjectPublicKeyInfo
340 */
341 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
342 tmp_buf, c - tmp_buf ) );
343 c -= pub_len;
344 len += pub_len;
345
346 /*
347 * Subject ::= Name
348 */
349 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
350
351 /*
352 * Validity ::= SEQUENCE {
353 * notBefore Time,
354 * notAfter Time }
355 */
356 sub_len = 0;
357
358 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
359 X509_RFC5280_UTC_TIME_LEN ) );
360
361 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
362 X509_RFC5280_UTC_TIME_LEN ) );
363
364 len += sub_len;
365 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200366 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
367 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
369 /*
370 * Issuer ::= Name
371 */
372 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
373
374 /*
375 * Signature ::= AlgorithmIdentifier
376 */
377 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
378 sig_oid, strlen( sig_oid ), 0 ) );
379
380 /*
381 * Serial ::= INTEGER
382 */
383 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
384
385 /*
386 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
387 */
Hanno Becker3c89dca2017-10-05 07:39:45 +0100388
389 if( ctx->version != X509_CRT_VERSION_1 )
390 {
391 sub_len = 0;
392 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
393 len += sub_len;
394 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
395 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
396 ASN1_CONSTRUCTED | 0 ) );
397 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
399 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200400 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
Hanno Becker3c89dca2017-10-05 07:39:45 +0100401 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 /*
404 * Make signature
405 */
406 md( md_info_from_type( ctx->md_alg ), c, len, hash );
407
408 if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
409 f_rng, p_rng ) ) != 0 )
410 {
411 return( ret );
412 }
413
414 /*
415 * Write data to output buffer
416 */
417 c2 = buf + size;
418 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
419 sig_oid, sig_oid_len, sig, sig_len ) );
420
Andres AG372bf792016-09-02 15:23:48 +0100421 if( len > (size_t)( c2 - buf ) )
422 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
423
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 c2 -= len;
425 memcpy( c2, c, len );
426
427 len += sig_and_oid_len;
428 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200429 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
430 ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200432 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433}
434
435#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
436#define PEM_END_CRT "-----END CERTIFICATE-----\n"
437
438#if defined(POLARSSL_PEM_WRITE_C)
439int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
440 int (*f_rng)(void *, unsigned char *, size_t),
441 void *p_rng )
442{
443 int ret;
444 unsigned char output_buf[4096];
445 size_t olen = 0;
446
447 if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
448 f_rng, p_rng ) ) < 0 )
449 {
450 return( ret );
451 }
452
453 if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
454 output_buf + sizeof(output_buf) - ret,
455 ret, buf, size, &olen ) ) != 0 )
456 {
457 return( ret );
458 }
459
460 return( 0 );
461}
462#endif /* POLARSSL_PEM_WRITE_C */
463
464#endif /* POLARSSL_X509_CRT_WRITE_C */