PolarSSL v1.3.7
x509write_crt.c
Go to the documentation of this file.
1 /*
2  * X.509 certificate writing
3  *
4  * Copyright (C) 2006-2014, 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 
32 #if !defined(POLARSSL_CONFIG_FILE)
33 #include "polarssl/config.h"
34 #else
35 #include POLARSSL_CONFIG_FILE
36 #endif
37 
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 
50 {
51  memset( ctx, 0, sizeof(x509write_cert) );
52 
53  mpi_init( &ctx->serial );
55 }
56 
58 {
59  mpi_free( &ctx->serial );
60 
64 
65  memset( ctx, 0, sizeof(x509write_cert) );
66 }
67 
68 void x509write_crt_set_version( x509write_cert *ctx, int version )
69 {
70  ctx->version = version;
71 }
72 
74 {
75  ctx->md_alg = md_alg;
76 }
77 
79 {
80  ctx->subject_key = key;
81 }
82 
84 {
85  ctx->issuer_key = key;
86 }
87 
89  const char *subject_name )
90 {
91  return x509_string_to_names( &ctx->subject, subject_name );
92 }
93 
95  const char *issuer_name )
96 {
97  return x509_string_to_names( &ctx->issuer, issuer_name );
98 }
99 
100 int 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 
110 int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
111  const char *not_after )
112 {
113  if( strlen(not_before) != X509_RFC5280_UTC_TIME_LEN - 1 ||
114  strlen(not_after) != X509_RFC5280_UTC_TIME_LEN - 1 )
115  {
117  }
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 
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 
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 )
147 
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 ) );
159  ASN1_SEQUENCE ) );
160 
163  0, buf + sizeof(buf) - len, len );
164 }
165 
166 #if defined(POLARSSL_SHA1_C)
168 {
169  int ret;
170  unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
171  unsigned char *c = buf + sizeof(buf);
172  size_t len = 0;
173 
174  memset( buf, 0, sizeof(buf));
175  ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
176 
177  sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
178  c = buf + sizeof(buf) - 20;
179  len = 20;
180 
181  ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
182  ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
183 
186  0, buf + sizeof(buf) - len, len );
187 }
188 
190 {
191  int ret;
192  unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
193  unsigned char *c = buf + sizeof(buf);
194  size_t len = 0;
195 
196  memset( buf, 0, sizeof(buf));
197  ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
198 
199  sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
200  c = buf + sizeof(buf) - 20;
201  len = 20;
202 
203  ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
204  ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
205 
206  ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
208  ASN1_SEQUENCE ) );
209 
212  0, buf + sizeof(buf) - len, len );
213 }
214 #endif /* POLARSSL_SHA1_C */
215 
216 int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
217 {
218  unsigned char buf[4];
219  unsigned char *c;
220  int ret;
221 
222  c = buf + 4;
223 
224  if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
225  return( ret );
226 
229  1, buf, 4 );
230  if( ret != 0 )
231  return( ret );
232 
233  return( 0 );
234 }
235 
237  unsigned char ns_cert_type )
238 {
239  unsigned char buf[4];
240  unsigned char *c;
241  int ret;
242 
243  c = buf + 4;
244 
245  if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
246  return( ret );
247 
250  0, buf, 4 );
251  if( ret != 0 )
252  return( ret );
253 
254  return( 0 );
255 }
256 
257 static int x509_write_time( unsigned char **p, unsigned char *start,
258  const char *time, size_t size )
259 {
260  int ret;
261  size_t len = 0;
262 
263  /*
264  * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
265  */
266  if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
267  {
268  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
269  (const unsigned char *) time + 2,
270  size - 2 ) );
271  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
272  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
273  }
274  else
275  {
276  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
277  (const unsigned char *) time,
278  size ) );
279  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
281  }
282 
283  return( (int) len );
284 }
285 
286 int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
287  int (*f_rng)(void *, unsigned char *, size_t),
288  void *p_rng )
289 {
290  int ret;
291  const char *sig_oid;
292  size_t sig_oid_len = 0;
293  unsigned char *c, *c2;
294  unsigned char hash[64];
295  unsigned char sig[POLARSSL_MPI_MAX_SIZE];
296  unsigned char tmp_buf[2048];
297  size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
298  size_t len = 0;
299  pk_type_t pk_alg;
300 
301  /*
302  * Prepare data to be signed in tmp_buf
303  */
304  c = tmp_buf + sizeof( tmp_buf );
305 
306  /* Signature algorithm needed in TBS, and later for actual signature */
307  pk_alg = pk_get_type( ctx->issuer_key );
308  if( pk_alg == POLARSSL_PK_ECKEY )
309  pk_alg = POLARSSL_PK_ECDSA;
310 
311  if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
312  &sig_oid, &sig_oid_len ) ) != 0 )
313  {
314  return( ret );
315  }
316 
317  /*
318  * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
319  */
320  ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
321  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
322  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
323  ASN1_SEQUENCE ) );
324  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
326  ASN1_CONSTRUCTED | 3 ) );
327 
328  /*
329  * SubjectPublicKeyInfo
330  */
332  tmp_buf, c - tmp_buf ) );
333  c -= pub_len;
334  len += pub_len;
335 
336  /*
337  * Subject ::= Name
338  */
339  ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
340 
341  /*
342  * Validity ::= SEQUENCE {
343  * notBefore Time,
344  * notAfter Time }
345  */
346  sub_len = 0;
347 
348  ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
350 
351  ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
353 
354  len += sub_len;
355  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
356  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
357  ASN1_SEQUENCE ) );
358 
359  /*
360  * Issuer ::= Name
361  */
362  ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
363 
364  /*
365  * Signature ::= AlgorithmIdentifier
366  */
368  sig_oid, strlen( sig_oid ), 0 ) );
369 
370  /*
371  * Serial ::= INTEGER
372  */
373  ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
374 
375  /*
376  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
377  */
378  sub_len = 0;
379  ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
380  len += sub_len;
381  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
383  ASN1_CONSTRUCTED | 0 ) );
384 
385  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
386  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
387  ASN1_SEQUENCE ) );
388 
389  /*
390  * Make signature
391  */
392  md( md_info_from_type( ctx->md_alg ), c, len, hash );
393 
394  if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
395  f_rng, p_rng ) ) != 0 )
396  {
397  return( ret );
398  }
399 
400  /*
401  * Write data to output buffer
402  */
403  c2 = buf + size;
404  ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
405  sig_oid, sig_oid_len, sig, sig_len ) );
406 
407  c2 -= len;
408  memcpy( c2, c, len );
409 
410  len += sig_and_oid_len;
411  ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
412  ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
413  ASN1_SEQUENCE ) );
414 
415  return( (int) len );
416 }
417 
418 #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
419 #define PEM_END_CRT "-----END CERTIFICATE-----\n"
420 
421 #if defined(POLARSSL_PEM_WRITE_C)
422 int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
423  int (*f_rng)(void *, unsigned char *, size_t),
424  void *p_rng )
425 {
426  int ret;
427  unsigned char output_buf[4096];
428  size_t olen = 0;
429 
430  if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
431  f_rng, p_rng ) ) < 0 )
432  {
433  return( ret );
434  }
435 
436  if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
437  output_buf + sizeof(output_buf) - ret,
438  ret, buf, size, &olen ) ) != 0 )
439  {
440  return( ret );
441  }
442 
443  return( 0 );
444 }
445 #endif /* POLARSSL_PEM_WRITE_C */
446 
447 #endif /* POLARSSL_X509_CRT_WRITE_C */
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
void x509write_crt_set_version(x509write_cert *ctx, int version)
Set the verion for a Certificate Default: X509_CRT_VERSION_3.
int x509write_crt_der(x509write_cert *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a built up certificate to a X509 DER structure Note: data is written at the end of the buffer! ...
int x509_set_extension(asn1_named_data **head, const char *oid, size_t oid_len, int critical, const unsigned char *val, size_t val_len)
int x509_string_to_names(asn1_named_data **head, const char *name)
#define ASN1_UTC_TIME
Definition: asn1.h:87
int x509write_crt_set_validity(x509write_cert *ctx, const char *not_before, const char *not_after)
Set the validity period for a Certificate Timestamps should be in string format for UTC timezone i...
#define ASN1_GENERALIZED_TIME
Definition: asn1.h:88
char not_after[X509_RFC5280_UTC_TIME_LEN+1]
Definition: x509_crt.h:121
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
Definition: bignum.h:91
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
asn1_named_data * extensions
Definition: x509_crt.h:122
#define ASN1_SEQUENCE
Definition: asn1.h:82
Configuration options (set of defines)
#define ASN1_CONSTRUCTED
Definition: asn1.h:92
pk_context * subject_key
Definition: x509_crt.h:115
void x509write_crt_set_md_alg(x509write_cert *ctx, md_type_t md_alg)
Set the MD algorithm to use for the signature (e.g.
MPI structure.
Definition: bignum.h:181
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
void mpi_init(mpi *X)
Initialize one MPI.
Object Identifier (OID) database.
#define OID_SIZE(x)
Returns the size of the binary string, without the trailing \0.
Definition: asn1.h:98
md_type_t
Definition: md.h:51
int asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format Note: function works backwards in data buffer.
int x509_write_names(unsigned char **p, unsigned char *start, asn1_named_data *first)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int pk_write_pubkey(unsigned char **p, unsigned char *start, const pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
int pk_write_pubkey_der(pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the bu...
int x509write_crt_set_key_usage(x509write_cert *ctx, unsigned char key_usage)
Set the Key Usage Extension flags (e.g.
void x509write_crt_free(x509write_cert *ctx)
Free the contents of a CRT write context.
Privacy Enhanced Mail (PEM) decoding.
#define X509_CRT_VERSION_3
Definition: x509_crt.h:103
int x509write_crt_set_subject_key_identifier(x509write_cert *ctx)
Set the subjectKeyIdentifier extension for a CRT Requires that x509write_crt_set_subject_key() has be...
char not_before[X509_RFC5280_UTC_TIME_LEN+1]
Definition: x509_crt.h:120
int asn1_write_raw_buffer(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write raw buffer data Note: function works backwards in data buffer.
int x509write_crt_set_extension(x509write_cert *ctx, const char *oid, size_t oid_len, int critical, const unsigned char *val, size_t val_len)
Generic function to add to or replace an extension in the CRT.
int x509write_crt_set_issuer_name(x509write_cert *ctx, const char *issuer_name)
Set the issuer name for a Certificate Issuer names should contain a comma-separated list of OID types...
#define OID_AUTHORITY_KEY_IDENTIFIER
id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 }
Definition: oid.h:133
void mpi_free(mpi *X)
Unallocate one MPI.
int x509_write_extensions(unsigned char **p, unsigned char *start, asn1_named_data *first)
asn1_named_data * issuer
Definition: x509_crt.h:118
#define OID_BASIC_CONSTRAINTS
id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
Definition: oid.h:141
X.509 certificate parsing and writing.
void x509write_crt_set_issuer_key(x509write_cert *ctx, pk_context *key)
Set the issuer key used for signing the certificate.
pk_type_t
Public key types.
Definition: pk.h:95
int x509write_crt_set_serial(x509write_cert *ctx, const mpi *serial)
Set the serial number for a Certificate.
#define ASN1_CONTEXT_SPECIFIC
Definition: asn1.h:93
int asn1_write_mpi(unsigned char **p, unsigned char *start, mpi *X)
Write a big number (ASN1_INTEGER) in ASN.1 format Note: function works backwards in data buffer...
int x509write_crt_set_authority_key_identifier(x509write_cert *ctx)
Set the authorityKeyIdentifier extension for a CRT Requires that x509write_crt_set_issuer_key() has b...
int x509write_crt_set_ns_cert_type(x509write_cert *ctx, unsigned char ns_cert_type)
Set the Netscape Cert Type flags (e.g.
int asn1_write_bitstring(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits)
Write a bitstring tag (ASN1_BIT_STRING) and value in ASN.1 format Note: function works backwards in d...
Container for writing a certificate (CRT)
Definition: x509_crt.h:111
int asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (ASN1_INTEGER) and value in ASN.1 format Note: function works backwards in data buff...
#define OID_SUBJECT_KEY_IDENTIFIER
id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 }
Definition: oid.h:134
int x509write_crt_set_subject_name(x509write_cert *ctx, const char *subject_name)
Set the subject name for a Certificate Subject names should contain a comma-separated list of OID typ...
void asn1_free_named_data_list(asn1_named_data **head)
Free all entries in a asn1_named_data list Head will be set to NULL.
#define ASN1_CHK_ADD(g, f)
Definition: asn1write.h:32
int asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len)
Write an AlgorithmIdentifier sequence in ASN.1 format Note: function works backwards in data buffer...
int pk_sign(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature.
SHA-1 cryptographic hash function.
int x509write_crt_set_basic_constraints(x509write_cert *ctx, int is_ca, int max_pathlen)
Set the basicConstraints extension for a CRT.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
md_type_t md_alg
Definition: x509_crt.h:119
pk_context * issuer_key
Definition: x509_crt.h:116
int asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format Note: function works backwards in data b...
int x509_write_sig(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, unsigned char *sig, size_t size)
ASN.1 buffer writing functionality.
#define ASN1_OCTET_STRING
Definition: asn1.h:78
void x509write_crt_init(x509write_cert *ctx)
Initialize a CRT writing context.
#define POLARSSL_ERR_X509_BAD_INPUT_DATA
Input invalid.
Definition: x509.h:67
int asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write a ASN.1 tag in ASN.1 format Note: function works backwards in data buffer.
void x509write_crt_set_subject_key(x509write_cert *ctx, pk_context *key)
Set the subject public key for the certificate.
#define OID_NS_CERT_TYPE
Definition: oid.h:153
int x509write_crt_pem(x509write_cert *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a built up certificate to a X509 PEM string.
#define X509_RFC5280_UTC_TIME_LEN
Definition: x509_crt.h:106
Public key container.
Definition: pk.h:182
int oid_get_oid_by_sig_alg(pk_type_t pk_alg, md_type_t md_alg, const char **oid, size_t *olen)
Translate md_type and pk_type into SignatureAlgorithm OID.
asn1_named_data * subject
Definition: x509_crt.h:117
#define OID_KEY_USAGE
id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
Definition: oid.h:135