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