x509.c 56 KB

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