x509write_crt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * X.509 certificate writing
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * References:
  9. * - certificates: RFC 5280, updated by RFC 6818
  10. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  11. * - attributes: PKCS#9 v2.0 aka RFC 2985
  12. */
  13. #include "common.h"
  14. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  15. #include "mbedtls/x509_crt.h"
  16. #include "x509_internal.h"
  17. #include "mbedtls/asn1write.h"
  18. #include "mbedtls/error.h"
  19. #include "mbedtls/oid.h"
  20. #include "mbedtls/platform.h"
  21. #include "mbedtls/platform_util.h"
  22. #include "mbedtls/md.h"
  23. #include <string.h>
  24. #include <stdint.h>
  25. #if defined(MBEDTLS_PEM_WRITE_C)
  26. #include "mbedtls/pem.h"
  27. #endif /* MBEDTLS_PEM_WRITE_C */
  28. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  29. #include "psa/crypto.h"
  30. #include "psa_util_internal.h"
  31. #include "mbedtls/psa_util.h"
  32. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  33. void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
  34. {
  35. memset(ctx, 0, sizeof(mbedtls_x509write_cert));
  36. ctx->version = MBEDTLS_X509_CRT_VERSION_3;
  37. }
  38. void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
  39. {
  40. if (ctx == NULL) {
  41. return;
  42. }
  43. mbedtls_asn1_free_named_data_list(&ctx->subject);
  44. mbedtls_asn1_free_named_data_list(&ctx->issuer);
  45. mbedtls_asn1_free_named_data_list(&ctx->extensions);
  46. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
  47. }
  48. void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
  49. int version)
  50. {
  51. ctx->version = version;
  52. }
  53. void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
  54. mbedtls_md_type_t md_alg)
  55. {
  56. ctx->md_alg = md_alg;
  57. }
  58. void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
  59. mbedtls_pk_context *key)
  60. {
  61. ctx->subject_key = key;
  62. }
  63. void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
  64. mbedtls_pk_context *key)
  65. {
  66. ctx->issuer_key = key;
  67. }
  68. int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
  69. const char *subject_name)
  70. {
  71. return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
  72. }
  73. int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
  74. const char *issuer_name)
  75. {
  76. return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
  77. }
  78. #if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
  79. int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
  80. const mbedtls_mpi *serial)
  81. {
  82. int ret;
  83. size_t tmp_len;
  84. /* Ensure that the MPI value fits into the buffer */
  85. tmp_len = mbedtls_mpi_size(serial);
  86. if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
  87. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  88. }
  89. ctx->serial_len = tmp_len;
  90. ret = mbedtls_mpi_write_binary(serial, ctx->serial, tmp_len);
  91. if (ret < 0) {
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. #endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
  97. int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx,
  98. unsigned char *serial, size_t serial_len)
  99. {
  100. if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
  101. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  102. }
  103. ctx->serial_len = serial_len;
  104. memcpy(ctx->serial, serial, serial_len);
  105. return 0;
  106. }
  107. int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
  108. const char *not_before,
  109. const char *not_after)
  110. {
  111. if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
  112. strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
  113. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  114. }
  115. strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
  116. strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
  117. ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  118. ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  119. return 0;
  120. }
  121. int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
  122. const mbedtls_x509_san_list *san_list)
  123. {
  124. return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
  125. }
  126. int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
  127. const char *oid, size_t oid_len,
  128. int critical,
  129. const unsigned char *val, size_t val_len)
  130. {
  131. return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
  132. critical, val, val_len);
  133. }
  134. int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
  135. int is_ca, int max_pathlen)
  136. {
  137. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  138. unsigned char buf[9];
  139. unsigned char *c = buf + sizeof(buf);
  140. size_t len = 0;
  141. memset(buf, 0, sizeof(buf));
  142. if (is_ca && max_pathlen > 127) {
  143. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  144. }
  145. if (is_ca) {
  146. if (max_pathlen >= 0) {
  147. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
  148. max_pathlen));
  149. }
  150. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
  151. }
  152. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  153. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
  154. MBEDTLS_ASN1_CONSTRUCTED |
  155. MBEDTLS_ASN1_SEQUENCE));
  156. return
  157. mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
  158. MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
  159. is_ca, buf + sizeof(buf) - len, len);
  160. }
  161. #if defined(MBEDTLS_MD_CAN_SHA1)
  162. static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
  163. int is_ca,
  164. unsigned char tag)
  165. {
  166. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  167. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  168. unsigned char *c = buf + sizeof(buf);
  169. size_t len = 0;
  170. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  171. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  172. size_t hash_length;
  173. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  174. memset(buf, 0, sizeof(buf));
  175. MBEDTLS_ASN1_CHK_ADD(len,
  176. mbedtls_pk_write_pubkey(&c,
  177. buf,
  178. is_ca ?
  179. ctx->issuer_key :
  180. ctx->subject_key));
  181. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  182. status = psa_hash_compute(PSA_ALG_SHA_1,
  183. buf + sizeof(buf) - len,
  184. len,
  185. buf + sizeof(buf) - 20,
  186. 20,
  187. &hash_length);
  188. if (status != PSA_SUCCESS) {
  189. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  190. }
  191. #else
  192. ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
  193. buf + sizeof(buf) - len, len,
  194. buf + sizeof(buf) - 20);
  195. if (ret != 0) {
  196. return ret;
  197. }
  198. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  199. c = buf + sizeof(buf) - 20;
  200. len = 20;
  201. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  202. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
  203. if (is_ca) { // writes AuthorityKeyIdentifier sequence
  204. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  205. MBEDTLS_ASN1_CHK_ADD(len,
  206. mbedtls_asn1_write_tag(&c,
  207. buf,
  208. MBEDTLS_ASN1_CONSTRUCTED |
  209. MBEDTLS_ASN1_SEQUENCE));
  210. }
  211. if (is_ca) {
  212. return mbedtls_x509write_crt_set_extension(ctx,
  213. MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
  214. MBEDTLS_OID_SIZE(
  215. MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
  216. 0, buf + sizeof(buf) - len, len);
  217. } else {
  218. return mbedtls_x509write_crt_set_extension(ctx,
  219. MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
  220. MBEDTLS_OID_SIZE(
  221. MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
  222. 0, buf + sizeof(buf) - len, len);
  223. }
  224. }
  225. int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
  226. {
  227. return mbedtls_x509write_crt_set_key_identifier(ctx,
  228. 0,
  229. MBEDTLS_ASN1_OCTET_STRING);
  230. }
  231. int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
  232. {
  233. return mbedtls_x509write_crt_set_key_identifier(ctx,
  234. 1,
  235. (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
  236. }
  237. #endif /* MBEDTLS_MD_CAN_SHA1 */
  238. int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
  239. unsigned int key_usage)
  240. {
  241. unsigned char buf[5] = { 0 }, ku[2] = { 0 };
  242. unsigned char *c;
  243. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  244. const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
  245. MBEDTLS_X509_KU_NON_REPUDIATION |
  246. MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
  247. MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
  248. MBEDTLS_X509_KU_KEY_AGREEMENT |
  249. MBEDTLS_X509_KU_KEY_CERT_SIGN |
  250. MBEDTLS_X509_KU_CRL_SIGN |
  251. MBEDTLS_X509_KU_ENCIPHER_ONLY |
  252. MBEDTLS_X509_KU_DECIPHER_ONLY;
  253. /* Check that nothing other than the allowed flags is set */
  254. if ((key_usage & ~allowed_bits) != 0) {
  255. return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  256. }
  257. c = buf + 5;
  258. MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
  259. ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
  260. if (ret < 0) {
  261. return ret;
  262. } else if (ret < 3 || ret > 5) {
  263. return MBEDTLS_ERR_X509_INVALID_FORMAT;
  264. }
  265. ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
  266. MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
  267. 1, c, (size_t) ret);
  268. if (ret != 0) {
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
  274. const mbedtls_asn1_sequence *exts)
  275. {
  276. unsigned char buf[256];
  277. unsigned char *c = buf + sizeof(buf);
  278. int ret;
  279. size_t len = 0;
  280. const mbedtls_asn1_sequence *last_ext = NULL;
  281. const mbedtls_asn1_sequence *ext;
  282. memset(buf, 0, sizeof(buf));
  283. /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
  284. if (exts == NULL) {
  285. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  286. }
  287. /* Iterate over exts backwards, so we write them out in the requested order */
  288. while (last_ext != exts) {
  289. for (ext = exts; ext->next != last_ext; ext = ext->next) {
  290. }
  291. if (ext->buf.tag != MBEDTLS_ASN1_OID) {
  292. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  293. }
  294. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
  295. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
  296. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
  297. last_ext = ext;
  298. }
  299. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  300. MBEDTLS_ASN1_CHK_ADD(len,
  301. mbedtls_asn1_write_tag(&c, buf,
  302. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
  303. return mbedtls_x509write_crt_set_extension(ctx,
  304. MBEDTLS_OID_EXTENDED_KEY_USAGE,
  305. MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
  306. 1, c, len);
  307. }
  308. int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
  309. unsigned char ns_cert_type)
  310. {
  311. unsigned char buf[4] = { 0 };
  312. unsigned char *c;
  313. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  314. c = buf + 4;
  315. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
  316. if (ret < 3 || ret > 4) {
  317. return ret;
  318. }
  319. ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
  320. MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
  321. 0, c, (size_t) ret);
  322. if (ret != 0) {
  323. return ret;
  324. }
  325. return 0;
  326. }
  327. static int x509_write_time(unsigned char **p, unsigned char *start,
  328. const char *t, size_t size)
  329. {
  330. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  331. size_t len = 0;
  332. /*
  333. * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
  334. */
  335. if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
  336. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  337. (const unsigned char *) t + 2,
  338. size - 2));
  339. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  340. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  341. MBEDTLS_ASN1_UTC_TIME));
  342. } else {
  343. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  344. (const unsigned char *) t,
  345. size));
  346. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  347. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  348. MBEDTLS_ASN1_GENERALIZED_TIME));
  349. }
  350. return (int) len;
  351. }
  352. int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
  353. unsigned char *buf, size_t size,
  354. int (*f_rng)(void *, unsigned char *, size_t),
  355. void *p_rng)
  356. {
  357. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  358. const char *sig_oid;
  359. size_t sig_oid_len = 0;
  360. unsigned char *c, *c2;
  361. unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
  362. size_t hash_length = 0;
  363. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  364. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  365. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  366. psa_algorithm_t psa_algorithm;
  367. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  368. size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
  369. size_t len = 0;
  370. mbedtls_pk_type_t pk_alg;
  371. int write_sig_null_par;
  372. /*
  373. * Prepare data to be signed at the end of the target buffer
  374. */
  375. c = buf + size;
  376. /* Signature algorithm needed in TBS, and later for actual signature */
  377. /* There's no direct way of extracting a signature algorithm
  378. * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
  379. if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
  380. pk_alg = MBEDTLS_PK_RSA;
  381. } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
  382. pk_alg = MBEDTLS_PK_ECDSA;
  383. } else {
  384. return MBEDTLS_ERR_X509_INVALID_ALG;
  385. }
  386. if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
  387. &sig_oid, &sig_oid_len)) != 0) {
  388. return ret;
  389. }
  390. /*
  391. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  392. */
  393. /* Only for v3 */
  394. if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
  395. MBEDTLS_ASN1_CHK_ADD(len,
  396. mbedtls_x509_write_extensions(&c,
  397. buf, ctx->extensions));
  398. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  399. MBEDTLS_ASN1_CHK_ADD(len,
  400. mbedtls_asn1_write_tag(&c, buf,
  401. MBEDTLS_ASN1_CONSTRUCTED |
  402. MBEDTLS_ASN1_SEQUENCE));
  403. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  404. MBEDTLS_ASN1_CHK_ADD(len,
  405. mbedtls_asn1_write_tag(&c, buf,
  406. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  407. MBEDTLS_ASN1_CONSTRUCTED | 3));
  408. }
  409. /*
  410. * SubjectPublicKeyInfo
  411. */
  412. MBEDTLS_ASN1_CHK_ADD(pub_len,
  413. mbedtls_pk_write_pubkey_der(ctx->subject_key,
  414. buf, (size_t) (c - buf)));
  415. c -= pub_len;
  416. len += pub_len;
  417. /*
  418. * Subject ::= Name
  419. */
  420. MBEDTLS_ASN1_CHK_ADD(len,
  421. mbedtls_x509_write_names(&c, buf,
  422. ctx->subject));
  423. /*
  424. * Validity ::= SEQUENCE {
  425. * notBefore Time,
  426. * notAfter Time }
  427. */
  428. sub_len = 0;
  429. MBEDTLS_ASN1_CHK_ADD(sub_len,
  430. x509_write_time(&c, buf, ctx->not_after,
  431. MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
  432. MBEDTLS_ASN1_CHK_ADD(sub_len,
  433. x509_write_time(&c, buf, ctx->not_before,
  434. MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
  435. len += sub_len;
  436. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
  437. MBEDTLS_ASN1_CHK_ADD(len,
  438. mbedtls_asn1_write_tag(&c, buf,
  439. MBEDTLS_ASN1_CONSTRUCTED |
  440. MBEDTLS_ASN1_SEQUENCE));
  441. /*
  442. * Issuer ::= Name
  443. */
  444. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
  445. ctx->issuer));
  446. /*
  447. * Signature ::= AlgorithmIdentifier
  448. */
  449. if (pk_alg == MBEDTLS_PK_ECDSA) {
  450. /*
  451. * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
  452. * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
  453. * https://www.rfc-editor.org/rfc/rfc5758#section-3.
  454. */
  455. write_sig_null_par = 0;
  456. } else {
  457. write_sig_null_par = 1;
  458. }
  459. MBEDTLS_ASN1_CHK_ADD(len,
  460. mbedtls_asn1_write_algorithm_identifier_ext(&c, buf,
  461. sig_oid, strlen(sig_oid),
  462. 0, write_sig_null_par));
  463. /*
  464. * Serial ::= INTEGER
  465. *
  466. * Written data is:
  467. * - "ctx->serial_len" bytes for the raw serial buffer
  468. * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
  469. * - 1 byte for the length
  470. * - 1 byte for the TAG
  471. */
  472. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
  473. ctx->serial, ctx->serial_len));
  474. if (*c & 0x80) {
  475. if (c - buf < 1) {
  476. return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  477. }
  478. *(--c) = 0x0;
  479. len++;
  480. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
  481. ctx->serial_len + 1));
  482. } else {
  483. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
  484. ctx->serial_len));
  485. }
  486. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
  487. MBEDTLS_ASN1_INTEGER));
  488. /*
  489. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  490. */
  491. /* Can be omitted for v1 */
  492. if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
  493. sub_len = 0;
  494. MBEDTLS_ASN1_CHK_ADD(sub_len,
  495. mbedtls_asn1_write_int(&c, buf, ctx->version));
  496. len += sub_len;
  497. MBEDTLS_ASN1_CHK_ADD(len,
  498. mbedtls_asn1_write_len(&c, buf, sub_len));
  499. MBEDTLS_ASN1_CHK_ADD(len,
  500. mbedtls_asn1_write_tag(&c, buf,
  501. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  502. MBEDTLS_ASN1_CONSTRUCTED | 0));
  503. }
  504. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  505. MBEDTLS_ASN1_CHK_ADD(len,
  506. mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  507. MBEDTLS_ASN1_SEQUENCE));
  508. /*
  509. * Make signature
  510. */
  511. /* Compute hash of CRT. */
  512. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  513. psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg);
  514. status = psa_hash_compute(psa_algorithm,
  515. c,
  516. len,
  517. hash,
  518. sizeof(hash),
  519. &hash_length);
  520. if (status != PSA_SUCCESS) {
  521. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  522. }
  523. #else
  524. if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
  525. len, hash)) != 0) {
  526. return ret;
  527. }
  528. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  529. if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
  530. hash, hash_length, sig, sizeof(sig), &sig_len,
  531. f_rng, p_rng)) != 0) {
  532. return ret;
  533. }
  534. /* Move CRT to the front of the buffer to have space
  535. * for the signature. */
  536. memmove(buf, c, len);
  537. c = buf + len;
  538. /* Add signature at the end of the buffer,
  539. * making sure that it doesn't underflow
  540. * into the CRT buffer. */
  541. c2 = buf + size;
  542. MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
  543. sig_oid, sig_oid_len,
  544. sig, sig_len, pk_alg));
  545. /*
  546. * Memory layout after this step:
  547. *
  548. * buf c=buf+len c2 buf+size
  549. * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
  550. */
  551. /* Move raw CRT to just before the signature. */
  552. c = c2 - len;
  553. memmove(c, buf, len);
  554. len += sig_and_oid_len;
  555. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  556. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
  557. MBEDTLS_ASN1_CONSTRUCTED |
  558. MBEDTLS_ASN1_SEQUENCE));
  559. return (int) len;
  560. }
  561. #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
  562. #define PEM_END_CRT "-----END CERTIFICATE-----\n"
  563. #if defined(MBEDTLS_PEM_WRITE_C)
  564. int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
  565. unsigned char *buf, size_t size,
  566. int (*f_rng)(void *, unsigned char *, size_t),
  567. void *p_rng)
  568. {
  569. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  570. size_t olen;
  571. if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
  572. f_rng, p_rng)) < 0) {
  573. return ret;
  574. }
  575. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
  576. buf + size - ret, ret,
  577. buf, size, &olen)) != 0) {
  578. return ret;
  579. }
  580. return 0;
  581. }
  582. #endif /* MBEDTLS_PEM_WRITE_C */
  583. #endif /* MBEDTLS_X509_CRT_WRITE_C */