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