blob: 1cb88c7b9c3757274e689a8f22aa1764adec0218 [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 Bakker7b0be682013-10-29 14:24:37 +010034#if defined(_MSC_VER) && !defined strncasecmp
35#define strncasecmp _strnicmp
36#endif
37
Paul Bakker50dc8502013-10-28 21:19:10 +010038int x509_string_to_names( asn1_named_data **head, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039{
40 int ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +010041 const char *s = name, *c = s;
42 const char *end = s + strlen( s );
Paul Bakkerfcc17212013-10-11 09:36:52 +020043 const char *oid = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044 int in_tag = 1;
45 asn1_named_data *cur;
46
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 Bakker7b0be682013-10-29 14:24:37 +010056 else if( c - s == 1 && strncasecmp( s, "C", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057 oid = OID_AT_COUNTRY;
Paul Bakker7b0be682013-10-29 14:24:37 +010058 else if( c - s == 1 && strncasecmp( s, "O", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059 oid = OID_AT_ORGANIZATION;
Paul Bakker7b0be682013-10-29 14:24:37 +010060 else if( c - s == 1 && strncasecmp( s, "L", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061 oid = OID_AT_LOCALITY;
Paul Bakker7b0be682013-10-29 14:24:37 +010062 else if( c - s == 1 && strncasecmp( s, "R", 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063 oid = OID_PKCS9_EMAIL;
Paul Bakker7b0be682013-10-29 14:24:37 +010064 else if( c - s == 2 && strncasecmp( s, "OU", 2 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065 oid = OID_AT_ORG_UNIT;
Paul Bakker7b0be682013-10-29 14:24:37 +010066 else if( c - s == 2 && strncasecmp( s, "ST", 2 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067 oid = OID_AT_STATE;
Paul Bakker7b0be682013-10-29 14:24:37 +010068 else if( c - s == 12 && strncasecmp( s, "serialNumber", 12 ) == 0 )
69 oid = OID_AT_SERIAL_NUMBER;
70 else if( c - s == 13 && strncasecmp( s, "postalAddress", 13 ) == 0 )
71 oid = OID_AT_POSTAL_ADDRESS;
72 else if( c - s == 10 && strncasecmp( s, "postalCode", 10 ) == 0 )
73 oid = OID_AT_POSTAL_CODE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074 else
75 {
Paul Bakker51876562013-09-17 14:36:05 +020076 ret = POLARSSL_ERR_X509_UNKNOWN_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077 goto exit;
78 }
79
80 s = c + 1;
81 in_tag = 0;
82 }
83
84 if( !in_tag && ( *c == ',' || c == end ) )
85 {
86 if( ( cur = asn1_store_named_data( head, oid, strlen( oid ),
87 (unsigned char *) s,
88 c - s ) ) == NULL )
89 {
Paul Bakker51876562013-09-17 14:36:05 +020090 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091 }
92
93 while( c < end && *(c + 1) == ' ' )
94 c++;
95
96 s = c + 1;
97 in_tag = 1;
98 }
99 c++;
100 }
101
102exit:
103
104 return( ret );
105}
106
107/* The first byte of the value in the asn1_named_data structure is reserved
108 * to store the critical boolean for us
109 */
110int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
111 int critical, const unsigned char *val, size_t val_len )
112{
113 asn1_named_data *cur;
114
115 if( ( cur = asn1_store_named_data( head, oid, oid_len,
116 NULL, val_len + 1 ) ) == NULL )
117 {
Paul Bakker51876562013-09-17 14:36:05 +0200118 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 }
120
121 cur->val.p[0] = critical;
122 memcpy( cur->val.p + 1, val, val_len );
123
124 return( 0 );
125}
126
127/*
128 * RelativeDistinguishedName ::=
129 * SET OF AttributeTypeAndValue
130 *
131 * AttributeTypeAndValue ::= SEQUENCE {
132 * type AttributeType,
133 * value AttributeValue }
134 *
135 * AttributeType ::= OBJECT IDENTIFIER
136 *
137 * AttributeValue ::= ANY DEFINED BY AttributeType
138 */
139static int x509_write_name( unsigned char **p, unsigned char *start,
140 const char *oid, size_t oid_len,
141 const unsigned char *name, size_t name_len )
142{
143 int ret;
144 size_t len = 0;
145
146 // Write PrintableString for all except OID_PKCS9_EMAIL
147 //
148 if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len &&
149 memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 )
150 {
151 ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start,
152 (const char *) name,
153 name_len ) );
154 }
155 else
156 {
157 ASN1_CHK_ADD( len, asn1_write_printable_string( p, start,
158 (const char *) name,
159 name_len ) );
160 }
161
162 // Write OID
163 //
164 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
165
166 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
167 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
168
169 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
170 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
171
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173}
174
175int x509_write_names( unsigned char **p, unsigned char *start,
176 asn1_named_data *first )
177{
178 int ret;
179 size_t len = 0;
180 asn1_named_data *cur = first;
181
182 while( cur != NULL )
183 {
184 ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
185 cur->oid.len,
186 cur->val.p, cur->val.len ) );
187 cur = cur->next;
188 }
189
190 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
191 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
192
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200193 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194}
195
196int x509_write_sig( unsigned char **p, unsigned char *start,
197 const char *oid, size_t oid_len,
198 unsigned char *sig, size_t size )
199{
200 int ret;
201 size_t len = 0;
202
203 if( *p - start < (int) size + 1 )
204 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
205
206 len = size;
207 (*p) -= len;
208 memcpy( *p, sig, len );
209
210 *--(*p) = 0;
211 len += 1;
212
213 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
214 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
215
216 // Write OID
217 //
218 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
219 oid_len, 0 ) );
220
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200221 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222}
223
224static int x509_write_extension( unsigned char **p, unsigned char *start,
225 asn1_named_data *ext )
226{
227 int ret;
228 size_t len = 0;
229
230 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1,
231 ext->val.len - 1 ) );
232 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) );
233 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
234
235 if( ext->val.p[0] != 0 )
236 {
237 ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) );
238 }
239
240 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p,
241 ext->oid.len ) );
242 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) );
243 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) );
244
245 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
246 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
247
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200248 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249}
250
251/*
252 * Extension ::= SEQUENCE {
253 * extnID OBJECT IDENTIFIER,
254 * critical BOOLEAN DEFAULT FALSE,
255 * extnValue OCTET STRING
256 * -- contains the DER encoding of an ASN.1 value
257 * -- corresponding to the extension type identified
258 * -- by extnID
259 * }
260 */
261int x509_write_extensions( unsigned char **p, unsigned char *start,
262 asn1_named_data *first )
263{
264 int ret;
265 size_t len = 0;
266 asn1_named_data *cur_ext = first;
267
268 while( cur_ext != NULL )
269 {
270 ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
271 cur_ext = cur_ext->next;
272 }
273
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200274 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275}
276
277#endif /* POLARSSL_X509_CREATE_C */