x509_create.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * X.509 base functions for creating certificates / CSRs
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_X509_CREATE_C)
  9. #include "x509_internal.h"
  10. #include "mbedtls/asn1write.h"
  11. #include "mbedtls/error.h"
  12. #include "mbedtls/oid.h"
  13. #include <string.h>
  14. #include "mbedtls/platform.h"
  15. #include "mbedtls/asn1.h"
  16. /* Structure linking OIDs for X.509 DN AttributeTypes to their
  17. * string representations and default string encodings used by Mbed TLS. */
  18. typedef struct {
  19. const char *name; /* String representation of AttributeType, e.g.
  20. * "CN" or "emailAddress". */
  21. size_t name_len; /* Length of 'name', without trailing 0 byte. */
  22. const char *oid; /* String representation of OID of AttributeType,
  23. * as per RFC 5280, Appendix A.1. encoded as per
  24. * X.690 */
  25. int default_tag; /* The default character encoding used for the
  26. * given attribute type, e.g.
  27. * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
  28. } x509_attr_descriptor_t;
  29. #define ADD_STRLEN(s) s, sizeof(s) - 1
  30. /* X.509 DN attributes from RFC 5280, Appendix A.1. */
  31. static const x509_attr_descriptor_t x509_attrs[] =
  32. {
  33. { ADD_STRLEN("CN"),
  34. MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
  35. { ADD_STRLEN("commonName"),
  36. MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
  37. { ADD_STRLEN("C"),
  38. MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
  39. { ADD_STRLEN("countryName"),
  40. MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
  41. { ADD_STRLEN("O"),
  42. MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
  43. { ADD_STRLEN("organizationName"),
  44. MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
  45. { ADD_STRLEN("L"),
  46. MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
  47. { ADD_STRLEN("locality"),
  48. MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
  49. { ADD_STRLEN("R"),
  50. MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
  51. { ADD_STRLEN("OU"),
  52. MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
  53. { ADD_STRLEN("organizationalUnitName"),
  54. MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
  55. { ADD_STRLEN("ST"),
  56. MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
  57. { ADD_STRLEN("stateOrProvinceName"),
  58. MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
  59. { ADD_STRLEN("emailAddress"),
  60. MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
  61. { ADD_STRLEN("serialNumber"),
  62. MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
  63. { ADD_STRLEN("postalAddress"),
  64. MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
  65. { ADD_STRLEN("postalCode"),
  66. MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
  67. { ADD_STRLEN("dnQualifier"),
  68. MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
  69. { ADD_STRLEN("title"),
  70. MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
  71. { ADD_STRLEN("surName"),
  72. MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
  73. { ADD_STRLEN("SN"),
  74. MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
  75. { ADD_STRLEN("givenName"),
  76. MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
  77. { ADD_STRLEN("GN"),
  78. MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
  79. { ADD_STRLEN("initials"),
  80. MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
  81. { ADD_STRLEN("pseudonym"),
  82. MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
  83. { ADD_STRLEN("generationQualifier"),
  84. MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
  85. { ADD_STRLEN("domainComponent"),
  86. MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
  87. { ADD_STRLEN("DC"),
  88. MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
  89. { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
  90. };
  91. static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
  92. {
  93. const x509_attr_descriptor_t *cur;
  94. for (cur = x509_attrs; cur->name != NULL; cur++) {
  95. if (cur->name_len == name_len &&
  96. strncmp(cur->name, name, name_len) == 0) {
  97. break;
  98. }
  99. }
  100. if (cur->name == NULL) {
  101. return NULL;
  102. }
  103. return cur;
  104. }
  105. static int hex_to_int(char c)
  106. {
  107. return ('0' <= c && c <= '9') ? (c - '0') :
  108. ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
  109. ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
  110. }
  111. static int hexpair_to_int(const char *hexpair)
  112. {
  113. int n1 = hex_to_int(*hexpair);
  114. int n2 = hex_to_int(*(hexpair + 1));
  115. if (n1 != -1 && n2 != -1) {
  116. return (n1 << 4) | n2;
  117. } else {
  118. return -1;
  119. }
  120. }
  121. static int parse_attribute_value_string(const char *s,
  122. int len,
  123. unsigned char *data,
  124. size_t *data_len)
  125. {
  126. const char *c;
  127. const char *end = s + len;
  128. unsigned char *d = data;
  129. int n;
  130. for (c = s; c < end; c++) {
  131. if (*c == '\\') {
  132. c++;
  133. /* Check for valid escaped characters as per RFC 4514 Section 3 */
  134. if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
  135. if (n == 0) {
  136. return MBEDTLS_ERR_X509_INVALID_NAME;
  137. }
  138. *(d++) = n;
  139. c++;
  140. } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
  141. *(d++) = *c;
  142. } else {
  143. return MBEDTLS_ERR_X509_INVALID_NAME;
  144. }
  145. } else {
  146. *(d++) = *c;
  147. }
  148. if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
  149. return MBEDTLS_ERR_X509_INVALID_NAME;
  150. }
  151. }
  152. *data_len = (size_t) (d - data);
  153. return 0;
  154. }
  155. /** Parse a hexstring containing a DER-encoded string.
  156. *
  157. * \param s A string of \p len bytes hexadecimal digits.
  158. * \param len Number of bytes to read from \p s.
  159. * \param data Output buffer of size \p data_size.
  160. * On success, it contains the payload that's DER-encoded
  161. * in the input (content without the tag and length).
  162. * If the DER tag is a string tag, the payload is guaranteed
  163. * not to contain null bytes.
  164. * \param data_size Length of the \p data buffer.
  165. * \param data_len On success, the length of the parsed string.
  166. * It is guaranteed to be less than
  167. * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
  168. * \param tag The ASN.1 tag that the payload in \p data is encoded in.
  169. *
  170. * \retval 0 on success.
  171. * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
  172. * a valid hexstring,
  173. * or if the decoded hexstring is not valid DER,
  174. * or if the payload does not fit in \p data,
  175. * or if the payload is more than
  176. * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
  177. * of if \p *tag is an ASN.1 string tag and the payload
  178. * contains a null byte.
  179. * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
  180. */
  181. static int parse_attribute_value_hex_der_encoded(const char *s,
  182. size_t len,
  183. unsigned char *data,
  184. size_t data_size,
  185. size_t *data_len,
  186. int *tag)
  187. {
  188. /* Step 1: preliminary length checks. */
  189. /* Each byte is encoded by exactly two hexadecimal digits. */
  190. if (len % 2 != 0) {
  191. /* Odd number of hex digits */
  192. return MBEDTLS_ERR_X509_INVALID_NAME;
  193. }
  194. size_t const der_length = len / 2;
  195. if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
  196. /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
  197. * (after subtracting the ASN.1 tag and length). Reject this early
  198. * to avoid allocating a large intermediate buffer. */
  199. return MBEDTLS_ERR_X509_INVALID_NAME;
  200. }
  201. if (der_length < 1) {
  202. /* Avoid empty-buffer shenanigans. A valid DER encoding is never
  203. * empty. */
  204. return MBEDTLS_ERR_X509_INVALID_NAME;
  205. }
  206. /* Step 2: Decode the hex string into an intermediate buffer. */
  207. unsigned char *der = mbedtls_calloc(1, der_length);
  208. if (der == NULL) {
  209. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  210. }
  211. /* Beyond this point, der needs to be freed on exit. */
  212. for (size_t i = 0; i < der_length; i++) {
  213. int c = hexpair_to_int(s + 2 * i);
  214. if (c < 0) {
  215. goto error;
  216. }
  217. der[i] = c;
  218. }
  219. /* Step 3: decode the DER. */
  220. /* We've checked that der_length >= 1 above. */
  221. *tag = der[0];
  222. {
  223. unsigned char *p = der + 1;
  224. if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
  225. goto error;
  226. }
  227. /* Now p points to the first byte of the payload inside der,
  228. * and *data_len is the length of the payload. */
  229. /* Step 4: payload validation */
  230. if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
  231. goto error;
  232. }
  233. /* Strings must not contain null bytes. */
  234. if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
  235. for (size_t i = 0; i < *data_len; i++) {
  236. if (p[i] == 0) {
  237. goto error;
  238. }
  239. }
  240. }
  241. /* Step 5: output the payload. */
  242. if (*data_len > data_size) {
  243. goto error;
  244. }
  245. memcpy(data, p, *data_len);
  246. }
  247. mbedtls_free(der);
  248. return 0;
  249. error:
  250. mbedtls_free(der);
  251. return MBEDTLS_ERR_X509_INVALID_NAME;
  252. }
  253. int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
  254. {
  255. int ret = MBEDTLS_ERR_X509_INVALID_NAME;
  256. int parse_ret = 0;
  257. const char *s = name, *c = s;
  258. const char *end = s + strlen(s);
  259. mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
  260. const x509_attr_descriptor_t *attr_descr = NULL;
  261. int in_attr_type = 1;
  262. int tag;
  263. int numericoid = 0;
  264. unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
  265. size_t data_len = 0;
  266. /* Clear existing chain if present */
  267. mbedtls_asn1_free_named_data_list(head);
  268. while (c <= end) {
  269. if (in_attr_type && *c == '=') {
  270. if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
  271. if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
  272. return MBEDTLS_ERR_X509_INVALID_NAME;
  273. } else {
  274. numericoid = 1;
  275. }
  276. } else {
  277. oid.len = strlen(attr_descr->oid);
  278. oid.p = mbedtls_calloc(1, oid.len);
  279. memcpy(oid.p, attr_descr->oid, oid.len);
  280. numericoid = 0;
  281. }
  282. s = c + 1;
  283. in_attr_type = 0;
  284. }
  285. if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
  286. if (s == c) {
  287. mbedtls_free(oid.p);
  288. return MBEDTLS_ERR_X509_INVALID_NAME;
  289. } else if (*s == '#') {
  290. /* We know that c >= s (loop invariant) and c != s (in this
  291. * else branch), hence c - s - 1 >= 0. */
  292. parse_ret = parse_attribute_value_hex_der_encoded(
  293. s + 1, (size_t) (c - s) - 1,
  294. data, sizeof(data), &data_len, &tag);
  295. if (parse_ret != 0) {
  296. mbedtls_free(oid.p);
  297. return parse_ret;
  298. }
  299. } else {
  300. if (numericoid) {
  301. mbedtls_free(oid.p);
  302. return MBEDTLS_ERR_X509_INVALID_NAME;
  303. } else {
  304. if ((parse_ret =
  305. parse_attribute_value_string(s, (int) (c - s), data,
  306. &data_len)) != 0) {
  307. mbedtls_free(oid.p);
  308. return parse_ret;
  309. }
  310. tag = attr_descr->default_tag;
  311. }
  312. }
  313. mbedtls_asn1_named_data *cur =
  314. mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
  315. (unsigned char *) data,
  316. data_len);
  317. mbedtls_free(oid.p);
  318. oid.p = NULL;
  319. if (cur == NULL) {
  320. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  321. }
  322. // set tagType
  323. cur->val.tag = tag;
  324. while (c < end && *(c + 1) == ' ') {
  325. c++;
  326. }
  327. s = c + 1;
  328. in_attr_type = 1;
  329. /* Successfully parsed one name, update ret to success */
  330. ret = 0;
  331. }
  332. c++;
  333. }
  334. if (oid.p != NULL) {
  335. mbedtls_free(oid.p);
  336. }
  337. return ret;
  338. }
  339. /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
  340. * to store the critical boolean for us
  341. */
  342. int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  343. int critical, const unsigned char *val, size_t val_len)
  344. {
  345. mbedtls_asn1_named_data *cur;
  346. if (val_len > (SIZE_MAX - 1)) {
  347. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  348. }
  349. if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
  350. NULL, val_len + 1)) == NULL) {
  351. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  352. }
  353. cur->val.p[0] = critical;
  354. memcpy(cur->val.p + 1, val, val_len);
  355. return 0;
  356. }
  357. /*
  358. * RelativeDistinguishedName ::=
  359. * SET OF AttributeTypeAndValue
  360. *
  361. * AttributeTypeAndValue ::= SEQUENCE {
  362. * type AttributeType,
  363. * value AttributeValue }
  364. *
  365. * AttributeType ::= OBJECT IDENTIFIER
  366. *
  367. * AttributeValue ::= ANY DEFINED BY AttributeType
  368. */
  369. static int x509_write_name(unsigned char **p,
  370. unsigned char *start,
  371. mbedtls_asn1_named_data *cur_name)
  372. {
  373. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  374. size_t len = 0;
  375. const char *oid = (const char *) cur_name->oid.p;
  376. size_t oid_len = cur_name->oid.len;
  377. const unsigned char *name = cur_name->val.p;
  378. size_t name_len = cur_name->val.len;
  379. // Write correct string tag and value
  380. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
  381. cur_name->val.tag,
  382. (const char *) name,
  383. name_len));
  384. // Write OID
  385. //
  386. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
  387. oid_len));
  388. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  389. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  390. MBEDTLS_ASN1_CONSTRUCTED |
  391. MBEDTLS_ASN1_SEQUENCE));
  392. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  393. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  394. MBEDTLS_ASN1_CONSTRUCTED |
  395. MBEDTLS_ASN1_SET));
  396. return (int) len;
  397. }
  398. int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
  399. mbedtls_asn1_named_data *first)
  400. {
  401. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  402. size_t len = 0;
  403. mbedtls_asn1_named_data *cur = first;
  404. while (cur != NULL) {
  405. MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
  406. cur = cur->next;
  407. }
  408. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  409. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  410. MBEDTLS_ASN1_SEQUENCE));
  411. return (int) len;
  412. }
  413. int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
  414. const char *oid, size_t oid_len,
  415. unsigned char *sig, size_t size,
  416. mbedtls_pk_type_t pk_alg)
  417. {
  418. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  419. int write_null_par;
  420. size_t len = 0;
  421. if (*p < start || (size_t) (*p - start) < size) {
  422. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  423. }
  424. len = size;
  425. (*p) -= len;
  426. memcpy(*p, sig, len);
  427. if (*p - start < 1) {
  428. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  429. }
  430. *--(*p) = 0;
  431. len += 1;
  432. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  433. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
  434. // Write OID
  435. //
  436. if (pk_alg == MBEDTLS_PK_ECDSA) {
  437. /*
  438. * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
  439. * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
  440. * https://www.rfc-editor.org/rfc/rfc5758#section-3.
  441. */
  442. write_null_par = 0;
  443. } else {
  444. write_null_par = 1;
  445. }
  446. MBEDTLS_ASN1_CHK_ADD(len,
  447. mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
  448. 0, write_null_par));
  449. return (int) len;
  450. }
  451. static int x509_write_extension(unsigned char **p, unsigned char *start,
  452. mbedtls_asn1_named_data *ext)
  453. {
  454. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  455. size_t len = 0;
  456. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
  457. ext->val.len - 1));
  458. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
  459. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
  460. if (ext->val.p[0] != 0) {
  461. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
  462. }
  463. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
  464. ext->oid.len));
  465. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
  466. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
  467. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  468. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  469. MBEDTLS_ASN1_SEQUENCE));
  470. return (int) len;
  471. }
  472. /*
  473. * Extension ::= SEQUENCE {
  474. * extnID OBJECT IDENTIFIER,
  475. * critical BOOLEAN DEFAULT FALSE,
  476. * extnValue OCTET STRING
  477. * -- contains the DER encoding of an ASN.1 value
  478. * -- corresponding to the extension type identified
  479. * -- by extnID
  480. * }
  481. */
  482. int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
  483. mbedtls_asn1_named_data *first)
  484. {
  485. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  486. size_t len = 0;
  487. mbedtls_asn1_named_data *cur_ext = first;
  488. while (cur_ext != NULL) {
  489. MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
  490. cur_ext = cur_ext->next;
  491. }
  492. return (int) len;
  493. }
  494. #endif /* MBEDTLS_X509_CREATE_C */