oid.c 25 KB

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