x509write_crt.c 19 KB

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