oid.c 27 KB

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