oid.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 OR GPL-2.0-or-later
  8. */
  9. #include "common.h"
  10. #if defined(MBEDTLS_OID_C)
  11. #include "mbedtls/oid.h"
  12. #include "mbedtls/rsa.h"
  13. #include "mbedtls/error.h"
  14. #include "mbedtls/pk.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "mbedtls/platform.h"
  18. /*
  19. * Macro to automatically add the size of #define'd OIDs
  20. */
  21. #define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s)
  22. /*
  23. * Macro to generate mbedtls_oid_descriptor_t
  24. */
  25. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  26. #define OID_DESCRIPTOR(s, name, description) { ADD_LEN(s), name, description }
  27. #define NULL_OID_DESCRIPTOR { NULL, 0, NULL, NULL }
  28. #else
  29. #define OID_DESCRIPTOR(s, name, description) { ADD_LEN(s) }
  30. #define NULL_OID_DESCRIPTOR { NULL, 0 }
  31. #endif
  32. /*
  33. * Macro to generate an internal function for oid_XXX_from_asn1() (used by
  34. * the other functions)
  35. */
  36. #define FN_OID_TYPED_FROM_ASN1(TYPE_T, NAME, LIST) \
  37. static const TYPE_T *oid_ ## NAME ## _from_asn1( \
  38. const mbedtls_asn1_buf *oid) \
  39. { \
  40. const TYPE_T *p = (LIST); \
  41. const mbedtls_oid_descriptor_t *cur = \
  42. (const mbedtls_oid_descriptor_t *) p; \
  43. if (p == NULL || oid == NULL) return NULL; \
  44. while (cur->asn1 != NULL) { \
  45. if (cur->asn1_len == oid->len && \
  46. memcmp(cur->asn1, oid->p, oid->len) == 0) { \
  47. return p; \
  48. } \
  49. p++; \
  50. cur = (const mbedtls_oid_descriptor_t *) p; \
  51. } \
  52. return NULL; \
  53. }
  54. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  55. /*
  56. * Macro to generate a function for retrieving a single attribute from the
  57. * descriptor of an mbedtls_oid_descriptor_t wrapper.
  58. */
  59. #define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
  60. int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1) \
  61. { \
  62. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1(oid); \
  63. if (data == NULL) return MBEDTLS_ERR_OID_NOT_FOUND; \
  64. *ATTR1 = data->descriptor.ATTR1; \
  65. return 0; \
  66. }
  67. #endif /* MBEDTLS_X509_REMOVE_INFO */
  68. /*
  69. * Macro to generate a function for retrieving a single attribute from an
  70. * mbedtls_oid_descriptor_t wrapper.
  71. */
  72. #define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
  73. int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1) \
  74. { \
  75. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1(oid); \
  76. if (data == NULL) return MBEDTLS_ERR_OID_NOT_FOUND; \
  77. *ATTR1 = data->ATTR1; \
  78. return 0; \
  79. }
  80. /*
  81. * Macro to generate a function for retrieving two attributes from an
  82. * mbedtls_oid_descriptor_t wrapper.
  83. */
  84. #define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
  85. ATTR2_TYPE, ATTR2) \
  86. int FN_NAME(const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1, \
  87. ATTR2_TYPE * ATTR2) \
  88. { \
  89. const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1(oid); \
  90. if (data == NULL) return MBEDTLS_ERR_OID_NOT_FOUND; \
  91. *(ATTR1) = data->ATTR1; \
  92. *(ATTR2) = data->ATTR2; \
  93. return 0; \
  94. }
  95. /*
  96. * Macro to generate a function for retrieving the OID based on a single
  97. * attribute from a mbedtls_oid_descriptor_t wrapper.
  98. */
  99. #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \
  100. int FN_NAME(ATTR1_TYPE ATTR1, const char **oid, size_t *olen) \
  101. { \
  102. const TYPE_T *cur = (LIST); \
  103. while (cur->descriptor.asn1 != NULL) { \
  104. if (cur->ATTR1 == (ATTR1)) { \
  105. *oid = cur->descriptor.asn1; \
  106. *olen = cur->descriptor.asn1_len; \
  107. return 0; \
  108. } \
  109. cur++; \
  110. } \
  111. return MBEDTLS_ERR_OID_NOT_FOUND; \
  112. }
  113. /*
  114. * Macro to generate a function for retrieving the OID based on two
  115. * attributes from a mbedtls_oid_descriptor_t wrapper.
  116. */
  117. #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \
  118. ATTR2_TYPE, ATTR2) \
  119. int FN_NAME(ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid, \
  120. size_t *olen) \
  121. { \
  122. const TYPE_T *cur = (LIST); \
  123. while (cur->descriptor.asn1 != NULL) { \
  124. if (cur->ATTR1 == (ATTR1) && cur->ATTR2 == (ATTR2)) { \
  125. *oid = cur->descriptor.asn1; \
  126. *olen = cur->descriptor.asn1_len; \
  127. return 0; \
  128. } \
  129. cur++; \
  130. } \
  131. return MBEDTLS_ERR_OID_NOT_FOUND; \
  132. }
  133. /*
  134. * For X520 attribute types
  135. */
  136. typedef struct {
  137. mbedtls_oid_descriptor_t descriptor;
  138. const char *short_name;
  139. } oid_x520_attr_t;
  140. static const oid_x520_attr_t oid_x520_attr_type[] =
  141. {
  142. {
  143. OID_DESCRIPTOR(MBEDTLS_OID_AT_CN, "id-at-commonName", "Common Name"),
  144. "CN",
  145. },
  146. {
  147. OID_DESCRIPTOR(MBEDTLS_OID_AT_COUNTRY, "id-at-countryName", "Country"),
  148. "C",
  149. },
  150. {
  151. OID_DESCRIPTOR(MBEDTLS_OID_AT_LOCALITY, "id-at-locality", "Locality"),
  152. "L",
  153. },
  154. {
  155. OID_DESCRIPTOR(MBEDTLS_OID_AT_STATE, "id-at-state", "State"),
  156. "ST",
  157. },
  158. {
  159. OID_DESCRIPTOR(MBEDTLS_OID_AT_ORGANIZATION, "id-at-organizationName",
  160. "Organization"),
  161. "O",
  162. },
  163. {
  164. OID_DESCRIPTOR(MBEDTLS_OID_AT_ORG_UNIT, "id-at-organizationalUnitName", "Org Unit"),
  165. "OU",
  166. },
  167. {
  168. OID_DESCRIPTOR(MBEDTLS_OID_PKCS9_EMAIL,
  169. "emailAddress",
  170. "E-mail address"),
  171. "emailAddress",
  172. },
  173. {
  174. OID_DESCRIPTOR(MBEDTLS_OID_AT_SERIAL_NUMBER,
  175. "id-at-serialNumber",
  176. "Serial number"),
  177. "serialNumber",
  178. },
  179. {
  180. OID_DESCRIPTOR(MBEDTLS_OID_AT_POSTAL_ADDRESS,
  181. "id-at-postalAddress",
  182. "Postal address"),
  183. "postalAddress",
  184. },
  185. {
  186. OID_DESCRIPTOR(MBEDTLS_OID_AT_POSTAL_CODE, "id-at-postalCode", "Postal code"),
  187. "postalCode",
  188. },
  189. {
  190. OID_DESCRIPTOR(MBEDTLS_OID_AT_SUR_NAME, "id-at-surName", "Surname"),
  191. "SN",
  192. },
  193. {
  194. OID_DESCRIPTOR(MBEDTLS_OID_AT_GIVEN_NAME, "id-at-givenName", "Given name"),
  195. "GN",
  196. },
  197. {
  198. OID_DESCRIPTOR(MBEDTLS_OID_AT_INITIALS, "id-at-initials", "Initials"),
  199. "initials",
  200. },
  201. {
  202. OID_DESCRIPTOR(MBEDTLS_OID_AT_GENERATION_QUALIFIER,
  203. "id-at-generationQualifier",
  204. "Generation qualifier"),
  205. "generationQualifier",
  206. },
  207. {
  208. OID_DESCRIPTOR(MBEDTLS_OID_AT_TITLE, "id-at-title", "Title"),
  209. "title",
  210. },
  211. {
  212. OID_DESCRIPTOR(MBEDTLS_OID_AT_DN_QUALIFIER,
  213. "id-at-dnQualifier",
  214. "Distinguished Name qualifier"),
  215. "dnQualifier",
  216. },
  217. {
  218. OID_DESCRIPTOR(MBEDTLS_OID_AT_PSEUDONYM, "id-at-pseudonym", "Pseudonym"),
  219. "pseudonym",
  220. },
  221. {
  222. OID_DESCRIPTOR(MBEDTLS_OID_UID, "id-uid", "User Id"),
  223. "uid",
  224. },
  225. {
  226. OID_DESCRIPTOR(MBEDTLS_OID_DOMAIN_COMPONENT,
  227. "id-domainComponent",
  228. "Domain component"),
  229. "DC",
  230. },
  231. {
  232. OID_DESCRIPTOR(MBEDTLS_OID_AT_UNIQUE_IDENTIFIER,
  233. "id-at-uniqueIdentifier",
  234. "Unique Identifier"),
  235. "uniqueIdentifier",
  236. },
  237. {
  238. NULL_OID_DESCRIPTOR,
  239. NULL,
  240. }
  241. };
  242. FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type)
  243. FN_OID_GET_ATTR1(mbedtls_oid_get_attr_short_name,
  244. oid_x520_attr_t,
  245. x520_attr,
  246. const char *,
  247. short_name)
  248. /*
  249. * For X509 extensions
  250. */
  251. typedef struct {
  252. mbedtls_oid_descriptor_t descriptor;
  253. int ext_type;
  254. } oid_x509_ext_t;
  255. static const oid_x509_ext_t oid_x509_ext[] =
  256. {
  257. {
  258. OID_DESCRIPTOR(MBEDTLS_OID_BASIC_CONSTRAINTS,
  259. "id-ce-basicConstraints",
  260. "Basic Constraints"),
  261. MBEDTLS_OID_X509_EXT_BASIC_CONSTRAINTS,
  262. },
  263. {
  264. OID_DESCRIPTOR(MBEDTLS_OID_KEY_USAGE, "id-ce-keyUsage", "Key Usage"),
  265. MBEDTLS_OID_X509_EXT_KEY_USAGE,
  266. },
  267. {
  268. OID_DESCRIPTOR(MBEDTLS_OID_EXTENDED_KEY_USAGE,
  269. "id-ce-extKeyUsage",
  270. "Extended Key Usage"),
  271. MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE,
  272. },
  273. {
  274. OID_DESCRIPTOR(MBEDTLS_OID_SUBJECT_ALT_NAME,
  275. "id-ce-subjectAltName",
  276. "Subject Alt Name"),
  277. MBEDTLS_OID_X509_EXT_SUBJECT_ALT_NAME,
  278. },
  279. {
  280. OID_DESCRIPTOR(MBEDTLS_OID_NS_CERT_TYPE,
  281. "id-netscape-certtype",
  282. "Netscape Certificate Type"),
  283. MBEDTLS_OID_X509_EXT_NS_CERT_TYPE,
  284. },
  285. {
  286. OID_DESCRIPTOR(MBEDTLS_OID_CERTIFICATE_POLICIES,
  287. "id-ce-certificatePolicies",
  288. "Certificate Policies"),
  289. MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES,
  290. },
  291. {
  292. OID_DESCRIPTOR(MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
  293. "id-ce-subjectKeyIdentifier",
  294. "Subject Key Identifier"),
  295. MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER,
  296. },
  297. {
  298. OID_DESCRIPTOR(MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
  299. "id-ce-authorityKeyIdentifier",
  300. "Authority Key Identifier"),
  301. MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER,
  302. },
  303. {
  304. NULL_OID_DESCRIPTOR,
  305. 0,
  306. },
  307. };
  308. FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
  309. FN_OID_GET_ATTR1(mbedtls_oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type)
  310. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  311. static const mbedtls_oid_descriptor_t oid_ext_key_usage[] =
  312. {
  313. OID_DESCRIPTOR(MBEDTLS_OID_SERVER_AUTH,
  314. "id-kp-serverAuth",
  315. "TLS Web Server Authentication"),
  316. OID_DESCRIPTOR(MBEDTLS_OID_CLIENT_AUTH,
  317. "id-kp-clientAuth",
  318. "TLS Web Client Authentication"),
  319. OID_DESCRIPTOR(MBEDTLS_OID_CODE_SIGNING, "id-kp-codeSigning", "Code Signing"),
  320. OID_DESCRIPTOR(MBEDTLS_OID_EMAIL_PROTECTION, "id-kp-emailProtection", "E-mail Protection"),
  321. OID_DESCRIPTOR(MBEDTLS_OID_TIME_STAMPING, "id-kp-timeStamping", "Time Stamping"),
  322. OID_DESCRIPTOR(MBEDTLS_OID_OCSP_SIGNING, "id-kp-OCSPSigning", "OCSP Signing"),
  323. OID_DESCRIPTOR(MBEDTLS_OID_WISUN_FAN,
  324. "id-kp-wisun-fan-device",
  325. "Wi-SUN Alliance Field Area Network (FAN)"),
  326. NULL_OID_DESCRIPTOR,
  327. };
  328. FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage)
  329. FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage,
  330. mbedtls_oid_descriptor_t,
  331. ext_key_usage,
  332. const char *,
  333. description)
  334. static const mbedtls_oid_descriptor_t oid_certificate_policies[] =
  335. {
  336. OID_DESCRIPTOR(MBEDTLS_OID_ANY_POLICY, "anyPolicy", "Any Policy"),
  337. NULL_OID_DESCRIPTOR,
  338. };
  339. FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, certificate_policies, oid_certificate_policies)
  340. FN_OID_GET_ATTR1(mbedtls_oid_get_certificate_policies,
  341. mbedtls_oid_descriptor_t,
  342. certificate_policies,
  343. const char *,
  344. description)
  345. #endif /* MBEDTLS_X509_REMOVE_INFO */
  346. /*
  347. * For SignatureAlgorithmIdentifier
  348. */
  349. typedef struct {
  350. mbedtls_oid_descriptor_t descriptor;
  351. mbedtls_md_type_t md_alg;
  352. mbedtls_pk_type_t pk_alg;
  353. } oid_sig_alg_t;
  354. static const oid_sig_alg_t oid_sig_alg[] =
  355. {
  356. #if defined(MBEDTLS_RSA_C)
  357. #if defined(MBEDTLS_MD_CAN_MD5)
  358. {
  359. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_MD5, "md5WithRSAEncryption", "RSA with MD5"),
  360. MBEDTLS_MD_MD5, MBEDTLS_PK_RSA,
  361. },
  362. #endif /* MBEDTLS_MD_CAN_MD5 */
  363. #if defined(MBEDTLS_MD_CAN_SHA1)
  364. {
  365. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA1, "sha-1WithRSAEncryption", "RSA with SHA1"),
  366. MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
  367. },
  368. #endif /* MBEDTLS_MD_CAN_SHA1 */
  369. #if defined(MBEDTLS_MD_CAN_SHA224)
  370. {
  371. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA224, "sha224WithRSAEncryption",
  372. "RSA with SHA-224"),
  373. MBEDTLS_MD_SHA224, MBEDTLS_PK_RSA,
  374. },
  375. #endif /* MBEDTLS_MD_CAN_SHA224 */
  376. #if defined(MBEDTLS_MD_CAN_SHA256)
  377. {
  378. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA256, "sha256WithRSAEncryption",
  379. "RSA with SHA-256"),
  380. MBEDTLS_MD_SHA256, MBEDTLS_PK_RSA,
  381. },
  382. #endif /* MBEDTLS_MD_CAN_SHA256 */
  383. #if defined(MBEDTLS_MD_CAN_SHA384)
  384. {
  385. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA384, "sha384WithRSAEncryption",
  386. "RSA with SHA-384"),
  387. MBEDTLS_MD_SHA384, MBEDTLS_PK_RSA,
  388. },
  389. #endif /* MBEDTLS_MD_CAN_SHA384 */
  390. #if defined(MBEDTLS_MD_CAN_SHA512)
  391. {
  392. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_SHA512, "sha512WithRSAEncryption",
  393. "RSA with SHA-512"),
  394. MBEDTLS_MD_SHA512, MBEDTLS_PK_RSA,
  395. },
  396. #endif /* MBEDTLS_MD_CAN_SHA512 */
  397. #if defined(MBEDTLS_MD_CAN_SHA1)
  398. {
  399. OID_DESCRIPTOR(MBEDTLS_OID_RSA_SHA_OBS, "sha-1WithRSAEncryption", "RSA with SHA1"),
  400. MBEDTLS_MD_SHA1, MBEDTLS_PK_RSA,
  401. },
  402. #endif /* MBEDTLS_MD_CAN_SHA1 */
  403. #endif /* MBEDTLS_RSA_C */
  404. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  405. #if defined(MBEDTLS_MD_CAN_SHA1)
  406. {
  407. OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA1, "ecdsa-with-SHA1", "ECDSA with SHA1"),
  408. MBEDTLS_MD_SHA1, MBEDTLS_PK_ECDSA,
  409. },
  410. #endif /* MBEDTLS_MD_CAN_SHA1 */
  411. #if defined(MBEDTLS_MD_CAN_SHA224)
  412. {
  413. OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA224, "ecdsa-with-SHA224", "ECDSA with SHA224"),
  414. MBEDTLS_MD_SHA224, MBEDTLS_PK_ECDSA,
  415. },
  416. #endif
  417. #if defined(MBEDTLS_MD_CAN_SHA256)
  418. {
  419. OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA256, "ecdsa-with-SHA256", "ECDSA with SHA256"),
  420. MBEDTLS_MD_SHA256, MBEDTLS_PK_ECDSA,
  421. },
  422. #endif /* MBEDTLS_MD_CAN_SHA256 */
  423. #if defined(MBEDTLS_MD_CAN_SHA384)
  424. {
  425. OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA384, "ecdsa-with-SHA384", "ECDSA with SHA384"),
  426. MBEDTLS_MD_SHA384, MBEDTLS_PK_ECDSA,
  427. },
  428. #endif /* MBEDTLS_MD_CAN_SHA384 */
  429. #if defined(MBEDTLS_MD_CAN_SHA512)
  430. {
  431. OID_DESCRIPTOR(MBEDTLS_OID_ECDSA_SHA512, "ecdsa-with-SHA512", "ECDSA with SHA512"),
  432. MBEDTLS_MD_SHA512, MBEDTLS_PK_ECDSA,
  433. },
  434. #endif /* MBEDTLS_MD_CAN_SHA512 */
  435. #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
  436. #if defined(MBEDTLS_RSA_C)
  437. {
  438. OID_DESCRIPTOR(MBEDTLS_OID_RSASSA_PSS, "RSASSA-PSS", "RSASSA-PSS"),
  439. MBEDTLS_MD_NONE, MBEDTLS_PK_RSASSA_PSS,
  440. },
  441. #endif /* MBEDTLS_RSA_C */
  442. {
  443. NULL_OID_DESCRIPTOR,
  444. MBEDTLS_MD_NONE, MBEDTLS_PK_NONE,
  445. },
  446. };
  447. FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg)
  448. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  449. FN_OID_GET_DESCRIPTOR_ATTR1(mbedtls_oid_get_sig_alg_desc,
  450. oid_sig_alg_t,
  451. sig_alg,
  452. const char *,
  453. description)
  454. #endif
  455. FN_OID_GET_ATTR2(mbedtls_oid_get_sig_alg,
  456. oid_sig_alg_t,
  457. sig_alg,
  458. mbedtls_md_type_t,
  459. md_alg,
  460. mbedtls_pk_type_t,
  461. pk_alg)
  462. FN_OID_GET_OID_BY_ATTR2(mbedtls_oid_get_oid_by_sig_alg,
  463. oid_sig_alg_t,
  464. oid_sig_alg,
  465. mbedtls_pk_type_t,
  466. pk_alg,
  467. mbedtls_md_type_t,
  468. md_alg)
  469. /*
  470. * For PublicKeyInfo (PKCS1, RFC 5480)
  471. */
  472. typedef struct {
  473. mbedtls_oid_descriptor_t descriptor;
  474. mbedtls_pk_type_t pk_alg;
  475. } oid_pk_alg_t;
  476. static const oid_pk_alg_t oid_pk_alg[] =
  477. {
  478. {
  479. OID_DESCRIPTOR(MBEDTLS_OID_PKCS1_RSA, "rsaEncryption", "RSA"),
  480. MBEDTLS_PK_RSA,
  481. },
  482. {
  483. OID_DESCRIPTOR(MBEDTLS_OID_EC_ALG_UNRESTRICTED, "id-ecPublicKey", "Generic EC key"),
  484. MBEDTLS_PK_ECKEY,
  485. },
  486. {
  487. OID_DESCRIPTOR(MBEDTLS_OID_EC_ALG_ECDH, "id-ecDH", "EC key for ECDH"),
  488. MBEDTLS_PK_ECKEY_DH,
  489. },
  490. {
  491. NULL_OID_DESCRIPTOR,
  492. MBEDTLS_PK_NONE,
  493. },
  494. };
  495. FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg)
  496. FN_OID_GET_ATTR1(mbedtls_oid_get_pk_alg, oid_pk_alg_t, pk_alg, mbedtls_pk_type_t, pk_alg)
  497. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_pk_alg,
  498. oid_pk_alg_t,
  499. oid_pk_alg,
  500. mbedtls_pk_type_t,
  501. pk_alg)
  502. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  503. /*
  504. * For elliptic curves that use namedCurve inside ECParams (RFC 5480)
  505. */
  506. typedef struct {
  507. mbedtls_oid_descriptor_t descriptor;
  508. mbedtls_ecp_group_id grp_id;
  509. } oid_ecp_grp_t;
  510. static const oid_ecp_grp_t oid_ecp_grp[] =
  511. {
  512. #if defined(MBEDTLS_ECP_HAVE_SECP192R1)
  513. {
  514. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP192R1, "secp192r1", "secp192r1"),
  515. MBEDTLS_ECP_DP_SECP192R1,
  516. },
  517. #endif /* MBEDTLS_ECP_HAVE_SECP192R1 */
  518. #if defined(MBEDTLS_ECP_HAVE_SECP224R1)
  519. {
  520. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP224R1, "secp224r1", "secp224r1"),
  521. MBEDTLS_ECP_DP_SECP224R1,
  522. },
  523. #endif /* MBEDTLS_ECP_HAVE_SECP224R1 */
  524. #if defined(MBEDTLS_ECP_HAVE_SECP256R1)
  525. {
  526. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP256R1, "secp256r1", "secp256r1"),
  527. MBEDTLS_ECP_DP_SECP256R1,
  528. },
  529. #endif /* MBEDTLS_ECP_HAVE_SECP256R1 */
  530. #if defined(MBEDTLS_ECP_HAVE_SECP384R1)
  531. {
  532. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP384R1, "secp384r1", "secp384r1"),
  533. MBEDTLS_ECP_DP_SECP384R1,
  534. },
  535. #endif /* MBEDTLS_ECP_HAVE_SECP384R1 */
  536. #if defined(MBEDTLS_ECP_HAVE_SECP521R1)
  537. {
  538. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP521R1, "secp521r1", "secp521r1"),
  539. MBEDTLS_ECP_DP_SECP521R1,
  540. },
  541. #endif /* MBEDTLS_ECP_HAVE_SECP521R1 */
  542. #if defined(MBEDTLS_ECP_HAVE_SECP192K1)
  543. {
  544. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP192K1, "secp192k1", "secp192k1"),
  545. MBEDTLS_ECP_DP_SECP192K1,
  546. },
  547. #endif /* MBEDTLS_ECP_HAVE_SECP192K1 */
  548. #if defined(MBEDTLS_ECP_HAVE_SECP224K1)
  549. {
  550. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP224K1, "secp224k1", "secp224k1"),
  551. MBEDTLS_ECP_DP_SECP224K1,
  552. },
  553. #endif /* MBEDTLS_ECP_HAVE_SECP224K1 */
  554. #if defined(MBEDTLS_ECP_HAVE_SECP256K1)
  555. {
  556. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_SECP256K1, "secp256k1", "secp256k1"),
  557. MBEDTLS_ECP_DP_SECP256K1,
  558. },
  559. #endif /* MBEDTLS_ECP_HAVE_SECP256K1 */
  560. #if defined(MBEDTLS_ECP_HAVE_BP256R1)
  561. {
  562. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP256R1, "brainpoolP256r1", "brainpool256r1"),
  563. MBEDTLS_ECP_DP_BP256R1,
  564. },
  565. #endif /* MBEDTLS_ECP_HAVE_BP256R1 */
  566. #if defined(MBEDTLS_ECP_HAVE_BP384R1)
  567. {
  568. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP384R1, "brainpoolP384r1", "brainpool384r1"),
  569. MBEDTLS_ECP_DP_BP384R1,
  570. },
  571. #endif /* MBEDTLS_ECP_HAVE_BP384R1 */
  572. #if defined(MBEDTLS_ECP_HAVE_BP512R1)
  573. {
  574. OID_DESCRIPTOR(MBEDTLS_OID_EC_GRP_BP512R1, "brainpoolP512r1", "brainpool512r1"),
  575. MBEDTLS_ECP_DP_BP512R1,
  576. },
  577. #endif /* MBEDTLS_ECP_HAVE_BP512R1 */
  578. {
  579. NULL_OID_DESCRIPTOR,
  580. MBEDTLS_ECP_DP_NONE,
  581. },
  582. };
  583. FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_t, grp_id, oid_ecp_grp)
  584. FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp, oid_ecp_grp_t, grp_id, mbedtls_ecp_group_id, grp_id)
  585. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp,
  586. oid_ecp_grp_t,
  587. oid_ecp_grp,
  588. mbedtls_ecp_group_id,
  589. grp_id)
  590. /*
  591. * For Elliptic Curve algorithms that are directly
  592. * encoded in the AlgorithmIdentifier (RFC 8410)
  593. */
  594. typedef struct {
  595. mbedtls_oid_descriptor_t descriptor;
  596. mbedtls_ecp_group_id grp_id;
  597. } oid_ecp_grp_algid_t;
  598. static const oid_ecp_grp_algid_t oid_ecp_grp_algid[] =
  599. {
  600. #if defined(MBEDTLS_ECP_HAVE_CURVE25519)
  601. {
  602. OID_DESCRIPTOR(MBEDTLS_OID_X25519, "X25519", "X25519"),
  603. MBEDTLS_ECP_DP_CURVE25519,
  604. },
  605. #endif /* MBEDTLS_ECP_HAVE_CURVE25519 */
  606. #if defined(MBEDTLS_ECP_HAVE_CURVE448)
  607. {
  608. OID_DESCRIPTOR(MBEDTLS_OID_X448, "X448", "X448"),
  609. MBEDTLS_ECP_DP_CURVE448,
  610. },
  611. #endif /* MBEDTLS_ECP_HAVE_CURVE448 */
  612. {
  613. NULL_OID_DESCRIPTOR,
  614. MBEDTLS_ECP_DP_NONE,
  615. },
  616. };
  617. FN_OID_TYPED_FROM_ASN1(oid_ecp_grp_algid_t, grp_id_algid, oid_ecp_grp_algid)
  618. FN_OID_GET_ATTR1(mbedtls_oid_get_ec_grp_algid,
  619. oid_ecp_grp_algid_t,
  620. grp_id_algid,
  621. mbedtls_ecp_group_id,
  622. grp_id)
  623. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_ec_grp_algid,
  624. oid_ecp_grp_algid_t,
  625. oid_ecp_grp_algid,
  626. mbedtls_ecp_group_id,
  627. grp_id)
  628. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  629. #if defined(MBEDTLS_CIPHER_C)
  630. /*
  631. * For PKCS#5 PBES2 encryption algorithm
  632. */
  633. typedef struct {
  634. mbedtls_oid_descriptor_t descriptor;
  635. mbedtls_cipher_type_t cipher_alg;
  636. } oid_cipher_alg_t;
  637. static const oid_cipher_alg_t oid_cipher_alg[] =
  638. {
  639. {
  640. OID_DESCRIPTOR(MBEDTLS_OID_DES_CBC, "desCBC", "DES-CBC"),
  641. MBEDTLS_CIPHER_DES_CBC,
  642. },
  643. {
  644. OID_DESCRIPTOR(MBEDTLS_OID_DES_EDE3_CBC, "des-ede3-cbc", "DES-EDE3-CBC"),
  645. MBEDTLS_CIPHER_DES_EDE3_CBC,
  646. },
  647. {
  648. OID_DESCRIPTOR(MBEDTLS_OID_AES_128_CBC, "aes128-cbc", "AES128-CBC"),
  649. MBEDTLS_CIPHER_AES_128_CBC,
  650. },
  651. {
  652. OID_DESCRIPTOR(MBEDTLS_OID_AES_192_CBC, "aes192-cbc", "AES192-CBC"),
  653. MBEDTLS_CIPHER_AES_192_CBC,
  654. },
  655. {
  656. OID_DESCRIPTOR(MBEDTLS_OID_AES_256_CBC, "aes256-cbc", "AES256-CBC"),
  657. MBEDTLS_CIPHER_AES_256_CBC,
  658. },
  659. {
  660. NULL_OID_DESCRIPTOR,
  661. MBEDTLS_CIPHER_NONE,
  662. },
  663. };
  664. FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg)
  665. FN_OID_GET_ATTR1(mbedtls_oid_get_cipher_alg,
  666. oid_cipher_alg_t,
  667. cipher_alg,
  668. mbedtls_cipher_type_t,
  669. cipher_alg)
  670. #endif /* MBEDTLS_CIPHER_C */
  671. /*
  672. * For digestAlgorithm
  673. */
  674. typedef struct {
  675. mbedtls_oid_descriptor_t descriptor;
  676. mbedtls_md_type_t md_alg;
  677. } oid_md_alg_t;
  678. static const oid_md_alg_t oid_md_alg[] =
  679. {
  680. #if defined(MBEDTLS_MD_CAN_MD5)
  681. {
  682. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_MD5, "id-md5", "MD5"),
  683. MBEDTLS_MD_MD5,
  684. },
  685. #endif
  686. #if defined(MBEDTLS_MD_CAN_SHA1)
  687. {
  688. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA1, "id-sha1", "SHA-1"),
  689. MBEDTLS_MD_SHA1,
  690. },
  691. #endif
  692. #if defined(MBEDTLS_MD_CAN_SHA224)
  693. {
  694. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA224, "id-sha224", "SHA-224"),
  695. MBEDTLS_MD_SHA224,
  696. },
  697. #endif
  698. #if defined(MBEDTLS_MD_CAN_SHA256)
  699. {
  700. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA256, "id-sha256", "SHA-256"),
  701. MBEDTLS_MD_SHA256,
  702. },
  703. #endif
  704. #if defined(MBEDTLS_MD_CAN_SHA384)
  705. {
  706. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA384, "id-sha384", "SHA-384"),
  707. MBEDTLS_MD_SHA384,
  708. },
  709. #endif
  710. #if defined(MBEDTLS_MD_CAN_SHA512)
  711. {
  712. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA512, "id-sha512", "SHA-512"),
  713. MBEDTLS_MD_SHA512,
  714. },
  715. #endif
  716. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  717. {
  718. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_RIPEMD160, "id-ripemd160", "RIPEMD-160"),
  719. MBEDTLS_MD_RIPEMD160,
  720. },
  721. #endif
  722. #if defined(MBEDTLS_MD_CAN_SHA3_224)
  723. {
  724. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_224, "id-sha3-224", "SHA-3-224"),
  725. MBEDTLS_MD_SHA3_224,
  726. },
  727. #endif
  728. #if defined(MBEDTLS_MD_CAN_SHA3_256)
  729. {
  730. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_256, "id-sha3-256", "SHA-3-256"),
  731. MBEDTLS_MD_SHA3_256,
  732. },
  733. #endif
  734. #if defined(MBEDTLS_MD_CAN_SHA3_384)
  735. {
  736. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_384, "id-sha3-384", "SHA-3-384"),
  737. MBEDTLS_MD_SHA3_384,
  738. },
  739. #endif
  740. #if defined(MBEDTLS_MD_CAN_SHA3_512)
  741. {
  742. OID_DESCRIPTOR(MBEDTLS_OID_DIGEST_ALG_SHA3_512, "id-sha3-512", "SHA-3-512"),
  743. MBEDTLS_MD_SHA3_512,
  744. },
  745. #endif
  746. {
  747. NULL_OID_DESCRIPTOR,
  748. MBEDTLS_MD_NONE,
  749. },
  750. };
  751. FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
  752. FN_OID_GET_ATTR1(mbedtls_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
  753. FN_OID_GET_OID_BY_ATTR1(mbedtls_oid_get_oid_by_md,
  754. oid_md_alg_t,
  755. oid_md_alg,
  756. mbedtls_md_type_t,
  757. md_alg)
  758. /*
  759. * For HMAC digestAlgorithm
  760. */
  761. typedef struct {
  762. mbedtls_oid_descriptor_t descriptor;
  763. mbedtls_md_type_t md_hmac;
  764. } oid_md_hmac_t;
  765. static const oid_md_hmac_t oid_md_hmac[] =
  766. {
  767. #if defined(MBEDTLS_MD_CAN_SHA1)
  768. {
  769. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA1, "hmacSHA1", "HMAC-SHA-1"),
  770. MBEDTLS_MD_SHA1,
  771. },
  772. #endif /* MBEDTLS_MD_CAN_SHA1 */
  773. #if defined(MBEDTLS_MD_CAN_SHA224)
  774. {
  775. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA224, "hmacSHA224", "HMAC-SHA-224"),
  776. MBEDTLS_MD_SHA224,
  777. },
  778. #endif /* MBEDTLS_MD_CAN_SHA224 */
  779. #if defined(MBEDTLS_MD_CAN_SHA256)
  780. {
  781. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA256, "hmacSHA256", "HMAC-SHA-256"),
  782. MBEDTLS_MD_SHA256,
  783. },
  784. #endif /* MBEDTLS_MD_CAN_SHA256 */
  785. #if defined(MBEDTLS_MD_CAN_SHA384)
  786. {
  787. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA384, "hmacSHA384", "HMAC-SHA-384"),
  788. MBEDTLS_MD_SHA384,
  789. },
  790. #endif /* MBEDTLS_MD_CAN_SHA384 */
  791. #if defined(MBEDTLS_MD_CAN_SHA512)
  792. {
  793. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA512, "hmacSHA512", "HMAC-SHA-512"),
  794. MBEDTLS_MD_SHA512,
  795. },
  796. #endif /* MBEDTLS_MD_CAN_SHA512 */
  797. #if defined(MBEDTLS_MD_CAN_SHA3_224)
  798. {
  799. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_224, "hmacSHA3-224", "HMAC-SHA3-224"),
  800. MBEDTLS_MD_SHA3_224,
  801. },
  802. #endif /* MBEDTLS_MD_CAN_SHA3_224 */
  803. #if defined(MBEDTLS_MD_CAN_SHA3_256)
  804. {
  805. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_256, "hmacSHA3-256", "HMAC-SHA3-256"),
  806. MBEDTLS_MD_SHA3_256,
  807. },
  808. #endif /* MBEDTLS_MD_CAN_SHA3_256 */
  809. #if defined(MBEDTLS_MD_CAN_SHA3_384)
  810. {
  811. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_384, "hmacSHA3-384", "HMAC-SHA3-384"),
  812. MBEDTLS_MD_SHA3_384,
  813. },
  814. #endif /* MBEDTLS_MD_CAN_SHA3_384 */
  815. #if defined(MBEDTLS_MD_CAN_SHA3_512)
  816. {
  817. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_SHA3_512, "hmacSHA3-512", "HMAC-SHA3-512"),
  818. MBEDTLS_MD_SHA3_512,
  819. },
  820. #endif /* MBEDTLS_MD_CAN_SHA3_512 */
  821. #if defined(MBEDTLS_MD_CAN_RIPEMD160)
  822. {
  823. OID_DESCRIPTOR(MBEDTLS_OID_HMAC_RIPEMD160, "hmacRIPEMD160", "HMAC-RIPEMD160"),
  824. MBEDTLS_MD_RIPEMD160,
  825. },
  826. #endif /* MBEDTLS_MD_CAN_RIPEMD160 */
  827. {
  828. NULL_OID_DESCRIPTOR,
  829. MBEDTLS_MD_NONE,
  830. },
  831. };
  832. FN_OID_TYPED_FROM_ASN1(oid_md_hmac_t, md_hmac, oid_md_hmac)
  833. FN_OID_GET_ATTR1(mbedtls_oid_get_md_hmac, oid_md_hmac_t, md_hmac, mbedtls_md_type_t, md_hmac)
  834. #if defined(MBEDTLS_PKCS12_C) && defined(MBEDTLS_CIPHER_C)
  835. /*
  836. * For PKCS#12 PBEs
  837. */
  838. typedef struct {
  839. mbedtls_oid_descriptor_t descriptor;
  840. mbedtls_md_type_t md_alg;
  841. mbedtls_cipher_type_t cipher_alg;
  842. } oid_pkcs12_pbe_alg_t;
  843. static const oid_pkcs12_pbe_alg_t oid_pkcs12_pbe_alg[] =
  844. {
  845. {
  846. OID_DESCRIPTOR(MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC,
  847. "pbeWithSHAAnd3-KeyTripleDES-CBC",
  848. "PBE with SHA1 and 3-Key 3DES"),
  849. MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE3_CBC,
  850. },
  851. {
  852. OID_DESCRIPTOR(MBEDTLS_OID_PKCS12_PBE_SHA1_DES2_EDE_CBC,
  853. "pbeWithSHAAnd2-KeyTripleDES-CBC",
  854. "PBE with SHA1 and 2-Key 3DES"),
  855. MBEDTLS_MD_SHA1, MBEDTLS_CIPHER_DES_EDE_CBC,
  856. },
  857. {
  858. NULL_OID_DESCRIPTOR,
  859. MBEDTLS_MD_NONE, MBEDTLS_CIPHER_NONE,
  860. },
  861. };
  862. FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
  863. FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg,
  864. oid_pkcs12_pbe_alg_t,
  865. pkcs12_pbe_alg,
  866. mbedtls_md_type_t,
  867. md_alg,
  868. mbedtls_cipher_type_t,
  869. cipher_alg)
  870. #endif /* MBEDTLS_PKCS12_C && MBEDTLS_CIPHER_C */
  871. /* Return the x.y.z.... style numeric string for the given OID */
  872. int mbedtls_oid_get_numeric_string(char *buf, size_t size,
  873. const mbedtls_asn1_buf *oid)
  874. {
  875. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  876. char *p = buf;
  877. size_t n = size;
  878. unsigned int value = 0;
  879. if (size > INT_MAX) {
  880. /* Avoid overflow computing return value */
  881. return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  882. }
  883. if (oid->len <= 0) {
  884. /* OID must not be empty */
  885. return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
  886. }
  887. for (size_t i = 0; i < oid->len; i++) {
  888. /* Prevent overflow in value. */
  889. if (value > (UINT_MAX >> 7)) {
  890. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  891. }
  892. if ((value == 0) && ((oid->p[i]) == 0x80)) {
  893. /* Overlong encoding is not allowed */
  894. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  895. }
  896. value <<= 7;
  897. value |= oid->p[i] & 0x7F;
  898. if (!(oid->p[i] & 0x80)) {
  899. /* Last byte */
  900. if (n == size) {
  901. int component1;
  902. unsigned int component2;
  903. /* First subidentifier contains first two OID components */
  904. if (value >= 80) {
  905. component1 = '2';
  906. component2 = value - 80;
  907. } else if (value >= 40) {
  908. component1 = '1';
  909. component2 = value - 40;
  910. } else {
  911. component1 = '0';
  912. component2 = value;
  913. }
  914. ret = mbedtls_snprintf(p, n, "%c.%u", component1, component2);
  915. } else {
  916. ret = mbedtls_snprintf(p, n, ".%u", value);
  917. }
  918. if (ret < 2 || (size_t) ret >= n) {
  919. return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
  920. }
  921. n -= (size_t) ret;
  922. p += ret;
  923. value = 0;
  924. }
  925. }
  926. if (value != 0) {
  927. /* Unterminated subidentifier */
  928. return MBEDTLS_ERR_ASN1_OUT_OF_DATA;
  929. }
  930. return (int) (size - n);
  931. }
  932. static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
  933. {
  934. int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  935. *num = 0;
  936. while (*p < bound && **p >= '0' && **p <= '9') {
  937. ret = 0;
  938. if (*num > (UINT_MAX / 10)) {
  939. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  940. }
  941. *num *= 10;
  942. *num += **p - '0';
  943. (*p)++;
  944. }
  945. return ret;
  946. }
  947. static size_t oid_subidentifier_num_bytes(unsigned int value)
  948. {
  949. size_t num_bytes = 0;
  950. do {
  951. value >>= 7;
  952. num_bytes++;
  953. } while (value != 0);
  954. return num_bytes;
  955. }
  956. static int oid_subidentifier_encode_into(unsigned char **p,
  957. unsigned char *bound,
  958. unsigned int value)
  959. {
  960. size_t num_bytes = oid_subidentifier_num_bytes(value);
  961. if ((size_t) (bound - *p) < num_bytes) {
  962. return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
  963. }
  964. (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
  965. value >>= 7;
  966. for (size_t i = 2; i <= num_bytes; i++) {
  967. (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
  968. value >>= 7;
  969. }
  970. *p += num_bytes;
  971. return 0;
  972. }
  973. /* Return the OID for the given x.y.z.... style numeric string */
  974. int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
  975. const char *oid_str, size_t size)
  976. {
  977. int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  978. const char *str_ptr = oid_str;
  979. const char *str_bound = oid_str + size;
  980. unsigned int val = 0;
  981. unsigned int component1, component2;
  982. size_t encoded_len;
  983. unsigned char *resized_mem;
  984. /* Count the number of dots to get a worst-case allocation size. */
  985. size_t num_dots = 0;
  986. for (size_t i = 0; i < size; i++) {
  987. if (oid_str[i] == '.') {
  988. num_dots++;
  989. }
  990. }
  991. /* Allocate maximum possible required memory:
  992. * There are (num_dots + 1) integer components, but the first 2 share the
  993. * same subidentifier, so we only need num_dots subidentifiers maximum. */
  994. if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
  995. return MBEDTLS_ERR_ASN1_INVALID_DATA;
  996. }
  997. /* Each byte can store 7 bits, calculate number of bytes for a
  998. * subidentifier:
  999. *
  1000. * bytes = ceil(subidentifer_size * 8 / 7)
  1001. */
  1002. size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
  1003. + 1;
  1004. size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
  1005. oid->p = mbedtls_calloc(max_possible_bytes, 1);
  1006. if (oid->p == NULL) {
  1007. return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
  1008. }
  1009. unsigned char *out_ptr = oid->p;
  1010. unsigned char *out_bound = oid->p + max_possible_bytes;
  1011. ret = oid_parse_number(&component1, &str_ptr, str_bound);
  1012. if (ret != 0) {
  1013. goto error;
  1014. }
  1015. if (component1 > 2) {
  1016. /* First component can't be > 2 */
  1017. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1018. goto error;
  1019. }
  1020. if (str_ptr >= str_bound || *str_ptr != '.') {
  1021. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1022. goto error;
  1023. }
  1024. str_ptr++;
  1025. ret = oid_parse_number(&component2, &str_ptr, str_bound);
  1026. if (ret != 0) {
  1027. goto error;
  1028. }
  1029. if ((component1 < 2) && (component2 > 39)) {
  1030. /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
  1031. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1032. goto error;
  1033. }
  1034. if (str_ptr < str_bound) {
  1035. if (*str_ptr == '.') {
  1036. str_ptr++;
  1037. } else {
  1038. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1039. goto error;
  1040. }
  1041. }
  1042. if (component2 > (UINT_MAX - (component1 * 40))) {
  1043. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1044. goto error;
  1045. }
  1046. ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
  1047. (component1 * 40) + component2);
  1048. if (ret != 0) {
  1049. goto error;
  1050. }
  1051. while (str_ptr < str_bound) {
  1052. ret = oid_parse_number(&val, &str_ptr, str_bound);
  1053. if (ret != 0) {
  1054. goto error;
  1055. }
  1056. if (str_ptr < str_bound) {
  1057. if (*str_ptr == '.') {
  1058. str_ptr++;
  1059. } else {
  1060. ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
  1061. goto error;
  1062. }
  1063. }
  1064. ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
  1065. if (ret != 0) {
  1066. goto error;
  1067. }
  1068. }
  1069. encoded_len = (size_t) (out_ptr - oid->p);
  1070. resized_mem = mbedtls_calloc(encoded_len, 1);
  1071. if (resized_mem == NULL) {
  1072. ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
  1073. goto error;
  1074. }
  1075. memcpy(resized_mem, oid->p, encoded_len);
  1076. mbedtls_free(oid->p);
  1077. oid->p = resized_mem;
  1078. oid->len = encoded_len;
  1079. oid->tag = MBEDTLS_ASN1_OID;
  1080. return 0;
  1081. error:
  1082. mbedtls_free(oid->p);
  1083. oid->p = NULL;
  1084. oid->len = 0;
  1085. return ret;
  1086. }
  1087. #endif /* MBEDTLS_OID_C */