x509.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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. #include "mbedtls/legacy_or_psa.h"
  49. #define CHECK(code) if ((ret = (code)) != 0) { return ret; }
  50. #define CHECK_RANGE(min, max, val) \
  51. do \
  52. { \
  53. if ((val) < (min) || (val) > (max)) \
  54. { \
  55. return ret; \
  56. } \
  57. } while (0)
  58. /*
  59. * CertificateSerialNumber ::= INTEGER
  60. */
  61. int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
  62. mbedtls_x509_buf *serial)
  63. {
  64. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  65. if ((end - *p) < 1) {
  66. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
  67. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  68. }
  69. if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
  70. **p != MBEDTLS_ASN1_INTEGER) {
  71. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
  72. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  73. }
  74. serial->tag = *(*p)++;
  75. if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
  76. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
  77. }
  78. serial->p = *p;
  79. *p += serial->len;
  80. return 0;
  81. }
  82. /* Get an algorithm identifier without parameters (eg for signatures)
  83. *
  84. * AlgorithmIdentifier ::= SEQUENCE {
  85. * algorithm OBJECT IDENTIFIER,
  86. * parameters ANY DEFINED BY algorithm OPTIONAL }
  87. */
  88. int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
  89. mbedtls_x509_buf *alg)
  90. {
  91. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  92. if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
  93. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  94. }
  95. return 0;
  96. }
  97. /*
  98. * Parse an algorithm identifier with (optional) parameters
  99. */
  100. int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
  101. mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
  102. {
  103. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  104. if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
  105. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Convert md type to string
  111. */
  112. static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
  113. {
  114. switch (md_alg) {
  115. #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA)
  116. case MBEDTLS_MD_MD5:
  117. return "MD5";
  118. #endif
  119. #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
  120. case MBEDTLS_MD_SHA1:
  121. return "SHA1";
  122. #endif
  123. #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA)
  124. case MBEDTLS_MD_SHA224:
  125. return "SHA224";
  126. #endif
  127. #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA)
  128. case MBEDTLS_MD_SHA256:
  129. return "SHA256";
  130. #endif
  131. #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA)
  132. case MBEDTLS_MD_SHA384:
  133. return "SHA384";
  134. #endif
  135. #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA)
  136. case MBEDTLS_MD_SHA512:
  137. return "SHA512";
  138. #endif
  139. #if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA)
  140. case MBEDTLS_MD_RIPEMD160:
  141. return "RIPEMD160";
  142. #endif
  143. case MBEDTLS_MD_NONE:
  144. return NULL;
  145. default:
  146. return NULL;
  147. }
  148. }
  149. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  150. /*
  151. * HashAlgorithm ::= AlgorithmIdentifier
  152. *
  153. * AlgorithmIdentifier ::= SEQUENCE {
  154. * algorithm OBJECT IDENTIFIER,
  155. * parameters ANY DEFINED BY algorithm OPTIONAL }
  156. *
  157. * For HashAlgorithm, parameters MUST be NULL or absent.
  158. */
  159. static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
  160. {
  161. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  162. unsigned char *p;
  163. const unsigned char *end;
  164. mbedtls_x509_buf md_oid;
  165. size_t len;
  166. /* Make sure we got a SEQUENCE and setup bounds */
  167. if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  168. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  169. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  170. }
  171. p = alg->p;
  172. end = p + alg->len;
  173. if (p >= end) {
  174. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  175. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  176. }
  177. /* Parse md_oid */
  178. md_oid.tag = *p;
  179. if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
  180. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  181. }
  182. md_oid.p = p;
  183. p += md_oid.len;
  184. /* Get md_alg from md_oid */
  185. if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
  186. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  187. }
  188. /* Make sure params is absent of NULL */
  189. if (p == end) {
  190. return 0;
  191. }
  192. if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
  193. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  194. }
  195. if (p != end) {
  196. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  197. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  198. }
  199. return 0;
  200. }
  201. /*
  202. * RSASSA-PSS-params ::= SEQUENCE {
  203. * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
  204. * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
  205. * saltLength [2] INTEGER DEFAULT 20,
  206. * trailerField [3] INTEGER DEFAULT 1 }
  207. * -- Note that the tags in this Sequence are explicit.
  208. *
  209. * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
  210. * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
  211. * option. Enforce this at parsing time.
  212. */
  213. int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
  214. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  215. int *salt_len)
  216. {
  217. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  218. unsigned char *p;
  219. const unsigned char *end, *end2;
  220. size_t len;
  221. mbedtls_x509_buf alg_id, alg_params;
  222. /* First set everything to defaults */
  223. *md_alg = MBEDTLS_MD_SHA1;
  224. *mgf_md = MBEDTLS_MD_SHA1;
  225. *salt_len = 20;
  226. /* Make sure params is a SEQUENCE and setup bounds */
  227. if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  228. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  229. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  230. }
  231. p = (unsigned char *) params->p;
  232. end = p + params->len;
  233. if (p == end) {
  234. return 0;
  235. }
  236. /*
  237. * HashAlgorithm
  238. */
  239. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  240. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  241. 0)) == 0) {
  242. end2 = p + len;
  243. /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
  244. if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
  245. return ret;
  246. }
  247. if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
  248. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  249. }
  250. if (p != end2) {
  251. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  252. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  253. }
  254. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  255. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  256. }
  257. if (p == end) {
  258. return 0;
  259. }
  260. /*
  261. * MaskGenAlgorithm
  262. */
  263. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  264. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  265. 1)) == 0) {
  266. end2 = p + len;
  267. /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
  268. if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
  269. return ret;
  270. }
  271. /* Only MFG1 is recognised for now */
  272. if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
  273. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
  274. MBEDTLS_ERR_OID_NOT_FOUND);
  275. }
  276. /* Parse HashAlgorithm */
  277. if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
  278. return ret;
  279. }
  280. if (p != end2) {
  281. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  282. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  283. }
  284. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  285. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  286. }
  287. if (p == end) {
  288. return 0;
  289. }
  290. /*
  291. * salt_len
  292. */
  293. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  294. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  295. 2)) == 0) {
  296. end2 = p + len;
  297. if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
  298. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  299. }
  300. if (p != end2) {
  301. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  302. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  303. }
  304. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  305. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  306. }
  307. if (p == end) {
  308. return 0;
  309. }
  310. /*
  311. * trailer_field (if present, must be 1)
  312. */
  313. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  314. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
  315. 3)) == 0) {
  316. int trailer_field;
  317. end2 = p + len;
  318. if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
  319. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  320. }
  321. if (p != end2) {
  322. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  323. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  324. }
  325. if (trailer_field != 1) {
  326. return MBEDTLS_ERR_X509_INVALID_ALG;
  327. }
  328. } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  329. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
  330. }
  331. if (p != end) {
  332. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
  333. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  334. }
  335. return 0;
  336. }
  337. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  338. /*
  339. * AttributeTypeAndValue ::= SEQUENCE {
  340. * type AttributeType,
  341. * value AttributeValue }
  342. *
  343. * AttributeType ::= OBJECT IDENTIFIER
  344. *
  345. * AttributeValue ::= ANY DEFINED BY AttributeType
  346. */
  347. static int x509_get_attr_type_value(unsigned char **p,
  348. const unsigned char *end,
  349. mbedtls_x509_name *cur)
  350. {
  351. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  352. size_t len;
  353. mbedtls_x509_buf *oid;
  354. mbedtls_x509_buf *val;
  355. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  356. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  357. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  358. }
  359. end = *p + len;
  360. if ((end - *p) < 1) {
  361. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  362. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  363. }
  364. oid = &cur->oid;
  365. oid->tag = **p;
  366. if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
  367. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  368. }
  369. oid->p = *p;
  370. *p += oid->len;
  371. if ((end - *p) < 1) {
  372. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  373. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  374. }
  375. if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
  376. **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
  377. **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
  378. **p != MBEDTLS_ASN1_BIT_STRING) {
  379. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  380. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  381. }
  382. val = &cur->val;
  383. val->tag = *(*p)++;
  384. if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
  385. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  386. }
  387. val->p = *p;
  388. *p += val->len;
  389. if (*p != end) {
  390. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
  391. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  392. }
  393. cur->next = NULL;
  394. return 0;
  395. }
  396. /*
  397. * Name ::= CHOICE { -- only one possibility for now --
  398. * rdnSequence RDNSequence }
  399. *
  400. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  401. *
  402. * RelativeDistinguishedName ::=
  403. * SET OF AttributeTypeAndValue
  404. *
  405. * AttributeTypeAndValue ::= SEQUENCE {
  406. * type AttributeType,
  407. * value AttributeValue }
  408. *
  409. * AttributeType ::= OBJECT IDENTIFIER
  410. *
  411. * AttributeValue ::= ANY DEFINED BY AttributeType
  412. *
  413. * The data structure is optimized for the common case where each RDN has only
  414. * one element, which is represented as a list of AttributeTypeAndValue.
  415. * For the general case we still use a flat list, but we mark elements of the
  416. * same set so that they are "merged" together in the functions that consume
  417. * this list, eg mbedtls_x509_dn_gets().
  418. *
  419. * On success, this function may allocate a linked list starting at cur->next
  420. * that must later be free'd by the caller using mbedtls_free(). In error
  421. * cases, this function frees all allocated memory internally and the caller
  422. * has no freeing responsibilities.
  423. */
  424. int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
  425. mbedtls_x509_name *cur)
  426. {
  427. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  428. size_t set_len;
  429. const unsigned char *end_set;
  430. mbedtls_x509_name *head = cur;
  431. /* don't use recursion, we'd risk stack overflow if not optimized */
  432. while (1) {
  433. /*
  434. * parse SET
  435. */
  436. if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
  437. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
  438. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
  439. goto error;
  440. }
  441. end_set = *p + set_len;
  442. while (1) {
  443. if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
  444. goto error;
  445. }
  446. if (*p == end_set) {
  447. break;
  448. }
  449. /* Mark this item as being no the only one in a set */
  450. cur->next_merged = 1;
  451. cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
  452. if (cur->next == NULL) {
  453. ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
  454. goto error;
  455. }
  456. cur = cur->next;
  457. }
  458. /*
  459. * continue until end of SEQUENCE is reached
  460. */
  461. if (*p == end) {
  462. return 0;
  463. }
  464. cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
  465. if (cur->next == NULL) {
  466. ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
  467. goto error;
  468. }
  469. cur = cur->next;
  470. }
  471. error:
  472. /* Skip the first element as we did not allocate it */
  473. mbedtls_asn1_free_named_data_list_shallow(head->next);
  474. head->next = NULL;
  475. return ret;
  476. }
  477. static int x509_parse_int(unsigned char **p, size_t n, int *res)
  478. {
  479. *res = 0;
  480. for (; n > 0; --n) {
  481. if ((**p < '0') || (**p > '9')) {
  482. return MBEDTLS_ERR_X509_INVALID_DATE;
  483. }
  484. *res *= 10;
  485. *res += (*(*p)++ - '0');
  486. }
  487. return 0;
  488. }
  489. static int x509_date_is_valid(const mbedtls_x509_time *t)
  490. {
  491. int ret = MBEDTLS_ERR_X509_INVALID_DATE;
  492. int month_len;
  493. CHECK_RANGE(0, 9999, t->year);
  494. CHECK_RANGE(0, 23, t->hour);
  495. CHECK_RANGE(0, 59, t->min);
  496. CHECK_RANGE(0, 59, t->sec);
  497. switch (t->mon) {
  498. case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  499. month_len = 31;
  500. break;
  501. case 4: case 6: case 9: case 11:
  502. month_len = 30;
  503. break;
  504. case 2:
  505. if ((!(t->year % 4) && t->year % 100) ||
  506. !(t->year % 400)) {
  507. month_len = 29;
  508. } else {
  509. month_len = 28;
  510. }
  511. break;
  512. default:
  513. return ret;
  514. }
  515. CHECK_RANGE(1, month_len, t->day);
  516. return 0;
  517. }
  518. /*
  519. * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
  520. * field.
  521. */
  522. static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
  523. mbedtls_x509_time *tm)
  524. {
  525. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  526. /*
  527. * Minimum length is 10 or 12 depending on yearlen
  528. */
  529. if (len < yearlen + 8) {
  530. return MBEDTLS_ERR_X509_INVALID_DATE;
  531. }
  532. len -= yearlen + 8;
  533. /*
  534. * Parse year, month, day, hour, minute
  535. */
  536. CHECK(x509_parse_int(p, yearlen, &tm->year));
  537. if (2 == yearlen) {
  538. if (tm->year < 50) {
  539. tm->year += 100;
  540. }
  541. tm->year += 1900;
  542. }
  543. CHECK(x509_parse_int(p, 2, &tm->mon));
  544. CHECK(x509_parse_int(p, 2, &tm->day));
  545. CHECK(x509_parse_int(p, 2, &tm->hour));
  546. CHECK(x509_parse_int(p, 2, &tm->min));
  547. /*
  548. * Parse seconds if present
  549. */
  550. if (len >= 2) {
  551. CHECK(x509_parse_int(p, 2, &tm->sec));
  552. len -= 2;
  553. } else {
  554. return MBEDTLS_ERR_X509_INVALID_DATE;
  555. }
  556. /*
  557. * Parse trailing 'Z' if present
  558. */
  559. if (1 == len && 'Z' == **p) {
  560. (*p)++;
  561. len--;
  562. }
  563. /*
  564. * We should have parsed all characters at this point
  565. */
  566. if (0 != len) {
  567. return MBEDTLS_ERR_X509_INVALID_DATE;
  568. }
  569. CHECK(x509_date_is_valid(tm));
  570. return 0;
  571. }
  572. /*
  573. * Time ::= CHOICE {
  574. * utcTime UTCTime,
  575. * generalTime GeneralizedTime }
  576. */
  577. int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
  578. mbedtls_x509_time *tm)
  579. {
  580. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  581. size_t len, year_len;
  582. unsigned char tag;
  583. if ((end - *p) < 1) {
  584. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  585. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  586. }
  587. tag = **p;
  588. if (tag == MBEDTLS_ASN1_UTC_TIME) {
  589. year_len = 2;
  590. } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
  591. year_len = 4;
  592. } else {
  593. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
  594. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  595. }
  596. (*p)++;
  597. ret = mbedtls_asn1_get_len(p, end, &len);
  598. if (ret != 0) {
  599. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
  600. }
  601. return x509_parse_time(p, len, year_len, tm);
  602. }
  603. int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
  604. {
  605. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  606. size_t len;
  607. int tag_type;
  608. if ((end - *p) < 1) {
  609. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
  610. MBEDTLS_ERR_ASN1_OUT_OF_DATA);
  611. }
  612. tag_type = **p;
  613. if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
  614. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
  615. }
  616. sig->tag = tag_type;
  617. sig->len = len;
  618. sig->p = *p;
  619. *p += len;
  620. return 0;
  621. }
  622. /*
  623. * Get signature algorithm from alg OID and optional parameters
  624. */
  625. int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  626. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  627. void **sig_opts)
  628. {
  629. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  630. if (*sig_opts != NULL) {
  631. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  632. }
  633. if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
  634. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
  635. }
  636. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  637. if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
  638. mbedtls_pk_rsassa_pss_options *pss_opts;
  639. pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
  640. if (pss_opts == NULL) {
  641. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  642. }
  643. ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
  644. md_alg,
  645. &pss_opts->mgf1_hash_id,
  646. &pss_opts->expected_salt_len);
  647. if (ret != 0) {
  648. mbedtls_free(pss_opts);
  649. return ret;
  650. }
  651. *sig_opts = (void *) pss_opts;
  652. } else
  653. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  654. {
  655. /* Make sure parameters are absent or NULL */
  656. if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
  657. sig_params->len != 0) {
  658. return MBEDTLS_ERR_X509_INVALID_ALG;
  659. }
  660. }
  661. return 0;
  662. }
  663. /*
  664. * X.509 Extensions (No parsing of extensions, pointer should
  665. * be either manually updated or extensions should be parsed!)
  666. */
  667. int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
  668. mbedtls_x509_buf *ext, int tag)
  669. {
  670. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  671. size_t len;
  672. /* Extension structure use EXPLICIT tagging. That is, the actual
  673. * `Extensions` structure is wrapped by a tag-length pair using
  674. * the respective context-specific tag. */
  675. ret = mbedtls_asn1_get_tag(p, end, &ext->len,
  676. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
  677. if (ret != 0) {
  678. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  679. }
  680. ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
  681. ext->p = *p;
  682. end = *p + ext->len;
  683. /*
  684. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  685. */
  686. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  687. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  688. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  689. }
  690. if (end != *p + len) {
  691. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  692. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  693. }
  694. return 0;
  695. }
  696. /*
  697. * Store the name in printable form into buf; no more
  698. * than size characters will be written
  699. */
  700. int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
  701. {
  702. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  703. size_t i, j, n;
  704. unsigned char c, merge = 0;
  705. const mbedtls_x509_name *name;
  706. const char *short_name = NULL;
  707. char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
  708. memset(s, 0, sizeof(s));
  709. name = dn;
  710. p = buf;
  711. n = size;
  712. while (name != NULL) {
  713. if (!name->oid.p) {
  714. name = name->next;
  715. continue;
  716. }
  717. if (name != dn) {
  718. ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
  719. MBEDTLS_X509_SAFE_SNPRINTF;
  720. }
  721. ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
  722. if (ret == 0) {
  723. ret = mbedtls_snprintf(p, n, "%s=", short_name);
  724. } else {
  725. ret = mbedtls_snprintf(p, n, "\?\?=");
  726. }
  727. MBEDTLS_X509_SAFE_SNPRINTF;
  728. for (i = 0, j = 0; i < name->val.len; i++, j++) {
  729. if (j >= sizeof(s) - 1) {
  730. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  731. }
  732. c = name->val.p[i];
  733. // Special characters requiring escaping, RFC 1779
  734. if (c && strchr(",=+<>#;\"\\", c)) {
  735. if (j + 1 >= sizeof(s) - 1) {
  736. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  737. }
  738. s[j++] = '\\';
  739. }
  740. if (c < 32 || c >= 127) {
  741. s[j] = '?';
  742. } else {
  743. s[j] = c;
  744. }
  745. }
  746. s[j] = '\0';
  747. ret = mbedtls_snprintf(p, n, "%s", s);
  748. MBEDTLS_X509_SAFE_SNPRINTF;
  749. merge = name->next_merged;
  750. name = name->next;
  751. }
  752. return (int) (size - n);
  753. }
  754. /*
  755. * Store the serial in printable form into buf; no more
  756. * than size characters will be written
  757. */
  758. int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
  759. {
  760. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  761. size_t i, n, nr;
  762. char *p;
  763. p = buf;
  764. n = size;
  765. nr = (serial->len <= 32)
  766. ? serial->len : 28;
  767. for (i = 0; i < nr; i++) {
  768. if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
  769. continue;
  770. }
  771. ret = mbedtls_snprintf(p, n, "%02X%s",
  772. serial->p[i], (i < nr - 1) ? ":" : "");
  773. MBEDTLS_X509_SAFE_SNPRINTF;
  774. }
  775. if (nr != serial->len) {
  776. ret = mbedtls_snprintf(p, n, "....");
  777. MBEDTLS_X509_SAFE_SNPRINTF;
  778. }
  779. return (int) (size - n);
  780. }
  781. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  782. /*
  783. * Helper for writing signature algorithms
  784. */
  785. int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
  786. mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  787. const void *sig_opts)
  788. {
  789. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  790. char *p = buf;
  791. size_t n = size;
  792. const char *desc = NULL;
  793. ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
  794. if (ret != 0) {
  795. ret = mbedtls_snprintf(p, n, "???");
  796. } else {
  797. ret = mbedtls_snprintf(p, n, "%s", desc);
  798. }
  799. MBEDTLS_X509_SAFE_SNPRINTF;
  800. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  801. if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
  802. const mbedtls_pk_rsassa_pss_options *pss_opts;
  803. pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
  804. const char *name = md_type_to_string(md_alg);
  805. const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
  806. ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
  807. name ? name : "???",
  808. mgf_name ? mgf_name : "???",
  809. (unsigned int) pss_opts->expected_salt_len);
  810. MBEDTLS_X509_SAFE_SNPRINTF;
  811. }
  812. #else
  813. ((void) pk_alg);
  814. ((void) md_alg);
  815. ((void) sig_opts);
  816. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  817. return (int) (size - n);
  818. }
  819. #endif /* MBEDTLS_X509_REMOVE_INFO */
  820. /*
  821. * Helper for writing "RSA key size", "EC key size", etc
  822. */
  823. int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
  824. {
  825. char *p = buf;
  826. size_t n = buf_size;
  827. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  828. ret = mbedtls_snprintf(p, n, "%s key size", name);
  829. MBEDTLS_X509_SAFE_SNPRINTF;
  830. return 0;
  831. }
  832. #if defined(MBEDTLS_HAVE_TIME_DATE)
  833. /*
  834. * Set the time structure to the current time.
  835. * Return 0 on success, non-zero on failure.
  836. */
  837. static int x509_get_current_time(mbedtls_x509_time *now)
  838. {
  839. struct tm *lt, tm_buf;
  840. mbedtls_time_t tt;
  841. int ret = 0;
  842. tt = mbedtls_time(NULL);
  843. lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
  844. if (lt == NULL) {
  845. ret = -1;
  846. } else {
  847. now->year = lt->tm_year + 1900;
  848. now->mon = lt->tm_mon + 1;
  849. now->day = lt->tm_mday;
  850. now->hour = lt->tm_hour;
  851. now->min = lt->tm_min;
  852. now->sec = lt->tm_sec;
  853. }
  854. return ret;
  855. }
  856. /*
  857. * Return 0 if before <= after, 1 otherwise
  858. */
  859. static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
  860. {
  861. if (before->year > after->year) {
  862. return 1;
  863. }
  864. if (before->year == after->year &&
  865. before->mon > after->mon) {
  866. return 1;
  867. }
  868. if (before->year == after->year &&
  869. before->mon == after->mon &&
  870. before->day > after->day) {
  871. return 1;
  872. }
  873. if (before->year == after->year &&
  874. before->mon == after->mon &&
  875. before->day == after->day &&
  876. before->hour > after->hour) {
  877. return 1;
  878. }
  879. if (before->year == after->year &&
  880. before->mon == after->mon &&
  881. before->day == after->day &&
  882. before->hour == after->hour &&
  883. before->min > after->min) {
  884. return 1;
  885. }
  886. if (before->year == after->year &&
  887. before->mon == after->mon &&
  888. before->day == after->day &&
  889. before->hour == after->hour &&
  890. before->min == after->min &&
  891. before->sec > after->sec) {
  892. return 1;
  893. }
  894. return 0;
  895. }
  896. int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
  897. {
  898. mbedtls_x509_time now;
  899. if (x509_get_current_time(&now) != 0) {
  900. return 1;
  901. }
  902. return x509_check_time(&now, to);
  903. }
  904. int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
  905. {
  906. mbedtls_x509_time now;
  907. if (x509_get_current_time(&now) != 0) {
  908. return 1;
  909. }
  910. return x509_check_time(from, &now);
  911. }
  912. #else /* MBEDTLS_HAVE_TIME_DATE */
  913. int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
  914. {
  915. ((void) to);
  916. return 0;
  917. }
  918. int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
  919. {
  920. ((void) from);
  921. return 0;
  922. }
  923. #endif /* MBEDTLS_HAVE_TIME_DATE */
  924. /* Common functions for parsing CRT and CSR. */
  925. #if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
  926. /*
  927. * OtherName ::= SEQUENCE {
  928. * type-id OBJECT IDENTIFIER,
  929. * value [0] EXPLICIT ANY DEFINED BY type-id }
  930. *
  931. * HardwareModuleName ::= SEQUENCE {
  932. * hwType OBJECT IDENTIFIER,
  933. * hwSerialNum OCTET STRING }
  934. *
  935. * NOTE: we currently only parse and use otherName of type HwModuleName,
  936. * as defined in RFC 4108.
  937. */
  938. static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
  939. mbedtls_x509_san_other_name *other_name)
  940. {
  941. int ret = 0;
  942. size_t len;
  943. unsigned char *p = subject_alt_name->p;
  944. const unsigned char *end = p + subject_alt_name->len;
  945. mbedtls_x509_buf cur_oid;
  946. if ((subject_alt_name->tag &
  947. (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
  948. (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
  949. /*
  950. * The given subject alternative name is not of type "othername".
  951. */
  952. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  953. }
  954. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  955. MBEDTLS_ASN1_OID)) != 0) {
  956. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  957. }
  958. cur_oid.tag = MBEDTLS_ASN1_OID;
  959. cur_oid.p = p;
  960. cur_oid.len = len;
  961. /*
  962. * Only HwModuleName is currently supported.
  963. */
  964. if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
  965. return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  966. }
  967. p += len;
  968. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  969. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
  970. 0) {
  971. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  972. }
  973. if (end != p + len) {
  974. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  975. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  976. }
  977. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  978. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  979. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  980. }
  981. if (end != p + len) {
  982. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  983. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  984. }
  985. if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
  986. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  987. }
  988. other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
  989. other_name->value.hardware_module_name.oid.p = p;
  990. other_name->value.hardware_module_name.oid.len = len;
  991. p += len;
  992. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  993. MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  994. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  995. }
  996. other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
  997. other_name->value.hardware_module_name.val.p = p;
  998. other_name->value.hardware_module_name.val.len = len;
  999. p += len;
  1000. if (p != end) {
  1001. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1002. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * SubjectAltName ::= GeneralNames
  1008. *
  1009. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  1010. *
  1011. * GeneralName ::= CHOICE {
  1012. * otherName [0] OtherName,
  1013. * rfc822Name [1] IA5String,
  1014. * dNSName [2] IA5String,
  1015. * x400Address [3] ORAddress,
  1016. * directoryName [4] Name,
  1017. * ediPartyName [5] EDIPartyName,
  1018. * uniformResourceIdentifier [6] IA5String,
  1019. * iPAddress [7] OCTET STRING,
  1020. * registeredID [8] OBJECT IDENTIFIER }
  1021. *
  1022. * OtherName ::= SEQUENCE {
  1023. * type-id OBJECT IDENTIFIER,
  1024. * value [0] EXPLICIT ANY DEFINED BY type-id }
  1025. *
  1026. * EDIPartyName ::= SEQUENCE {
  1027. * nameAssigner [0] DirectoryString OPTIONAL,
  1028. * partyName [1] DirectoryString }
  1029. *
  1030. * We list all types, but use the following GeneralName types from RFC 5280:
  1031. * "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
  1032. * of type "otherName", as defined in RFC 4108.
  1033. */
  1034. int mbedtls_x509_get_subject_alt_name(unsigned char **p,
  1035. const unsigned char *end,
  1036. mbedtls_x509_sequence *subject_alt_name)
  1037. {
  1038. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1039. size_t len, tag_len;
  1040. mbedtls_asn1_sequence *cur = subject_alt_name;
  1041. /* Get main sequence tag */
  1042. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  1043. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  1044. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  1045. }
  1046. if (*p + len != end) {
  1047. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1048. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  1049. }
  1050. while (*p < end) {
  1051. mbedtls_x509_subject_alternative_name dummy_san_buf;
  1052. mbedtls_x509_buf tmp_san_buf;
  1053. memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
  1054. tmp_san_buf.tag = **p;
  1055. (*p)++;
  1056. if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
  1057. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  1058. }
  1059. tmp_san_buf.p = *p;
  1060. tmp_san_buf.len = tag_len;
  1061. if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
  1062. MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
  1063. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1064. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  1065. }
  1066. /*
  1067. * Check that the SAN is structured correctly.
  1068. */
  1069. ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
  1070. /*
  1071. * In case the extension is malformed, return an error,
  1072. * and clear the allocated sequences.
  1073. */
  1074. if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
  1075. mbedtls_asn1_sequence_free(subject_alt_name->next);
  1076. subject_alt_name->next = NULL;
  1077. return ret;
  1078. }
  1079. /* Allocate and assign next pointer */
  1080. if (cur->buf.p != NULL) {
  1081. if (cur->next != NULL) {
  1082. return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
  1083. }
  1084. cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
  1085. if (cur->next == NULL) {
  1086. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1087. MBEDTLS_ERR_ASN1_ALLOC_FAILED);
  1088. }
  1089. cur = cur->next;
  1090. }
  1091. cur->buf = tmp_san_buf;
  1092. *p += tmp_san_buf.len;
  1093. }
  1094. /* Set final sequence entry's next pointer to NULL */
  1095. cur->next = NULL;
  1096. if (*p != end) {
  1097. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1098. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  1099. }
  1100. return 0;
  1101. }
  1102. int mbedtls_x509_get_ns_cert_type(unsigned char **p,
  1103. const unsigned char *end,
  1104. unsigned char *ns_cert_type)
  1105. {
  1106. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1107. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  1108. if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
  1109. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  1110. }
  1111. /* A bitstring with no flags set is still technically valid, as it will mean
  1112. that the certificate has no designated purpose at the time of creation. */
  1113. if (bs.len == 0) {
  1114. *ns_cert_type = 0;
  1115. return 0;
  1116. }
  1117. if (bs.len != 1) {
  1118. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1119. MBEDTLS_ERR_ASN1_INVALID_LENGTH);
  1120. }
  1121. /* Get actual bitstring */
  1122. *ns_cert_type = *bs.p;
  1123. return 0;
  1124. }
  1125. int mbedtls_x509_get_key_usage(unsigned char **p,
  1126. const unsigned char *end,
  1127. unsigned int *key_usage)
  1128. {
  1129. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1130. size_t i;
  1131. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  1132. if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
  1133. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  1134. }
  1135. /* A bitstring with no flags set is still technically valid, as it will mean
  1136. that the certificate has no designated purpose at the time of creation. */
  1137. if (bs.len == 0) {
  1138. *key_usage = 0;
  1139. return 0;
  1140. }
  1141. /* Get actual bitstring */
  1142. *key_usage = 0;
  1143. for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
  1144. *key_usage |= (unsigned int) bs.p[i] << (8*i);
  1145. }
  1146. return 0;
  1147. }
  1148. int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
  1149. mbedtls_x509_subject_alternative_name *san)
  1150. {
  1151. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1152. switch (san_buf->tag &
  1153. (MBEDTLS_ASN1_TAG_CLASS_MASK |
  1154. MBEDTLS_ASN1_TAG_VALUE_MASK)) {
  1155. /*
  1156. * otherName
  1157. */
  1158. case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
  1159. {
  1160. mbedtls_x509_san_other_name other_name;
  1161. ret = x509_get_other_name(san_buf, &other_name);
  1162. if (ret != 0) {
  1163. return ret;
  1164. }
  1165. memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
  1166. san->type = MBEDTLS_X509_SAN_OTHER_NAME;
  1167. memcpy(&san->san.other_name,
  1168. &other_name, sizeof(other_name));
  1169. }
  1170. break;
  1171. /*
  1172. * uniformResourceIdentifier
  1173. */
  1174. case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
  1175. {
  1176. memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
  1177. san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
  1178. memcpy(&san->san.unstructured_name,
  1179. san_buf, sizeof(*san_buf));
  1180. }
  1181. break;
  1182. /*
  1183. * dNSName
  1184. */
  1185. case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
  1186. {
  1187. memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
  1188. san->type = MBEDTLS_X509_SAN_DNS_NAME;
  1189. memcpy(&san->san.unstructured_name,
  1190. san_buf, sizeof(*san_buf));
  1191. }
  1192. break;
  1193. /*
  1194. * RFC822 Name
  1195. */
  1196. case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
  1197. {
  1198. memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
  1199. san->type = MBEDTLS_X509_SAN_RFC822_NAME;
  1200. memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
  1201. }
  1202. break;
  1203. /*
  1204. * Type not supported
  1205. */
  1206. default:
  1207. return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  1208. }
  1209. return 0;
  1210. }
  1211. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  1212. int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
  1213. const mbedtls_x509_sequence
  1214. *subject_alt_name,
  1215. const char *prefix)
  1216. {
  1217. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1218. size_t i;
  1219. size_t n = *size;
  1220. char *p = *buf;
  1221. const mbedtls_x509_sequence *cur = subject_alt_name;
  1222. mbedtls_x509_subject_alternative_name san;
  1223. int parse_ret;
  1224. while (cur != NULL) {
  1225. memset(&san, 0, sizeof(san));
  1226. parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
  1227. if (parse_ret != 0) {
  1228. if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
  1229. ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
  1230. MBEDTLS_X509_SAFE_SNPRINTF;
  1231. } else {
  1232. ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
  1233. MBEDTLS_X509_SAFE_SNPRINTF;
  1234. }
  1235. cur = cur->next;
  1236. continue;
  1237. }
  1238. switch (san.type) {
  1239. /*
  1240. * otherName
  1241. */
  1242. case MBEDTLS_X509_SAN_OTHER_NAME:
  1243. {
  1244. mbedtls_x509_san_other_name *other_name = &san.san.other_name;
  1245. ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
  1246. MBEDTLS_X509_SAFE_SNPRINTF;
  1247. if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
  1248. &other_name->value.hardware_module_name.oid) != 0) {
  1249. ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
  1250. MBEDTLS_X509_SAFE_SNPRINTF;
  1251. ret =
  1252. mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
  1253. MBEDTLS_X509_SAFE_SNPRINTF;
  1254. ret = mbedtls_oid_get_numeric_string(p,
  1255. n,
  1256. &other_name->value.hardware_module_name.oid);
  1257. MBEDTLS_X509_SAFE_SNPRINTF;
  1258. ret =
  1259. mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
  1260. MBEDTLS_X509_SAFE_SNPRINTF;
  1261. for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
  1262. ret = mbedtls_snprintf(p,
  1263. n,
  1264. "%02X",
  1265. other_name->value.hardware_module_name.val.p[i]);
  1266. MBEDTLS_X509_SAFE_SNPRINTF;
  1267. }
  1268. }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
  1269. }
  1270. break;
  1271. /*
  1272. * uniformResourceIdentifier
  1273. */
  1274. case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
  1275. {
  1276. ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
  1277. MBEDTLS_X509_SAFE_SNPRINTF;
  1278. if (san.san.unstructured_name.len >= n) {
  1279. *p = '\0';
  1280. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  1281. }
  1282. memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
  1283. p += san.san.unstructured_name.len;
  1284. n -= san.san.unstructured_name.len;
  1285. }
  1286. break;
  1287. /*
  1288. * dNSName
  1289. * RFC822 Name
  1290. */
  1291. case MBEDTLS_X509_SAN_DNS_NAME:
  1292. case MBEDTLS_X509_SAN_RFC822_NAME:
  1293. {
  1294. const char *dns_name = "dNSName";
  1295. const char *rfc822_name = "rfc822Name";
  1296. ret = mbedtls_snprintf(p, n,
  1297. "\n%s %s : ",
  1298. prefix,
  1299. san.type ==
  1300. MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
  1301. MBEDTLS_X509_SAFE_SNPRINTF;
  1302. if (san.san.unstructured_name.len >= n) {
  1303. *p = '\0';
  1304. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  1305. }
  1306. memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
  1307. p += san.san.unstructured_name.len;
  1308. n -= san.san.unstructured_name.len;
  1309. }
  1310. break;
  1311. /*
  1312. * Type not supported, skip item.
  1313. */
  1314. default:
  1315. ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
  1316. MBEDTLS_X509_SAFE_SNPRINTF;
  1317. break;
  1318. }
  1319. cur = cur->next;
  1320. }
  1321. *p = '\0';
  1322. *size = n;
  1323. *buf = p;
  1324. return 0;
  1325. }
  1326. #define PRINT_ITEM(i) \
  1327. { \
  1328. ret = mbedtls_snprintf(p, n, "%s" i, sep); \
  1329. MBEDTLS_X509_SAFE_SNPRINTF; \
  1330. sep = ", "; \
  1331. }
  1332. #define CERT_TYPE(type, name) \
  1333. if (ns_cert_type & (type)) \
  1334. PRINT_ITEM(name);
  1335. int mbedtls_x509_info_cert_type(char **buf, size_t *size,
  1336. unsigned char ns_cert_type)
  1337. {
  1338. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1339. size_t n = *size;
  1340. char *p = *buf;
  1341. const char *sep = "";
  1342. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
  1343. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
  1344. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
  1345. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
  1346. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
  1347. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
  1348. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
  1349. CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
  1350. *size = n;
  1351. *buf = p;
  1352. return 0;
  1353. }
  1354. #define KEY_USAGE(code, name) \
  1355. if (key_usage & (code)) \
  1356. PRINT_ITEM(name);
  1357. int mbedtls_x509_info_key_usage(char **buf, size_t *size,
  1358. unsigned int key_usage)
  1359. {
  1360. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1361. size_t n = *size;
  1362. char *p = *buf;
  1363. const char *sep = "";
  1364. KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
  1365. KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
  1366. KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
  1367. KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
  1368. KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
  1369. KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
  1370. KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
  1371. KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
  1372. KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
  1373. *size = n;
  1374. *buf = p;
  1375. return 0;
  1376. }
  1377. #endif /* MBEDTLS_X509_REMOVE_INFO */
  1378. #endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
  1379. #endif /* MBEDTLS_X509_USE_C */