blob: 8e12c14cd48e5193c150b224d487a0f9c133bfdc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 base functions for creating certificates / CSRs
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#include "polarssl/config.h"
27
28#if defined(POLARSSL_X509_CREATE_C)
29
30#include "polarssl/x509.h"
31#include "polarssl/asn1write.h"
32#include "polarssl/oid.h"
33
Paul Bakker6edcd412013-10-29 15:22:54 +010034#if defined(_MSC_VER) && !defined strncasecmp && !defined(EFIX64) && \
35 !defined(EFI32)
Paul Bakker7b0be682013-10-29 14:24:37 +010036#define strncasecmp _strnicmp
37#endif
38
Paul Bakker50dc8502013-10-28 21:19:10 +010039int x509_string_to_names( asn1_named_data **head, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040{
41 int ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +010042 const char *s = name, *c = s;
43 const char *end = s + strlen( s );
Paul Bakkerfcc17212013-10-11 09:36:52 +020044 const char *oid = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 int in_tag = 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046
47 /* Clear existing chain if present */
48 asn1_free_named_data_list( head );
49
50 while( c <= end )
51 {
52 if( in_tag && *c == '=' )
53 {
Paul Bakker7b0be682013-10-29 14:24:37 +010054 if( c - s == 2 && strncasecmp( s, "CN", 2 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055 oid = OID_AT_CN;
Paul Bakker63844402014-04-30 15:34:12 +020056 else if( c - s == 10 && strncasecmp( s, "commonName", 10 ) == 0 )
57 oid = OID_AT_CN;
Paul Bakker7b0be682013-10-29 14:24:37 +010058 else if( c - s == 1 && strncasecmp( s, "C", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059 oid = OID_AT_COUNTRY;
Paul Bakker63844402014-04-30 15:34:12 +020060 else if( c - s == 11 && strncasecmp( s, "countryName", 11 ) == 0 )
61 oid = OID_AT_COUNTRY;
Paul Bakker7b0be682013-10-29 14:24:37 +010062 else if( c - s == 1 && strncasecmp( s, "O", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063 oid = OID_AT_ORGANIZATION;
Paul Bakker63844402014-04-30 15:34:12 +020064 else if( c - s == 16 && strncasecmp( s, "organizationName", 16 ) == 0 )
65 oid = OID_AT_ORGANIZATION;
Paul Bakker7b0be682013-10-29 14:24:37 +010066 else if( c - s == 1 && strncasecmp( s, "L", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067 oid = OID_AT_LOCALITY;
Paul Bakker63844402014-04-30 15:34:12 +020068 else if( c - s == 8 && strncasecmp( s, "locality", 8 ) == 0 )
69 oid = OID_AT_LOCALITY;
Paul Bakker7b0be682013-10-29 14:24:37 +010070 else if( c - s == 1 && strncasecmp( s, "R", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071 oid = OID_PKCS9_EMAIL;
Paul Bakker7b0be682013-10-29 14:24:37 +010072 else if( c - s == 2 && strncasecmp( s, "OU", 2 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073 oid = OID_AT_ORG_UNIT;
Paul Bakker63844402014-04-30 15:34:12 +020074 else if( c - s == 22 && strncasecmp( s, "organizationalUnitName", 22 ) == 0 )
75 oid = OID_AT_ORG_UNIT;
Paul Bakker7b0be682013-10-29 14:24:37 +010076 else if( c - s == 2 && strncasecmp( s, "ST", 2 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077 oid = OID_AT_STATE;
Paul Bakker63844402014-04-30 15:34:12 +020078 else if( c - s == 19 && strncasecmp( s, "stateOrProvinceName", 19 ) == 0 )
79 oid = OID_AT_STATE;
Paul Bakker0767e672014-04-18 14:11:37 +020080 else if( c - s == 12 && strncasecmp( s, "emailAddress", 12 ) == 0 )
81 oid = OID_PKCS9_EMAIL;
Paul Bakker7b0be682013-10-29 14:24:37 +010082 else if( c - s == 12 && strncasecmp( s, "serialNumber", 12 ) == 0 )
83 oid = OID_AT_SERIAL_NUMBER;
84 else if( c - s == 13 && strncasecmp( s, "postalAddress", 13 ) == 0 )
85 oid = OID_AT_POSTAL_ADDRESS;
86 else if( c - s == 10 && strncasecmp( s, "postalCode", 10 ) == 0 )
87 oid = OID_AT_POSTAL_CODE;
Paul Bakker63844402014-04-30 15:34:12 +020088 else if( c - s == 11 && strncasecmp( s, "dnQualifier", 11 ) == 0 )
89 oid = OID_AT_DN_QUALIFIER;
90 else if( c - s == 5 && strncasecmp( s, "title", 5 ) == 0 )
91 oid = OID_AT_TITLE;
92 else if( c - s == 7 && strncasecmp( s, "surName", 7 ) == 0 )
93 oid = OID_AT_SUR_NAME;
94 else if( c - s == 2 && strncasecmp( s, "SN", 2 ) == 0 )
95 oid = OID_AT_SUR_NAME;
96 else if( c - s == 9 && strncasecmp( s, "givenName", 9 ) == 0 )
97 oid = OID_AT_GIVEN_NAME;
98 else if( c - s == 2 && strncasecmp( s, "GN", 2 ) == 0 )
99 oid = OID_AT_GIVEN_NAME;
100 else if( c - s == 8 && strncasecmp( s, "initials", 8 ) == 0 )
101 oid = OID_AT_INITIALS;
102 else if( c - s == 9 && strncasecmp( s, "pseudonym", 9 ) == 0 )
103 oid = OID_AT_PSEUDONYM;
104 else if( c - s == 19 && strncasecmp( s, "generationQualifier", 19 ) == 0 )
105 oid = OID_AT_GENERATION_QUALIFIER;
106 else if( c - s == 15 && strncasecmp( s, "domainComponent", 15 ) == 0 )
107 oid = OID_DOMAIN_COMPONENT;
108 else if( c - s == 2 && strncasecmp( s, "DC", 2 ) == 0 )
109 oid = OID_DOMAIN_COMPONENT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110 else
111 {
Paul Bakker51876562013-09-17 14:36:05 +0200112 ret = POLARSSL_ERR_X509_UNKNOWN_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 goto exit;
114 }
115
116 s = c + 1;
117 in_tag = 0;
118 }
119
120 if( !in_tag && ( *c == ',' || c == end ) )
121 {
Paul Bakkera9c16d22014-04-17 12:42:18 +0200122 if( asn1_store_named_data( head, oid, strlen( oid ),
123 (unsigned char *) s,
124 c - s ) == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125 {
Paul Bakker51876562013-09-17 14:36:05 +0200126 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127 }
128
129 while( c < end && *(c + 1) == ' ' )
130 c++;
131
132 s = c + 1;
133 in_tag = 1;
134 }
135 c++;
136 }
137
138exit:
139
140 return( ret );
141}
142
143/* The first byte of the value in the asn1_named_data structure is reserved
144 * to store the critical boolean for us
145 */
146int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
147 int critical, const unsigned char *val, size_t val_len )
148{
149 asn1_named_data *cur;
150
151 if( ( cur = asn1_store_named_data( head, oid, oid_len,
152 NULL, val_len + 1 ) ) == NULL )
153 {
Paul Bakker51876562013-09-17 14:36:05 +0200154 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 }
156
157 cur->val.p[0] = critical;
158 memcpy( cur->val.p + 1, val, val_len );
159
160 return( 0 );
161}
162
163/*
164 * RelativeDistinguishedName ::=
165 * SET OF AttributeTypeAndValue
166 *
167 * AttributeTypeAndValue ::= SEQUENCE {
168 * type AttributeType,
169 * value AttributeValue }
170 *
171 * AttributeType ::= OBJECT IDENTIFIER
172 *
173 * AttributeValue ::= ANY DEFINED BY AttributeType
174 */
175static int x509_write_name( unsigned char **p, unsigned char *start,
176 const char *oid, size_t oid_len,
177 const unsigned char *name, size_t name_len )
178{
179 int ret;
180 size_t len = 0;
181
182 // Write PrintableString for all except OID_PKCS9_EMAIL
183 //
184 if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len &&
185 memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 )
186 {
187 ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start,
188 (const char *) name,
189 name_len ) );
190 }
191 else
192 {
193 ASN1_CHK_ADD( len, asn1_write_printable_string( p, start,
194 (const char *) name,
195 name_len ) );
196 }
197
198 // Write OID
199 //
200 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
201
202 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
203 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
204
205 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
206 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
207
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200208 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209}
210
211int x509_write_names( unsigned char **p, unsigned char *start,
212 asn1_named_data *first )
213{
214 int ret;
215 size_t len = 0;
216 asn1_named_data *cur = first;
217
218 while( cur != NULL )
219 {
220 ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
221 cur->oid.len,
222 cur->val.p, cur->val.len ) );
223 cur = cur->next;
224 }
225
226 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
227 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
228
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200229 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230}
231
232int x509_write_sig( unsigned char **p, unsigned char *start,
233 const char *oid, size_t oid_len,
234 unsigned char *sig, size_t size )
235{
236 int ret;
237 size_t len = 0;
238
239 if( *p - start < (int) size + 1 )
240 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
241
242 len = size;
243 (*p) -= len;
244 memcpy( *p, sig, len );
245
246 *--(*p) = 0;
247 len += 1;
248
249 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
250 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
251
252 // Write OID
253 //
254 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
255 oid_len, 0 ) );
256
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258}
259
260static int x509_write_extension( unsigned char **p, unsigned char *start,
261 asn1_named_data *ext )
262{
263 int ret;
264 size_t len = 0;
265
266 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1,
267 ext->val.len - 1 ) );
268 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) );
269 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
270
271 if( ext->val.p[0] != 0 )
272 {
273 ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) );
274 }
275
276 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p,
277 ext->oid.len ) );
278 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) );
279 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) );
280
281 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
282 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
283
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200284 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285}
286
287/*
288 * Extension ::= SEQUENCE {
289 * extnID OBJECT IDENTIFIER,
290 * critical BOOLEAN DEFAULT FALSE,
291 * extnValue OCTET STRING
292 * -- contains the DER encoding of an ASN.1 value
293 * -- corresponding to the extension type identified
294 * -- by extnID
295 * }
296 */
297int x509_write_extensions( unsigned char **p, unsigned char *start,
298 asn1_named_data *first )
299{
300 int ret;
301 size_t len = 0;
302 asn1_named_data *cur_ext = first;
303
304 while( cur_ext != NULL )
305 {
306 ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
307 cur_ext = cur_ext->next;
308 }
309
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200310 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311}
312
313#endif /* POLARSSL_X509_CREATE_C */