source: azure_iot_hub_mbedtls/trunk/mbedtls-2.16.1/library/x509_create.c@ 398

Last change on this file since 398 was 398, checked in by coas-nagasima, 5 years ago

mbedTLS版Azure IoT Hub接続サンプルのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 12.5 KB
Line 
1/*
2 * X.509 base functions for creating certificates / CSRs
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_X509_CREATE_C)
29
30#include "mbedtls/x509.h"
31#include "mbedtls/asn1write.h"
32#include "mbedtls/oid.h"
33
34#include <string.h>
35
36/* Structure linking OIDs for X.509 DN AttributeTypes to their
37 * string representations and default string encodings used by Mbed TLS. */
38typedef struct {
39 const char *name; /* String representation of AttributeType, e.g.
40 * "CN" or "emailAddress". */
41 size_t name_len; /* Length of 'name', without trailing 0 byte. */
42 const char *oid; /* String representation of OID of AttributeType,
43 * as per RFC 5280, Appendix A.1. */
44 int default_tag; /* The default character encoding used for the
45 * given attribute type, e.g.
46 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
47} x509_attr_descriptor_t;
48
49#define ADD_STRLEN( s ) s, sizeof( s ) - 1
50
51/* X.509 DN attributes from RFC 5280, Appendix A.1. */
52static const x509_attr_descriptor_t x509_attrs[] =
53{
54 { ADD_STRLEN( "CN" ),
55 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
56 { ADD_STRLEN( "commonName" ),
57 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
58 { ADD_STRLEN( "C" ),
59 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
60 { ADD_STRLEN( "countryName" ),
61 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
62 { ADD_STRLEN( "O" ),
63 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
64 { ADD_STRLEN( "organizationName" ),
65 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
66 { ADD_STRLEN( "L" ),
67 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
68 { ADD_STRLEN( "locality" ),
69 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
70 { ADD_STRLEN( "R" ),
71 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
72 { ADD_STRLEN( "OU" ),
73 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
74 { ADD_STRLEN( "organizationalUnitName" ),
75 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
76 { ADD_STRLEN( "ST" ),
77 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
78 { ADD_STRLEN( "stateOrProvinceName" ),
79 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
80 { ADD_STRLEN( "emailAddress" ),
81 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
82 { ADD_STRLEN( "serialNumber" ),
83 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
84 { ADD_STRLEN( "postalAddress" ),
85 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
86 { ADD_STRLEN( "postalCode" ),
87 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
88 { ADD_STRLEN( "dnQualifier" ),
89 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
90 { ADD_STRLEN( "title" ),
91 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
92 { ADD_STRLEN( "surName" ),
93 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
94 { ADD_STRLEN( "SN" ),
95 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
96 { ADD_STRLEN( "givenName" ),
97 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
98 { ADD_STRLEN( "GN" ),
99 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
100 { ADD_STRLEN( "initials" ),
101 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
102 { ADD_STRLEN( "pseudonym" ),
103 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
104 { ADD_STRLEN( "generationQualifier" ),
105 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
106 { ADD_STRLEN( "domainComponent" ),
107 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
108 { ADD_STRLEN( "DC" ),
109 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
110 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
111};
112
113static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
114{
115 const x509_attr_descriptor_t *cur;
116
117 for( cur = x509_attrs; cur->name != NULL; cur++ )
118 if( cur->name_len == name_len &&
119 strncmp( cur->name, name, name_len ) == 0 )
120 break;
121
122 if ( cur->name == NULL )
123 return( NULL );
124
125 return( cur );
126}
127
128int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
129{
130 int ret = 0;
131 const char *s = name, *c = s;
132 const char *end = s + strlen( s );
133 const char *oid = NULL;
134 const x509_attr_descriptor_t* attr_descr = NULL;
135 int in_tag = 1;
136 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
137 char *d = data;
138
139 /* Clear existing chain if present */
140 mbedtls_asn1_free_named_data_list( head );
141
142 while( c <= end )
143 {
144 if( in_tag && *c == '=' )
145 {
146 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
147 {
148 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
149 goto exit;
150 }
151
152 oid = attr_descr->oid;
153 s = c + 1;
154 in_tag = 0;
155 d = data;
156 }
157
158 if( !in_tag && *c == '\\' && c != end )
159 {
160 c++;
161
162 /* Check for valid escaped characters */
163 if( c == end || *c != ',' )
164 {
165 ret = MBEDTLS_ERR_X509_INVALID_NAME;
166 goto exit;
167 }
168 }
169 else if( !in_tag && ( *c == ',' || c == end ) )
170 {
171 mbedtls_asn1_named_data* cur =
172 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
173 (unsigned char *) data,
174 d - data );
175
176 if(cur == NULL )
177 {
178 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
179 }
180
181 // set tagType
182 cur->val.tag = attr_descr->default_tag;
183
184 while( c < end && *(c + 1) == ' ' )
185 c++;
186
187 s = c + 1;
188 in_tag = 1;
189 }
190
191 if( !in_tag && s != c + 1 )
192 {
193 *(d++) = *c;
194
195 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
196 {
197 ret = MBEDTLS_ERR_X509_INVALID_NAME;
198 goto exit;
199 }
200 }
201
202 c++;
203 }
204
205exit:
206
207 return( ret );
208}
209
210/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
211 * to store the critical boolean for us
212 */
213int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
214 int critical, const unsigned char *val, size_t val_len )
215{
216 mbedtls_asn1_named_data *cur;
217
218 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
219 NULL, val_len + 1 ) ) == NULL )
220 {
221 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
222 }
223
224 cur->val.p[0] = critical;
225 memcpy( cur->val.p + 1, val, val_len );
226
227 return( 0 );
228}
229
230/*
231 * RelativeDistinguishedName ::=
232 * SET OF AttributeTypeAndValue
233 *
234 * AttributeTypeAndValue ::= SEQUENCE {
235 * type AttributeType,
236 * value AttributeValue }
237 *
238 * AttributeType ::= OBJECT IDENTIFIER
239 *
240 * AttributeValue ::= ANY DEFINED BY AttributeType
241 */
242static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
243{
244 int ret;
245 size_t len = 0;
246 const char *oid = (const char*)cur_name->oid.p;
247 size_t oid_len = cur_name->oid.len;
248 const unsigned char *name = cur_name->val.p;
249 size_t name_len = cur_name->val.len;
250
251 // Write correct string tag and value
252 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
253 cur_name->val.tag,
254 (const char *) name,
255 name_len ) );
256 // Write OID
257 //
258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
259 oid_len ) );
260
261 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
263 MBEDTLS_ASN1_CONSTRUCTED |
264 MBEDTLS_ASN1_SEQUENCE ) );
265
266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
267 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
268 MBEDTLS_ASN1_CONSTRUCTED |
269 MBEDTLS_ASN1_SET ) );
270
271 return( (int) len );
272}
273
274int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
275 mbedtls_asn1_named_data *first )
276{
277 int ret;
278 size_t len = 0;
279 mbedtls_asn1_named_data *cur = first;
280
281 while( cur != NULL )
282 {
283 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
284 cur = cur->next;
285 }
286
287 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
289 MBEDTLS_ASN1_SEQUENCE ) );
290
291 return( (int) len );
292}
293
294int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
295 const char *oid, size_t oid_len,
296 unsigned char *sig, size_t size )
297{
298 int ret;
299 size_t len = 0;
300
301 if( *p < start || (size_t)( *p - start ) < size )
302 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
303
304 len = size;
305 (*p) -= len;
306 memcpy( *p, sig, len );
307
308 if( *p - start < 1 )
309 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
310
311 *--(*p) = 0;
312 len += 1;
313
314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
315 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
316
317 // Write OID
318 //
319 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
320 oid_len, 0 ) );
321
322 return( (int) len );
323}
324
325static int x509_write_extension( unsigned char **p, unsigned char *start,
326 mbedtls_asn1_named_data *ext )
327{
328 int ret;
329 size_t len = 0;
330
331 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
332 ext->val.len - 1 ) );
333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
334 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
335
336 if( ext->val.p[0] != 0 )
337 {
338 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
339 }
340
341 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
342 ext->oid.len ) );
343 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
345
346 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
347 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
348 MBEDTLS_ASN1_SEQUENCE ) );
349
350 return( (int) len );
351}
352
353/*
354 * Extension ::= SEQUENCE {
355 * extnID OBJECT IDENTIFIER,
356 * critical BOOLEAN DEFAULT FALSE,
357 * extnValue OCTET STRING
358 * -- contains the DER encoding of an ASN.1 value
359 * -- corresponding to the extension type identified
360 * -- by extnID
361 * }
362 */
363int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
364 mbedtls_asn1_named_data *first )
365{
366 int ret;
367 size_t len = 0;
368 mbedtls_asn1_named_data *cur_ext = first;
369
370 while( cur_ext != NULL )
371 {
372 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
373 cur_ext = cur_ext->next;
374 }
375
376 return( (int) len );
377}
378
379#endif /* MBEDTLS_X509_CREATE_C */
Note: See TracBrowser for help on using the repository browser.