Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedtls by
oid.c
00001 /** 00002 * \file oid.c 00003 * 00004 * \brief Object Identifier (OID) database 00005 * 00006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00007 * SPDX-License-Identifier: Apache-2.0 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00010 * not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00017 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 * 00021 * This file is part of mbed TLS (https://tls.mbed.org) 00022 */ 00023 00024 #if !defined(MBEDTLS_CONFIG_FILE) 00025 #include "mbedtls/config.h" 00026 #else 00027 #include MBEDTLS_CONFIG_FILE 00028 #endif 00029 00030 #if defined(MBEDTLS_OID_C) 00031 00032 #include "mbedtls/oid.h" 00033 #include "mbedtls/rsa.h" 00034 00035 #include <stdio.h> 00036 #include <string.h> 00037 00038 #if defined(MBEDTLS_PLATFORM_C) 00039 #include "mbedtls/platform.h" 00040 #else 00041 #define mbedtls_snprintf snprintf 00042 #endif 00043 00044 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C) 00045 #include "mbedtls/x509.h" 00046 #endif 00047 00048 /* 00049 * Macro to automatically add the size of #define'd OIDs 00050 */ 00051 #define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s) 00052 00053 /* 00054 * Macro to generate an internal function for oid_XXX_from_asn1() (used by 00055 * the other functions) 00056 */ 00057 #define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \ 00058 static const TYPE_T * oid_ ## NAME ## _from_asn1( const mbedtls_asn1_buf *oid ) \ 00059 { \ 00060 const TYPE_T *p = LIST; \ 00061 const mbedtls_oid_descriptor_t *cur = (const mbedtls_oid_descriptor_t *) p; \ 00062 if( p == NULL || oid == NULL ) return( NULL ); \ 00063 while( cur->asn1 != NULL ) { \ 00064 if( cur->asn1_len == oid->len && \ 00065 memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \ 00066 return( p ); \ 00067 } \ 00068 p++; \ 00069 cur = (const mbedtls_oid_descriptor_t *) p; \ 00070 } \ 00071 return( NULL ); \ 00072 } 00073 00074 /* 00075 * Macro to generate a function for retrieving a single attribute from the 00076 * descriptor of an mbedtls_oid_descriptor_t wrapper. 00077 */ 00078 #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \ 00079 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \ 00080 { \ 00081 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \ 00082 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \ 00083 *ATTR1 = data->descriptor.ATTR1; \ 00084 return( 0 ); \ 00085 } 00086 00087 /* 00088 * Macro to generate a function for retrieving a single attribute from an 00089 * mbedtls_oid_descriptor_t wrapper. 00090 */ 00091 #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \ 00092 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \ 00093 { \ 00094 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \ 00095 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \ 00096 *ATTR1 = data->ATTR1; \ 00097 return( 0 ); \ 00098 } 00099 00100 /* 00101 * Macro to generate a function for retrieving two attributes from an 00102 * mbedtls_oid_descriptor_t wrapper. 00103 */ 00104 #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \ 00105 ATTR2_TYPE, ATTR2) \ 00106 int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \ 00107 { \ 00108 const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \ 00109 if( data == NULL ) return( MBEDTLS_ERR_OID_NOT_FOUND ); \ 00110 *ATTR1 = data->ATTR1; \ 00111 *ATTR2 = data->ATTR2; \ 00112 return( 0 ); \ 00113 } 00114 00115 /* 00116 * Macro to generate a function for retrieving the OID based on a single 00117 * attribute from a mbedtls_oid_descriptor_t wrapper. 00118 */ 00119 #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \ 00120 int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \ 00121 { \ 00122 const TYPE_T *cur = LIST; \ 00123 while( cur->descriptor.asn1 != NULL ) { \ 00124 if( cur->ATTR1 == ATTR1 ) { \ 00125 *oid = cur->descriptor.asn1; \ 00126 *olen = cur->descriptor.asn1_len; \ 00127 return( 0 ); \ 00128 } \ 00129 cur++; \ 00130 } \ 00131 return( MBEDTLS_ERR_OID_NOT_FOUND ); \ 00132 } 00133 00134 /* 00135 * Macro to generate a function for retrieving the OID based on two 00136 * attributes from a mbedtls_oid_descriptor_t wrapper. 00137 */ 00138 #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \ 00139 ATTR2_TYPE, ATTR2) \ 00140 int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \ 00141 size_t *olen ) \ 00142 { \ 00143 const TYPE_T *cur = LIST; \ 00144 while( cur->descriptor.asn1 != NULL ) { \ 00145 if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \ 00146 *oid = cur->descriptor.asn1; \ 00147 *olen = cur->descriptor.asn1_len; \ 00148 return( 0 ); \ 00149 } \ 00150 cur++; \ 00151 } \ 00152 return( MBEDTLS_ERR_OID_NOT_FOUND ); \ 00153 } 00154 00155 /* 00156 * For X520 attribute types 00157 */ 00158 typedef struct { 00159 mbedtls_oid_descriptor_t descriptor; 00160 const char *short_name; 00161 } oid_x520_attr_t; 00162 00163 static const oid_x520_attr_t oid_x520_attr_type[] = 00164 { 00165 { 00166 { ADD_LEN( MBEDTLS_OID_AT_CN ), "id-at-commonName", "Common Name" }, 00167 "CN", 00168 }, 00169 { 00170 { ADD_LEN( MBEDTLS_OID_AT_COUNTRY ), "id-at-countryName", "Country" }, 00171 "C", 00172 }, 00173 { 00174 { ADD_LEN( MBEDTLS_OID_AT_LOCALITY ), "id-at-locality", "Locality" }, 00175 "L", 00176 }, 00177 { 00178 { ADD_LEN( MBEDTLS_OID_AT_STATE ), "id-at-state", "State" }, 00179 "ST", 00180 }, 00181 { 00182 { ADD_LEN( MBEDTLS_OID_AT_ORGANIZATION ),"id-at-organizationName", "Organization" }, 00183 "O", 00184 }, 00185 { 00186 { ADD_LEN( MBEDTLS_OID_AT_ORG_UNIT ), "id-at-organizationalUnitName", "Org Unit" }, 00187 "OU", 00188 }, 00189 { 00190 { ADD_LEN( MBEDTLS_OID_PKCS9_EMAIL ), "emailAddress", "E-mail address" }, 00191 "emailAddress", 00192 }, 00193 { 00194 { ADD_LEN( MBEDTLS_OID_AT_SERIAL_NUMBER ),"id-at-serialNumber", "Serial number" }, 00195 "serialNumber", 00196 }, 00197 { 00198 { ADD_LEN( MBEDTLS_OID_AT_POSTAL_ADDRESS ),"id-at-postalAddress", "Postal address" }, 00199 "postalAddress", 00200 }, 00201 { 00202 { ADD_LEN( MBEDTLS_OID_AT_POSTAL_CODE ), "id-at-postalCode", "Postal code" }, 00203 "postalCode", 00204 }, 00205 { 00206 { ADD_LEN( MBEDTLS_OID_AT_SUR_NAME ), "id-at-surName", "Surname" }, 00207 "SN", 00208 }, 00209 { 00210 { ADD_LEN( MBEDTLS_OID_AT_GIVEN_NAME ), "id-at-givenName", "Given name" }, 00211 "GN", 00212 }, 00213 { 00214 { ADD_LEN( MBEDTLS_OID_AT_INITIALS ), "id-at-initials", "Initials" }, 00215 "initials", 00216 }, 00217 { 00218 { ADD_LEN( MBEDTLS_OID_AT_GENERATION_QUALIFIER ), "id-at-generationQualifier", "Generation qualifier" }, 00219 "generationQualifier", 00220 }, 00221 { 00222 { ADD_LEN( MBEDTLS_OID_AT_TITLE ), "id-at-title", "Title" }, 00223 "title", 00224 }, 00225 { 00226 { ADD_LEN( MBEDTLS_OID_AT_DN_QUALIFIER ),"id-at-dnQualifier", "Distinguished Name qualifier" }, 00227 "dnQualifier", 00228 }, 00229 { 00230 { ADD_LEN( MBEDTLS_OID_AT_PSEUDONYM ), "id-at-pseudonym", "Pseudonym" }, 00231 "pseudonym", 00232 }, 00233 { 00234 { ADD_LEN( MBEDTLS_OID_DOMAIN_COMPONENT ), "id-domainComponent", "Domain component" }, 00235 "DC", 00236 }, 00237 { 00238 { ADD_LEN( MBEDTLS_OID_AT_UNIQUE_IDENTIFIER ), "id-at-uniqueIdentifier", "Unique Identifier" }, 00239 "uniqueIdentifier", 00240 }, 00241 { 00242 { NULL, 0, NULL, NULL }, 00243 NULL, 00244 } 00245 }; 00246 00247 FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type) 00248 FN_OID_GET_ATTR1(mbedtls_oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name) 00249 00250 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C) 00251 /* 00252 * For X509 extensions 00253 */ 00254 typedef struct { 00255 mbedtls_oid_descriptor_t descriptor; 00256 int ext_type; 00257 } oid_x509_ext_t; 00258 00259 static const oid_x509_ext_t oid_x509_ext[] = 00260 { 00261 { 00262 { ADD_LEN( MBEDTLS_OID_BASIC_CONSTRAINTS ), "id-ce-basicConstraints", "Basic Constraints" }, 00263 MBEDTLS_X509_EXT_BASIC_CONSTRAINTS, 00264 }, 00265 { 00266 { ADD_LEN( MBEDTLS_OID_KEY_USAGE ), "id-ce-keyUsage", "Key Usage" }, 00267 MBEDTLS_X509_EXT_KEY_USAGE, 00268 }, 00269 { 00270 { ADD_LEN( MBEDTLS_OID_EXTENDED_KEY_USAGE ), "id-ce-extKeyUsage", "Extended Key Usage" }, 00271 MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE, 00272 }, 00273 { 00274 { ADD_LEN( MBEDTLS_OID_SUBJECT_ALT_NAME ), "id-ce-subjectAltName", "Subject Alt Name" }, 00275 MBEDTLS_X509_EXT_SUBJECT_ALT_NAME, 00276 }, 00277 { 00278 { ADD_LEN( MBEDTLS_OID_NS_CERT_TYPE ), "id-netscape-certtype", "Netscape Certificate Type" }, 00279 MBEDTLS_X509_EXT_NS_CERT_TYPE, 00280 }, 00281 { 00282 { NULL, 0, NULL, NULL }, 00283 0, 00284 }, 00285 }; 00286 00287 FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext) 00288 FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type) 00289 00290 static const mbedtls_oid_descriptor_t oid_ext_key_usage[] = 00291 { 00292 { ADD_LEN( MBEDTLS_OID_SERVER_AUTH ), "id-kp-serverAuth", "TLS Web Server Authentication" }, 00293 { ADD_LEN( MBEDTLS_OID_CLIENT_AUTH ), "id-kp-clientAuth", "TLS Web Client Authentication" }, 00294 { ADD_LEN( MBEDTLS_OID_CODE_SIGNING ), "id-kp-codeSigning", "Code Signing" }, 00295 { ADD_LEN( MBEDTLS_OID_EMAIL_PROTECTION ), "id-kp-emailProtection", "E-mail Protection" }, 00296 { ADD_LEN( MBEDTLS_OID_TIME_STAMPING ), "id-kp-timeStamping", "Time Stamping" }, 00297 { ADD_LEN( MBEDTLS_OID_OCSP_SIGNING ), "id-kp-OCSPSigning", "OCSP Signing" }, 00298 { NULL, 0, NULL, NULL }, 00299 }; 00300 00301 FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage) 00302 FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage, mbedtls_oid_descriptor_t, ext_key_usage, const char *, description) 00303 #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */ 00304 00305 #if defined(MBEDTLS_MD_C) 00306 /* 00307 * For SignatureAlgorithmIdentifier 00308 */ 00309 typedef struct { 00310 mbedtls_oid_descriptor_t descriptor; 00311 mbedtls_md_type_t md_alg; 00312 mbedtls_pk_type_t pk_alg; 00313 } oid_sig_alg_t; 00314 00315 static const oid_sig_alg_t oid_sig_alg[] = 00316 { 00317 { 00318 { ADD_LEN( MBEDTLS_OID_PKCS1_MD2 ), "md2WithRSAEncryption", "RSA with MD2" }, 00319 MBEDTLS_MD_MD2, MBEDTLS_PK_RSA, 00320 }, 00321 { 00322 { ADD_LEN( MBEDTLS_OID_PKCS1_MD4 ), "md4WithRSAEncryption", "RSA with MD4" }, 00323 MBEDTLS_MD_MD4, MBEDTLS_PK_RSA, 00324 }, 00325 { 00326 { ADD_LEN( MBEDTLS_OID_PKCS1_MD5 ), "md5WithRSAEncryption", "RSA with MD5" }, 00327 MBEDTLS_MD_MD5, MBEDTLS_PK_RSA, 00328 }, 00329 { 00330 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA1 ), "sha-1WithRSAEncryption", "RSA with SHA1" }, 00331 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA, 00332 }, 00333 { 00334 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA224 ), "sha224WithRSAEncryption", "RSA with SHA-224" }, 00335 MBEDTLS_MD_SHA224, MBEDTLS_PK_RSA, 00336 }, 00337 { 00338 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA256 ), "sha256WithRSAEncryption", "RSA with SHA-256" }, 00339 MBEDTLS_MD_SHA256, MBEDTLS_PK_RSA, 00340 }, 00341 { 00342 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA384 ), "sha384WithRSAEncryption", "RSA with SHA-384" }, 00343 MBEDTLS_MD_SHA384, MBEDTLS_PK_RSA, 00344 }, 00345 { 00346 { ADD_LEN( MBEDTLS_OID_PKCS1_SHA512 ), "sha512WithRSAEncryption", "RSA with SHA-512" }, 00347 MBEDTLS_MD_SHA512, MBEDTLS_PK_RSA, 00348 }, 00349 { 00350 { ADD_LEN( MBEDTLS_OID_RSA_SHA_OBS ), "sha-1WithRSAEncryption", "RSA with SHA1" }, 00351 MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA, 00352 }, 00353 { 00354 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA1 ), "ecdsa-with-SHA1", "ECDSA with SHA1" }, 00355 MBEDTLS_MD_SHA1, MBEDTLS_PK_ECDSA, 00356 }, 00357 { 00358 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA224 ), "ecdsa-with-SHA224", "ECDSA with SHA224" }, 00359 MBEDTLS_MD_SHA224, MBEDTLS_PK_ECDSA, 00360 }, 00361 { 00362 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA256 ), "ecdsa-with-SHA256", "ECDSA with SHA256" }, 00363 MBEDTLS_MD_SHA256, MBEDTLS_PK_ECDSA, 00364 }, 00365 { 00366 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA384 ), "ecdsa-with-SHA384", "ECDSA with SHA384" }, 00367 MBEDTLS_MD_SHA384, MBEDTLS_PK_ECDSA, 00368 }, 00369 { 00370 { ADD_LEN( MBEDTLS_OID_ECDSA_SHA512 ), "ecdsa-with-SHA512", "ECDSA with SHA512" }, 00371 MBEDTLS_MD_SHA512, MBEDTLS_PK_ECDSA, 00372 }, 00373 { 00374 { ADD_LEN( MBEDTLS_OID_RSASSA_PSS ), "RSASSA-PSS", "RSASSA-PSS" }, 00375 MBEDTLS_MD_NONE, MBEDTLS_PK_RSASSA_PSS, 00376 }, 00377 { 00378 { NULL, 0, NULL, NULL }, 00379 MBEDTLS_MD_NONE, MBEDTLS_PK_NONE, 00380 }, 00381 }; 00382 00383 FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg) 00384 FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description) 00385 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) 00386 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) 00387 #endif /* MBEDTLS_MD_C */ 00388 00389 /* 00390 * For PublicKeyInfo (PKCS1, RFC 5480) 00391 */ 00392 typedef struct { 00393 mbedtls_oid_descriptor_t descriptor; 00394 mbedtls_pk_type_t pk_alg; 00395 } oid_pk_alg_t; 00396 00397 static const oid_pk_alg_t oid_pk_alg[] = 00398 { 00399 { 00400 { ADD_LEN( MBEDTLS_OID_PKCS1_RSA ), "rsaEncryption", "RSA" }, 00401 MBEDTLS_PK_RSA, 00402 }, 00403 { 00404 { ADD_LEN( MBEDTLS_OID_EC_ALG_UNRESTRICTED ), "id-ecPublicKey", "Generic EC key" }, 00405 MBEDTLS_PK_ECKEY, 00406 }, 00407 { 00408 { ADD_LEN( MBEDTLS_OID_EC_ALG_ECDH ), "id-ecDH", "EC key for ECDH" }, 00409 MBEDTLS_PK_ECKEY_DH, 00410 }, 00411 { 00412 { NULL, 0, NULL, NULL }, 00413 MBEDTLS_PK_NONE, 00414 }, 00415 }; 00416 00417 FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg) 00418 FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg) 00419 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) 00420 00421 #if defined(MBEDTLS_ECP_C) 00422 /* 00423 * For namedCurve (RFC 5480) 00424 */ 00425 typedef struct { 00426 mbedtls_oid_descriptor_t descriptor; 00427 mbedtls_ecp_group_id grp_id; 00428 } oid_ecp_grp_t; 00429 00430 static const oid_ecp_grp_t oid_ecp_grp[] = 00431 { 00432 { 00433 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192R1 ), "secp192r1", "secp192r1" }, 00434 MBEDTLS_ECP_DP_SECP192R1 , 00435 }, 00436 { 00437 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224R1 ), "secp224r1", "secp224r1" }, 00438 MBEDTLS_ECP_DP_SECP224R1 , 00439 }, 00440 { 00441 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256R1 ), "secp256r1", "secp256r1" }, 00442 MBEDTLS_ECP_DP_SECP256R1 , 00443 }, 00444 { 00445 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP384R1 ), "secp384r1", "secp384r1" }, 00446 MBEDTLS_ECP_DP_SECP384R1 , 00447 }, 00448 { 00449 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP521R1 ), "secp521r1", "secp521r1" }, 00450 MBEDTLS_ECP_DP_SECP521R1 , 00451 }, 00452 { 00453 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP192K1 ), "secp192k1", "secp192k1" }, 00454 MBEDTLS_ECP_DP_SECP192K1 , 00455 }, 00456 { 00457 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP224K1 ), "secp224k1", "secp224k1" }, 00458 MBEDTLS_ECP_DP_SECP224K1 , 00459 }, 00460 { 00461 { ADD_LEN( MBEDTLS_OID_EC_GRP_SECP256K1 ), "secp256k1", "secp256k1" }, 00462 MBEDTLS_ECP_DP_SECP256K1 , 00463 }, 00464 { 00465 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP256R1 ), "brainpoolP256r1","brainpool256r1" }, 00466 MBEDTLS_ECP_DP_BP256R1 , 00467 }, 00468 { 00469 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP384R1 ), "brainpoolP384r1","brainpool384r1" }, 00470 MBEDTLS_ECP_DP_BP384R1 , 00471 }, 00472 { 00473 { ADD_LEN( MBEDTLS_OID_EC_GRP_BP512R1 ), "brainpoolP512r1","brainpool512r1" }, 00474 MBEDTLS_ECP_DP_BP512R1 , 00475 }, 00476 { 00477 { NULL, 0, NULL, NULL }, 00478 MBEDTLS_ECP_DP_NONE, 00479 }, 00480 }; 00481 00482 FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp) 00483 FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id) 00484 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) 00485 #endif /* MBEDTLS_ECP_C */ 00486 00487 #if defined(MBEDTLS_CIPHER_C) 00488 /* 00489 * For PKCS#5 PBES2 encryption algorithm 00490 */ 00491 typedef struct { 00492 mbedtls_oid_descriptor_t descriptor; 00493 mbedtls_cipher_type_t cipher_alg; 00494 } oid_cipher_alg_t; 00495 00496 static const oid_cipher_alg_t oid_cipher_alg[] = 00497 { 00498 { 00499 { ADD_LEN( MBEDTLS_OID_DES_CBC ), "desCBC", "DES-CBC" }, 00500 MBEDTLS_CIPHER_DES_CBC, 00501 }, 00502 { 00503 { ADD_LEN( MBEDTLS_OID_DES_EDE3_CBC ), "des-ede3-cbc", "DES-EDE3-CBC" }, 00504 MBEDTLS_CIPHER_DES_EDE3_CBC, 00505 }, 00506 { 00507 { NULL, 0, NULL, NULL }, 00508 MBEDTLS_CIPHER_NONE, 00509 }, 00510 }; 00511 00512 FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg) 00513 FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, mbedtls_cipher_type_t, cipher_alg) 00514 #endif /* MBEDTLS_CIPHER_C */ 00515 00516 #if defined(MBEDTLS_MD_C) 00517 /* 00518 * For digestAlgorithm 00519 */ 00520 typedef struct { 00521 mbedtls_oid_descriptor_t descriptor; 00522 mbedtls_md_type_t md_alg; 00523 } oid_md_alg_t; 00524 00525 static const oid_md_alg_t oid_md_alg[] = 00526 { 00527 { 00528 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD2 ), "id-md2", "MD2" }, 00529 MBEDTLS_MD_MD2, 00530 }, 00531 { 00532 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD4 ), "id-md4", "MD4" }, 00533 MBEDTLS_MD_MD4, 00534 }, 00535 { 00536 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_MD5 ), "id-md5", "MD5" }, 00537 MBEDTLS_MD_MD5, 00538 }, 00539 { 00540 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA1 ), "id-sha1", "SHA-1" }, 00541 MBEDTLS_MD_SHA1, 00542 }, 00543 { 00544 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA224 ), "id-sha224", "SHA-224" }, 00545 MBEDTLS_MD_SHA224, 00546 }, 00547 { 00548 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA256 ), "id-sha256", "SHA-256" }, 00549 MBEDTLS_MD_SHA256, 00550 }, 00551 { 00552 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA384 ), "id-sha384", "SHA-384" }, 00553 MBEDTLS_MD_SHA384, 00554 }, 00555 { 00556 { ADD_LEN( MBEDTLS_OID_DIGEST_ALG_SHA512 ), "id-sha512", "SHA-512" }, 00557 MBEDTLS_MD_SHA512, 00558 }, 00559 { 00560 { NULL, 0, NULL, NULL }, 00561 MBEDTLS_MD_NONE, 00562 }, 00563 }; 00564 00565 FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg) 00566 FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg) 00567 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) 00568 #endif /* MBEDTLS_MD_C */ 00569 00570 #if defined(MBEDTLS_PKCS12_C) 00571 /* 00572 * For PKCS#12 PBEs 00573 */ 00574 typedef struct { 00575 mbedtls_oid_descriptor_t descriptor; 00576 mbedtls_md_type_t md_alg; 00577 mbedtls_cipher_type_t cipher_alg; 00578 } oid_pkcs12_pbe_alg_t; 00579 00580 static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] = 00581 { 00582 { 00583 { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC ), "pbeWithSHAAnd3-KeyTripleDES-CBC", "PBE with SHA1 and 3-Key 3DES" }, 00584 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE3_CBC, 00585 }, 00586 { 00587 { ADD_LEN( MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC ), "pbeWithSHAAnd2-KeyTripleDES-CBC", "PBE with SHA1 and 2-Key 3DES" }, 00588 MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE_CBC, 00589 }, 00590 { 00591 { NULL, 0, NULL, NULL }, 00592 MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE, 00593 }, 00594 }; 00595 00596 FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg) 00597 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) 00598 #endif /* MBEDTLS_PKCS12_C */ 00599 00600 #define OID_SAFE_SNPRINTF \ 00601 do { \ 00602 if( ret < 0 || (size_t) ret >= n ) \ 00603 return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \ 00604 \ 00605 n -= (size_t) ret; \ 00606 p += (size_t) ret; \ 00607 } while( 0 ) 00608 00609 /* Return the x.y.z.... style numeric string for the given OID */ 00610 int mbedtls_oid_get_numeric_string( char *buf, size_t size, 00611 const mbedtls_asn1_buf *oid ) 00612 { 00613 int ret; 00614 size_t i, n; 00615 unsigned int value; 00616 char *p; 00617 00618 p = buf; 00619 n = size; 00620 00621 /* First byte contains first two dots */ 00622 if( oid->len > 0 ) 00623 { 00624 ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 ); 00625 OID_SAFE_SNPRINTF; 00626 } 00627 00628 value = 0; 00629 for( i = 1; i < oid->len; i++ ) 00630 { 00631 /* Prevent overflow in value. */ 00632 if( ( ( value << 7 ) >> 7 ) != value ) 00633 return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); 00634 00635 value <<= 7; 00636 value += oid->p[i] & 0x7F; 00637 00638 if( !( oid->p[i] & 0x80 ) ) 00639 { 00640 /* Last byte */ 00641 ret = mbedtls_snprintf( p, n, ".%d", value ); 00642 OID_SAFE_SNPRINTF; 00643 value = 0; 00644 } 00645 } 00646 00647 return( (int) ( size - n ) ); 00648 } 00649 00650 #endif /* MBEDTLS_OID_C */
Generated on Tue Jul 12 2022 12:52:45 by
