1 /**
2 * \file oid.c
3 *
4 * \brief Object Identifier (OID) database
5 *
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8 *
9 * This file is provided under the Apache License 2.0, or the
10 * GNU General Public License v2.0 or later.
11 *
12 * **********
13 * Apache License 2.0:
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *
27 * **********
28 *
29 * **********
30 * GNU General Public License v2.0 or later:
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License along
43 * with this program; if not, write to the Free Software Foundation, Inc.,
44 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45 *
46 * **********
47 */
48
49 #if !defined(MBEDTLS_CONFIG_FILE)
50 #include "mbedtls/config.h"
51 #else
52 #include MBEDTLS_CONFIG_FILE
53 #endif
54
55 #if defined(MBEDTLS_OID_C)
56
57 #include "mbedtls/oid.h"
58 #include "mbedtls/rsa.h"
59
60 #include <stdio.h>
61 #include <string.h>
62
63 #if defined(MBEDTLS_PLATFORM_C)
64 #include "mbedtls/platform.h"
65 #else
66 #define mbedtls_snprintf snprintf
67 #endif
68
69 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
70 #include "mbedtls/x509.h"
71 #endif
72
73 /*
74 * Macro to automatically add the size of #define'd OIDs
75 */
76 #define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s)
77
78 /*
79 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
80 * the other functions)
81 */
82 #define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
83 static const TYPE_T * oid_ ## NAME ## _from_asn1( \
84 const mbedtls_asn1_buf *oid ) \
85 { \
86 const TYPE_T *p = (LIST); \
87 const mbedtls_oid_descriptor_t *cur = \
88 (const mbedtls_oid_descriptor_t *) p; \
89 if( p == NULL || oid == NULL ) return( NULL ); \
90 while( cur->asn1 != NULL ) { \
91 if( cur->asn1_len == oid->len && \
92 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
93 return( p ); \
94 } \
95 p++; \
96 cur = (const mbedtls_oid_descriptor_t *) p; \
97 } \
98 return( NULL ); \
99 }
100
101 /*
102 * Macro to generate a function for retrieving a single attribute from the
103 * descriptor of an mbedtls_oid_descriptor_t wrapper.
104 */
105 #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
106 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
107 { \
108 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
109 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
110 *ATTR1 = data->descriptor.ATTR1; \
111 return( 0 ); \
112 }
113
114 /*
115 * Macro to generate a function for retrieving a single attribute from an
116 * mbedtls_oid_descriptor_t wrapper.
117 */
118 #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
119 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
120 { \
121 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
122 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
123 *ATTR1 = data->ATTR1; \
124 return( 0 ); \
125 }
126
127 /*
128 * Macro to generate a function for retrieving two attributes from an
129 * mbedtls_oid_descriptor_t wrapper.
130 */
131 #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
132 ATTR2_TYPE, ATTR2) \
133 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, \
134 ATTR2_TYPE * ATTR2 ) \
135 { \
136 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
137 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \
138 *(ATTR1) = data->ATTR1; \
139 *(ATTR2) = data->ATTR2; \
140 return( 0 ); \
141 }
142
143 /*
144 * Macro to generate a function for retrieving the OID based on a single
145 * attribute from a mbedtls_oid_descriptor_t wrapper.
146 */
147 #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
148 int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \
149 { \
150 const TYPE_T *cur = (LIST); \
151 while( cur->descriptor.asn1 != NULL ) { \
152 if( cur->ATTR1 == (ATTR1) ) { \
153 *oid = cur->descriptor.asn1; \
154 *olen = cur->descriptor.asn1_len; \
155 return( 0 ); \
156 } \
157 cur++; \
158 } \
159 return( MBEDTLS_ERR_OID_NOT_FOUND ); \
160 }
161
162 /*
163 * Macro to generate a function for retrieving the OID based on two
164 * attributes from a mbedtls_oid_descriptor_t wrapper.
165 */
166 #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
167 ATTR2_TYPE, ATTR2) \
168 int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \
169 size_t *olen ) \
170 { \
171 const TYPE_T *cur = (LIST); \
172 while( cur->descriptor.asn1 != NULL ) { \
173 if( cur->ATTR1 == (ATTR1) && cur->ATTR2 == (ATTR2) ) { \
174 *oid = cur->descriptor.asn1; \
175 *olen = cur->descriptor.asn1_len; \
176 return( 0 ); \
177 } \
178 cur++; \
179 } \
180 return( MBEDTLS_ERR_OID_NOT_FOUND ); \
181 }
182
183 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
184 /*
185 * For X520 attribute types
186 */
187 typedef struct {
188 mbedtls_oid_descriptor_t descriptor;
189 const char *short_name;
190 } oid_x520_attr_t;
191
192 static const oid_x520_attr_t oid_x520_attr_type[] =
193 {
194 {
195 { ADD_LEN( MBEDTLS_OID_AT_CN ), "id-at-commonName", "Common Name" },
196 "CN",
197 },
198 {
199 { ADD_LEN( MBEDTLS_OID_AT_COUNTRY ), "id-at-countryName", "Country" },
200 "C",
201 },
202 {
203 { ADD_LEN( MBEDTLS_OID_AT_LOCALITY ), "id-at-locality", "Locality" },
204 "L",
205 },
206 {
207 { ADD_LEN( MBEDTLS_OID_AT_STATE ), "id-at-state", "State" },
208 "ST",
209 },
210 {
211 { ADD_LEN( MBEDTLS_OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" },
212 "O",
213 },
214 {
215 { ADD_LEN( MBEDTLS_OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" },
216 "OU",
217 },
218 {
219 { ADD_LEN( MBEDTLS_OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" },
220 "emailAddress",
221 },
222 {
223 { ADD_LEN( MBEDTLS_OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" },
224 "serialNumber",
225 },
226 {
227 { ADD_LEN( MBEDTLS_OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" },
228 "postalAddress",
229 },
230 {
231 { ADD_LEN( MBEDTLS_OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" },
232 "postalCode",
233 },
234 {
235 { ADD_LEN( MBEDTLS_OID_AT_SUR_NAME ), "id-at-surName", "Surname" },
236 "SN",
237 },
238 {
239 { ADD_LEN( MBEDTLS_OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" },
240 "GN",
241 },
242 {
243 { ADD_LEN( MBEDTLS_OID_AT_INITIALS ), "id-at-initials", "Initials" },
244 "initials",
245 },
246 {
247 { ADD_LEN( MBEDTLS_OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" },
248 "generationQualifier",
249 },
250 {
251 { ADD_LEN( MBEDTLS_OID_AT_TITLE ), "id-at-title", "Title" },
252 "title",
253 },
254 {
255 { ADD_LEN( MBEDTLS_OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" },
256 "dnQualifier",
257 },
258 {
259 { ADD_LEN( MBEDTLS_OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" },
260 "pseudonym",
261 },
262 {
263 { ADD_LEN( MBEDTLS_OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" },
264 "DC",
265 },
266 {
267 { ADD_LEN( MBEDTLS_OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier", "Unique Identifier" },
268 "uniqueIdentifier",
269 },
270 {
271 { NULL, 0, NULL, NULL },
272 NULL,
273 }
274 };
275
276 FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type)
277 FN_OID_GET_ATTR1(mbedtls_oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name)
278
279 /*
280 * For X509 extensions
281 */
282 typedef struct {
283 mbedtls_oid_descriptor_t descriptor;
284 int ext_type;
285 } oid_x509_ext_t;
286
287 static const oid_x509_ext_t oid_x509_ext[] =
288 {
289 {
290 { ADD_LEN( MBEDTLS_OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" },
291 MBEDTLS_X509_EXT_BASIC_CONSTRAINTS,
292 },
293 {
294 { ADD_LEN( MBEDTLS_OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" },
295 MBEDTLS_X509_EXT_KEY_USAGE,
296 },
297 {
298 { ADD_LEN( MBEDTLS_OID_EXTENDED_KEY_USAGE ), "id-ce-extKeyUsage", "Extended Key Usage" },
299 MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE,
300 },
301 {
302 { ADD_LEN( MBEDTLS_OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" },
303 MBEDTLS_X509_EXT_SUBJECT_ALT_NAME,
304 },
305 {
306 { ADD_LEN( MBEDTLS_OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" },
307 MBEDTLS_X509_EXT_NS_CERT_TYPE,
308 },
309 {
310 { NULL, 0, NULL, NULL },
311 0,
312 },
313 };
314
315 FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
316 FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type)
317
318 static const mbedtls_oid_descriptor_t oid_ext_key_usage[] =
319 {
320 { ADD_LEN( MBEDTLS_OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" },
321 { ADD_LEN( MBEDTLS_OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" },
322 { ADD_LEN( MBEDTLS_OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" },
323 { ADD_LEN( MBEDTLS_OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" },
324 { ADD_LEN( MBEDTLS_OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" },
325 { ADD_LEN( MBEDTLS_OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" },
326 { NULL, 0, NULL, NULL },
327 };
328
329 FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage)
330 FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage, mbedtls_oid_descriptor_t, ext_key_usage, const char *, description)
331 #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
332
333 #if defined(MBEDTLS_MD_C)
334 /*
335 * For SignatureAlgorithmIdentifier
336 */
337 typedef struct {
338 mbedtls_oid_descriptor_t descriptor;
339 mbedtls_md_type_t md_alg;
340 mbedtls_pk_type_t pk_alg;
341 } oid_sig_alg_t;
342
343 static const oid_sig_alg_t oid_sig_alg[] =
344 {
345 #if defined(MBEDTLS_RSA_C)
346 #if defined(MBEDTLS_MD2_C)
347 {
348 { ADD_LEN( MBEDTLS_OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" },
349 MBEDTLS_MD_MD2, MBEDTLS_PK_RSA,
350 },
351 #endif /* MBEDTLS_MD2_C */
352 #if defined(MBEDTLS_MD4_C)
353 {
354 { ADD_LEN( MBEDTLS_OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" },
355 MBEDTLS_MD_MD4, MBEDTLS_PK_RSA,
356 },
357 #endif /* MBEDTLS_MD4_C */
358 #if defined(MBEDTLS_MD5_C)
359 {
360 { ADD_LEN( MBEDTLS_OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" },
361 MBEDTLS_MD_MD5, MBEDTLS_PK_RSA,
362 },
363 #endif /* MBEDTLS_MD5_C */
364 #if defined(MBEDTLS_SHA1_C)
365 {
366 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" },
367 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
368 },
369 #endif /* MBEDTLS_SHA1_C */
370 #if defined(MBEDTLS_SHA256_C)
371 {
372 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" },
373 MBEDTLS_MD_SHA224, MBEDTLS_PK_RSA,
374 },
375 {
376 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" },
377 MBEDTLS_MD_SHA256, MBEDTLS_PK_RSA,
378 },
379 #endif /* MBEDTLS_SHA256_C */
380 #if defined(MBEDTLS_SHA512_C)
381 {
382 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" },
383 MBEDTLS_MD_SHA384, MBEDTLS_PK_RSA,
384 },
385 {
386 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" },
387 MBEDTLS_MD_SHA512, MBEDTLS_PK_RSA,
388 },
389 #endif /* MBEDTLS_SHA512_C */
390 #if defined(MBEDTLS_SHA1_C)
391 {
392 { ADD_LEN( MBEDTLS_OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" },
393 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
394 },
395 #endif /* MBEDTLS_SHA1_C */
396 #endif /* MBEDTLS_RSA_C */
397 #if defined(MBEDTLS_ECDSA_C)
398 #if defined(MBEDTLS_SHA1_C)
399 {
400 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" },
401 MBEDTLS_MD_SHA1, MBEDTLS_PK_ECDSA,
402 },
403 #endif /* MBEDTLS_SHA1_C */
404 #if defined(MBEDTLS_SHA256_C)
405 {
406 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" },
407 MBEDTLS_MD_SHA224, MBEDTLS_PK_ECDSA,
408 },
409 {
410 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" },
411 MBEDTLS_MD_SHA256, MBEDTLS_PK_ECDSA,
412 },
413 #endif /* MBEDTLS_SHA256_C */
414 #if defined(MBEDTLS_SHA512_C)
415 {
416 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" },
417 MBEDTLS_MD_SHA384, MBEDTLS_PK_ECDSA,
418 },
419 {
420 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" },
421 MBEDTLS_MD_SHA512, MBEDTLS_PK_ECDSA,
422 },
423 #endif /* MBEDTLS_SHA512_C */
424 #endif /* MBEDTLS_ECDSA_C */
425 #if defined(MBEDTLS_RSA_C)
426 {
427 { ADD_LEN( MBEDTLS_OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" },
428 MBEDTLS_MD_NONE, MBEDTLS_PK_RSASSA_PSS,
429 },
430 #endif /* MBEDTLS_RSA_C */
431 {
432 { NULL, 0, NULL, NULL },
433 MBEDTLS_MD_NONE, MBEDTLS_PK_NONE,
434 },
435 };
436
437 FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg)
438 FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description)
439 FN_OID_GET_ATTR2(mbedtls_oid_get_sig_alg, oid_sig_alg_t, sig_alg, mbedtls_md_type_t, md_alg, mbedtls_pk_type_t, pk_alg)
440 FN_OID_GET_OID_BY_ATTR2(mbedtls_oid_get_oid_by_sig_alg, oid_sig_alg_t, oid_sig_alg, mbedtls_pk_type_t, pk_alg, mbedtls_md_type_t, md_alg)
441 #endif /* MBEDTLS_MD_C */
442
443 /*
444 * For PublicKeyInfo (PKCS1, RFC 5480)
445 */
446 typedef struct {
447 mbedtls_oid_descriptor_t descriptor;
448 mbedtls_pk_type_t pk_alg;
449 } oid_pk_alg_t;
450
451 static const oid_pk_alg_t oid_pk_alg[] =
452 {
453 {
454 { ADD_LEN( MBEDTLS_OID_PKCS1_RSA ), "rsaEncryption", "RSA" },
455 MBEDTLS_PK_RSA,
456 },
457 {
458 { ADD_LEN( MBEDTLS_OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" },
459 MBEDTLS_PK_ECKEY,
460 },
461 {
462 { ADD_LEN( MBEDTLS_OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" },
463 MBEDTLS_PK_ECKEY_DH,
464 },
465 {
466 { NULL, 0, NULL, NULL },
467 MBEDTLS_PK_NONE,
468 },
469 };
470
471 FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg)
472 FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg)
473 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_pk_alg, oid_pk_alg_t, oid_pk_alg, mbedtls_pk_type_t, pk_alg)
474
475 #if defined(MBEDTLS_ECP_C)
476 /*
477 * For namedCurve (RFC 5480)
478 */
479 typedef struct {
480 mbedtls_oid_descriptor_t descriptor;
481 mbedtls_ecp_group_id grp_id;
482 } oid_ecp_grp_t;
483
484 static const oid_ecp_grp_t oid_ecp_grp[] =
485 {
486 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
487 {
488 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" },
489 MBEDTLS_ECP_DP_SECP192R1,
490 },
491 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
492 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
493 {
494 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" },
495 MBEDTLS_ECP_DP_SECP224R1,
496 },
497 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
498 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
499 {
500 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" },
501 MBEDTLS_ECP_DP_SECP256R1,
502 },
503 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
504 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
505 {
506 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" },
507 MBEDTLS_ECP_DP_SECP384R1,
508 },
509 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
510 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
511 {
512 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" },
513 MBEDTLS_ECP_DP_SECP521R1,
514 },
515 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
516 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
517 {
518 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" },
519 MBEDTLS_ECP_DP_SECP192K1,
520 },
521 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
522 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
523 {
524 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" },
525 MBEDTLS_ECP_DP_SECP224K1,
526 },
527 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
528 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
529 {
530 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" },
531 MBEDTLS_ECP_DP_SECP256K1,
532 },
533 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
534 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
535 {
536 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" },
537 MBEDTLS_ECP_DP_BP256R1,
538 },
539 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
540 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
541 {
542 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" },
543 MBEDTLS_ECP_DP_BP384R1,
544 },
545 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
546 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
547 {
548 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" },
549 MBEDTLS_ECP_DP_BP512R1,
550 },
551 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
552 {
553 { NULL, 0, NULL, NULL },
554 MBEDTLS_ECP_DP_NONE,
555 },
556 };
557
558 FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp)
559 FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id)
560 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp, oid_ecp_grp_t, oid_ecp_grp, mbedtls_ecp_group_id, grp_id)
561 #endif /* MBEDTLS_ECP_C */
562
563 #if defined(MBEDTLS_CIPHER_C)
564 /*
565 * For PKCS#5 PBES2 encryption algorithm
566 */
567 typedef struct {
568 mbedtls_oid_descriptor_t descriptor;
569 mbedtls_cipher_type_t cipher_alg;
570 } oid_cipher_alg_t;
571
572 static const oid_cipher_alg_t oid_cipher_alg[] =
573 {
574 {
575 { ADD_LEN( MBEDTLS_OID_DES_CBC ), "desCBC", "DES-CBC" },
576 MBEDTLS_CIPHER_DES_CBC,
577 },
578 {
579 { ADD_LEN( MBEDTLS_OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" },
580 MBEDTLS_CIPHER_DES_EDE3_CBC,
581 },
582 {
583 { NULL, 0, NULL, NULL },
584 MBEDTLS_CIPHER_NONE,
585 },
586 };
587
588 FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg)
589 FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, mbedtls_cipher_type_t, cipher_alg)
590 #endif /* MBEDTLS_CIPHER_C */
591
592 #if defined(MBEDTLS_MD_C)
593 /*
594 * For digestAlgorithm
595 */
596 typedef struct {
597 mbedtls_oid_descriptor_t descriptor;
598 mbedtls_md_type_t md_alg;
599 } oid_md_alg_t;
600
601 static const oid_md_alg_t oid_md_alg[] =
602 {
603 #if defined(MBEDTLS_MD2_C)
604 {
605 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" },
606 MBEDTLS_MD_MD2,
607 },
608 #endif /* MBEDTLS_MD2_C */
609 #if defined(MBEDTLS_MD4_C)
610 {
611 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" },
612 MBEDTLS_MD_MD4,
613 },
614 #endif /* MBEDTLS_MD4_C */
615 #if defined(MBEDTLS_MD5_C)
616 {
617 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" },
618 MBEDTLS_MD_MD5,
619 },
620 #endif /* MBEDTLS_MD5_C */
621 #if defined(MBEDTLS_SHA1_C)
622 {
623 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" },
624 MBEDTLS_MD_SHA1,
625 },
626 #endif /* MBEDTLS_SHA1_C */
627 #if defined(MBEDTLS_SHA256_C)
628 {
629 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" },
630 MBEDTLS_MD_SHA224,
631 },
632 {
633 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" },
634 MBEDTLS_MD_SHA256,
635 },
636 #endif /* MBEDTLS_SHA256_C */
637 #if defined(MBEDTLS_SHA512_C)
638 {
639 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" },
640 MBEDTLS_MD_SHA384,
641 },
642 {
643 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" },
644 MBEDTLS_MD_SHA512,
645 },
646 #endif /* MBEDTLS_SHA512_C */
647 {
648 { NULL, 0, NULL, NULL },
649 MBEDTLS_MD_NONE,
650 },
651 };
652
653 FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
654 FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
655 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_md, oid_md_alg_t, oid_md_alg, mbedtls_md_type_t, md_alg)
656
657 /*
658 * For HMAC digestAlgorithm
659 */
660 typedef struct {
661 mbedtls_oid_descriptor_t descriptor;
662 mbedtls_md_type_t md_hmac;
663 } oid_md_hmac_t;
664
665 static const oid_md_hmac_t oid_md_hmac[] =
666 {
667 #if defined(MBEDTLS_SHA1_C)
668 {
669 { ADD_LEN( MBEDTLS_OID_HMAC_SHA1 ), "hmacSHA1", "HMAC-SHA-1" },
670 MBEDTLS_MD_SHA1,
671 },
672 #endif /* MBEDTLS_SHA1_C */
673 #if defined(MBEDTLS_SHA256_C)
674 {
675 { ADD_LEN( MBEDTLS_OID_HMAC_SHA224 ), "hmacSHA224", "HMAC-SHA-224" },
676 MBEDTLS_MD_SHA224,
677 },
678 {
679 { ADD_LEN( MBEDTLS_OID_HMAC_SHA256 ), "hmacSHA256", "HMAC-SHA-256" },
680 MBEDTLS_MD_SHA256,
681 },
682 #endif /* MBEDTLS_SHA256_C */
683 #if defined(MBEDTLS_SHA512_C)
684 {
685 { ADD_LEN( MBEDTLS_OID_HMAC_SHA384 ), "hmacSHA384", "HMAC-SHA-384" },
686 MBEDTLS_MD_SHA384,
687 },
688 {
689 { ADD_LEN( MBEDTLS_OID_HMAC_SHA512 ), "hmacSHA512", "HMAC-SHA-512" },
690 MBEDTLS_MD_SHA512,
691 },
692 #endif /* MBEDTLS_SHA512_C */
693 {
694 { NULL, 0, NULL, NULL },
695 MBEDTLS_MD_NONE,
696 },
697 };
698
699 FN_OID_TYPED_FROM_ASN1(oid_md_hmac_t, md_hmac, oid_md_hmac)
700 FN_OID_GET_ATTR1(mbedtls_oid_get_md_hmac, oid_md_hmac_t, md_hmac, mbedtls_md_type_t, md_hmac)
701 #endif /* MBEDTLS_MD_C */
702
703 #if defined(MBEDTLS_PKCS12_C)
704 /*
705 * For PKCS#12 PBEs
706 */
707 typedef struct {
708 mbedtls_oid_descriptor_t descriptor;
709 mbedtls_md_type_t md_alg;
710 mbedtls_cipher_type_t cipher_alg;
711 } oid_pkcs12_pbe_alg_t;
712
713 static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
714 {
715 {
716 { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" },
717 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE3_CBC,
718 },
719 {
720 { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" },
721 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE_CBC,
722 },
723 {
724 { NULL, 0, NULL, NULL },
725 MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE,
726 },
727 };
728
FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t,pkcs12_pbe_alg,oid_pkcs12_pbe_alg)729 FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
730 FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, mbedtls_md_type_t, md_alg, mbedtls_cipher_type_t, cipher_alg)
731 #endif /* MBEDTLS_PKCS12_C */
732
733 #define OID_SAFE_SNPRINTF \
734 do { \
735 if( ret < 0 || (size_t) ret >= n ) \
736 return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
737 \
738 n -= (size_t) ret; \
739 p += (size_t) ret; \
740 } while( 0 )
741
742 /* Return the x.y.z.... style numeric string for the given OID */
743 int mbedtls_oid_get_numeric_string( char *buf, size_t size,
744 const mbedtls_asn1_buf *oid )
745 {
746 int ret;
747 size_t i, n;
748 unsigned int value;
749 char *p;
750
751 p = buf;
752 n = size;
753
754 /* First byte contains first two dots */
755 if( oid->len > 0 )
756 {
757 ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
758 OID_SAFE_SNPRINTF;
759 }
760
761 value = 0;
762 for( i = 1; i < oid->len; i++ )
763 {
764 /* Prevent overflow in value. */
765 if( ( ( value << 7 ) >> 7 ) != value )
766 return( MBEDTLS_ERR_OID_BUF_TOO_SMALL );
767
768 value <<= 7;
769 value += oid->p[i] & 0x7F;
770
771 if( !( oid->p[i] & 0x80 ) )
772 {
773 /* Last byte */
774 ret = mbedtls_snprintf( p, n, ".%d", value );
775 OID_SAFE_SNPRINTF;
776 value = 0;
777 }
778 }
779
780 return( (int) ( size - n ) );
781 }
782
783 #endif /* MBEDTLS_OID_C */
784