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