oid.c 26 KB

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