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