blob: d8000f015729bbd4e616978e713984cb492246d0 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * X509 buffer writing functionality
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
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
Manuel Pégourié-Gonnardd4eb5b52013-09-11 18:16:20 +020026/*
27 * References:
28 * - certificates: RFC 5280, updated by RFC 6818
29 * - CSRs: PKCS#10 v1.7 aka RFC 2986
30 * - attributes: PKCS#9 v2.0 aka RFC 2985
31 */
32
Paul Bakkerbdb912d2012-02-13 23:11:30 +000033#include "polarssl/config.h"
34
35#if defined(POLARSSL_X509_WRITE_C)
36
37#include "polarssl/asn1write.h"
38#include "polarssl/x509write.h"
39#include "polarssl/x509.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020040#include "polarssl/md.h"
41#include "polarssl/oid.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000042
Paul Bakker15162a02013-09-06 19:27:21 +020043#include "polarssl/sha1.h"
44
Paul Bakker135f1e92013-08-26 16:54:13 +020045#if defined(POLARSSL_BASE64_C)
46#include "polarssl/base64.h"
47#endif
48
Paul Bakker8eabfc12013-08-25 10:18:25 +020049#if defined(POLARSSL_MEMORY_C)
50#include "polarssl/memory.h"
51#else
52#include <stdlib.h>
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5f45e622013-09-09 12:02:36 +020057static int x509write_string_to_names( asn1_named_data **head, char *name )
Paul Bakker8eabfc12013-08-25 10:18:25 +020058{
59 int ret = 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +020060 char *s = name, *c = s;
Paul Bakker8eabfc12013-08-25 10:18:25 +020061 char *end = s + strlen( s );
62 char *oid = NULL;
63 int in_tag = 1;
Paul Bakker5f45e622013-09-09 12:02:36 +020064 asn1_named_data *cur;
Paul Bakker8eabfc12013-08-25 10:18:25 +020065
Manuel Pégourié-Gonnardda7317e2013-09-10 15:52:52 +020066 /* Clear existing chain if present */
67 asn1_free_named_data_list( head );
Paul Bakker8eabfc12013-08-25 10:18:25 +020068
69 while( c <= end )
70 {
71 if( in_tag && *c == '=' )
72 {
73 if( memcmp( s, "CN", 2 ) == 0 && c - s == 2 )
74 oid = OID_AT_CN;
75 else if( memcmp( s, "C", 1 ) == 0 && c - s == 1 )
76 oid = OID_AT_COUNTRY;
77 else if( memcmp( s, "O", 1 ) == 0 && c - s == 1 )
78 oid = OID_AT_ORGANIZATION;
79 else if( memcmp( s, "L", 1 ) == 0 && c - s == 1 )
80 oid = OID_AT_LOCALITY;
81 else if( memcmp( s, "R", 1 ) == 0 && c - s == 1 )
82 oid = OID_PKCS9_EMAIL;
83 else if( memcmp( s, "OU", 2 ) == 0 && c - s == 2 )
84 oid = OID_AT_ORG_UNIT;
85 else if( memcmp( s, "ST", 2 ) == 0 && c - s == 2 )
86 oid = OID_AT_STATE;
87 else
88 {
Paul Bakker0e06c0f2013-08-25 11:21:30 +020089 ret = POLARSSL_ERR_X509WRITE_UNKNOWN_OID;
Paul Bakker8eabfc12013-08-25 10:18:25 +020090 goto exit;
91 }
92
93 s = c + 1;
94 in_tag = 0;
95 }
96
97 if( !in_tag && ( *c == ',' || c == end ) )
98 {
Paul Bakker5f45e622013-09-09 12:02:36 +020099 if( ( cur = asn1_store_named_data( head, oid, strlen( oid ),
100 (unsigned char *) s,
101 c - s ) ) == NULL )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200102 {
Paul Bakker5f45e622013-09-09 12:02:36 +0200103 return( POLARSSL_ERR_X509WRITE_MALLOC_FAILED );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200104 }
105
Paul Bakkerd4bf8702013-09-09 13:59:11 +0200106 while( c < end && *(c + 1) == ' ' )
107 c++;
108
Paul Bakker8eabfc12013-08-25 10:18:25 +0200109 s = c + 1;
110 in_tag = 1;
111 }
112 c++;
113 }
114
115exit:
116
117 return( ret );
118}
119
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200120#if defined(POLARSSL_RSA_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200121/*
122 * RSAPublicKey ::= SEQUENCE {
123 * modulus INTEGER, -- n
124 * publicExponent INTEGER -- e
125 * }
126 */
127static int x509_write_rsa_pubkey( unsigned char **p, unsigned char *start,
128 rsa_context *rsa )
129{
130 int ret;
131 size_t len = 0;
132
133 ASN1_CHK_ADD( len, asn1_write_mpi( p, start, &rsa->E ) );
134 ASN1_CHK_ADD( len, asn1_write_mpi( p, start, &rsa->N ) );
135
136 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
137 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
138
139 return( len );
140}
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200141#endif /* POLARSSL_RSA_C */
142
143#if defined(POLARSSL_ECP_C)
144/*
145 * EC public key is an EC point
146 */
147static int x509_write_ec_pubkey( unsigned char **p, unsigned char *start,
148 ecp_keypair *ec )
149{
150 int ret;
151 size_t len = 0;
152 unsigned char buf[POLARSSL_ECP_MAX_PT_LEN];
153
154 if( ( ret = ecp_point_write_binary( &ec->grp, &ec->Q,
155 POLARSSL_ECP_PF_UNCOMPRESSED,
156 &len, buf, sizeof( buf ) ) ) != 0 )
157 {
158 return( ret );
159 }
160
161 if( *p - start < (int) len )
162 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
163
164 *p -= len;
165 memcpy( *p, buf, len );
166
167 return( len );
168}
169
170/*
171 * ECParameters ::= CHOICE {
172 * namedCurve OBJECT IDENTIFIER
173 * }
174 */
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200175static int x509_write_ec_param( unsigned char **p, unsigned char *start,
176 ecp_keypair *ec )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200177{
178 int ret;
179 size_t len = 0;
180 const char *oid;
181 size_t oid_len;
182
183 if( ( ret = oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
184 return( ret );
185
186 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
187
188 return( len );
189}
190#endif /* POLARSSL_ECP_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200191
Paul Bakkercd358032013-09-09 12:08:11 +0200192void x509write_csr_init( x509write_csr *ctx )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200193{
Paul Bakkercd358032013-09-09 12:08:11 +0200194 memset( ctx, 0, sizeof(x509write_csr) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195}
196
Paul Bakkercd358032013-09-09 12:08:11 +0200197void x509write_csr_free( x509write_csr *ctx )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200198{
Paul Bakker5f45e622013-09-09 12:02:36 +0200199 asn1_free_named_data_list( &ctx->subject );
200 asn1_free_named_data_list( &ctx->extensions );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201
Paul Bakkercd358032013-09-09 12:08:11 +0200202 memset( ctx, 0, sizeof(x509write_csr) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203}
204
Paul Bakkercd358032013-09-09 12:08:11 +0200205void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206{
207 ctx->md_alg = md_alg;
208}
209
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200210void x509write_csr_set_key( x509write_csr *ctx, pk_context *key )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211{
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200212 ctx->key = key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213}
214
Paul Bakkercd358032013-09-09 12:08:11 +0200215int x509write_csr_set_subject_name( x509write_csr *ctx, char *subject_name )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216{
217 return x509write_string_to_names( &ctx->subject, subject_name );
218}
219
Paul Bakker15162a02013-09-06 19:27:21 +0200220/* The first byte of the value in the asn1_named_data structure is reserved
221 * to store the critical boolean for us
222 */
223static int x509_set_extension( asn1_named_data **head,
224 const char *oid, size_t oid_len,
225 int critical,
226 const unsigned char *val, size_t val_len )
Paul Bakkerfde42702013-08-25 14:47:27 +0200227{
Paul Bakkere5eae762013-08-26 12:05:14 +0200228 asn1_named_data *cur;
Paul Bakkere5eae762013-08-26 12:05:14 +0200229
Paul Bakker59ba59f2013-09-09 11:26:00 +0200230 if( ( cur = asn1_store_named_data( head, oid, oid_len,
231 NULL, val_len + 1 ) ) == NULL )
Paul Bakkere5eae762013-08-26 12:05:14 +0200232 {
Paul Bakker59ba59f2013-09-09 11:26:00 +0200233 return( POLARSSL_ERR_X509WRITE_MALLOC_FAILED );
Paul Bakker1c0e5502013-08-26 13:41:01 +0200234 }
235
Paul Bakker15162a02013-09-06 19:27:21 +0200236 cur->val.p[0] = critical;
237 memcpy( cur->val.p + 1, val, val_len );
Paul Bakker1c0e5502013-08-26 13:41:01 +0200238
239 return( 0 );
240}
241
Paul Bakkercd358032013-09-09 12:08:11 +0200242int x509write_csr_set_extension( x509write_csr *ctx,
Paul Bakker15162a02013-09-06 19:27:21 +0200243 const char *oid, size_t oid_len,
244 const unsigned char *val, size_t val_len )
245{
246 return x509_set_extension( &ctx->extensions, oid, oid_len,
247 0, val, val_len );
248}
249
Paul Bakkercd358032013-09-09 12:08:11 +0200250int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
Paul Bakker1c0e5502013-08-26 13:41:01 +0200251{
252 unsigned char buf[4];
253 unsigned char *c;
254 int ret;
255
256 c = buf + 4;
257
Paul Bakker624d03a2013-08-26 14:12:57 +0200258 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
Paul Bakker1c0e5502013-08-26 13:41:01 +0200259 return( ret );
260
261 ret = x509write_csr_set_extension( ctx, OID_KEY_USAGE,
262 OID_SIZE( OID_KEY_USAGE ),
263 buf, 4 );
264 if( ret != 0 )
265 return( ret );
266
267 return( 0 );
268}
269
Paul Bakkercd358032013-09-09 12:08:11 +0200270int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
271 unsigned char ns_cert_type )
Paul Bakker1c0e5502013-08-26 13:41:01 +0200272{
273 unsigned char buf[4];
274 unsigned char *c;
275 int ret;
276
277 c = buf + 4;
278
279 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
280 return( ret );
281
282 ret = x509write_csr_set_extension( ctx, OID_NS_CERT_TYPE,
283 OID_SIZE( OID_NS_CERT_TYPE ),
284 buf, 4 );
285 if( ret != 0 )
286 return( ret );
Paul Bakkere5eae762013-08-26 12:05:14 +0200287
288 return( 0 );
Paul Bakkerfde42702013-08-25 14:47:27 +0200289}
290
Paul Bakker9397dcb2013-09-06 09:55:26 +0200291void x509write_crt_init( x509write_cert *ctx )
292{
293 memset( ctx, 0, sizeof(x509write_cert) );
294
295 mpi_init( &ctx->serial );
296 ctx->version = X509_CRT_VERSION_3;
297}
298
299void x509write_crt_free( x509write_cert *ctx )
300{
Paul Bakker9397dcb2013-09-06 09:55:26 +0200301 mpi_free( &ctx->serial );
302
Paul Bakker5f45e622013-09-09 12:02:36 +0200303 asn1_free_named_data_list( &ctx->subject );
304 asn1_free_named_data_list( &ctx->issuer );
305 asn1_free_named_data_list( &ctx->extensions );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306
Paul Bakkercd358032013-09-09 12:08:11 +0200307 memset( ctx, 0, sizeof(x509write_csr) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200308}
309
310void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
311{
312 ctx->md_alg = md_alg;
313}
314
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200315void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316{
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200317 ctx->subject_key = pk_rsa( *key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200318}
319
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200320void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200321{
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200322 ctx->issuer_key = pk_rsa( *key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200323}
324
325int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name )
326{
327 return x509write_string_to_names( &ctx->subject, subject_name );
328}
329
330int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name )
331{
332 return x509write_string_to_names( &ctx->issuer, issuer_name );
333}
334
335int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
336{
337 int ret;
338
339 if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
340 return( ret );
341
342 return( 0 );
343}
344
345int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
346 char *not_after )
347{
348 if( strlen(not_before) != X509_RFC5280_UTC_TIME_LEN - 1 ||
349 strlen(not_after) != X509_RFC5280_UTC_TIME_LEN - 1 )
350 {
351 return( POLARSSL_ERR_X509WRITE_BAD_INPUT_DATA );
352 }
353 strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
354 strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
355 ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
356 ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
357
358 return( 0 );
359}
360
Paul Bakker15162a02013-09-06 19:27:21 +0200361int x509write_crt_set_extension( x509write_cert *ctx,
362 const char *oid, size_t oid_len,
363 int critical,
364 const unsigned char *val, size_t val_len )
365{
366 return x509_set_extension( &ctx->extensions, oid, oid_len,
367 critical, val, val_len );
368}
369
370int x509write_crt_set_basic_constraints( x509write_cert *ctx,
371 int is_ca, int max_pathlen )
372{
373 int ret;
374 unsigned char buf[9];
375 unsigned char *c = buf + sizeof(buf);
376 size_t len = 0;
377
378 memset( buf, 0, sizeof(buf) );
379
380 if( is_ca && max_pathlen > 127 )
381 return( POLARSSL_ERR_X509WRITE_BAD_INPUT_DATA );
382
383 if( is_ca )
384 {
385 if( max_pathlen >= 0 )
386 {
387 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
388 }
389 ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
390 }
391
392 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
393 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
394
395 return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
396 OID_SIZE( OID_BASIC_CONSTRAINTS ),
397 0, buf + sizeof(buf) - len, len );
398}
399
400int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
401{
402 int ret;
403 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
404 unsigned char *c = buf + sizeof(buf);
405 size_t len = 0;
406
407 memset( buf, 0, sizeof(buf));
408 ASN1_CHK_ADD( len, x509_write_rsa_pubkey( &c, buf, ctx->subject_key ) );
409
410 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
411 c = buf + sizeof(buf) - 20;
412 len = 20;
413
414 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
415 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
416
417 return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
418 OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
419 0, buf + sizeof(buf) - len, len );
420}
421
422int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
423{
424 int ret;
425 unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
426 unsigned char *c = buf + sizeof(buf);
427 size_t len = 0;
428
429 memset( buf, 0, sizeof(buf));
430 ASN1_CHK_ADD( len, x509_write_rsa_pubkey( &c, buf, ctx->issuer_key ) );
431
432 sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
433 c = buf + sizeof(buf) - 20;
434 len = 20;
435
436 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
437 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
438
439 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
440 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
441
442 return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
443 OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
444 0, buf + sizeof(buf) - len, len );
445}
446
Paul Bakker52be08c2013-09-09 12:37:54 +0200447int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
448{
449 unsigned char buf[4];
450 unsigned char *c;
451 int ret;
452
453 c = buf + 4;
454
455 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
456 return( ret );
457
458 ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
459 OID_SIZE( OID_KEY_USAGE ),
460 1, buf, 4 );
461 if( ret != 0 )
462 return( ret );
463
464 return( 0 );
465}
466
467int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
468 unsigned char ns_cert_type )
469{
470 unsigned char buf[4];
471 unsigned char *c;
472 int ret;
473
474 c = buf + 4;
475
476 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
477 return( ret );
478
479 ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
480 OID_SIZE( OID_NS_CERT_TYPE ),
481 0, buf, 4 );
482 if( ret != 0 )
483 return( ret );
484
485 return( 0 );
486}
487
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +0200488int x509write_pubkey_der( pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000489{
490 int ret;
491 unsigned char *c;
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200492 size_t len = 0, par_len = 0, oid_len;
493 const char *oid;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000494
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200495 c = buf + size;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000496
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200497#if defined(POLARSSL_RSA_C)
498 if( pk_get_type( key ) == POLARSSL_PK_RSA )
499 ASN1_CHK_ADD( len, x509_write_rsa_pubkey( &c, buf, pk_rsa( *key ) ) );
500 else
501#endif
502#if defined(POLARSSL_ECP_C)
503 if( pk_get_type( key ) == POLARSSL_PK_ECKEY )
504 ASN1_CHK_ADD( len, x509_write_ec_pubkey( &c, buf, pk_ec( *key ) ) );
505 else
506#endif
507 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000508
509 if( c - buf < 1 )
510 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
511
Paul Bakker8eabfc12013-08-25 10:18:25 +0200512 /*
513 * SubjectPublicKeyInfo ::= SEQUENCE {
514 * algorithm AlgorithmIdentifier,
515 * subjectPublicKey BIT STRING }
516 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000517 *--c = 0;
518 len += 1;
519
520 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
521 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) );
522
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200523 if( ( ret = oid_get_oid_by_pk_alg( pk_get_type( key ),
524 &oid, &oid_len ) ) != 0 )
525 {
526 return( ret );
527 }
528
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200529#if defined(POLARSSL_ECP_C)
530 if( pk_get_type( key ) == POLARSSL_PK_ECKEY )
531 {
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200532 ASN1_CHK_ADD( par_len, x509_write_ec_param( &c, buf, pk_ec( *key ) ) );
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200533 }
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200534#endif
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200535
536 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
537 par_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000538
539 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
540 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
541
542 return( len );
543}
544
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +0200545int x509write_key_der( pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000546{
547 int ret;
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +0200548 unsigned char *c = buf + size;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000549 size_t len = 0;
550
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +0200551#if defined(POLARSSL_RSA_C)
552 if( pk_get_type( key ) == POLARSSL_PK_RSA )
553 {
554 rsa_context *rsa = pk_rsa( *key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000555
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +0200556 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) );
557 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) );
558 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) );
559 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) );
560 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) );
561 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) );
562 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) );
563 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) );
564 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000565
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +0200566 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
567 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
568 }
569 else
570#endif
571#if defined(POLARSSL_ECP_C)
572 if( pk_get_type( key ) == POLARSSL_PK_ECKEY )
573 {
574 ecp_keypair *ec = pk_ec( *key );
575 size_t pub_len = 0, par_len = 0;
576
577 /*
578 * RFC 5915, or SEC1 Appendix C.4
579 *
580 * ECPrivateKey ::= SEQUENCE {
581 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
582 * privateKey OCTET STRING,
583 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
584 * publicKey [1] BIT STRING OPTIONAL
585 * }
586 */
587
588 /* publicKey */
589 ASN1_CHK_ADD( pub_len, x509_write_ec_pubkey( &c, buf, ec ) );
590
591 if( c - buf < 1 )
592 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
593 *--c = 0;
594 pub_len += 1;
595
596 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, buf, pub_len ) );
597 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) );
598
599 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, buf, pub_len ) );
600 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, buf,
601 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) );
602 len += pub_len;
603
604 /* parameters */
605 ASN1_CHK_ADD( par_len, x509_write_ec_param( &c, buf, ec ) );
606
607 ASN1_CHK_ADD( par_len, asn1_write_len( &c, buf, par_len ) );
608 ASN1_CHK_ADD( par_len, asn1_write_tag( &c, buf,
609 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) );
610 len += par_len;
611
612 /* privateKey: write as MPI then fix tag */
613 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &ec->d ) );
614 *c = ASN1_OCTET_STRING;
615
616 /* version */
617 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 1 ) );
618
619 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
620 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
621 }
622 else
623#endif
624 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000625
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000626 return( len );
627}
628
Paul Bakker9397dcb2013-09-06 09:55:26 +0200629/*
630 * RelativeDistinguishedName ::=
631 * SET OF AttributeTypeAndValue
632 *
633 * AttributeTypeAndValue ::= SEQUENCE {
634 * type AttributeType,
635 * value AttributeValue }
636 *
637 * AttributeType ::= OBJECT IDENTIFIER
638 *
639 * AttributeValue ::= ANY DEFINED BY AttributeType
640 */
Paul Bakker5f45e622013-09-09 12:02:36 +0200641static int x509_write_name( unsigned char **p, unsigned char *start,
642 const char *oid, size_t oid_len,
643 const unsigned char *name, size_t name_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000644{
645 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000646 size_t len = 0;
647
Paul Bakker05888152012-02-16 10:26:57 +0000648 // Write PrintableString for all except OID_PKCS9_EMAIL
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000649 //
Paul Bakker5f45e622013-09-09 12:02:36 +0200650 if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len &&
651 memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 )
Paul Bakker05888152012-02-16 10:26:57 +0000652 {
Paul Bakker5f45e622013-09-09 12:02:36 +0200653 ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start,
654 (const char *) name,
655 name_len ) );
Paul Bakker05888152012-02-16 10:26:57 +0000656 }
657 else
Paul Bakker5f45e622013-09-09 12:02:36 +0200658 {
659 ASN1_CHK_ADD( len, asn1_write_printable_string( p, start,
660 (const char *) name,
661 name_len ) );
662 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000663
664 // Write OID
665 //
Paul Bakker5f45e622013-09-09 12:02:36 +0200666 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000667
Paul Bakker5f45e622013-09-09 12:02:36 +0200668 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000669 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
670
671 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
672 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
673
674 return( len );
675}
676
Paul Bakker9397dcb2013-09-06 09:55:26 +0200677static int x509_write_names( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200678 asn1_named_data *first )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200679{
680 int ret;
681 size_t len = 0;
Paul Bakker5f45e622013-09-09 12:02:36 +0200682 asn1_named_data *cur = first;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200683
684 while( cur != NULL )
685 {
Paul Bakker5f45e622013-09-09 12:02:36 +0200686 ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
687 cur->oid.len,
688 cur->val.p, cur->val.len ) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200689 cur = cur->next;
690 }
691
692 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
693 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
694
695 return( len );
696}
697
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200698static int x509_write_sig( unsigned char **p, unsigned char *start,
Paul Bakker1c3853b2013-09-10 11:43:44 +0200699 const char *oid, size_t oid_len,
700 unsigned char *sig, size_t size )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000701{
702 int ret;
703 size_t len = 0;
704
705 if( *p - start < (int) size + 1 )
706 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
707
708 len = size;
709 (*p) -= len;
710 memcpy( *p, sig, len );
711
712 *--(*p) = 0;
713 len += 1;
714
715 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
716 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
717
718 // Write OID
719 //
Paul Bakker5f45e622013-09-09 12:02:36 +0200720 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200721 oid_len, 0 ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000722
723 return( len );
724}
725
Paul Bakker9397dcb2013-09-06 09:55:26 +0200726static int x509_write_time( unsigned char **p, unsigned char *start,
727 const char *time, size_t size )
728{
729 int ret;
730 size_t len = 0;
731
Paul Bakker9c208aa2013-09-08 15:44:31 +0200732 /*
733 * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
734 */
735 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
736 {
737 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
738 (const unsigned char *) time + 2,
739 size - 2 ) );
740 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
741 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
742 }
743 else
744 {
745 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
746 (const unsigned char *) time,
747 size ) );
748 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
749 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
750 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200751
752 return( len );
753}
754
Paul Bakker15162a02013-09-06 19:27:21 +0200755static int x509_write_extension( unsigned char **p, unsigned char *start,
756 asn1_named_data *ext )
757{
758 int ret;
759 size_t len = 0;
760
761 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1,
762 ext->val.len - 1 ) );
763 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) );
764 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
765
766 if( ext->val.p[0] != 0 )
767 {
768 ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) );
769 }
770
771 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p,
772 ext->oid.len ) );
773 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) );
774 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) );
775
776 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
777 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
778
779 return( len );
780}
781
782/*
783 * Extension ::= SEQUENCE {
784 * extnID OBJECT IDENTIFIER,
785 * critical BOOLEAN DEFAULT FALSE,
786 * extnValue OCTET STRING
787 * -- contains the DER encoding of an ASN.1 value
788 * -- corresponding to the extension type identified
789 * -- by extnID
790 * }
791 */
792static int x509_write_extensions( unsigned char **p, unsigned char *start,
793 asn1_named_data *first )
794{
795 int ret;
796 size_t len = 0;
797 asn1_named_data *cur_ext = first;
798
799 while( cur_ext != NULL )
800 {
801 ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
802 cur_ext = cur_ext->next;
803 }
804
805 return( len );
806}
807
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200808int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
809 int (*f_rng)(void *, unsigned char *, size_t),
810 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000811{
812 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200813 const char *sig_oid;
Paul Bakker1c3853b2013-09-10 11:43:44 +0200814 size_t sig_oid_len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000815 unsigned char *c, *c2;
Paul Bakker3cac5e02012-02-16 14:08:06 +0000816 unsigned char hash[64];
817 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000818 unsigned char tmp_buf[2048];
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200819 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000820 size_t len = 0;
Manuel Pégourié-Gonnard0088c692013-09-12 02:38:04 +0200821 pk_type_t pk_alg;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000822
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200823 /*
824 * Prepare data to be signed in tmp_buf
825 */
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200826 c = tmp_buf + sizeof( tmp_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000827
Paul Bakker15162a02013-09-06 19:27:21 +0200828 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
Paul Bakkerfde42702013-08-25 14:47:27 +0200829
Paul Bakkere5eae762013-08-26 12:05:14 +0200830 if( len )
Paul Bakkerfde42702013-08-25 14:47:27 +0200831 {
Paul Bakkere5eae762013-08-26 12:05:14 +0200832 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
833 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
Paul Bakkerfde42702013-08-25 14:47:27 +0200834
Paul Bakkere5eae762013-08-26 12:05:14 +0200835 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
836 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SET ) );
Paul Bakkerfde42702013-08-25 14:47:27 +0200837
Paul Bakker5f45e622013-09-09 12:02:36 +0200838 ASN1_CHK_ADD( len, asn1_write_oid( &c, tmp_buf, OID_PKCS9_CSR_EXT_REQ,
839 OID_SIZE( OID_PKCS9_CSR_EXT_REQ ) ) );
Paul Bakkerfde42702013-08-25 14:47:27 +0200840
Paul Bakkere5eae762013-08-26 12:05:14 +0200841 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
842 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
Paul Bakkerfde42702013-08-25 14:47:27 +0200843 }
844
Paul Bakkere5eae762013-08-26 12:05:14 +0200845 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000846 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) );
847
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +0200848 ASN1_CHK_ADD( pub_len, x509write_pubkey_der( ctx->key,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200849 tmp_buf, c - tmp_buf ) );
Manuel Pégourié-Gonnard6dcf0bf2013-09-11 13:09:04 +0200850 c -= pub_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000851 len += pub_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000852
Paul Bakker9397dcb2013-09-06 09:55:26 +0200853 /*
854 * Subject ::= Name
855 */
856 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000857
Paul Bakker8eabfc12013-08-25 10:18:25 +0200858 /*
859 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
860 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000861 ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
862
863 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
864 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200865
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200866 /*
867 * Prepare signature
868 */
Paul Bakker8eabfc12013-08-25 10:18:25 +0200869 md( md_info_from_type( ctx->md_alg ), c, len, hash );
Paul Bakker3cac5e02012-02-16 14:08:06 +0000870
Manuel Pégourié-Gonnard0088c692013-09-12 02:38:04 +0200871 pk_alg = pk_get_type( ctx->key );
872 if( pk_alg == POLARSSL_PK_ECKEY )
873 pk_alg = POLARSSL_PK_ECDSA;
874
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200875 if( ( ret = pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200876 f_rng, p_rng ) ) != 0 ||
Manuel Pégourié-Gonnard0088c692013-09-12 02:38:04 +0200877 ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200878 &sig_oid, &sig_oid_len ) ) != 0 )
879 {
880 return( ret );
881 }
Manuel Pégourié-Gonnard5353a032013-09-11 12:14:26 +0200882
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200883 /*
884 * Write data to output buffer
885 */
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200886 c2 = buf + size;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200887 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
888 sig_oid, sig_oid_len, sig, sig_len ) );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200889
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000890 c2 -= len;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200891 memcpy( c2, c, len );
892
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200893 len += sig_and_oid_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000894 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
895 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
896
897 return( len );
898}
899
Paul Bakker9397dcb2013-09-06 09:55:26 +0200900int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size )
901{
902 int ret;
903 const char *sig_oid;
Paul Bakker1c3853b2013-09-10 11:43:44 +0200904 size_t sig_oid_len = 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200905 unsigned char *c, *c2;
906 unsigned char hash[64];
907 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
908 unsigned char tmp_buf[2048];
909 size_t sub_len = 0, pub_len = 0, sig_len = 0;
910 size_t len = 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200911
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +0200912 // temporary compatibility hack
913 pk_context subject_key;
914 subject_key.pk_info = pk_info_from_type( POLARSSL_PK_RSA );
915 subject_key.pk_ctx = ctx->subject_key;
916
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200917 c = tmp_buf + sizeof( tmp_buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200918
919 // Generate correct OID
920 //
Paul Bakker1c3853b2013-09-10 11:43:44 +0200921 ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, ctx->md_alg, &sig_oid,
922 &sig_oid_len );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200923 if( ret != 0 )
924 return( ret );
925
Paul Bakker15162a02013-09-06 19:27:21 +0200926 /*
927 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
928 */
929 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
930 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
931 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
932 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
933 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200934
935 /*
Manuel Pégourié-Gonnard6dcf0bf2013-09-11 13:09:04 +0200936 * SubjectPublicKeyInfo
Paul Bakker9397dcb2013-09-06 09:55:26 +0200937 */
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +0200938 ASN1_CHK_ADD( pub_len, x509write_pubkey_der( &subject_key,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200939 tmp_buf, c - tmp_buf ) );
Manuel Pégourié-Gonnard6dcf0bf2013-09-11 13:09:04 +0200940 c -= pub_len;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200941 len += pub_len;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200942
943 /*
944 * Subject ::= Name
945 */
946 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
947
948 /*
949 * Validity ::= SEQUENCE {
950 * notBefore Time,
951 * notAfter Time }
952 */
953 sub_len = 0;
954
955 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
956 X509_RFC5280_UTC_TIME_LEN ) );
957
958 ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
959 X509_RFC5280_UTC_TIME_LEN ) );
960
961 len += sub_len;
962 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
963 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
964
965 /*
966 * Issuer ::= Name
967 */
968 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
969
970 /*
971 * Signature ::= AlgorithmIdentifier
972 */
973 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200974 sig_oid, strlen( sig_oid ), 0 ) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200975
976 /*
977 * Serial ::= INTEGER
978 */
979 ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
980
981 /*
982 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
983 */
984 sub_len = 0;
985 ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
986 len += sub_len;
987 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
988 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) );
989
990 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
991 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
992
993 md( md_info_from_type( ctx->md_alg ), c, len, hash );
994
995 rsa_pkcs1_sign( ctx->issuer_key, NULL, NULL, RSA_PRIVATE, ctx->md_alg, 0, hash, sig );
996
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200997 c2 = buf + size;
Paul Bakker1c3853b2013-09-10 11:43:44 +0200998 ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig_oid_len,
999 sig, ctx->issuer_key->len ) );
Paul Bakker9397dcb2013-09-06 09:55:26 +02001000
1001 c2 -= len;
1002 memcpy( c2, c, len );
1003
1004 len += sig_len;
1005 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
1006 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
1007
1008 return( len );
1009}
1010
1011#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
1012#define PEM_END_CRT "-----END CERTIFICATE-----\n"
1013
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001014#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
1015#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
1016
1017#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
1018#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
1019
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02001020#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
1021#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
1022#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
1023#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
Paul Bakker135f1e92013-08-26 16:54:13 +02001024
1025#if defined(POLARSSL_BASE64_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001026static int x509write_pemify( const char *begin_str, const char *end_str,
1027 const unsigned char *der_data, size_t der_len,
1028 unsigned char *buf, size_t size )
Paul Bakker135f1e92013-08-26 16:54:13 +02001029{
1030 int ret;
Paul Bakker135f1e92013-08-26 16:54:13 +02001031 unsigned char base_buf[4096];
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001032 unsigned char *c = base_buf, *p = buf;
1033 size_t len = 0, olen = sizeof(base_buf);
Paul Bakker135f1e92013-08-26 16:54:13 +02001034
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001035 if( ( ret = base64_encode( base_buf, &olen, der_data, der_len ) ) != 0 )
Paul Bakker135f1e92013-08-26 16:54:13 +02001036 return( ret );
1037
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001038 if( olen + strlen( begin_str ) + strlen( end_str ) +
Paul Bakker135f1e92013-08-26 16:54:13 +02001039 olen / 64 > size )
1040 {
1041 return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
1042 }
1043
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001044 memcpy( p, begin_str, strlen( begin_str ) );
1045 p += strlen( begin_str );
Paul Bakker135f1e92013-08-26 16:54:13 +02001046
1047 while( olen )
1048 {
1049 len = ( olen > 64 ) ? 64 : olen;
1050 memcpy( p, c, len );
1051 olen -= len;
1052 p += len;
1053 c += len;
1054 *p++ = '\n';
1055 }
1056
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001057 memcpy( p, end_str, strlen( end_str ) );
1058 p += strlen( end_str );
Paul Bakker135f1e92013-08-26 16:54:13 +02001059
1060 *p = '\0';
1061
1062 return( 0 );
1063}
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001064
Paul Bakker9397dcb2013-09-06 09:55:26 +02001065int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size )
1066{
1067 int ret;
1068 unsigned char output_buf[4096];
1069
1070 if( ( ret = x509write_crt_der( crt, output_buf,
1071 sizeof(output_buf) ) ) < 0 )
1072 {
1073 return( ret );
1074 }
1075
1076 if( ( ret = x509write_pemify( PEM_BEGIN_CRT, PEM_END_CRT,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +02001077 output_buf + sizeof(output_buf) - ret,
Paul Bakker9397dcb2013-09-06 09:55:26 +02001078 ret, buf, size ) ) != 0 )
1079 {
1080 return( ret );
1081 }
1082
1083 return( 0 );
1084}
1085
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +02001086int x509write_pubkey_pem( pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001087{
1088 int ret;
1089 unsigned char output_buf[4096];
1090
Manuel Pégourié-Gonnarde1f821a2013-09-12 00:59:40 +02001091 if( ( ret = x509write_pubkey_der( key, output_buf,
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001092 sizeof(output_buf) ) ) < 0 )
1093 {
1094 return( ret );
1095 }
1096
1097 if( ( ret = x509write_pemify( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +02001098 output_buf + sizeof(output_buf) - ret,
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001099 ret, buf, size ) ) != 0 )
1100 {
1101 return( ret );
1102 }
1103
1104 return( 0 );
1105}
1106
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02001107int x509write_key_pem( pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001108{
1109 int ret;
1110 unsigned char output_buf[4096];
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02001111 char *begin, *end;
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001112
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02001113 if( ( ret = x509write_key_der( key, output_buf,
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001114 sizeof(output_buf) ) ) < 0 )
1115 {
1116 return( ret );
1117 }
1118
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02001119#if defined(POLARSSL_RSA_C)
1120 if( pk_get_type( key ) == POLARSSL_PK_RSA )
1121 {
1122 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
1123 end = PEM_END_PRIVATE_KEY_RSA;
1124 }
1125 else
1126#endif
1127#if defined(POLARSSL_ECP_C)
1128 if( pk_get_type( key ) == POLARSSL_PK_ECKEY )
1129 {
1130 begin = PEM_BEGIN_PRIVATE_KEY_EC;
1131 end = PEM_END_PRIVATE_KEY_EC;
1132 }
1133 else
1134#endif
1135 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
1136
1137 if( ( ret = x509write_pemify( begin, end,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +02001138 output_buf + sizeof(output_buf) - ret,
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001139 ret, buf, size ) ) != 0 )
1140 {
1141 return( ret );
1142 }
1143
1144 return( 0 );
1145}
1146
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +02001147int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
1148 int (*f_rng)(void *, unsigned char *, size_t),
1149 void *p_rng )
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001150{
1151 int ret;
1152 unsigned char output_buf[4096];
1153
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +02001154 if( ( ret = x509write_csr_der( ctx, output_buf, sizeof(output_buf),
1155 f_rng, p_rng ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001156 {
1157 return( ret );
1158 }
1159
1160 if( ( ret = x509write_pemify( PEM_BEGIN_CSR, PEM_END_CSR,
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +02001161 output_buf + sizeof(output_buf) - ret,
Paul Bakkerf3df61a2013-08-26 17:22:23 +02001162 ret, buf, size ) ) != 0 )
1163 {
1164 return( ret );
1165 }
1166
1167 return( 0 );
1168}
Paul Bakker135f1e92013-08-26 16:54:13 +02001169#endif /* POLARSSL_BASE64_C */
1170
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001171#endif