x509write_crt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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/sha1.h"
  33. #include <string.h>
  34. #if defined(MBEDTLS_PEM_WRITE_C)
  35. #include "mbedtls/pem.h"
  36. #endif /* MBEDTLS_PEM_WRITE_C */
  37. void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
  38. {
  39. memset(ctx, 0, sizeof(mbedtls_x509write_cert));
  40. mbedtls_mpi_init(&ctx->serial);
  41. ctx->version = MBEDTLS_X509_CRT_VERSION_3;
  42. }
  43. void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
  44. {
  45. mbedtls_mpi_free(&ctx->serial);
  46. mbedtls_asn1_free_named_data_list(&ctx->subject);
  47. mbedtls_asn1_free_named_data_list(&ctx->issuer);
  48. mbedtls_asn1_free_named_data_list(&ctx->extensions);
  49. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
  50. }
  51. void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
  52. int version)
  53. {
  54. ctx->version = version;
  55. }
  56. void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
  57. mbedtls_md_type_t md_alg)
  58. {
  59. ctx->md_alg = md_alg;
  60. }
  61. void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
  62. mbedtls_pk_context *key)
  63. {
  64. ctx->subject_key = key;
  65. }
  66. void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
  67. mbedtls_pk_context *key)
  68. {
  69. ctx->issuer_key = key;
  70. }
  71. int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
  72. const char *subject_name)
  73. {
  74. return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
  75. }
  76. int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
  77. const char *issuer_name)
  78. {
  79. return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
  80. }
  81. int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
  82. const mbedtls_mpi *serial)
  83. {
  84. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  85. if (mbedtls_mpi_size(serial) > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
  86. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  87. }
  88. if ((ret = mbedtls_mpi_copy(&ctx->serial, serial)) != 0) {
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
  94. const char *not_before,
  95. const char *not_after)
  96. {
  97. if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
  98. strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
  99. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  100. }
  101. strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
  102. strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
  103. ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  104. ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  105. return 0;
  106. }
  107. int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
  108. const char *oid, size_t oid_len,
  109. int critical,
  110. const unsigned char *val, size_t val_len)
  111. {
  112. return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
  113. critical, val, val_len);
  114. }
  115. int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
  116. int is_ca, int max_pathlen)
  117. {
  118. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  119. unsigned char buf[9];
  120. unsigned char *c = buf + sizeof(buf);
  121. size_t len = 0;
  122. memset(buf, 0, sizeof(buf));
  123. if (is_ca && max_pathlen > 127) {
  124. return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  125. }
  126. if (is_ca) {
  127. if (max_pathlen >= 0) {
  128. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
  129. max_pathlen));
  130. }
  131. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
  132. }
  133. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  134. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
  135. MBEDTLS_ASN1_CONSTRUCTED |
  136. MBEDTLS_ASN1_SEQUENCE));
  137. return
  138. mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
  139. MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
  140. is_ca, buf + sizeof(buf) - len, len);
  141. }
  142. #if defined(MBEDTLS_SHA1_C)
  143. int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
  144. {
  145. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  146. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  147. unsigned char *c = buf + sizeof(buf);
  148. size_t len = 0;
  149. memset(buf, 0, sizeof(buf));
  150. MBEDTLS_ASN1_CHK_ADD(len,
  151. mbedtls_pk_write_pubkey(&c, buf, ctx->subject_key));
  152. ret = mbedtls_sha1_ret(buf + sizeof(buf) - len, len,
  153. buf + sizeof(buf) - 20);
  154. if (ret != 0) {
  155. return ret;
  156. }
  157. c = buf + sizeof(buf) - 20;
  158. len = 20;
  159. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  160. MBEDTLS_ASN1_CHK_ADD(len,
  161. mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OCTET_STRING));
  162. return mbedtls_x509write_crt_set_extension(ctx,
  163. MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
  164. MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
  165. 0, buf + sizeof(buf) - len, len);
  166. }
  167. int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
  168. {
  169. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  170. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  171. unsigned char *c = buf + sizeof(buf);
  172. size_t len = 0;
  173. memset(buf, 0, sizeof(buf));
  174. MBEDTLS_ASN1_CHK_ADD(len,
  175. mbedtls_pk_write_pubkey(&c, buf, ctx->issuer_key));
  176. ret = mbedtls_sha1_ret(buf + sizeof(buf) - len, len,
  177. buf + sizeof(buf) - 20);
  178. if (ret != 0) {
  179. return ret;
  180. }
  181. c = buf + sizeof(buf) - 20;
  182. len = 20;
  183. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  184. MBEDTLS_ASN1_CHK_ADD(len,
  185. mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
  186. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  187. MBEDTLS_ASN1_CHK_ADD(len,
  188. mbedtls_asn1_write_tag(&c, buf,
  189. MBEDTLS_ASN1_CONSTRUCTED |
  190. MBEDTLS_ASN1_SEQUENCE));
  191. return mbedtls_x509write_crt_set_extension(
  192. ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
  193. MBEDTLS_OID_SIZE(MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
  194. 0, buf + sizeof(buf) - len, len);
  195. }
  196. #endif /* MBEDTLS_SHA1_C */
  197. int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
  198. unsigned int key_usage)
  199. {
  200. unsigned char buf[5] = { 0 }, ku[2] = { 0 };
  201. unsigned char *c;
  202. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  203. const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
  204. MBEDTLS_X509_KU_NON_REPUDIATION |
  205. MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
  206. MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
  207. MBEDTLS_X509_KU_KEY_AGREEMENT |
  208. MBEDTLS_X509_KU_KEY_CERT_SIGN |
  209. MBEDTLS_X509_KU_CRL_SIGN |
  210. MBEDTLS_X509_KU_ENCIPHER_ONLY |
  211. MBEDTLS_X509_KU_DECIPHER_ONLY;
  212. /* Check that nothing other than the allowed flags is set */
  213. if ((key_usage & ~allowed_bits) != 0) {
  214. return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  215. }
  216. c = buf + 5;
  217. MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
  218. ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
  219. if (ret < 0) {
  220. return ret;
  221. } else if (ret < 3 || ret > 5) {
  222. return MBEDTLS_ERR_X509_INVALID_FORMAT;
  223. }
  224. ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
  225. MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
  226. 1, c, (size_t) ret);
  227. if (ret != 0) {
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
  233. unsigned char ns_cert_type)
  234. {
  235. unsigned char buf[4] = { 0 };
  236. unsigned char *c;
  237. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  238. c = buf + 4;
  239. ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
  240. if (ret < 3 || ret > 4) {
  241. return ret;
  242. }
  243. ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
  244. MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
  245. 0, c, (size_t) ret);
  246. if (ret != 0) {
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. static int x509_write_time(unsigned char **p, unsigned char *start,
  252. const char *t, size_t size)
  253. {
  254. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  255. size_t len = 0;
  256. /*
  257. * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
  258. */
  259. if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
  260. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  261. (const unsigned char *) t + 2,
  262. size - 2));
  263. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  264. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  265. MBEDTLS_ASN1_UTC_TIME));
  266. } else {
  267. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  268. (const unsigned char *) t,
  269. size));
  270. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  271. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  272. MBEDTLS_ASN1_GENERALIZED_TIME));
  273. }
  274. return (int) len;
  275. }
  276. int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
  277. unsigned char *buf, size_t size,
  278. int (*f_rng)(void *, unsigned char *, size_t),
  279. void *p_rng)
  280. {
  281. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  282. const char *sig_oid;
  283. size_t sig_oid_len = 0;
  284. unsigned char *c, *c2;
  285. unsigned char hash[64];
  286. unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
  287. size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
  288. size_t len = 0;
  289. mbedtls_pk_type_t pk_alg;
  290. /*
  291. * Prepare data to be signed at the end of the target buffer
  292. */
  293. c = buf + size;
  294. /* Signature algorithm needed in TBS, and later for actual signature */
  295. /* There's no direct way of extracting a signature algorithm
  296. * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
  297. if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
  298. pk_alg = MBEDTLS_PK_RSA;
  299. } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
  300. pk_alg = MBEDTLS_PK_ECDSA;
  301. } else {
  302. return MBEDTLS_ERR_X509_INVALID_ALG;
  303. }
  304. if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
  305. &sig_oid, &sig_oid_len)) != 0) {
  306. return ret;
  307. }
  308. /*
  309. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  310. */
  311. /* Only for v3 */
  312. if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
  313. MBEDTLS_ASN1_CHK_ADD(len,
  314. mbedtls_x509_write_extensions(&c,
  315. buf, ctx->extensions));
  316. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  317. MBEDTLS_ASN1_CHK_ADD(len,
  318. mbedtls_asn1_write_tag(&c, buf,
  319. MBEDTLS_ASN1_CONSTRUCTED |
  320. MBEDTLS_ASN1_SEQUENCE));
  321. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  322. MBEDTLS_ASN1_CHK_ADD(len,
  323. mbedtls_asn1_write_tag(&c, buf,
  324. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  325. MBEDTLS_ASN1_CONSTRUCTED | 3));
  326. }
  327. /*
  328. * SubjectPublicKeyInfo
  329. */
  330. MBEDTLS_ASN1_CHK_ADD(pub_len,
  331. mbedtls_pk_write_pubkey_der(ctx->subject_key,
  332. buf, c - buf));
  333. c -= pub_len;
  334. len += pub_len;
  335. /*
  336. * Subject ::= Name
  337. */
  338. MBEDTLS_ASN1_CHK_ADD(len,
  339. mbedtls_x509_write_names(&c, buf,
  340. ctx->subject));
  341. /*
  342. * Validity ::= SEQUENCE {
  343. * notBefore Time,
  344. * notAfter Time }
  345. */
  346. sub_len = 0;
  347. MBEDTLS_ASN1_CHK_ADD(sub_len,
  348. x509_write_time(&c, buf, ctx->not_after,
  349. MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
  350. MBEDTLS_ASN1_CHK_ADD(sub_len,
  351. x509_write_time(&c, buf, ctx->not_before,
  352. MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
  353. len += sub_len;
  354. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
  355. MBEDTLS_ASN1_CHK_ADD(len,
  356. mbedtls_asn1_write_tag(&c, buf,
  357. MBEDTLS_ASN1_CONSTRUCTED |
  358. MBEDTLS_ASN1_SEQUENCE));
  359. /*
  360. * Issuer ::= Name
  361. */
  362. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
  363. ctx->issuer));
  364. /*
  365. * Signature ::= AlgorithmIdentifier
  366. */
  367. MBEDTLS_ASN1_CHK_ADD(len,
  368. mbedtls_asn1_write_algorithm_identifier(&c, buf,
  369. sig_oid, strlen(sig_oid), 0));
  370. /*
  371. * Serial ::= INTEGER
  372. */
  373. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&c, buf,
  374. &ctx->serial));
  375. /*
  376. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  377. */
  378. /* Can be omitted for v1 */
  379. if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
  380. sub_len = 0;
  381. MBEDTLS_ASN1_CHK_ADD(sub_len,
  382. mbedtls_asn1_write_int(&c, buf, ctx->version));
  383. len += sub_len;
  384. MBEDTLS_ASN1_CHK_ADD(len,
  385. mbedtls_asn1_write_len(&c, buf, sub_len));
  386. MBEDTLS_ASN1_CHK_ADD(len,
  387. mbedtls_asn1_write_tag(&c, buf,
  388. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  389. MBEDTLS_ASN1_CONSTRUCTED | 0));
  390. }
  391. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  392. MBEDTLS_ASN1_CHK_ADD(len,
  393. mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  394. MBEDTLS_ASN1_SEQUENCE));
  395. /*
  396. * Make signature
  397. */
  398. /* Compute hash of CRT. */
  399. if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
  400. len, hash)) != 0) {
  401. return ret;
  402. }
  403. if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
  404. hash, 0, sig, &sig_len,
  405. f_rng, p_rng)) != 0) {
  406. return ret;
  407. }
  408. /* Move CRT to the front of the buffer to have space
  409. * for the signature. */
  410. memmove(buf, c, len);
  411. c = buf + len;
  412. /* Add signature at the end of the buffer,
  413. * making sure that it doesn't underflow
  414. * into the CRT buffer. */
  415. c2 = buf + size;
  416. MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
  417. sig_oid, sig_oid_len, sig,
  418. sig_len));
  419. /*
  420. * Memory layout after this step:
  421. *
  422. * buf c=buf+len c2 buf+size
  423. * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
  424. */
  425. /* Move raw CRT to just before the signature. */
  426. c = c2 - len;
  427. memmove(c, buf, len);
  428. len += sig_and_oid_len;
  429. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  430. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
  431. MBEDTLS_ASN1_CONSTRUCTED |
  432. MBEDTLS_ASN1_SEQUENCE));
  433. return (int) len;
  434. }
  435. #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
  436. #define PEM_END_CRT "-----END CERTIFICATE-----\n"
  437. #if defined(MBEDTLS_PEM_WRITE_C)
  438. int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
  439. unsigned char *buf, size_t size,
  440. int (*f_rng)(void *, unsigned char *, size_t),
  441. void *p_rng)
  442. {
  443. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  444. size_t olen;
  445. if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
  446. f_rng, p_rng)) < 0) {
  447. return ret;
  448. }
  449. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
  450. buf + size - ret, ret,
  451. buf, size, &olen)) != 0) {
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. #endif /* MBEDTLS_PEM_WRITE_C */
  457. #endif /* MBEDTLS_X509_CRT_WRITE_C */