x509write_crt.c 17 KB

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