x509_csr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * X.509 Certificate Signing Request (CSR) parsing
  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_CSR_PARSE_C)
  19. #include "mbedtls/x509_csr.h"
  20. #include "x509_internal.h"
  21. #include "mbedtls/error.h"
  22. #include "mbedtls/oid.h"
  23. #include "mbedtls/platform_util.h"
  24. #include <string.h>
  25. #if defined(MBEDTLS_PEM_PARSE_C)
  26. #include "mbedtls/pem.h"
  27. #endif
  28. #include "mbedtls/platform.h"
  29. #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
  30. #include <stdio.h>
  31. #endif
  32. /*
  33. * Version ::= INTEGER { v1(0) }
  34. */
  35. static int x509_csr_get_version(unsigned char **p,
  36. const unsigned char *end,
  37. int *ver)
  38. {
  39. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  40. if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
  41. if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
  42. *ver = 0;
  43. return 0;
  44. }
  45. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
  46. }
  47. return 0;
  48. }
  49. /*
  50. * Parse CSR extension requests in DER format
  51. */
  52. static int x509_csr_parse_extensions(mbedtls_x509_csr *csr,
  53. unsigned char **p, const unsigned char *end,
  54. mbedtls_x509_csr_ext_cb_t cb,
  55. void *p_ctx)
  56. {
  57. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  58. size_t len;
  59. unsigned char *end_ext_data, *end_ext_octet;
  60. while (*p < end) {
  61. mbedtls_x509_buf extn_oid = { 0, 0, NULL };
  62. int is_critical = 0; /* DEFAULT FALSE */
  63. int ext_type = 0;
  64. /* Read sequence tag */
  65. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  66. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  67. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  68. }
  69. end_ext_data = *p + len;
  70. /* Get extension ID */
  71. if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
  72. MBEDTLS_ASN1_OID)) != 0) {
  73. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  74. }
  75. extn_oid.tag = MBEDTLS_ASN1_OID;
  76. extn_oid.p = *p;
  77. *p += extn_oid.len;
  78. /* Get optional critical */
  79. if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
  80. (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
  81. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  82. }
  83. /* Data should be octet string type */
  84. if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
  85. MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  86. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  87. }
  88. end_ext_octet = *p + len;
  89. if (end_ext_octet != end_ext_data) {
  90. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  91. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  92. }
  93. /*
  94. * Detect supported extensions and skip unsupported extensions
  95. */
  96. ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
  97. if (ret != 0) {
  98. /* Give the callback (if any) a chance to handle the extension */
  99. if (cb != NULL) {
  100. ret = cb(p_ctx, csr, &extn_oid, is_critical, *p, end_ext_octet);
  101. if (ret != 0 && is_critical) {
  102. return ret;
  103. }
  104. *p = end_ext_octet;
  105. continue;
  106. }
  107. /* No parser found, skip extension */
  108. *p = end_ext_octet;
  109. if (is_critical) {
  110. /* Data is marked as critical: fail */
  111. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  112. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  113. }
  114. continue;
  115. }
  116. /* Forbid repeated extensions */
  117. if ((csr->ext_types & ext_type) != 0) {
  118. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  119. MBEDTLS_ERR_ASN1_INVALID_DATA);
  120. }
  121. csr->ext_types |= ext_type;
  122. switch (ext_type) {
  123. case MBEDTLS_X509_EXT_KEY_USAGE:
  124. /* Parse key usage */
  125. if ((ret = mbedtls_x509_get_key_usage(p, end_ext_data,
  126. &csr->key_usage)) != 0) {
  127. return ret;
  128. }
  129. break;
  130. case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
  131. /* Parse subject alt name */
  132. if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_data,
  133. &csr->subject_alt_names)) != 0) {
  134. return ret;
  135. }
  136. break;
  137. case MBEDTLS_X509_EXT_NS_CERT_TYPE:
  138. /* Parse netscape certificate type */
  139. if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_data,
  140. &csr->ns_cert_type)) != 0) {
  141. return ret;
  142. }
  143. break;
  144. default:
  145. /*
  146. * If this is a non-critical extension, which the oid layer
  147. * supports, but there isn't an x509 parser for it,
  148. * skip the extension.
  149. */
  150. if (is_critical) {
  151. return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  152. } else {
  153. *p = end_ext_octet;
  154. }
  155. }
  156. }
  157. if (*p != end) {
  158. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  159. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Parse CSR attributes in DER format
  165. */
  166. static int x509_csr_parse_attributes(mbedtls_x509_csr *csr,
  167. const unsigned char *start, const unsigned char *end,
  168. mbedtls_x509_csr_ext_cb_t cb,
  169. void *p_ctx)
  170. {
  171. int ret;
  172. size_t len;
  173. unsigned char *end_attr_data;
  174. unsigned char **p = (unsigned char **) &start;
  175. while (*p < end) {
  176. mbedtls_x509_buf attr_oid = { 0, 0, NULL };
  177. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  178. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  179. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  180. }
  181. end_attr_data = *p + len;
  182. /* Get attribute ID */
  183. if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len,
  184. MBEDTLS_ASN1_OID)) != 0) {
  185. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  186. }
  187. attr_oid.tag = MBEDTLS_ASN1_OID;
  188. attr_oid.p = *p;
  189. *p += attr_oid.len;
  190. /* Check that this is an extension-request attribute */
  191. if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) {
  192. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  193. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
  194. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  195. }
  196. if ((ret = mbedtls_asn1_get_tag(p, end, &len,
  197. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
  198. 0) {
  199. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
  200. }
  201. if ((ret = x509_csr_parse_extensions(csr, p, *p + len, cb, p_ctx)) != 0) {
  202. return ret;
  203. }
  204. if (*p != end_attr_data) {
  205. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  206. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  207. }
  208. }
  209. *p = end_attr_data;
  210. }
  211. if (*p != end) {
  212. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  213. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  214. }
  215. return 0;
  216. }
  217. /*
  218. * Parse a CSR in DER format
  219. */
  220. static int mbedtls_x509_csr_parse_der_internal(mbedtls_x509_csr *csr,
  221. const unsigned char *buf, size_t buflen,
  222. mbedtls_x509_csr_ext_cb_t cb,
  223. void *p_ctx)
  224. {
  225. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  226. size_t len;
  227. unsigned char *p, *end;
  228. mbedtls_x509_buf sig_params;
  229. memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
  230. /*
  231. * Check for valid input
  232. */
  233. if (csr == NULL || buf == NULL || buflen == 0) {
  234. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  235. }
  236. mbedtls_x509_csr_init(csr);
  237. /*
  238. * first copy the raw DER data
  239. */
  240. p = mbedtls_calloc(1, len = buflen);
  241. if (p == NULL) {
  242. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  243. }
  244. memcpy(p, buf, buflen);
  245. csr->raw.p = p;
  246. csr->raw.len = len;
  247. end = p + len;
  248. /*
  249. * CertificationRequest ::= SEQUENCE {
  250. * certificationRequestInfo CertificationRequestInfo,
  251. * signatureAlgorithm AlgorithmIdentifier,
  252. * signature BIT STRING
  253. * }
  254. */
  255. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  256. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  257. mbedtls_x509_csr_free(csr);
  258. return MBEDTLS_ERR_X509_INVALID_FORMAT;
  259. }
  260. if (len != (size_t) (end - p)) {
  261. mbedtls_x509_csr_free(csr);
  262. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
  263. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  264. }
  265. /*
  266. * CertificationRequestInfo ::= SEQUENCE {
  267. */
  268. csr->cri.p = p;
  269. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  270. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  271. mbedtls_x509_csr_free(csr);
  272. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
  273. }
  274. end = p + len;
  275. csr->cri.len = (size_t) (end - csr->cri.p);
  276. /*
  277. * Version ::= INTEGER { v1(0) }
  278. */
  279. if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
  280. mbedtls_x509_csr_free(csr);
  281. return ret;
  282. }
  283. if (csr->version != 0) {
  284. mbedtls_x509_csr_free(csr);
  285. return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
  286. }
  287. csr->version++;
  288. /*
  289. * subject Name
  290. */
  291. csr->subject_raw.p = p;
  292. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  293. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  294. mbedtls_x509_csr_free(csr);
  295. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
  296. }
  297. if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
  298. mbedtls_x509_csr_free(csr);
  299. return ret;
  300. }
  301. csr->subject_raw.len = (size_t) (p - csr->subject_raw.p);
  302. /*
  303. * subjectPKInfo SubjectPublicKeyInfo
  304. */
  305. if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
  306. mbedtls_x509_csr_free(csr);
  307. return ret;
  308. }
  309. /*
  310. * attributes [0] Attributes
  311. *
  312. * The list of possible attributes is open-ended, though RFC 2985
  313. * (PKCS#9) defines a few in section 5.4. We currently don't support any,
  314. * so we just ignore them. This is a safe thing to do as the worst thing
  315. * that could happen is that we issue a certificate that does not match
  316. * the requester's expectations - this cannot cause a violation of our
  317. * signature policies.
  318. */
  319. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  320. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
  321. 0) {
  322. mbedtls_x509_csr_free(csr);
  323. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
  324. }
  325. if ((ret = x509_csr_parse_attributes(csr, p, p + len, cb, p_ctx)) != 0) {
  326. mbedtls_x509_csr_free(csr);
  327. return ret;
  328. }
  329. p += len;
  330. end = csr->raw.p + csr->raw.len;
  331. /*
  332. * signatureAlgorithm AlgorithmIdentifier,
  333. * signature BIT STRING
  334. */
  335. if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
  336. mbedtls_x509_csr_free(csr);
  337. return ret;
  338. }
  339. if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
  340. &csr->sig_md, &csr->sig_pk,
  341. &csr->sig_opts)) != 0) {
  342. mbedtls_x509_csr_free(csr);
  343. return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
  344. }
  345. if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
  346. mbedtls_x509_csr_free(csr);
  347. return ret;
  348. }
  349. if (p != end) {
  350. mbedtls_x509_csr_free(csr);
  351. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
  352. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Parse a CSR in DER format
  358. */
  359. int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
  360. const unsigned char *buf, size_t buflen)
  361. {
  362. return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, NULL, NULL);
  363. }
  364. /*
  365. * Parse a CSR in DER format with callback for unknown extensions
  366. */
  367. int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr,
  368. const unsigned char *buf, size_t buflen,
  369. mbedtls_x509_csr_ext_cb_t cb,
  370. void *p_ctx)
  371. {
  372. return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, cb, p_ctx);
  373. }
  374. /*
  375. * Parse a CSR, allowing for PEM or raw DER encoding
  376. */
  377. int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
  378. {
  379. #if defined(MBEDTLS_PEM_PARSE_C)
  380. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  381. size_t use_len;
  382. mbedtls_pem_context pem;
  383. #endif
  384. /*
  385. * Check for valid input
  386. */
  387. if (csr == NULL || buf == NULL || buflen == 0) {
  388. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  389. }
  390. #if defined(MBEDTLS_PEM_PARSE_C)
  391. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  392. if (buf[buflen - 1] == '\0') {
  393. mbedtls_pem_init(&pem);
  394. ret = mbedtls_pem_read_buffer(&pem,
  395. "-----BEGIN CERTIFICATE REQUEST-----",
  396. "-----END CERTIFICATE REQUEST-----",
  397. buf, NULL, 0, &use_len);
  398. if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  399. ret = mbedtls_pem_read_buffer(&pem,
  400. "-----BEGIN NEW CERTIFICATE REQUEST-----",
  401. "-----END NEW CERTIFICATE REQUEST-----",
  402. buf, NULL, 0, &use_len);
  403. }
  404. if (ret == 0) {
  405. /*
  406. * Was PEM encoded, parse the result
  407. */
  408. ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
  409. }
  410. mbedtls_pem_free(&pem);
  411. if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  412. return ret;
  413. }
  414. }
  415. #endif /* MBEDTLS_PEM_PARSE_C */
  416. return mbedtls_x509_csr_parse_der(csr, buf, buflen);
  417. }
  418. #if defined(MBEDTLS_FS_IO)
  419. /*
  420. * Load a CSR into the structure
  421. */
  422. int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
  423. {
  424. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  425. size_t n;
  426. unsigned char *buf;
  427. if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
  428. return ret;
  429. }
  430. ret = mbedtls_x509_csr_parse(csr, buf, n);
  431. mbedtls_zeroize_and_free(buf, n);
  432. return ret;
  433. }
  434. #endif /* MBEDTLS_FS_IO */
  435. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  436. #define BEFORE_COLON 14
  437. #define BC "14"
  438. /*
  439. * Return an informational string about the CSR.
  440. */
  441. int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
  442. const mbedtls_x509_csr *csr)
  443. {
  444. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  445. size_t n;
  446. char *p;
  447. char key_size_str[BEFORE_COLON];
  448. p = buf;
  449. n = size;
  450. ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
  451. prefix, csr->version);
  452. MBEDTLS_X509_SAFE_SNPRINTF;
  453. ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
  454. MBEDTLS_X509_SAFE_SNPRINTF;
  455. ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
  456. MBEDTLS_X509_SAFE_SNPRINTF;
  457. ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
  458. MBEDTLS_X509_SAFE_SNPRINTF;
  459. ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
  460. csr->sig_opts);
  461. MBEDTLS_X509_SAFE_SNPRINTF;
  462. if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
  463. mbedtls_pk_get_name(&csr->pk))) != 0) {
  464. return ret;
  465. }
  466. ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
  467. (int) mbedtls_pk_get_bitlen(&csr->pk));
  468. MBEDTLS_X509_SAFE_SNPRINTF;
  469. /*
  470. * Optional extensions
  471. */
  472. if (csr->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
  473. ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
  474. MBEDTLS_X509_SAFE_SNPRINTF;
  475. if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
  476. &csr->subject_alt_names,
  477. prefix)) != 0) {
  478. return ret;
  479. }
  480. }
  481. if (csr->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
  482. ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
  483. MBEDTLS_X509_SAFE_SNPRINTF;
  484. if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) {
  485. return ret;
  486. }
  487. }
  488. if (csr->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
  489. ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
  490. MBEDTLS_X509_SAFE_SNPRINTF;
  491. if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) {
  492. return ret;
  493. }
  494. }
  495. if (csr->ext_types != 0) {
  496. ret = mbedtls_snprintf(p, n, "\n");
  497. MBEDTLS_X509_SAFE_SNPRINTF;
  498. }
  499. return (int) (size - n);
  500. }
  501. #endif /* MBEDTLS_X509_REMOVE_INFO */
  502. /*
  503. * Initialize a CSR
  504. */
  505. void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
  506. {
  507. memset(csr, 0, sizeof(mbedtls_x509_csr));
  508. }
  509. /*
  510. * Unallocate all CSR data
  511. */
  512. void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
  513. {
  514. if (csr == NULL) {
  515. return;
  516. }
  517. mbedtls_pk_free(&csr->pk);
  518. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  519. mbedtls_free(csr->sig_opts);
  520. #endif
  521. mbedtls_asn1_free_named_data_list_shallow(csr->subject.next);
  522. mbedtls_asn1_sequence_free(csr->subject_alt_names.next);
  523. if (csr->raw.p != NULL) {
  524. mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len);
  525. }
  526. mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
  527. }
  528. #endif /* MBEDTLS_X509_CSR_PARSE_C */