blob: 302b8771edd08d20e2e3e02e61d9377cdcbc5ec3 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate writing
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * References:
27 * - certificates: RFC 5280, updated by RFC 6818
28 * - CSRs: PKCS#10 v1.7 aka RFC 2986
29 * - attributes: PKCS#9 v2.0 aka RFC 2985
30 */
31
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
38#if defined(POLARSSL_X509_CRT_WRITE_C)
39
40#include "polarssl/x509_crt.h"
41#include "polarssl/oid.h"
42#include "polarssl/asn1write.h"
43#include "polarssl/sha1.h"
44
45#if defined(POLARSSL_PEM_WRITE_C)
46#include "polarssl/pem.h"
47#endif /* POLARSSL_PEM_WRITE_C */
48
49void x509write_crt_init( x509write_cert *ctx )
50{
51 memset( ctx, 0, sizeof(x509write_cert) );
52
53 mpi_init( &ctx->serial );
54 ctx->version = X509_CRT_VERSION_3;
55}
56
57void x509write_crt_free( x509write_cert *ctx )
58{
59 mpi_free( &ctx->serial );
60
61 asn1_free_named_data_list( &ctx->subject );
62 asn1_free_named_data_list( &ctx->issuer );
63 asn1_free_named_data_list( &ctx->extensions );
64
65 memset( ctx, 0, sizeof(x509write_cert) );
66}
67
Paul Bakker5191e922013-10-11 10:54:28 +020068void x509write_crt_set_version( x509write_cert *ctx, int version )
69{
70 ctx->version = version;
71}
72
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
74{
75 ctx->md_alg = md_alg;
76}
77
78void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
79{
80 ctx->subject_key = key;
81}
82
83void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
84{
85 ctx->issuer_key = key;
86}
87
Paul Bakker50dc8502013-10-28 21:19:10 +010088int x509write_crt_set_subject_name( x509write_cert *ctx,
89 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090{
Paul Bakker86d0c192013-09-18 11:11:02 +020091 return x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092}
93
Paul Bakker50dc8502013-10-28 21:19:10 +010094int x509write_crt_set_issuer_name( x509write_cert *ctx,
95 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096{
Paul Bakker86d0c192013-09-18 11:11:02 +020097 return x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098}
99
100int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
101{
102 int ret;
103
104 if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
105 return( ret );
106
107 return( 0 );
108}
109
Paul Bakker50dc8502013-10-28 21:19:10 +0100110int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
111 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112{
113 if( strlen(not_before) != X509_RFC5280_UTC_TIME_LEN - 1 ||
114 strlen(not_after) != X509_RFC5280_UTC_TIME_LEN - 1 )
115 {
Paul Bakker51876562013-09-17 14:36:05 +0200116 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 }
118 strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
119 strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
120 ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
121 ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
122
123 return( 0 );
124}
125
126int x509write_crt_set_extension( x509write_cert *ctx,
127 const char *oid, size_t oid_len,
128 int critical,
129 const unsigned char *val, size_t val_len )
130{
131 return x509_set_extension( &ctx->extensions, oid, oid_len,
132 critical, val, val_len );
133}
134
135int x509write_crt_set_basic_constraints( x509write_cert *ctx,
136 int is_ca, int max_pathlen )
137{
138 int ret;
139 unsigned char buf[9];
140 unsigned char *c = buf + sizeof(buf);
141 size_t len = 0;
142
143 memset( buf, 0, sizeof(buf) );
144
145 if( is_ca && max_pathlen > 127 )
Paul Bakker51876562013-09-17 14:36:05 +0200146 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
148 if( is_ca )
149 {
150 if( max_pathlen >= 0 )
151 {
152 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
153 }
154 ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
155 }
156
157 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
158 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
159
160 return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
161 OID_SIZE( OID_BASIC_CONSTRAINTS ),
162 0, buf + sizeof(buf) - len, len );
163}
164
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100165#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
167{
168 int ret;
169 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
170 unsigned char *c = buf + sizeof(buf);
171 size_t len = 0;
172
173 memset( buf, 0, sizeof(buf));
174 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
175
176 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
177 c = buf + sizeof(buf) - 20;
178 len = 20;
179
180 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
181 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
182
183 return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
184 OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
185 0, buf + sizeof(buf) - len, len );
186}
187
188int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
189{
190 int ret;
191 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
192 unsigned char *c = buf + sizeof(buf);
193 size_t len = 0;
194
195 memset( buf, 0, sizeof(buf));
196 ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
197
198 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
199 c = buf + sizeof(buf) - 20;
200 len = 20;
201
202 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
203 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
204
205 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
206 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
207
208 return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
209 OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
210 0, buf + sizeof(buf) - len, len );
211}
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100212#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
215{
216 unsigned char buf[4];
217 unsigned char *c;
218 int ret;
219
220 c = buf + 4;
221
222 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
223 return( ret );
224
225 ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
226 OID_SIZE( OID_KEY_USAGE ),
227 1, buf, 4 );
228 if( ret != 0 )
229 return( ret );
230
231 return( 0 );
232}
233
234int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
235 unsigned char ns_cert_type )
236{
237 unsigned char buf[4];
238 unsigned char *c;
239 int ret;
240
241 c = buf + 4;
242
243 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
244 return( ret );
245
246 ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
247 OID_SIZE( OID_NS_CERT_TYPE ),
248 0, buf, 4 );
249 if( ret != 0 )
250 return( ret );
251
252 return( 0 );
253}
254
255static int x509_write_time( unsigned char **p, unsigned char *start,
256 const char *time, size_t size )
257{
258 int ret;
259 size_t len = 0;
260
261 /*
262 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
263 */
264 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
265 {
266 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
267 (const unsigned char *) time + 2,
268 size - 2 ) );
269 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
270 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
271 }
272 else
273 {
274 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
275 (const unsigned char *) time,
276 size ) );
277 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
278 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
279 }
280
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282}
283
284int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
285 int (*f_rng)(void *, unsigned char *, size_t),
286 void *p_rng )
287{
288 int ret;
289 const char *sig_oid;
290 size_t sig_oid_len = 0;
291 unsigned char *c, *c2;
292 unsigned char hash[64];
293 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
294 unsigned char tmp_buf[2048];
295 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
296 size_t len = 0;
297 pk_type_t pk_alg;
298
299 /*
300 * Prepare data to be signed in tmp_buf
301 */
302 c = tmp_buf + sizeof( tmp_buf );
303
304 /* Signature algorithm needed in TBS, and later for actual signature */
305 pk_alg = pk_get_type( ctx->issuer_key );
306 if( pk_alg == POLARSSL_PK_ECKEY )
307 pk_alg = POLARSSL_PK_ECDSA;
308
309 if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
310 &sig_oid, &sig_oid_len ) ) != 0 )
311 {
312 return( ret );
313 }
314
315 /*
316 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
317 */
318 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
319 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
320 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
321 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
322 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) );
323
324 /*
325 * SubjectPublicKeyInfo
326 */
327 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
328 tmp_buf, c - tmp_buf ) );
329 c -= pub_len;
330 len += pub_len;
331
332 /*
333 * Subject ::= Name
334 */
335 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
336
337 /*
338 * Validity ::= SEQUENCE {
339 * notBefore Time,
340 * notAfter Time }
341 */
342 sub_len = 0;
343
344 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
345 X509_RFC5280_UTC_TIME_LEN ) );
346
347 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
348 X509_RFC5280_UTC_TIME_LEN ) );
349
350 len += sub_len;
351 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
352 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
353
354 /*
355 * Issuer ::= Name
356 */
357 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
358
359 /*
360 * Signature ::= AlgorithmIdentifier
361 */
362 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
363 sig_oid, strlen( sig_oid ), 0 ) );
364
365 /*
366 * Serial ::= INTEGER
367 */
368 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
369
370 /*
371 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
372 */
373 sub_len = 0;
374 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
375 len += sub_len;
376 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
377 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) );
378
379 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
380 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
381
382 /*
383 * Make signature
384 */
385 md( md_info_from_type( ctx->md_alg ), c, len, hash );
386
387 if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
388 f_rng, p_rng ) ) != 0 )
389 {
390 return( ret );
391 }
392
393 /*
394 * Write data to output buffer
395 */
396 c2 = buf + size;
397 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
398 sig_oid, sig_oid_len, sig, sig_len ) );
399
400 c2 -= len;
401 memcpy( c2, c, len );
402
403 len += sig_and_oid_len;
404 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
405 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
406
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200407 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408}
409
410#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
411#define PEM_END_CRT "-----END CERTIFICATE-----\n"
412
413#if defined(POLARSSL_PEM_WRITE_C)
414int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
415 int (*f_rng)(void *, unsigned char *, size_t),
416 void *p_rng )
417{
418 int ret;
419 unsigned char output_buf[4096];
420 size_t olen = 0;
421
422 if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
423 f_rng, p_rng ) ) < 0 )
424 {
425 return( ret );
426 }
427
428 if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
429 output_buf + sizeof(output_buf) - ret,
430 ret, buf, size, &olen ) ) != 0 )
431 {
432 return( ret );
433 }
434
435 return( 0 );
436}
437#endif /* POLARSSL_PEM_WRITE_C */
438
439#endif /* POLARSSL_X509_CRT_WRITE_C */