x509write_crt.c 18 KB

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