oid.c 25 KB

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