x509write_crt.c 24 KB

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