2 * X.509 base functions for creating certificates / CSRs
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
28 * GNU General Public License v2.0 or later:
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
50 #include MBEDTLS_CONFIG_FILE
53 #if defined(MBEDTLS_X509_CREATE_C)
55 #include "mbedtls/x509.h"
56 #include "mbedtls/asn1write.h"
57 #include "mbedtls/oid.h"
61 /* Structure linking OIDs for X.509 DN AttributeTypes to their
62 * string representations and default string encodings used by Mbed TLS. */
64 const char *name
; /* String representation of AttributeType, e.g.
65 * "CN" or "emailAddress". */
66 size_t name_len
; /* Length of 'name', without trailing 0 byte. */
67 const char *oid
; /* String representation of OID of AttributeType,
68 * as per RFC 5280, Appendix A.1. */
69 int default_tag
; /* The default character encoding used for the
70 * given attribute type, e.g.
71 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
72 } x509_attr_descriptor_t
;
74 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
76 /* X.509 DN attributes from RFC 5280, Appendix A.1. */
77 static const x509_attr_descriptor_t x509_attrs
[] =
80 MBEDTLS_OID_AT_CN
, MBEDTLS_ASN1_UTF8_STRING
},
81 { ADD_STRLEN( "commonName" ),
82 MBEDTLS_OID_AT_CN
, MBEDTLS_ASN1_UTF8_STRING
},
84 MBEDTLS_OID_AT_COUNTRY
, MBEDTLS_ASN1_PRINTABLE_STRING
},
85 { ADD_STRLEN( "countryName" ),
86 MBEDTLS_OID_AT_COUNTRY
, MBEDTLS_ASN1_PRINTABLE_STRING
},
88 MBEDTLS_OID_AT_ORGANIZATION
, MBEDTLS_ASN1_UTF8_STRING
},
89 { ADD_STRLEN( "organizationName" ),
90 MBEDTLS_OID_AT_ORGANIZATION
, MBEDTLS_ASN1_UTF8_STRING
},
92 MBEDTLS_OID_AT_LOCALITY
, MBEDTLS_ASN1_UTF8_STRING
},
93 { ADD_STRLEN( "locality" ),
94 MBEDTLS_OID_AT_LOCALITY
, MBEDTLS_ASN1_UTF8_STRING
},
96 MBEDTLS_OID_PKCS9_EMAIL
, MBEDTLS_ASN1_IA5_STRING
},
98 MBEDTLS_OID_AT_ORG_UNIT
, MBEDTLS_ASN1_UTF8_STRING
},
99 { ADD_STRLEN( "organizationalUnitName" ),
100 MBEDTLS_OID_AT_ORG_UNIT
, MBEDTLS_ASN1_UTF8_STRING
},
101 { ADD_STRLEN( "ST" ),
102 MBEDTLS_OID_AT_STATE
, MBEDTLS_ASN1_UTF8_STRING
},
103 { ADD_STRLEN( "stateOrProvinceName" ),
104 MBEDTLS_OID_AT_STATE
, MBEDTLS_ASN1_UTF8_STRING
},
105 { ADD_STRLEN( "emailAddress" ),
106 MBEDTLS_OID_PKCS9_EMAIL
, MBEDTLS_ASN1_IA5_STRING
},
107 { ADD_STRLEN( "serialNumber" ),
108 MBEDTLS_OID_AT_SERIAL_NUMBER
, MBEDTLS_ASN1_PRINTABLE_STRING
},
109 { ADD_STRLEN( "postalAddress" ),
110 MBEDTLS_OID_AT_POSTAL_ADDRESS
, MBEDTLS_ASN1_PRINTABLE_STRING
},
111 { ADD_STRLEN( "postalCode" ),
112 MBEDTLS_OID_AT_POSTAL_CODE
, MBEDTLS_ASN1_PRINTABLE_STRING
},
113 { ADD_STRLEN( "dnQualifier" ),
114 MBEDTLS_OID_AT_DN_QUALIFIER
, MBEDTLS_ASN1_PRINTABLE_STRING
},
115 { ADD_STRLEN( "title" ),
116 MBEDTLS_OID_AT_TITLE
, MBEDTLS_ASN1_UTF8_STRING
},
117 { ADD_STRLEN( "surName" ),
118 MBEDTLS_OID_AT_SUR_NAME
, MBEDTLS_ASN1_UTF8_STRING
},
119 { ADD_STRLEN( "SN" ),
120 MBEDTLS_OID_AT_SUR_NAME
, MBEDTLS_ASN1_UTF8_STRING
},
121 { ADD_STRLEN( "givenName" ),
122 MBEDTLS_OID_AT_GIVEN_NAME
, MBEDTLS_ASN1_UTF8_STRING
},
123 { ADD_STRLEN( "GN" ),
124 MBEDTLS_OID_AT_GIVEN_NAME
, MBEDTLS_ASN1_UTF8_STRING
},
125 { ADD_STRLEN( "initials" ),
126 MBEDTLS_OID_AT_INITIALS
, MBEDTLS_ASN1_UTF8_STRING
},
127 { ADD_STRLEN( "pseudonym" ),
128 MBEDTLS_OID_AT_PSEUDONYM
, MBEDTLS_ASN1_UTF8_STRING
},
129 { ADD_STRLEN( "generationQualifier" ),
130 MBEDTLS_OID_AT_GENERATION_QUALIFIER
, MBEDTLS_ASN1_UTF8_STRING
},
131 { ADD_STRLEN( "domainComponent" ),
132 MBEDTLS_OID_DOMAIN_COMPONENT
, MBEDTLS_ASN1_IA5_STRING
},
133 { ADD_STRLEN( "DC" ),
134 MBEDTLS_OID_DOMAIN_COMPONENT
, MBEDTLS_ASN1_IA5_STRING
},
135 { NULL
, 0, NULL
, MBEDTLS_ASN1_NULL
}
138 static const x509_attr_descriptor_t
*x509_attr_descr_from_name( const char *name
, size_t name_len
)
140 const x509_attr_descriptor_t
*cur
;
142 for( cur
= x509_attrs
; cur
->name
!= NULL
; cur
++ )
143 if( cur
->name_len
== name_len
&&
144 strncmp( cur
->name
, name
, name_len
) == 0 )
147 if ( cur
->name
== NULL
)
153 int mbedtls_x509_string_to_names( mbedtls_asn1_named_data
**head
, const char *name
)
156 const char *s
= name
, *c
= s
;
157 const char *end
= s
+ strlen( s
);
158 const char *oid
= NULL
;
159 const x509_attr_descriptor_t
* attr_descr
= NULL
;
161 char data
[MBEDTLS_X509_MAX_DN_NAME_SIZE
];
164 /* Clear existing chain if present */
165 mbedtls_asn1_free_named_data_list( head
);
169 if( in_tag
&& *c
== '=' )
171 if( ( attr_descr
= x509_attr_descr_from_name( s
, c
- s
) ) == NULL
)
173 ret
= MBEDTLS_ERR_X509_UNKNOWN_OID
;
177 oid
= attr_descr
->oid
;
183 if( !in_tag
&& *c
== '\\' && c
!= end
)
187 /* Check for valid escaped characters */
188 if( c
== end
|| *c
!= ',' )
190 ret
= MBEDTLS_ERR_X509_INVALID_NAME
;
194 else if( !in_tag
&& ( *c
== ',' || c
== end
) )
196 mbedtls_asn1_named_data
* cur
=
197 mbedtls_asn1_store_named_data( head
, oid
, strlen( oid
),
198 (unsigned char *) data
,
203 return( MBEDTLS_ERR_X509_ALLOC_FAILED
);
207 cur
->val
.tag
= attr_descr
->default_tag
;
209 while( c
< end
&& *(c
+ 1) == ' ' )
216 if( !in_tag
&& s
!= c
+ 1 )
220 if( d
- data
== MBEDTLS_X509_MAX_DN_NAME_SIZE
)
222 ret
= MBEDTLS_ERR_X509_INVALID_NAME
;
235 /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
236 * to store the critical boolean for us
238 int mbedtls_x509_set_extension( mbedtls_asn1_named_data
**head
, const char *oid
, size_t oid_len
,
239 int critical
, const unsigned char *val
, size_t val_len
)
241 mbedtls_asn1_named_data
*cur
;
243 if( ( cur
= mbedtls_asn1_store_named_data( head
, oid
, oid_len
,
244 NULL
, val_len
+ 1 ) ) == NULL
)
246 return( MBEDTLS_ERR_X509_ALLOC_FAILED
);
249 cur
->val
.p
[0] = critical
;
250 memcpy( cur
->val
.p
+ 1, val
, val_len
);
256 * RelativeDistinguishedName ::=
257 * SET OF AttributeTypeAndValue
259 * AttributeTypeAndValue ::= SEQUENCE {
260 * type AttributeType,
261 * value AttributeValue }
263 * AttributeType ::= OBJECT IDENTIFIER
265 * AttributeValue ::= ANY DEFINED BY AttributeType
267 static int x509_write_name( unsigned char **p
, unsigned char *start
, mbedtls_asn1_named_data
* cur_name
)
271 const char *oid
= (const char*)cur_name
->oid
.p
;
272 size_t oid_len
= cur_name
->oid
.len
;
273 const unsigned char *name
= cur_name
->val
.p
;
274 size_t name_len
= cur_name
->val
.len
;
276 // Write correct string tag and value
277 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tagged_string( p
, start
,
283 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_oid( p
, start
, oid
,
286 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, len
) );
287 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
,
288 MBEDTLS_ASN1_CONSTRUCTED
|
289 MBEDTLS_ASN1_SEQUENCE
) );
291 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, len
) );
292 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
,
293 MBEDTLS_ASN1_CONSTRUCTED
|
294 MBEDTLS_ASN1_SET
) );
299 int mbedtls_x509_write_names( unsigned char **p
, unsigned char *start
,
300 mbedtls_asn1_named_data
*first
)
304 mbedtls_asn1_named_data
*cur
= first
;
308 MBEDTLS_ASN1_CHK_ADD( len
, x509_write_name( p
, start
, cur
) );
312 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, len
) );
313 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
, MBEDTLS_ASN1_CONSTRUCTED
|
314 MBEDTLS_ASN1_SEQUENCE
) );
319 int mbedtls_x509_write_sig( unsigned char **p
, unsigned char *start
,
320 const char *oid
, size_t oid_len
,
321 unsigned char *sig
, size_t size
)
326 if( *p
< start
|| (size_t)( *p
- start
) < size
)
327 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
331 memcpy( *p
, sig
, len
);
334 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
339 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, len
) );
340 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
, MBEDTLS_ASN1_BIT_STRING
) );
344 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_algorithm_identifier( p
, start
, oid
,
350 static int x509_write_extension( unsigned char **p
, unsigned char *start
,
351 mbedtls_asn1_named_data
*ext
)
356 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_raw_buffer( p
, start
, ext
->val
.p
+ 1,
357 ext
->val
.len
- 1 ) );
358 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, ext
->val
.len
- 1 ) );
359 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
, MBEDTLS_ASN1_OCTET_STRING
) );
361 if( ext
->val
.p
[0] != 0 )
363 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_bool( p
, start
, 1 ) );
366 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_raw_buffer( p
, start
, ext
->oid
.p
,
368 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, ext
->oid
.len
) );
369 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
, MBEDTLS_ASN1_OID
) );
371 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_len( p
, start
, len
) );
372 MBEDTLS_ASN1_CHK_ADD( len
, mbedtls_asn1_write_tag( p
, start
, MBEDTLS_ASN1_CONSTRUCTED
|
373 MBEDTLS_ASN1_SEQUENCE
) );
379 * Extension ::= SEQUENCE {
380 * extnID OBJECT IDENTIFIER,
381 * critical BOOLEAN DEFAULT FALSE,
382 * extnValue OCTET STRING
383 * -- contains the DER encoding of an ASN.1 value
384 * -- corresponding to the extension type identified
388 int mbedtls_x509_write_extensions( unsigned char **p
, unsigned char *start
,
389 mbedtls_asn1_named_data
*first
)
393 mbedtls_asn1_named_data
*cur_ext
= first
;
395 while( cur_ext
!= NULL
)
397 MBEDTLS_ASN1_CHK_ADD( len
, x509_write_extension( p
, start
, cur_ext
) );
398 cur_ext
= cur_ext
->next
;
404 #endif /* MBEDTLS_X509_CREATE_C */