x509.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /*
  2. * X.509 common functions for parsing and verification
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * The ITU-T X.509 standard defines a certificate format for PKI.
  21. *
  22. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  23. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  24. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  25. *
  26. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  27. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  28. */
  29. #include "common.h"
  30. #if defined(MBEDTLS_X509_USE_C)
  31. #include "mbedtls/x509.h"
  32. #include "mbedtls/asn1.h"
  33. #include "mbedtls/error.h"
  34. #include "mbedtls/oid.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #if defined(MBEDTLS_PEM_PARSE_C)
  38. #include "mbedtls/pem.h"
  39. #endif
  40. #include "mbedtls/platform.h"
  41. #if defined(MBEDTLS_HAVE_TIME)
  42. #include "mbedtls/platform_time.h"
  43. #endif
  44. #if defined(MBEDTLS_HAVE_TIME_DATE)
  45. #include "mbedtls/platform_util.h"
  46. #include <time.h>
  47. #endif
  48. #define CHECK(code) \
  49. do { \
  50. if ((ret = (code)) != 0) { \
  51. return ret; \
  52. } \
  53. } while (0)
  54. #define CHECK_RANGE(min, max, val) \
  55. do { \
  56. if ((val) < (min) || (val) > (max)) { \
  57. return ret; \
  58. } \
  59. } while (0)
  60. /*
  61. * CertificateSerialNumber ::= INTEGER
  62. */
  63. int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
  64. mbedtls_x509_buf *serial)
  65. {
  66. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  67. if ((end - *p) < 1) {
  68. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
  69. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  70. }
  71. if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
  72. **p != MBEDTLS_ASN1_INTEGER) {
  73. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
  74. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  75. }
  76. serial->tag = *(*p)++;
  77. if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
  78. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
  79. }
  80. serial->p = *p;
  81. *p += serial->len;
  82. return 0;
  83. }
  84. /* Get an algorithm identifier without parameters (eg for signatures)
  85. *
  86. * AlgorithmIdentifier ::= SEQUENCE {
  87. * algorithm OBJECT IDENTIFIER,
  88. * parameters ANY DEFINED BY algorithm OPTIONAL }
  89. */
  90. int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
  91. mbedtls_x509_buf *alg)
  92. {
  93. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  94. if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
  95. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  96. }
  97. return 0;
  98. }
  99. /*
  100. * Parse an algorithm identifier with (optional) parameters
  101. */
  102. int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
  103. mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
  104. {
  105. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  106. if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
  107. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  108. }
  109. return 0;
  110. }
  111. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  112. /*
  113. * HashAlgorithm ::= AlgorithmIdentifier
  114. *
  115. * AlgorithmIdentifier ::= SEQUENCE {
  116. * algorithm OBJECT IDENTIFIER,
  117. * parameters ANY DEFINED BY algorithm OPTIONAL }
  118. *
  119. * For HashAlgorithm, parameters MUST be NULL or absent.
  120. */
  121. static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
  122. {
  123. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  124. unsigned char *p;
  125. const unsigned char *end;
  126. mbedtls_x509_buf md_oid;
  127. size_t len;
  128. /* Make sure we got a SEQUENCE and setup bounds */
  129. if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  130. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  131. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  132. }
  133. p = alg->p;
  134. end = p + alg->len;
  135. if (p >= end) {
  136. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  137. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  138. }
  139. /* Parse md_oid */
  140. md_oid.tag = *p;
  141. if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
  142. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  143. }
  144. md_oid.p = p;
  145. p += md_oid.len;
  146. /* Get md_alg from md_oid */
  147. if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
  148. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  149. }
  150. /* Make sure params is absent of NULL */
  151. if (p == end) {
  152. return 0;
  153. }
  154. if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
  155. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  156. }
  157. if (p != end) {
  158. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  159. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  160. }
  161. return 0;
  162. }
  163. /*
  164. * RSASSA-PSS-params ::= SEQUENCE {
  165. * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
  166. * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
  167. * saltLength [2] INTEGER DEFAULT 20,
  168. * trailerField [3] INTEGER DEFAULT 1 }
  169. * -- Note that the tags in this Sequence are explicit.
  170. *
  171. * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
  172. * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
  173. * option. Enforce this at parsing time.
  174. */
  175. int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
  176. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  177. int *salt_len)
  178. {
  179. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  180. unsigned char *p;
  181. const unsigned char *end, *end2;
  182. size_t len;
  183. mbedtls_x509_buf alg_id, alg_params;
  184. /* First set everything to defaults */
  185. *md_alg = MBEDTLS_MD_SHA1;
  186. *mgf_md = MBEDTLS_MD_SHA1;
  187. *salt_len = 20;
  188. /* Make sure params is a SEQUENCE and setup bounds */
  189. if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  190. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  191. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  192. }
  193. p = (unsigned char *) params->p;
  194. end = p + params->len;
  195. if (p == end) {
  196. return 0;
  197. }
  198. /*
  199. * HashAlgorithm
  200. */
  201. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  202. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  203. 0)) == 0) {
  204. end2 = p + len;
  205. /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
  206. if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
  207. return ret;
  208. }
  209. if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
  210. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  211. }
  212. if (p != end2) {
  213. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  214. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  215. }
  216. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  217. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  218. }
  219. if (p == end) {
  220. return 0;
  221. }
  222. /*
  223. * MaskGenAlgorithm
  224. */
  225. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  226. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  227. 1)) == 0) {
  228. end2 = p + len;
  229. /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
  230. if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
  231. return ret;
  232. }
  233. /* Only MFG1 is recognised for now */
  234. if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
  235. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
  236. MBEDTLS_ERR_OID_NOT_FOUND);
  237. }
  238. /* Parse HashAlgorithm */
  239. if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
  240. return ret;
  241. }
  242. if (p != end2) {
  243. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  244. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  245. }
  246. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  247. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  248. }
  249. if (p == end) {
  250. return 0;
  251. }
  252. /*
  253. * salt_len
  254. */
  255. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  256. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  257. 2)) == 0) {
  258. end2 = p + len;
  259. if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
  260. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  261. }
  262. if (p != end2) {
  263. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  264. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  265. }
  266. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  267. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  268. }
  269. if (p == end) {
  270. return 0;
  271. }
  272. /*
  273. * trailer_field (if present, must be 1)
  274. */
  275. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  276. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  277. 3)) == 0) {
  278. int trailer_field;
  279. end2 = p + len;
  280. if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
  281. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  282. }
  283. if (p != end2) {
  284. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  285. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  286. }
  287. if (trailer_field != 1) {
  288. return MBEDTLS_ERR_X509_INVALID_ALG;
  289. }
  290. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  291. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  292. }
  293. if (p != end) {
  294. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  295. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  296. }
  297. return 0;
  298. }
  299. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  300. /*
  301. * AttributeTypeAndValue ::= SEQUENCE {
  302. * type AttributeType,
  303. * value AttributeValue }
  304. *
  305. * AttributeType ::= OBJECT IDENTIFIER
  306. *
  307. * AttributeValue ::= ANY DEFINED BY AttributeType
  308. */
  309. static int x509_get_attr_type_value(unsigned char **p,
  310. const unsigned char *end,
  311. mbedtls_x509_name *cur)
  312. {
  313. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  314. size_t len;
  315. mbedtls_x509_buf *oid;
  316. mbedtls_x509_buf *val;
  317. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  318. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  319. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  320. }
  321. end = *p + len;
  322. if ((end - *p) < 1) {
  323. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  324. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  325. }
  326. oid = &cur->oid;
  327. oid->tag = **p;
  328. if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
  329. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  330. }
  331. oid->p = *p;
  332. *p += oid->len;
  333. if ((end - *p) < 1) {
  334. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  335. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  336. }
  337. if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
  338. **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
  339. **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
  340. **p != MBEDTLS_ASN1_BIT_STRING) {
  341. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  342. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  343. }
  344. val = &cur->val;
  345. val->tag = *(*p)++;
  346. if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
  347. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  348. }
  349. val->p = *p;
  350. *p += val->len;
  351. if (*p != end) {
  352. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  353. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  354. }
  355. cur->next = NULL;
  356. return 0;
  357. }
  358. /*
  359. * Name ::= CHOICE { -- only one possibility for now --
  360. * rdnSequence RDNSequence }
  361. *
  362. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  363. *
  364. * RelativeDistinguishedName ::=
  365. * SET OF AttributeTypeAndValue
  366. *
  367. * AttributeTypeAndValue ::= SEQUENCE {
  368. * type AttributeType,
  369. * value AttributeValue }
  370. *
  371. * AttributeType ::= OBJECT IDENTIFIER
  372. *
  373. * AttributeValue ::= ANY DEFINED BY AttributeType
  374. *
  375. * The data structure is optimized for the common case where each RDN has only
  376. * one element, which is represented as a list of AttributeTypeAndValue.
  377. * For the general case we still use a flat list, but we mark elements of the
  378. * same set so that they are "merged" together in the functions that consume
  379. * this list, eg mbedtls_x509_dn_gets().
  380. *
  381. * On success, this function may allocate a linked list starting at cur->next
  382. * that must later be free'd by the caller using mbedtls_free(). In error
  383. * cases, this function frees all allocated memory internally and the caller
  384. * has no freeing responsibilities.
  385. */
  386. int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
  387. mbedtls_x509_name *cur)
  388. {
  389. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  390. size_t set_len;
  391. const unsigned char *end_set;
  392. mbedtls_x509_name *head = cur;
  393. mbedtls_x509_name *prev, *allocated;
  394. /* don't use recursion, we'd risk stack overflow if not optimized */
  395. while (1) {
  396. /*
  397. * parse SET
  398. */
  399. if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
  400. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
  401. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  402. goto error;
  403. }
  404. end_set = *p + set_len;
  405. while (1) {
  406. if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
  407. goto error;
  408. }
  409. if (*p == end_set) {
  410. break;
  411. }
  412. /* Mark this item as being no the only one in a set */
  413. cur->next_merged = 1;
  414. cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
  415. if (cur->next == NULL) {
  416. ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
  417. goto error;
  418. }
  419. cur = cur->next;
  420. }
  421. /*
  422. * continue until end of SEQUENCE is reached
  423. */
  424. if (*p == end) {
  425. return 0;
  426. }
  427. cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
  428. if (cur->next == NULL) {
  429. ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
  430. goto error;
  431. }
  432. cur = cur->next;
  433. }
  434. error:
  435. /* Skip the first element as we did not allocate it */
  436. allocated = head->next;
  437. while (allocated != NULL) {
  438. prev = allocated;
  439. allocated = allocated->next;
  440. mbedtls_platform_zeroize(prev, sizeof(*prev));
  441. mbedtls_free(prev);
  442. }
  443. mbedtls_platform_zeroize(head, sizeof(*head));
  444. return ret;
  445. }
  446. static int x509_parse_int(unsigned char **p, size_t n, int *res)
  447. {
  448. *res = 0;
  449. for (; n > 0; --n) {
  450. if ((**p < '0') || (**p > '9')) {
  451. return MBEDTLS_ERR_X509_INVALID_DATE;
  452. }
  453. *res *= 10;
  454. *res += (*(*p)++ - '0');
  455. }
  456. return 0;
  457. }
  458. static int x509_date_is_valid(const mbedtls_x509_time *t)
  459. {
  460. int ret = MBEDTLS_ERR_X509_INVALID_DATE;
  461. int month_len;
  462. CHECK_RANGE(0, 9999, t->year);
  463. CHECK_RANGE(0, 23, t->hour);
  464. CHECK_RANGE(0, 59, t->min);
  465. CHECK_RANGE(0, 59, t->sec);
  466. switch (t->mon) {
  467. case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  468. month_len = 31;
  469. break;
  470. case 4: case 6: case 9: case 11:
  471. month_len = 30;
  472. break;
  473. case 2:
  474. if ((!(t->year % 4) && t->year % 100) ||
  475. !(t->year % 400)) {
  476. month_len = 29;
  477. } else {
  478. month_len = 28;
  479. }
  480. break;
  481. default:
  482. return ret;
  483. }
  484. CHECK_RANGE(1, month_len, t->day);
  485. return 0;
  486. }
  487. /*
  488. * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
  489. * field.
  490. */
  491. static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
  492. mbedtls_x509_time *tm)
  493. {
  494. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  495. /*
  496. * Minimum length is 10 or 12 depending on yearlen
  497. */
  498. if (len < yearlen + 8) {
  499. return MBEDTLS_ERR_X509_INVALID_DATE;
  500. }
  501. len -= yearlen + 8;
  502. /*
  503. * Parse year, month, day, hour, minute
  504. */
  505. CHECK(x509_parse_int(p, yearlen, &tm->year));
  506. if (2 == yearlen) {
  507. if (tm->year < 50) {
  508. tm->year += 100;
  509. }
  510. tm->year += 1900;
  511. }
  512. CHECK(x509_parse_int(p, 2, &tm->mon));
  513. CHECK(x509_parse_int(p, 2, &tm->day));
  514. CHECK(x509_parse_int(p, 2, &tm->hour));
  515. CHECK(x509_parse_int(p, 2, &tm->min));
  516. /*
  517. * Parse seconds if present
  518. */
  519. if (len >= 2) {
  520. CHECK(x509_parse_int(p, 2, &tm->sec));
  521. len -= 2;
  522. } else {
  523. return MBEDTLS_ERR_X509_INVALID_DATE;
  524. }
  525. /*
  526. * Parse trailing 'Z' if present
  527. */
  528. if (1 == len && 'Z' == **p) {
  529. (*p)++;
  530. len--;
  531. }
  532. /*
  533. * We should have parsed all characters at this point
  534. */
  535. if (0 != len) {
  536. return MBEDTLS_ERR_X509_INVALID_DATE;
  537. }
  538. CHECK(x509_date_is_valid(tm));
  539. return 0;
  540. }
  541. /*
  542. * Time ::= CHOICE {
  543. * utcTime UTCTime,
  544. * generalTime GeneralizedTime }
  545. */
  546. int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
  547. mbedtls_x509_time *tm)
  548. {
  549. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  550. size_t len, year_len;
  551. unsigned char tag;
  552. if ((end - *p) < 1) {
  553. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  554. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  555. }
  556. tag = **p;
  557. if (tag == MBEDTLS_ASN1_UTC_TIME) {
  558. year_len = 2;
  559. } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
  560. year_len = 4;
  561. } else {
  562. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  563. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  564. }
  565. (*p)++;
  566. ret = mbedtls_asn1_get_len(p, end, &len);
  567. if (ret != 0) {
  568. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
  569. }
  570. return x509_parse_time(p, len, year_len, tm);
  571. }
  572. int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
  573. {
  574. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  575. size_t len;
  576. int tag_type;
  577. if ((end - *p) < 1) {
  578. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
  579. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  580. }
  581. tag_type = **p;
  582. if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
  583. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
  584. }
  585. sig->tag = tag_type;
  586. sig->len = len;
  587. sig->p = *p;
  588. *p += len;
  589. return 0;
  590. }
  591. /*
  592. * Get signature algorithm from alg OID and optional parameters
  593. */
  594. int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  595. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  596. void **sig_opts)
  597. {
  598. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  599. if (*sig_opts != NULL) {
  600. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  601. }
  602. if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
  603. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
  604. }
  605. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  606. if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
  607. mbedtls_pk_rsassa_pss_options *pss_opts;
  608. pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
  609. if (pss_opts == NULL) {
  610. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  611. }
  612. ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
  613. md_alg,
  614. &pss_opts->mgf1_hash_id,
  615. &pss_opts->expected_salt_len);
  616. if (ret != 0) {
  617. mbedtls_free(pss_opts);
  618. return ret;
  619. }
  620. *sig_opts = (void *) pss_opts;
  621. } else
  622. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  623. {
  624. /* Make sure parameters are absent or NULL */
  625. if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
  626. sig_params->len != 0) {
  627. return MBEDTLS_ERR_X509_INVALID_ALG;
  628. }
  629. }
  630. return 0;
  631. }
  632. /*
  633. * X.509 Extensions (No parsing of extensions, pointer should
  634. * be either manually updated or extensions should be parsed!)
  635. */
  636. int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
  637. mbedtls_x509_buf *ext, int tag)
  638. {
  639. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  640. size_t len;
  641. /* Extension structure use EXPLICIT tagging. That is, the actual
  642. * `Extensions` structure is wrapped by a tag-length pair using
  643. * the respective context-specific tag. */
  644. ret = mbedtls_asn1_get_tag(p, end, &ext->len,
  645. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
  646. if (ret != 0) {
  647. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  648. }
  649. ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
  650. ext->p = *p;
  651. end = *p + ext->len;
  652. /*
  653. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  654. */
  655. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  656. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  657. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  658. }
  659. if (end != *p + len) {
  660. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  661. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  662. }
  663. return 0;
  664. }
  665. /*
  666. * Store the name in printable form into buf; no more
  667. * than size characters will be written
  668. */
  669. int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
  670. {
  671. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  672. size_t i, j, n;
  673. unsigned char c, merge = 0;
  674. const mbedtls_x509_name *name;
  675. const char *short_name = NULL;
  676. char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
  677. memset(s, 0, sizeof(s));
  678. name = dn;
  679. p = buf;
  680. n = size;
  681. while (name != NULL) {
  682. if (!name->oid.p) {
  683. name = name->next;
  684. continue;
  685. }
  686. if (name != dn) {
  687. ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
  688. MBEDTLS_X509_SAFE_SNPRINTF;
  689. }
  690. ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
  691. if (ret == 0) {
  692. ret = mbedtls_snprintf(p, n, "%s=", short_name);
  693. } else {
  694. ret = mbedtls_snprintf(p, n, "\?\?=");
  695. }
  696. MBEDTLS_X509_SAFE_SNPRINTF;
  697. for (i = 0, j = 0; i < name->val.len; i++, j++) {
  698. if (j >= sizeof(s) - 1) {
  699. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  700. }
  701. c = name->val.p[i];
  702. // Special characters requiring escaping, RFC 1779
  703. if (c && strchr(",=+<>#;\"\\", c)) {
  704. if (j + 1 >= sizeof(s) - 1) {
  705. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  706. }
  707. s[j++] = '\\';
  708. }
  709. if (c < 32 || c >= 127) {
  710. s[j] = '?';
  711. } else {
  712. s[j] = c;
  713. }
  714. }
  715. s[j] = '\0';
  716. ret = mbedtls_snprintf(p, n, "%s", s);
  717. MBEDTLS_X509_SAFE_SNPRINTF;
  718. merge = name->next_merged;
  719. name = name->next;
  720. }
  721. return (int) (size - n);
  722. }
  723. /*
  724. * Store the serial in printable form into buf; no more
  725. * than size characters will be written
  726. */
  727. int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
  728. {
  729. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  730. size_t i, n, nr;
  731. char *p;
  732. p = buf;
  733. n = size;
  734. nr = (serial->len <= 32)
  735. ? serial->len : 28;
  736. for (i = 0; i < nr; i++) {
  737. if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
  738. continue;
  739. }
  740. ret = mbedtls_snprintf(p, n, "%02X%s",
  741. serial->p[i], (i < nr - 1) ? ":" : "");
  742. MBEDTLS_X509_SAFE_SNPRINTF;
  743. }
  744. if (nr != serial->len) {
  745. ret = mbedtls_snprintf(p, n, "....");
  746. MBEDTLS_X509_SAFE_SNPRINTF;
  747. }
  748. return (int) (size - n);
  749. }
  750. /*
  751. * Helper for writing signature algorithms
  752. */
  753. int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
  754. mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  755. const void *sig_opts)
  756. {
  757. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  758. char *p = buf;
  759. size_t n = size;
  760. const char *desc = NULL;
  761. ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
  762. if (ret != 0) {
  763. ret = mbedtls_snprintf(p, n, "???");
  764. } else {
  765. ret = mbedtls_snprintf(p, n, "%s", desc);
  766. }
  767. MBEDTLS_X509_SAFE_SNPRINTF;
  768. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  769. if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
  770. const mbedtls_pk_rsassa_pss_options *pss_opts;
  771. const mbedtls_md_info_t *md_info, *mgf_md_info;
  772. pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
  773. md_info = mbedtls_md_info_from_type(md_alg);
  774. mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
  775. ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
  776. md_info ? mbedtls_md_get_name(md_info) : "???",
  777. mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???",
  778. (unsigned int) pss_opts->expected_salt_len);
  779. MBEDTLS_X509_SAFE_SNPRINTF;
  780. }
  781. #else
  782. ((void) pk_alg);
  783. ((void) md_alg);
  784. ((void) sig_opts);
  785. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  786. return (int) (size - n);
  787. }
  788. /*
  789. * Helper for writing "RSA key size", "EC key size", etc
  790. */
  791. int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
  792. {
  793. char *p = buf;
  794. size_t n = buf_size;
  795. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  796. ret = mbedtls_snprintf(p, n, "%s key size", name);
  797. MBEDTLS_X509_SAFE_SNPRINTF;
  798. return 0;
  799. }
  800. #if defined(MBEDTLS_HAVE_TIME_DATE)
  801. /*
  802. * Set the time structure to the current time.
  803. * Return 0 on success, non-zero on failure.
  804. */
  805. static int x509_get_current_time(mbedtls_x509_time *now)
  806. {
  807. struct tm *lt, tm_buf;
  808. mbedtls_time_t tt;
  809. int ret = 0;
  810. tt = mbedtls_time(NULL);
  811. lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
  812. if (lt == NULL) {
  813. ret = -1;
  814. } else {
  815. now->year = lt->tm_year + 1900;
  816. now->mon = lt->tm_mon + 1;
  817. now->day = lt->tm_mday;
  818. now->hour = lt->tm_hour;
  819. now->min = lt->tm_min;
  820. now->sec = lt->tm_sec;
  821. }
  822. return ret;
  823. }
  824. /*
  825. * Return 0 if before <= after, 1 otherwise
  826. */
  827. static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
  828. {
  829. if (before->year > after->year) {
  830. return 1;
  831. }
  832. if (before->year == after->year &&
  833. before->mon > after->mon) {
  834. return 1;
  835. }
  836. if (before->year == after->year &&
  837. before->mon == after->mon &&
  838. before->day > after->day) {
  839. return 1;
  840. }
  841. if (before->year == after->year &&
  842. before->mon == after->mon &&
  843. before->day == after->day &&
  844. before->hour > after->hour) {
  845. return 1;
  846. }
  847. if (before->year == after->year &&
  848. before->mon == after->mon &&
  849. before->day == after->day &&
  850. before->hour == after->hour &&
  851. before->min > after->min) {
  852. return 1;
  853. }
  854. if (before->year == after->year &&
  855. before->mon == after->mon &&
  856. before->day == after->day &&
  857. before->hour == after->hour &&
  858. before->min == after->min &&
  859. before->sec > after->sec) {
  860. return 1;
  861. }
  862. return 0;
  863. }
  864. int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
  865. {
  866. mbedtls_x509_time now;
  867. if (x509_get_current_time(&now) != 0) {
  868. return 1;
  869. }
  870. return x509_check_time(&now, to);
  871. }
  872. int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
  873. {
  874. mbedtls_x509_time now;
  875. if (x509_get_current_time(&now) != 0) {
  876. return 1;
  877. }
  878. return x509_check_time(from, &now);
  879. }
  880. #else /* MBEDTLS_HAVE_TIME_DATE */
  881. int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
  882. {
  883. ((void) to);
  884. return 0;
  885. }
  886. int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
  887. {
  888. ((void) from);
  889. return 0;
  890. }
  891. #endif /* MBEDTLS_HAVE_TIME_DATE */
  892. #if defined(MBEDTLS_SELF_TEST)
  893. #include "mbedtls/x509_crt.h"
  894. #include "mbedtls/certs.h"
  895. /*
  896. * Checkup routine
  897. */
  898. int mbedtls_x509_self_test(int verbose)
  899. {
  900. int ret = 0;
  901. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
  902. uint32_t flags;
  903. mbedtls_x509_crt cacert;
  904. mbedtls_x509_crt clicert;
  905. if (verbose != 0) {
  906. mbedtls_printf(" X.509 certificate load: ");
  907. }
  908. mbedtls_x509_crt_init(&cacert);
  909. mbedtls_x509_crt_init(&clicert);
  910. ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
  911. mbedtls_test_cli_crt_len);
  912. if (ret != 0) {
  913. if (verbose != 0) {
  914. mbedtls_printf("failed\n");
  915. }
  916. goto cleanup;
  917. }
  918. ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt,
  919. mbedtls_test_ca_crt_len);
  920. if (ret != 0) {
  921. if (verbose != 0) {
  922. mbedtls_printf("failed\n");
  923. }
  924. goto cleanup;
  925. }
  926. if (verbose != 0) {
  927. mbedtls_printf("passed\n X.509 signature verify: ");
  928. }
  929. ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL);
  930. if (ret != 0) {
  931. if (verbose != 0) {
  932. mbedtls_printf("failed\n");
  933. }
  934. goto cleanup;
  935. }
  936. if (verbose != 0) {
  937. mbedtls_printf("passed\n\n");
  938. }
  939. cleanup:
  940. mbedtls_x509_crt_free(&cacert);
  941. mbedtls_x509_crt_free(&clicert);
  942. #else
  943. ((void) verbose);
  944. #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
  945. return ret;
  946. }
  947. #endif /* MBEDTLS_SELF_TEST */
  948. #endif /* MBEDTLS_X509_USE_C */