PolarSSL v1.3.8
x509_create.c
Go to the documentation of this file.
1 /*
2  * X.509 base functions for creating certificates / CSRs
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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
32 #if defined(POLARSSL_X509_CREATE_C)
33 
34 #include "polarssl/x509.h"
35 #include "polarssl/asn1write.h"
36 #include "polarssl/oid.h"
37 
38 #if defined(_MSC_VER) && !defined strncasecmp && !defined(EFIX64) && \
39  !defined(EFI32)
40 #define strncasecmp _strnicmp
41 #endif
42 
43 typedef struct {
44  const char *name;
45  size_t name_len;
46  const char*oid;
47 } x509_attr_descriptor_t;
48 
49 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
50 
51 static const x509_attr_descriptor_t x509_attrs[] =
52 {
53  { ADD_STRLEN( "CN" ), OID_AT_CN },
54  { ADD_STRLEN( "commonName" ), OID_AT_CN },
55  { ADD_STRLEN( "C" ), OID_AT_COUNTRY },
56  { ADD_STRLEN( "countryName" ), OID_AT_COUNTRY },
57  { ADD_STRLEN( "O" ), OID_AT_ORGANIZATION },
58  { ADD_STRLEN( "organizationName" ), OID_AT_ORGANIZATION },
59  { ADD_STRLEN( "L" ), OID_AT_LOCALITY },
60  { ADD_STRLEN( "locality" ), OID_AT_LOCALITY },
61  { ADD_STRLEN( "R" ), OID_PKCS9_EMAIL },
62  { ADD_STRLEN( "OU" ), OID_AT_ORG_UNIT },
63  { ADD_STRLEN( "organizationalUnitName" ), OID_AT_ORG_UNIT },
64  { ADD_STRLEN( "ST" ), OID_AT_STATE },
65  { ADD_STRLEN( "stateOrProvinceName" ), OID_AT_STATE },
66  { ADD_STRLEN( "emailAddress" ), OID_PKCS9_EMAIL },
67  { ADD_STRLEN( "serialNumber" ), OID_AT_SERIAL_NUMBER },
68  { ADD_STRLEN( "postalAddress" ), OID_AT_POSTAL_ADDRESS },
69  { ADD_STRLEN( "postalCode" ), OID_AT_POSTAL_CODE },
70  { ADD_STRLEN( "dnQualifier" ), OID_AT_DN_QUALIFIER },
71  { ADD_STRLEN( "title" ), OID_AT_TITLE },
72  { ADD_STRLEN( "surName" ), OID_AT_SUR_NAME },
73  { ADD_STRLEN( "SN" ), OID_AT_SUR_NAME },
74  { ADD_STRLEN( "givenName" ), OID_AT_GIVEN_NAME },
75  { ADD_STRLEN( "GN" ), OID_AT_GIVEN_NAME },
76  { ADD_STRLEN( "initials" ), OID_AT_INITIALS },
77  { ADD_STRLEN( "pseudonym" ), OID_AT_PSEUDONYM },
78  { ADD_STRLEN( "generationQualifier" ), OID_AT_GENERATION_QUALIFIER },
79  { ADD_STRLEN( "domainComponent" ), OID_DOMAIN_COMPONENT },
80  { ADD_STRLEN( "DC" ), OID_DOMAIN_COMPONENT },
81  { NULL, 0, NULL }
82 };
83 
84 static const char *x509_at_oid_from_name( const char *name, size_t name_len )
85 {
86  const x509_attr_descriptor_t *cur;
87 
88  for( cur = x509_attrs; cur->name != NULL; cur++ )
89  if( cur->name_len == name_len &&
90  strncasecmp( cur->name, name, name_len ) == 0 )
91  break;
92 
93  return( cur->oid );
94 }
95 
96 int x509_string_to_names( asn1_named_data **head, const char *name )
97 {
98  int ret = 0;
99  const char *s = name, *c = s;
100  const char *end = s + strlen( s );
101  const char *oid = NULL;
102  int in_tag = 1;
103 
104  /* Clear existing chain if present */
106 
107  while( c <= end )
108  {
109  if( in_tag && *c == '=' )
110  {
111  if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
112  {
114  goto exit;
115  }
116 
117  s = c + 1;
118  in_tag = 0;
119  }
120 
121  if( !in_tag && ( *c == ',' || c == end ) )
122  {
123  if( asn1_store_named_data( head, oid, strlen( oid ),
124  (unsigned char *) s,
125  c - s ) == NULL )
126  {
128  }
129 
130  while( c < end && *(c + 1) == ' ' )
131  c++;
132 
133  s = c + 1;
134  in_tag = 1;
135  }
136  c++;
137  }
138 
139 exit:
140 
141  return( ret );
142 }
143 
144 /* The first byte of the value in the asn1_named_data structure is reserved
145  * to store the critical boolean for us
146  */
147 int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
148  int critical, const unsigned char *val, size_t val_len )
149 {
150  asn1_named_data *cur;
151 
152  if( ( cur = asn1_store_named_data( head, oid, oid_len,
153  NULL, val_len + 1 ) ) == NULL )
154  {
156  }
157 
158  cur->val.p[0] = critical;
159  memcpy( cur->val.p + 1, val, val_len );
160 
161  return( 0 );
162 }
163 
164 /*
165  * RelativeDistinguishedName ::=
166  * SET OF AttributeTypeAndValue
167  *
168  * AttributeTypeAndValue ::= SEQUENCE {
169  * type AttributeType,
170  * value AttributeValue }
171  *
172  * AttributeType ::= OBJECT IDENTIFIER
173  *
174  * AttributeValue ::= ANY DEFINED BY AttributeType
175  */
176 static int x509_write_name( unsigned char **p, unsigned char *start,
177  const char *oid, size_t oid_len,
178  const unsigned char *name, size_t name_len )
179 {
180  int ret;
181  size_t len = 0;
182 
183  // Write PrintableString for all except OID_PKCS9_EMAIL
184  //
185  if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len &&
186  memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 )
187  {
188  ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start,
189  (const char *) name,
190  name_len ) );
191  }
192  else
193  {
195  (const char *) name,
196  name_len ) );
197  }
198 
199  // Write OID
200  //
201  ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
202 
203  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
204  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
205  ASN1_SEQUENCE ) );
206 
207  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
208  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
209  ASN1_SET ) );
210 
211  return( (int) len );
212 }
213 
214 int x509_write_names( unsigned char **p, unsigned char *start,
215  asn1_named_data *first )
216 {
217  int ret;
218  size_t len = 0;
219  asn1_named_data *cur = first;
220 
221  while( cur != NULL )
222  {
223  ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
224  cur->oid.len,
225  cur->val.p, cur->val.len ) );
226  cur = cur->next;
227  }
228 
229  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
230  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
231  ASN1_SEQUENCE ) );
232 
233  return( (int) len );
234 }
235 
236 int x509_write_sig( unsigned char **p, unsigned char *start,
237  const char *oid, size_t oid_len,
238  unsigned char *sig, size_t size )
239 {
240  int ret;
241  size_t len = 0;
242 
243  if( *p - start < (int) size + 1 )
245 
246  len = size;
247  (*p) -= len;
248  memcpy( *p, sig, len );
249 
250  *--(*p) = 0;
251  len += 1;
252 
253  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
254  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
255 
256  // Write OID
257  //
258  ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
259  oid_len, 0 ) );
260 
261  return( (int) len );
262 }
263 
264 static int x509_write_extension( unsigned char **p, unsigned char *start,
265  asn1_named_data *ext )
266 {
267  int ret;
268  size_t len = 0;
269 
270  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1,
271  ext->val.len - 1 ) );
272  ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) );
273  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
274 
275  if( ext->val.p[0] != 0 )
276  {
277  ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) );
278  }
279 
280  ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p,
281  ext->oid.len ) );
282  ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) );
283  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) );
284 
285  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
286  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
287  ASN1_SEQUENCE ) );
288 
289  return( (int) len );
290 }
291 
292 /*
293  * Extension ::= SEQUENCE {
294  * extnID OBJECT IDENTIFIER,
295  * critical BOOLEAN DEFAULT FALSE,
296  * extnValue OCTET STRING
297  * -- contains the DER encoding of an ASN.1 value
298  * -- corresponding to the extension type identified
299  * -- by extnID
300  * }
301  */
302 int x509_write_extensions( unsigned char **p, unsigned char *start,
303  asn1_named_data *first )
304 {
305  int ret;
306  size_t len = 0;
307  asn1_named_data *cur_ext = first;
308 
309  while( cur_ext != NULL )
310  {
311  ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
312  cur_ext = cur_ext->next;
313  }
314 
315  return( (int) len );
316 }
317 
318 #endif /* POLARSSL_X509_CREATE_C */
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_OID
Definition: asn1.h:80
int asn1_write_ia5_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write an IA5 string tag (ASN1_IA5_STRING) and value in ASN.1 format Note: function works backwards in...
asn1_named_data * asn1_store_named_data(asn1_named_data **list, const char *oid, size_t oid_len, const unsigned char *val, size_t val_len)
Create or find a specific named_data entry for writing in a sequence or list based on the OID...
#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL
Buffer too small when writing ASN.1 data structure.
Definition: asn1.h:60
#define ASN1_SEQUENCE
Definition: asn1.h:82
Configuration options (set of defines)
#define ASN1_CONSTRUCTED
Definition: asn1.h:92
Object Identifier (OID) database.
#define OID_AT_INITIALS
id-at-initials AttributeType:= {id-at 43}
Definition: oid.h:123
#define OID_AT_CN
id-at-commonName AttributeType:= {id-at 3}
Definition: oid.h:111
#define OID_SIZE(x)
Returns the size of the binary string, without the trailing \0.
Definition: asn1.h:98
#define OID_AT_TITLE
id-at-title AttributeType:= {id-at 12}
Definition: oid.h:119
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 asn1_write_printable_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write a printable string tag (ASN1_PRINTABLE_STRING) and value in ASN.1 format Note: function works b...
#define ASN1_SET
Definition: asn1.h:83
asn1_buf val
The named value.
Definition: asn1.h:159
#define OID_AT_GENERATION_QUALIFIER
id-at-generationQualifier AttributeType:= {id-at 44}
Definition: oid.h:124
int x509_write_names(unsigned char **p, unsigned char *start, asn1_named_data *first)
asn1_buf oid
The object identifier.
Definition: asn1.h:158
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.
#define POLARSSL_ERR_X509_UNKNOWN_OID
Requested OID is unknown.
Definition: x509.h:53
#define OID_AT_POSTAL_ADDRESS
id-at-postalAddress AttributeType:= {id-at 16}
Definition: oid.h:120
#define OID_AT_ORGANIZATION
id-at-organizationName AttributeType:= {id-at 10}
Definition: oid.h:117
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:128
int x509_write_extensions(unsigned char **p, unsigned char *start, asn1_named_data *first)
#define OID_AT_GIVEN_NAME
id-at-givenName AttributeType:= {id-at 42}
Definition: oid.h:122
#define OID_AT_ORG_UNIT
id-at-organizationalUnitName AttributeType:= {id-at 11}
Definition: oid.h:118
#define OID_AT_POSTAL_CODE
id-at-postalCode AttributeType:= {id-at 17}
Definition: oid.h:121
#define OID_PKCS9_EMAIL
emailAddress AttributeType ::= { pkcs-9 1 }
Definition: oid.h:208
#define OID_AT_PSEUDONYM
id-at-pseudonym AttributeType:= {id-at 65}
Definition: oid.h:126
#define ASN1_BIT_STRING
Definition: asn1.h:77
X.509 generic defines and structures.
#define OID_AT_LOCALITY
id-at-locality AttributeType:= {id-at 7}
Definition: oid.h:115
Container for a sequence or list of &#39;named&#39; ASN.1 data items.
Definition: asn1.h:156
size_t len
ASN1 length, e.g.
Definition: asn1.h:127
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
#define OID_AT_SUR_NAME
id-at-surName AttributeType:= {id-at 4}
Definition: oid.h:112
#define OID_AT_SERIAL_NUMBER
id-at-serialNumber AttributeType:= {id-at 5}
Definition: oid.h:113
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 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...
struct _asn1_named_data * next
The next entry in the sequence.
Definition: asn1.h:160
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.
int asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (ASN1_OID) and data in ASN.1 format Note: function works backwards in data buffer...
#define ASN1_OCTET_STRING
Definition: asn1.h:78
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.
#define POLARSSL_ERR_X509_MALLOC_FAILED
Allocation of memory failed.
Definition: x509.h:68
#define OID_AT_STATE
id-at-state AttributeType:= {id-at 8}
Definition: oid.h:116
#define OID_AT_DN_QUALIFIER
id-at-dnQualifier AttributeType:= {id-at 46}
Definition: oid.h:125
#define OID_DOMAIN_COMPONENT
Definition: oid.h:128
#define OID_AT_COUNTRY
id-at-countryName AttributeType:= {id-at 6}
Definition: oid.h:114