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
10 #include "common.h"
11
12 #if defined(MBEDTLS_OID_C)
13
14 #include "mbedtls/oid.h"
15 #include "mbedtls/rsa.h"
16 #include "mbedtls/error.h"
17 #include "mbedtls/pk.h"
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "mbedtls/platform.h"
23
24 /*
25 * Macro to automatically add the size of #define'd OIDs
26 */
27 #define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s)
28
29 /*
30 * Macro to generate mbedtls_oid_descriptor_t
31 */
32 #if !defined(MBEDTLS_X509_REMOVE_INFO)
33 #define OID_DESCRIPTOR(s, name, description) { ADD_LEN(s), name, description }
34 #define NULL_OID_DESCRIPTOR { NULL, 0, NULL, NULL }
35 #else
36 #define OID_DESCRIPTOR(s, name, description) { ADD_LEN(s) }
37 #define NULL_OID_DESCRIPTOR { NULL, 0 }
38 #endif
39
40 /*
41 * Macro to generate an internal function for oid_XXX_from_asn1() (used by
42 * the other functions)
43 */
44 #define FN_OID_TYPED_FROM_ASN1(TYPE_T, NAME, LIST) \
45 static const TYPE_T *oid_ ## NAME ## _from_asn1( \
46 const mbedtls_asn1_buf *oid) \
47 { \
48 const TYPE_T *p = (LIST); \
49 const mbedtls_oid_descriptor_t *cur = \
50 (const mbedtls_oid_descriptor_t *) p; \
51 if (p == NULL || oid == NULL) return NULL; \
52 while (cur->asn1 != NULL) { \
53 if (cur->asn1_len == oid->len && \
54 memcmp(cur->asn1, oid->p, oid->len) == 0) { \
55 return p; \
56 } \
57 p++; \
58 cur = (const mbedtls_oid_descriptor_t *) p; \
59 } \
60 return NULL; \
61 }
62
63 #if !defined(MBEDTLS_X509_REMOVE_INFO)
64 /*
65 * Macro to generate a function for retrieving a single attribute from the
66 * descriptor of an mbedtls_oid_descriptor_t wrapper.
67 */
68 #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
69 int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1) \
70 { \
71 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1(oid); \
72 if (data == NULL) return MBEDTLS_ERR_OID_NOT_FOUND; \
73 *ATTR1 = data->descriptor.ATTR1; \
74 return 0; \
75 }
76 #endif /* MBEDTLS_X509_REMOVE_INFO */
77
78 /*
79 * Macro to generate a function for retrieving a single attribute from an
80 * mbedtls_oid_descriptor_t wrapper.
81 */
82 #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
83 int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1) \
84 { \
85 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1(oid); \
86 if (data == NULL) return MBEDTLS_ERR_OID_NOT_FOUND; \
87 *ATTR1 = data->ATTR1; \
88 return 0; \
89 }
90
91 /*
92 * Macro to generate a function for retrieving two attributes from an
93 * mbedtls_oid_descriptor_t wrapper.
94 */
95 #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
96 ATTR2_TYPE, ATTR2) \
97 int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, \
98 ATTR2_TYPE * ATTR2) \
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 *(ATTR2) = data->ATTR2; \
104 return 0; \
105 }
106
107 /*
108 * Macro to generate a function for retrieving the OID based on a single
109 * attribute from a mbedtls_oid_descriptor_t wrapper.
110 */
111 #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
112 int FN_NAME(ATTR1_TYPE ATTR1, const char **oid, size_t *olen) \
113 { \
114 const TYPE_T *cur = (LIST); \
115 while (cur->descriptor.asn1 != NULL) { \
116 if (cur->ATTR1 == (ATTR1)) { \
117 *oid = cur->descriptor.asn1; \
118 *olen = cur->descriptor.asn1_len; \
119 return 0; \
120 } \
121 cur++; \
122 } \
123 return MBEDTLS_ERR_OID_NOT_FOUND; \
124 }
125
126 /*
127 * Macro to generate a function for retrieving the OID based on two
128 * attributes from a mbedtls_oid_descriptor_t wrapper.
129 */
130 #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
131 ATTR2_TYPE, ATTR2) \
132 int FN_NAME(ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid, \
133 size_t *olen) \
134 { \
135 const TYPE_T *cur = (LIST); \
136 while (cur->descriptor.asn1 != NULL) { \
137 if (cur->ATTR1 == (ATTR1) && cur->ATTR2 == (ATTR2)) { \
138 *oid = cur->descriptor.asn1; \
139 *olen = cur->descriptor.asn1_len; \
140 return 0; \
141 } \
142 cur++; \
143 } \
144 return MBEDTLS_ERR_OID_NOT_FOUND; \
145 }
146
147 /*
148 * For X520 attribute types
149 */
150 typedef struct {
151 mbedtls_oid_descriptor_t descriptor;
152 const char *short_name;
153 } oid_x520_attr_t;
154
155 static const oid_x520_attr_t oid_x520_attr_type[] =
156 {
157 {
158 OID_DESCRIPTOR(MBEDTLS_OID_AT_CN, "id-at-commonName", "Common Name"),
159 "CN",
160 },
161 {
162 OID_DESCRIPTOR(MBEDTLS_OID_AT_COUNTRY, "id-at-countryName", "Country"),
163 "C",
164 },
165 {
166 OID_DESCRIPTOR(MBEDTLS_OID_AT_LOCALITY, "id-at-locality", "Locality"),
167 "L",
168 },
169 {
170 OID_DESCRIPTOR(MBEDTLS_OID_AT_STATE, "id-at-state", "State"),
171 "ST",
172 },
173 {
174 OID_DESCRIPTOR(MBEDTLS_OID_AT_ORGANIZATION, "id-at-organizationName",
175 "Organization"),
176 "O",
177 },
178 {
179 OID_DESCRIPTOR(MBEDTLS_OID_AT_ORG_UNIT, "id-at-organizationalUnitName", "Org Unit"),
180 "OU",
181 },
182 {
183 OID_DESCRIPTOR(MBEDTLS_OID_PKCS9_EMAIL,
184 "emailAddress",
185 "E-mail address"),
186 "emailAddress",
187 },
188 {
189 OID_DESCRIPTOR(MBEDTLS_OID_AT_SERIAL_NUMBER,
190 "id-at-serialNumber",
191 "Serial number"),
192 "serialNumber",
193 },
194 {
195 OID_DESCRIPTOR(MBEDTLS_OID_AT_POSTAL_ADDRESS,
196 "id-at-postalAddress",
197 "Postal address"),
198 "postalAddress",
199 },
200 {
201 OID_DESCRIPTOR(MBEDTLS_OID_AT_POSTAL_CODE, "id-at-postalCode", "Postal code"),
202 "postalCode",
203 },
204 {
205 OID_DESCRIPTOR(MBEDTLS_OID_AT_SUR_NAME, "id-at-surName", "Surname"),
206 "SN",
207 },
208 {
209 OID_DESCRIPTOR(MBEDTLS_OID_AT_GIVEN_NAME, "id-at-givenName", "Given name"),
210 "GN",
211 },
212 {
213 OID_DESCRIPTOR(MBEDTLS_OID_AT_INITIALS, "id-at-initials", "Initials"),
214 "initials",
215 },
216 {
217 OID_DESCRIPTOR(MBEDTLS_OID_AT_GENERATION_QUALIFIER,
218 "id-at-generationQualifier",
219 "Generation qualifier"),
220 "generationQualifier",
221 },
222 {
223 OID_DESCRIPTOR(MBEDTLS_OID_AT_TITLE, "id-at-title", "Title"),
224 "title",
225 },
226 {
227 OID_DESCRIPTOR(MBEDTLS_OID_AT_DN_QUALIFIER,
228 "id-at-dnQualifier",
229 "Distinguished Name qualifier"),
230 "dnQualifier",
231 },
232 {
233 OID_DESCRIPTOR(MBEDTLS_OID_AT_PSEUDONYM, "id-at-pseudonym", "Pseudonym"),
234 "pseudonym",
235 },
236 {
237 OID_DESCRIPTOR(MBEDTLS_OID_UID, "id-uid", "User Id"),
238 "uid",
239 },
240 {
241 OID_DESCRIPTOR(MBEDTLS_OID_DOMAIN_COMPONENT,
242 "id-domainComponent",
243 "Domain component"),
244 "DC",
245 },
246 {
247 OID_DESCRIPTOR(MBEDTLS_OID_AT_UNIQUE_IDENTIFIER,
248 "id-at-uniqueIdentifier",
249 "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,
260 oid_x520_attr_t,
261 x520_attr,
262 const char *,
263 short_name)
264
265 /*
266 * For X509 extensions
267 */
268 typedef struct {
269 mbedtls_oid_descriptor_t descriptor;
270 int ext_type;
271 } oid_x509_ext_t;
272
273 static const oid_x509_ext_t oid_x509_ext[] =
274 {
275 {
276 OID_DESCRIPTOR(MBEDTLS_OID_BASIC_CONSTRAINTS,
277 "id-ce-basicConstraints",
278 "Basic Constraints"),
279 MBEDTLS_OID_X509_EXT_BASIC_CONSTRAINTS,
280 },
281 {
282 OID_DESCRIPTOR(MBEDTLS_OID_KEY_USAGE, "id-ce-keyUsage", "Key Usage"),
283 MBEDTLS_OID_X509_EXT_KEY_USAGE,
284 },
285 {
286 OID_DESCRIPTOR(MBEDTLS_OID_EXTENDED_KEY_USAGE,
287 "id-ce-extKeyUsage",
288 "Extended Key Usage"),
289 MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE,
290 },
291 {
292 OID_DESCRIPTOR(MBEDTLS_OID_SUBJECT_ALT_NAME,
293 "id-ce-subjectAltName",
294 "Subject Alt Name"),
295 MBEDTLS_OID_X509_EXT_SUBJECT_ALT_NAME,
296 },
297 {
298 OID_DESCRIPTOR(MBEDTLS_OID_NS_CERT_TYPE,
299 "id-netscape-certtype",
300 "Netscape Certificate Type"),
301 MBEDTLS_OID_X509_EXT_NS_CERT_TYPE,
302 },
303 {
304 OID_DESCRIPTOR(MBEDTLS_OID_CERTIFICATE_POLICIES,
305 "id-ce-certificatePolicies",
306 "Certificate Policies"),
307 MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES,
308 },
309 {
310 OID_DESCRIPTOR(MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
311 "id-ce-subjectKeyIdentifier",
312 "Subject Key Identifier"),
313 MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER,
314 },
315 {
316 OID_DESCRIPTOR(MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
317 "id-ce-authorityKeyIdentifier",
318 "Authority Key Identifier"),
319 MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER,
320 },
321 {
322 NULL_OID_DESCRIPTOR,
323 0,
324 },
325 };
326
327 FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
328 FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type)
329
330 #if !defined(MBEDTLS_X509_REMOVE_INFO)
331 static const mbedtls_oid_descriptor_t oid_ext_key_usage[] =
332 {
333 OID_DESCRIPTOR(MBEDTLS_OID_SERVER_AUTH,
334 "id-kp-serverAuth",
335 "TLS Web Server Authentication"),
336 OID_DESCRIPTOR(MBEDTLS_OID_CLIENT_AUTH,
337 "id-kp-clientAuth",
338 "TLS Web Client Authentication"),
339 OID_DESCRIPTOR(MBEDTLS_OID_CODE_SIGNING, "id-kp-codeSigning", "Code Signing"),
340 OID_DESCRIPTOR(MBEDTLS_OID_EMAIL_PROTECTION, "id-kp-emailProtection", "E-mail Protection"),
341 OID_DESCRIPTOR(MBEDTLS_OID_TIME_STAMPING, "id-kp-timeStamping", "Time Stamping"),
342 OID_DESCRIPTOR(MBEDTLS_OID_OCSP_SIGNING, "id-kp-OCSPSigning", "OCSP Signing"),
343 OID_DESCRIPTOR(MBEDTLS_OID_WISUN_FAN,
344 "id-kp-wisun-fan-device",
345 "Wi-SUN Alliance Field Area Network (FAN)"),
346 NULL_OID_DESCRIPTOR,
347 };
348
349 FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage)
350 FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage,
351 mbedtls_oid_descriptor_t,
352 ext_key_usage,
353 const char *,
354 description)
355
356 static const mbedtls_oid_descriptor_t oid_certificate_policies[] =
357 {
358 OID_DESCRIPTOR(MBEDTLS_OID_ANY_POLICY, "anyPolicy", "Any Policy"),
359 NULL_OID_DESCRIPTOR,
360 };
361
362 FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, certificate_policies, oid_certificate_policies)
363 FN_OID_GET_ATTR1(mbedtls_oid_get_certificate_policies,
364 mbedtls_oid_descriptor_t,
365 certificate_policies,
366 const char *,
367 description)
368 #endif /* MBEDTLS_X509_REMOVE_INFO */
369
370 /*
371 * For SignatureAlgorithmIdentifier
372 */
373 typedef struct {
374 mbedtls_oid_descriptor_t descriptor;
375 mbedtls_md_type_t md_alg;
376 mbedtls_pk_type_t pk_alg;
377 } oid_sig_alg_t;
378
379 static const oid_sig_alg_t oid_sig_alg[] =
380 {
381 #if defined(MBEDTLS_RSA_C)
382 #if defined(MBEDTLS_MD_CAN_MD5)
383 {
384 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_MD5, "md5WithRSAEncryption", "RSA with MD5"),
385 MBEDTLS_MD_MD5, MBEDTLS_PK_RSA,
386 },
387 #endif /* MBEDTLS_MD_CAN_MD5 */
388 #if defined(MBEDTLS_MD_CAN_SHA1)
389 {
390 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA1, "sha-1WithRSAEncryption", "RSA with SHA1"),
391 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
392 },
393 #endif /* MBEDTLS_MD_CAN_SHA1 */
394 #if defined(MBEDTLS_MD_CAN_SHA224)
395 {
396 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA224, "sha224WithRSAEncryption",
397 "RSA with SHA-224"),
398 MBEDTLS_MD_SHA224, MBEDTLS_PK_RSA,
399 },
400 #endif /* MBEDTLS_MD_CAN_SHA224 */
401 #if defined(MBEDTLS_MD_CAN_SHA256)
402 {
403 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA256, "sha256WithRSAEncryption",
404 "RSA with SHA-256"),
405 MBEDTLS_MD_SHA256, MBEDTLS_PK_RSA,
406 },
407 #endif /* MBEDTLS_MD_CAN_SHA256 */
408 #if defined(MBEDTLS_MD_CAN_SHA384)
409 {
410 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA384, "sha384WithRSAEncryption",
411 "RSA with SHA-384"),
412 MBEDTLS_MD_SHA384, MBEDTLS_PK_RSA,
413 },
414 #endif /* MBEDTLS_MD_CAN_SHA384 */
415 #if defined(MBEDTLS_MD_CAN_SHA512)
416 {
417 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA512, "sha512WithRSAEncryption",
418 "RSA with SHA-512"),
419 MBEDTLS_MD_SHA512, MBEDTLS_PK_RSA,
420 },
421 #endif /* MBEDTLS_MD_CAN_SHA512 */
422 #if defined(MBEDTLS_MD_CAN_SHA1)
423 {
424 OID_DESCRIPTOR(MBEDTLS_OID_RSA_SHA_OBS, "sha-1WithRSAEncryption", "RSA with SHA1"),
425 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
426 },
427 #endif /* MBEDTLS_MD_CAN_SHA1 */
428 #endif /* MBEDTLS_RSA_C */
429 #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
430 #if defined(MBEDTLS_MD_CAN_SHA1)
431 {
432 OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA1, "ecdsa-with-SHA1", "ECDSA with SHA1"),
433 MBEDTLS_MD_SHA1, MBEDTLS_PK_ECDSA,
434 },
435 #endif /* MBEDTLS_MD_CAN_SHA1 */
436 #if defined(MBEDTLS_MD_CAN_SHA224)
437 {
438 OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA224, "ecdsa-with-SHA224", "ECDSA with SHA224"),
439 MBEDTLS_MD_SHA224, MBEDTLS_PK_ECDSA,
440 },
441 #endif
442 #if defined(MBEDTLS_MD_CAN_SHA256)
443 {
444 OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA256, "ecdsa-with-SHA256", "ECDSA with SHA256"),
445 MBEDTLS_MD_SHA256, MBEDTLS_PK_ECDSA,
446 },
447 #endif /* MBEDTLS_MD_CAN_SHA256 */
448 #if defined(MBEDTLS_MD_CAN_SHA384)
449 {
450 OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA384, "ecdsa-with-SHA384", "ECDSA with SHA384"),
451 MBEDTLS_MD_SHA384, MBEDTLS_PK_ECDSA,
452 },
453 #endif /* MBEDTLS_MD_CAN_SHA384 */
454 #if defined(MBEDTLS_MD_CAN_SHA512)
455 {
456 OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA512, "ecdsa-with-SHA512", "ECDSA with SHA512"),
457 MBEDTLS_MD_SHA512, MBEDTLS_PK_ECDSA,
458 },
459 #endif /* MBEDTLS_MD_CAN_SHA512 */
460 #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
461 #if defined(MBEDTLS_RSA_C)
462 {
463 OID_DESCRIPTOR(MBEDTLS_OID_RSASSA_PSS, "RSASSA-PSS", "RSASSA-PSS"),
464 MBEDTLS_MD_NONE, MBEDTLS_PK_RSASSA_PSS,
465 },
466 #endif /* MBEDTLS_RSA_C */
467 {
468 NULL_OID_DESCRIPTOR,
469 MBEDTLS_MD_NONE, MBEDTLS_PK_NONE,
470 },
471 };
472
473 FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg)
474
475 #if !defined(MBEDTLS_X509_REMOVE_INFO)
476 FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc,
477 oid_sig_alg_t,
478 sig_alg,
479 const char *,
480 description)
481 #endif
482
483 FN_OID_GET_ATTR2(mbedtls_oid_get_sig_alg,
484 oid_sig_alg_t,
485 sig_alg,
486 mbedtls_md_type_t,
487 md_alg,
488 mbedtls_pk_type_t,
489 pk_alg)
490 FN_OID_GET_OID_BY_ATTR2(mbedtls_oid_get_oid_by_sig_alg,
491 oid_sig_alg_t,
492 oid_sig_alg,
493 mbedtls_pk_type_t,
494 pk_alg,
495 mbedtls_md_type_t,
496 md_alg)
497
498 /*
499 * For PublicKeyInfo (PKCS1, RFC 5480)
500 */
501 typedef struct {
502 mbedtls_oid_descriptor_t descriptor;
503 mbedtls_pk_type_t pk_alg;
504 } oid_pk_alg_t;
505
506 static const oid_pk_alg_t oid_pk_alg[] =
507 {
508 {
509 OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_RSA, "rsaEncryption", "RSA"),
510 MBEDTLS_PK_RSA,
511 },
512 {
513 OID_DESCRIPTOR(MBEDTLS_OID_EC_ALG_UNRESTRICTED, "id-ecPublicKey", "Generic EC key"),
514 MBEDTLS_PK_ECKEY,
515 },
516 {
517 OID_DESCRIPTOR(MBEDTLS_OID_EC_ALG_ECDH, "id-ecDH", "EC key for ECDH"),
518 MBEDTLS_PK_ECKEY_DH,
519 },
520 {
521 NULL_OID_DESCRIPTOR,
522 MBEDTLS_PK_NONE,
523 },
524 };
525
526 FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg)
527 FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg)
528 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_pk_alg,
529 oid_pk_alg_t,
530 oid_pk_alg,
531 mbedtls_pk_type_t,
532 pk_alg)
533
534 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
535 /*
536 * For elliptic curves that use namedCurve inside ECParams (RFC 5480)
537 */
538 typedef struct {
539 mbedtls_oid_descriptor_t descriptor;
540 mbedtls_ecp_group_id grp_id;
541 } oid_ecp_grp_t;
542
543 static const oid_ecp_grp_t oid_ecp_grp[] =
544 {
545 #if defined(MBEDTLS_ECP_HAVE_SECP192R1)
546 {
547 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP192R1, "secp192r1", "secp192r1"),
548 MBEDTLS_ECP_DP_SECP192R1,
549 },
550 #endif /* MBEDTLS_ECP_HAVE_SECP192R1 */
551 #if defined(MBEDTLS_ECP_HAVE_SECP224R1)
552 {
553 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP224R1, "secp224r1", "secp224r1"),
554 MBEDTLS_ECP_DP_SECP224R1,
555 },
556 #endif /* MBEDTLS_ECP_HAVE_SECP224R1 */
557 #if defined(MBEDTLS_ECP_HAVE_SECP256R1)
558 {
559 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP256R1, "secp256r1", "secp256r1"),
560 MBEDTLS_ECP_DP_SECP256R1,
561 },
562 #endif /* MBEDTLS_ECP_HAVE_SECP256R1 */
563 #if defined(MBEDTLS_ECP_HAVE_SECP384R1)
564 {
565 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP384R1, "secp384r1", "secp384r1"),
566 MBEDTLS_ECP_DP_SECP384R1,
567 },
568 #endif /* MBEDTLS_ECP_HAVE_SECP384R1 */
569 #if defined(MBEDTLS_ECP_HAVE_SECP521R1)
570 {
571 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP521R1, "secp521r1", "secp521r1"),
572 MBEDTLS_ECP_DP_SECP521R1,
573 },
574 #endif /* MBEDTLS_ECP_HAVE_SECP521R1 */
575 #if defined(MBEDTLS_ECP_HAVE_SECP192K1)
576 {
577 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP192K1, "secp192k1", "secp192k1"),
578 MBEDTLS_ECP_DP_SECP192K1,
579 },
580 #endif /* MBEDTLS_ECP_HAVE_SECP192K1 */
581 #if defined(MBEDTLS_ECP_HAVE_SECP224K1)
582 {
583 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP224K1, "secp224k1", "secp224k1"),
584 MBEDTLS_ECP_DP_SECP224K1,
585 },
586 #endif /* MBEDTLS_ECP_HAVE_SECP224K1 */
587 #if defined(MBEDTLS_ECP_HAVE_SECP256K1)
588 {
589 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP256K1, "secp256k1", "secp256k1"),
590 MBEDTLS_ECP_DP_SECP256K1,
591 },
592 #endif /* MBEDTLS_ECP_HAVE_SECP256K1 */
593 #if defined(MBEDTLS_ECP_HAVE_BP256R1)
594 {
595 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP256R1, "brainpoolP256r1", "brainpool256r1"),
596 MBEDTLS_ECP_DP_BP256R1,
597 },
598 #endif /* MBEDTLS_ECP_HAVE_BP256R1 */
599 #if defined(MBEDTLS_ECP_HAVE_BP384R1)
600 {
601 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP384R1, "brainpoolP384r1", "brainpool384r1"),
602 MBEDTLS_ECP_DP_BP384R1,
603 },
604 #endif /* MBEDTLS_ECP_HAVE_BP384R1 */
605 #if defined(MBEDTLS_ECP_HAVE_BP512R1)
606 {
607 OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP512R1, "brainpoolP512r1", "brainpool512r1"),
608 MBEDTLS_ECP_DP_BP512R1,
609 },
610 #endif /* MBEDTLS_ECP_HAVE_BP512R1 */
611 {
612 NULL_OID_DESCRIPTOR,
613 MBEDTLS_ECP_DP_NONE,
614 },
615 };
616
617 FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp)
618 FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id)
619 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp,
620 oid_ecp_grp_t,
621 oid_ecp_grp,
622 mbedtls_ecp_group_id,
623 grp_id)
624
625 /*
626 * For Elliptic Curve algorithms that are directly
627 * encoded in the AlgorithmIdentifier (RFC 8410)
628 */
629 typedef struct {
630 mbedtls_oid_descriptor_t descriptor;
631 mbedtls_ecp_group_id grp_id;
632 } oid_ecp_grp_algid_t;
633
634 static const oid_ecp_grp_algid_t oid_ecp_grp_algid[] =
635 {
636 #if defined(MBEDTLS_ECP_HAVE_CURVE25519)
637 {
638 OID_DESCRIPTOR(MBEDTLS_OID_X25519, "X25519", "X25519"),
639 MBEDTLS_ECP_DP_CURVE25519,
640 },
641 #endif /* MBEDTLS_ECP_HAVE_CURVE25519 */
642 #if defined(MBEDTLS_ECP_HAVE_CURVE448)
643 {
644 OID_DESCRIPTOR(MBEDTLS_OID_X448, "X448", "X448"),
645 MBEDTLS_ECP_DP_CURVE448,
646 },
647 #endif /* MBEDTLS_ECP_HAVE_CURVE448 */
648 {
649 NULL_OID_DESCRIPTOR,
650 MBEDTLS_ECP_DP_NONE,
651 },
652 };
653
654 FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_algid_t, grp_id_algid, oid_ecp_grp_algid)
655 FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp_algid,
656 oid_ecp_grp_algid_t,
657 grp_id_algid,
658 mbedtls_ecp_group_id,
659 grp_id)
660 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp_algid,
661 oid_ecp_grp_algid_t,
662 oid_ecp_grp_algid,
663 mbedtls_ecp_group_id,
664 grp_id)
665 #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
666
667 #if defined(MBEDTLS_CIPHER_C)
668 /*
669 * For PKCS#5 PBES2 encryption algorithm
670 */
671 typedef struct {
672 mbedtls_oid_descriptor_t descriptor;
673 mbedtls_cipher_type_t cipher_alg;
674 } oid_cipher_alg_t;
675
676 static const oid_cipher_alg_t oid_cipher_alg[] =
677 {
678 {
679 OID_DESCRIPTOR(MBEDTLS_OID_DES_CBC, "desCBC", "DES-CBC"),
680 MBEDTLS_CIPHER_DES_CBC,
681 },
682 {
683 OID_DESCRIPTOR(MBEDTLS_OID_DES_EDE3_CBC, "des-ede3-cbc", "DES-EDE3-CBC"),
684 MBEDTLS_CIPHER_DES_EDE3_CBC,
685 },
686 {
687 NULL_OID_DESCRIPTOR,
688 MBEDTLS_CIPHER_NONE,
689 },
690 };
691
692 FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg)
693 FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg,
694 oid_cipher_alg_t,
695 cipher_alg,
696 mbedtls_cipher_type_t,
697 cipher_alg)
698 #endif /* MBEDTLS_CIPHER_C */
699
700 /*
701 * For digestAlgorithm
702 */
703 typedef struct {
704 mbedtls_oid_descriptor_t descriptor;
705 mbedtls_md_type_t md_alg;
706 } oid_md_alg_t;
707
708 static const oid_md_alg_t oid_md_alg[] =
709 {
710 #if defined(MBEDTLS_MD_CAN_MD5)
711 {
712 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_MD5, "id-md5", "MD5"),
713 MBEDTLS_MD_MD5,
714 },
715 #endif
716 #if defined(MBEDTLS_MD_CAN_SHA1)
717 {
718 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1"),
719 MBEDTLS_MD_SHA1,
720 },
721 #endif
722 #if defined(MBEDTLS_MD_CAN_SHA224)
723 {
724 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA224, "id-sha224", "SHA-224"),
725 MBEDTLS_MD_SHA224,
726 },
727 #endif
728 #if defined(MBEDTLS_MD_CAN_SHA256)
729 {
730 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA256, "id-sha256", "SHA-256"),
731 MBEDTLS_MD_SHA256,
732 },
733 #endif
734 #if defined(MBEDTLS_MD_CAN_SHA384)
735 {
736 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA384, "id-sha384", "SHA-384"),
737 MBEDTLS_MD_SHA384,
738 },
739 #endif
740 #if defined(MBEDTLS_MD_CAN_SHA512)
741 {
742 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA512, "id-sha512", "SHA-512"),
743 MBEDTLS_MD_SHA512,
744 },
745 #endif
746 #if defined(MBEDTLS_MD_CAN_RIPEMD160)
747 {
748 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_RIPEMD160, "id-ripemd160", "RIPEMD-160"),
749 MBEDTLS_MD_RIPEMD160,
750 },
751 #endif
752 #if defined(MBEDTLS_MD_CAN_SHA3_224)
753 {
754 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_224, "id-sha3-224", "SHA-3-224"),
755 MBEDTLS_MD_SHA3_224,
756 },
757 #endif
758 #if defined(MBEDTLS_MD_CAN_SHA3_256)
759 {
760 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_256, "id-sha3-256", "SHA-3-256"),
761 MBEDTLS_MD_SHA3_256,
762 },
763 #endif
764 #if defined(MBEDTLS_MD_CAN_SHA3_384)
765 {
766 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_384, "id-sha3-384", "SHA-3-384"),
767 MBEDTLS_MD_SHA3_384,
768 },
769 #endif
770 #if defined(MBEDTLS_MD_CAN_SHA3_512)
771 {
772 OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_512, "id-sha3-512", "SHA-3-512"),
773 MBEDTLS_MD_SHA3_512,
774 },
775 #endif
776 {
777 NULL_OID_DESCRIPTOR,
778 MBEDTLS_MD_NONE,
779 },
780 };
781
782 FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
783 FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
784 FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_md,
785 oid_md_alg_t,
786 oid_md_alg,
787 mbedtls_md_type_t,
788 md_alg)
789
790 /*
791 * For HMAC digestAlgorithm
792 */
793 typedef struct {
794 mbedtls_oid_descriptor_t descriptor;
795 mbedtls_md_type_t md_hmac;
796 } oid_md_hmac_t;
797
798 static const oid_md_hmac_t oid_md_hmac[] =
799 {
800 #if defined(MBEDTLS_MD_CAN_SHA1)
801 {
802 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA1, "hmacSHA1", "HMAC-SHA-1"),
803 MBEDTLS_MD_SHA1,
804 },
805 #endif /* MBEDTLS_MD_CAN_SHA1 */
806 #if defined(MBEDTLS_MD_CAN_SHA224)
807 {
808 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA224, "hmacSHA224", "HMAC-SHA-224"),
809 MBEDTLS_MD_SHA224,
810 },
811 #endif /* MBEDTLS_MD_CAN_SHA224 */
812 #if defined(MBEDTLS_MD_CAN_SHA256)
813 {
814 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA256, "hmacSHA256", "HMAC-SHA-256"),
815 MBEDTLS_MD_SHA256,
816 },
817 #endif /* MBEDTLS_MD_CAN_SHA256 */
818 #if defined(MBEDTLS_MD_CAN_SHA384)
819 {
820 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA384, "hmacSHA384", "HMAC-SHA-384"),
821 MBEDTLS_MD_SHA384,
822 },
823 #endif /* MBEDTLS_MD_CAN_SHA384 */
824 #if defined(MBEDTLS_MD_CAN_SHA512)
825 {
826 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA512, "hmacSHA512", "HMAC-SHA-512"),
827 MBEDTLS_MD_SHA512,
828 },
829 #endif /* MBEDTLS_MD_CAN_SHA512 */
830 #if defined(MBEDTLS_MD_CAN_SHA3_224)
831 {
832 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_224, "hmacSHA3-224", "HMAC-SHA3-224"),
833 MBEDTLS_MD_SHA3_224,
834 },
835 #endif /* MBEDTLS_MD_CAN_SHA3_224 */
836 #if defined(MBEDTLS_MD_CAN_SHA3_256)
837 {
838 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_256, "hmacSHA3-256", "HMAC-SHA3-256"),
839 MBEDTLS_MD_SHA3_256,
840 },
841 #endif /* MBEDTLS_MD_CAN_SHA3_256 */
842 #if defined(MBEDTLS_MD_CAN_SHA3_384)
843 {
844 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_384, "hmacSHA3-384", "HMAC-SHA3-384"),
845 MBEDTLS_MD_SHA3_384,
846 },
847 #endif /* MBEDTLS_MD_CAN_SHA3_384 */
848 #if defined(MBEDTLS_MD_CAN_SHA3_512)
849 {
850 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_512, "hmacSHA3-512", "HMAC-SHA3-512"),
851 MBEDTLS_MD_SHA3_512,
852 },
853 #endif /* MBEDTLS_MD_CAN_SHA3_512 */
854 #if defined(MBEDTLS_MD_CAN_RIPEMD160)
855 {
856 OID_DESCRIPTOR(MBEDTLS_OID_HMAC_RIPEMD160, "hmacRIPEMD160", "HMAC-RIPEMD160"),
857 MBEDTLS_MD_RIPEMD160,
858 },
859 #endif /* MBEDTLS_MD_CAN_RIPEMD160 */
860 {
861 NULL_OID_DESCRIPTOR,
862 MBEDTLS_MD_NONE,
863 },
864 };
865
866 FN_OID_TYPED_FROM_ASN1(oid_md_hmac_t, md_hmac, oid_md_hmac)
867 FN_OID_GET_ATTR1(mbedtls_oid_get_md_hmac, oid_md_hmac_t, md_hmac, mbedtls_md_type_t, md_hmac)
868
869 #if defined(MBEDTLS_PKCS12_C)
870 /*
871 * For PKCS#12 PBEs
872 */
873 typedef struct {
874 mbedtls_oid_descriptor_t descriptor;
875 mbedtls_md_type_t md_alg;
876 mbedtls_cipher_type_t cipher_alg;
877 } oid_pkcs12_pbe_alg_t;
878
879 static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
880 {
881 {
882 OID_DESCRIPTOR(MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC,
883 "pbeWithSHAAnd3-KeyTripleDES-CBC",
884 "PBE with SHA1 and 3-Key 3DES"),
885 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE3_CBC,
886 },
887 {
888 OID_DESCRIPTOR(MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC,
889 "pbeWithSHAAnd2-KeyTripleDES-CBC",
890 "PBE with SHA1 and 2-Key 3DES"),
891 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE_CBC,
892 },
893 {
894 NULL_OID_DESCRIPTOR,
895 MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE,
896 },
897 };
898
FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t,pkcs12_pbe_alg,oid_pkcs12_pbe_alg)899 FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
900 FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg,
901 oid_pkcs12_pbe_alg_t,
902 pkcs12_pbe_alg,
903 mbedtls_md_type_t,
904 md_alg,
905 mbedtls_cipher_type_t,
906 cipher_alg)
907 #endif /* MBEDTLS_PKCS12_C */
908
909 /* Return the x.y.z.... style numeric string for the given OID */
910 int mbedtls_oid_get_numeric_string(char *buf, size_t size,
911 const mbedtls_asn1_buf *oid)
912 {
913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
914 char *p = buf;
915 size_t n = size;
916 unsigned int value = 0;
917
918 if (size > INT_MAX) {
919 /* Avoid overflow computing return value */
920 return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
921 }
922
923 if (oid->len <= 0) {
924 /* OID must not be empty */
925 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
926 }
927
928 for (size_t i = 0; i < oid->len; i++) {
929 /* Prevent overflow in value. */
930 if (value > (UINT_MAX >> 7)) {
931 return MBEDTLS_ERR_ASN1_INVALID_DATA;
932 }
933 if ((value == 0) && ((oid->p[i]) == 0x80)) {
934 /* Overlong encoding is not allowed */
935 return MBEDTLS_ERR_ASN1_INVALID_DATA;
936 }
937
938 value <<= 7;
939 value |= oid->p[i] & 0x7F;
940
941 if (!(oid->p[i] & 0x80)) {
942 /* Last byte */
943 if (n == size) {
944 int component1;
945 unsigned int component2;
946 /* First subidentifier contains first two OID components */
947 if (value >= 80) {
948 component1 = '2';
949 component2 = value - 80;
950 } else if (value >= 40) {
951 component1 = '1';
952 component2 = value - 40;
953 } else {
954 component1 = '0';
955 component2 = value;
956 }
957 ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
958 } else {
959 ret = mbedtls_snprintf(p, n, ".%u", value);
960 }
961 if (ret < 2 || (size_t) ret >= n) {
962 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
963 }
964 n -= (size_t) ret;
965 p += ret;
966 value = 0;
967 }
968 }
969
970 if (value != 0) {
971 /* Unterminated subidentifier */
972 return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
973 }
974
975 return (int) (size - n);
976 }
977
oid_parse_number(unsigned int * num,const char ** p,const char * bound)978 static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
979 {
980 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
981
982 *num = 0;
983
984 while (*p < bound && **p >= '0' && **p <= '9') {
985 ret = 0;
986 if (*num > (UINT_MAX / 10)) {
987 return MBEDTLS_ERR_ASN1_INVALID_DATA;
988 }
989 *num *= 10;
990 *num += **p - '0';
991 (*p)++;
992 }
993 return ret;
994 }
995
oid_subidentifier_num_bytes(unsigned int value)996 static size_t oid_subidentifier_num_bytes(unsigned int value)
997 {
998 size_t num_bytes = 0;
999
1000 do {
1001 value >>= 7;
1002 num_bytes++;
1003 } while (value != 0);
1004
1005 return num_bytes;
1006 }
1007
oid_subidentifier_encode_into(unsigned char ** p,unsigned char * bound,unsigned int value)1008 static int oid_subidentifier_encode_into(unsigned char **p,
1009 unsigned char *bound,
1010 unsigned int value)
1011 {
1012 size_t num_bytes = oid_subidentifier_num_bytes(value);
1013
1014 if ((size_t) (bound - *p) < num_bytes) {
1015 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
1016 }
1017 (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
1018 value >>= 7;
1019
1020 for (size_t i = 2; i <= num_bytes; i++) {
1021 (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
1022 value >>= 7;
1023 }
1024 *p += num_bytes;
1025
1026 return 0;
1027 }
1028
1029 /* Return the OID for the given x.y.z.... style numeric string */
mbedtls_oid_from_numeric_string(mbedtls_asn1_buf * oid,const char * oid_str,size_t size)1030 int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
1031 const char *oid_str, size_t size)
1032 {
1033 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1034 const char *str_ptr = oid_str;
1035 const char *str_bound = oid_str + size;
1036 unsigned int val = 0;
1037 unsigned int component1, component2;
1038 size_t encoded_len;
1039 unsigned char *resized_mem;
1040
1041 /* Count the number of dots to get a worst-case allocation size. */
1042 size_t num_dots = 0;
1043 for (size_t i = 0; i < size; i++) {
1044 if (oid_str[i] == '.') {
1045 num_dots++;
1046 }
1047 }
1048 /* Allocate maximum possible required memory:
1049 * There are (num_dots + 1) integer components, but the first 2 share the
1050 * same subidentifier, so we only need num_dots subidentifiers maximum. */
1051 if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
1052 return MBEDTLS_ERR_ASN1_INVALID_DATA;
1053 }
1054 /* Each byte can store 7 bits, calculate number of bytes for a
1055 * subidentifier:
1056 *
1057 * bytes = ceil(subidentifer_size * 8 / 7)
1058 */
1059 size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
1060 + 1;
1061 size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
1062 oid->p = mbedtls_calloc(max_possible_bytes, 1);
1063 if (oid->p == NULL) {
1064 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
1065 }
1066 unsigned char *out_ptr = oid->p;
1067 unsigned char *out_bound = oid->p + max_possible_bytes;
1068
1069 ret = oid_parse_number(&component1, &str_ptr, str_bound);
1070 if (ret != 0) {
1071 goto error;
1072 }
1073 if (component1 > 2) {
1074 /* First component can't be > 2 */
1075 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1076 goto error;
1077 }
1078 if (str_ptr >= str_bound || *str_ptr != '.') {
1079 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1080 goto error;
1081 }
1082 str_ptr++;
1083
1084 ret = oid_parse_number(&component2, &str_ptr, str_bound);
1085 if (ret != 0) {
1086 goto error;
1087 }
1088 if ((component1 < 2) && (component2 > 39)) {
1089 /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
1090 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1091 goto error;
1092 }
1093 if (str_ptr < str_bound) {
1094 if (*str_ptr == '.') {
1095 str_ptr++;
1096 } else {
1097 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1098 goto error;
1099 }
1100 }
1101
1102 if (component2 > (UINT_MAX - (component1 * 40))) {
1103 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1104 goto error;
1105 }
1106 ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
1107 (component1 * 40) + component2);
1108 if (ret != 0) {
1109 goto error;
1110 }
1111
1112 while (str_ptr < str_bound) {
1113 ret = oid_parse_number(&val, &str_ptr, str_bound);
1114 if (ret != 0) {
1115 goto error;
1116 }
1117 if (str_ptr < str_bound) {
1118 if (*str_ptr == '.') {
1119 str_ptr++;
1120 } else {
1121 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
1122 goto error;
1123 }
1124 }
1125
1126 ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
1127 if (ret != 0) {
1128 goto error;
1129 }
1130 }
1131
1132 encoded_len = out_ptr - oid->p;
1133 resized_mem = mbedtls_calloc(encoded_len, 1);
1134 if (resized_mem == NULL) {
1135 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
1136 goto error;
1137 }
1138 memcpy(resized_mem, oid->p, encoded_len);
1139 mbedtls_free(oid->p);
1140 oid->p = resized_mem;
1141 oid->len = encoded_len;
1142
1143 oid->tag = MBEDTLS_ASN1_OID;
1144
1145 return 0;
1146
1147 error:
1148 mbedtls_free(oid->p);
1149 oid->p = NULL;
1150 oid->len = 0;
1151 return ret;
1152 }
1153
1154 #endif /* MBEDTLS_OID_C */
1155