pkwrite.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Public Key layer for writing key files and structures
  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. #include "common.h"
  20. #if defined(MBEDTLS_PK_WRITE_C)
  21. #include "mbedtls/pk.h"
  22. #include "mbedtls/asn1write.h"
  23. #include "mbedtls/oid.h"
  24. #include "mbedtls/platform_util.h"
  25. #include "mbedtls/error.h"
  26. #include <string.h>
  27. #if defined(MBEDTLS_RSA_C)
  28. #include "mbedtls/rsa.h"
  29. #endif
  30. #if defined(MBEDTLS_ECP_C)
  31. #include "mbedtls/bignum.h"
  32. #include "mbedtls/ecp.h"
  33. #include "mbedtls/platform_util.h"
  34. #endif
  35. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
  36. #include "pkwrite.h"
  37. #endif
  38. #if defined(MBEDTLS_ECDSA_C)
  39. #include "mbedtls/ecdsa.h"
  40. #endif
  41. #if defined(MBEDTLS_PEM_WRITE_C)
  42. #include "mbedtls/pem.h"
  43. #endif
  44. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  45. #include "psa/crypto.h"
  46. #include "mbedtls/psa_util.h"
  47. #endif
  48. #include "mbedtls/platform.h"
  49. #if defined(MBEDTLS_RSA_C)
  50. /*
  51. * RSAPublicKey ::= SEQUENCE {
  52. * modulus INTEGER, -- n
  53. * publicExponent INTEGER -- e
  54. * }
  55. */
  56. static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
  57. mbedtls_rsa_context *rsa)
  58. {
  59. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  60. size_t len = 0;
  61. mbedtls_mpi T;
  62. mbedtls_mpi_init(&T);
  63. /* Export E */
  64. if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
  65. (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
  66. goto end_of_export;
  67. }
  68. len += ret;
  69. /* Export N */
  70. if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
  71. (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
  72. goto end_of_export;
  73. }
  74. len += ret;
  75. end_of_export:
  76. mbedtls_mpi_free(&T);
  77. if (ret < 0) {
  78. return ret;
  79. }
  80. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  81. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  82. MBEDTLS_ASN1_SEQUENCE));
  83. return (int) len;
  84. }
  85. #endif /* MBEDTLS_RSA_C */
  86. #if defined(MBEDTLS_ECP_C)
  87. /*
  88. * EC public key is an EC point
  89. */
  90. static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
  91. mbedtls_ecp_keypair *ec)
  92. {
  93. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  94. size_t len = 0;
  95. unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
  96. if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
  97. MBEDTLS_ECP_PF_UNCOMPRESSED,
  98. &len, buf, sizeof(buf))) != 0) {
  99. return ret;
  100. }
  101. if (*p < start || (size_t) (*p - start) < len) {
  102. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  103. }
  104. *p -= len;
  105. memcpy(*p, buf, len);
  106. return (int) len;
  107. }
  108. /*
  109. * ECParameters ::= CHOICE {
  110. * namedCurve OBJECT IDENTIFIER
  111. * }
  112. */
  113. static int pk_write_ec_param(unsigned char **p, unsigned char *start,
  114. mbedtls_ecp_keypair *ec)
  115. {
  116. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  117. size_t len = 0;
  118. const char *oid;
  119. size_t oid_len;
  120. if ((ret = mbedtls_oid_get_oid_by_ec_grp(ec->grp.id, &oid, &oid_len)) != 0) {
  121. return ret;
  122. }
  123. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
  124. return (int) len;
  125. }
  126. /*
  127. * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
  128. */
  129. static int pk_write_ec_private(unsigned char **p, unsigned char *start,
  130. mbedtls_ecp_keypair *ec)
  131. {
  132. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  133. size_t byte_length = (ec->grp.pbits + 7) / 8;
  134. unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
  135. ret = mbedtls_ecp_write_key(ec, tmp, byte_length);
  136. if (ret != 0) {
  137. goto exit;
  138. }
  139. ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
  140. exit:
  141. mbedtls_platform_zeroize(tmp, byte_length);
  142. return ret;
  143. }
  144. #endif /* MBEDTLS_ECP_C */
  145. int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
  146. const mbedtls_pk_context *key)
  147. {
  148. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  149. size_t len = 0;
  150. #if defined(MBEDTLS_RSA_C)
  151. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
  152. MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key)));
  153. } else
  154. #endif
  155. #if defined(MBEDTLS_ECP_C)
  156. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
  157. MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key)));
  158. } else
  159. #endif
  160. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  161. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) {
  162. size_t buffer_size;
  163. mbedtls_svc_key_id_t *key_id = (mbedtls_svc_key_id_t *) key->pk_ctx;
  164. if (*p < start) {
  165. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  166. }
  167. buffer_size = (size_t) (*p - start);
  168. if (psa_export_public_key(*key_id, start, buffer_size, &len)
  169. != PSA_SUCCESS) {
  170. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  171. } else {
  172. *p -= len;
  173. memmove(*p, start, len);
  174. }
  175. } else
  176. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  177. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  178. return (int) len;
  179. }
  180. int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  181. {
  182. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  183. unsigned char *c;
  184. size_t len = 0, par_len = 0, oid_len;
  185. mbedtls_pk_type_t pk_type;
  186. const char *oid;
  187. if (size == 0) {
  188. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  189. }
  190. c = buf + size;
  191. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
  192. if (c - buf < 1) {
  193. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  194. }
  195. /*
  196. * SubjectPublicKeyInfo ::= SEQUENCE {
  197. * algorithm AlgorithmIdentifier,
  198. * subjectPublicKey BIT STRING }
  199. */
  200. *--c = 0;
  201. len += 1;
  202. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  203. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
  204. pk_type = mbedtls_pk_get_type(key);
  205. #if defined(MBEDTLS_ECP_C)
  206. if (pk_type == MBEDTLS_PK_ECKEY) {
  207. MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, mbedtls_pk_ec(*key)));
  208. }
  209. #endif
  210. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  211. if (pk_type == MBEDTLS_PK_OPAQUE) {
  212. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  213. psa_key_type_t key_type;
  214. mbedtls_svc_key_id_t key_id;
  215. psa_ecc_family_t curve;
  216. size_t bits;
  217. key_id = *((mbedtls_svc_key_id_t *) key->pk_ctx);
  218. if (PSA_SUCCESS != psa_get_key_attributes(key_id, &attributes)) {
  219. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  220. }
  221. key_type = psa_get_key_type(&attributes);
  222. bits = psa_get_key_bits(&attributes);
  223. psa_reset_key_attributes(&attributes);
  224. if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
  225. curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
  226. if (curve == 0) {
  227. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  228. }
  229. ret = mbedtls_psa_get_ecc_oid_from_id(curve, bits,
  230. &oid, &oid_len);
  231. if (ret != 0) {
  232. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  233. }
  234. /* Write EC algorithm parameters; that's akin
  235. * to pk_write_ec_param() above. */
  236. MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_oid(&c, buf,
  237. oid,
  238. oid_len));
  239. /* The rest of the function works as for legacy EC contexts. */
  240. pk_type = MBEDTLS_PK_ECKEY;
  241. } else if (PSA_KEY_TYPE_IS_RSA(key_type)) {
  242. /* The rest of the function works as for legacy RSA contexts. */
  243. pk_type = MBEDTLS_PK_RSA;
  244. } else {
  245. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  246. }
  247. }
  248. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  249. if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid,
  250. &oid_len)) != 0) {
  251. return ret;
  252. }
  253. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(&c, buf, oid, oid_len,
  254. par_len));
  255. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  256. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  257. MBEDTLS_ASN1_SEQUENCE));
  258. return (int) len;
  259. }
  260. int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  261. {
  262. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  263. unsigned char *c;
  264. size_t len = 0;
  265. if (size == 0) {
  266. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  267. }
  268. c = buf + size;
  269. #if defined(MBEDTLS_RSA_C)
  270. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
  271. mbedtls_mpi T; /* Temporary holding the exported parameters */
  272. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
  273. /*
  274. * Export the parameters one after another to avoid simultaneous copies.
  275. */
  276. mbedtls_mpi_init(&T);
  277. /* Export QP */
  278. if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
  279. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  280. goto end_of_export;
  281. }
  282. len += ret;
  283. /* Export DQ */
  284. if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
  285. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  286. goto end_of_export;
  287. }
  288. len += ret;
  289. /* Export DP */
  290. if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
  291. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  292. goto end_of_export;
  293. }
  294. len += ret;
  295. /* Export Q */
  296. if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
  297. &T, NULL, NULL)) != 0 ||
  298. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  299. goto end_of_export;
  300. }
  301. len += ret;
  302. /* Export P */
  303. if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
  304. NULL, NULL, NULL)) != 0 ||
  305. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  306. goto end_of_export;
  307. }
  308. len += ret;
  309. /* Export D */
  310. if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
  311. NULL, &T, NULL)) != 0 ||
  312. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  313. goto end_of_export;
  314. }
  315. len += ret;
  316. /* Export E */
  317. if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
  318. NULL, NULL, &T)) != 0 ||
  319. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  320. goto end_of_export;
  321. }
  322. len += ret;
  323. /* Export N */
  324. if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
  325. NULL, NULL, NULL)) != 0 ||
  326. (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
  327. goto end_of_export;
  328. }
  329. len += ret;
  330. end_of_export:
  331. mbedtls_mpi_free(&T);
  332. if (ret < 0) {
  333. return ret;
  334. }
  335. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
  336. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  337. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c,
  338. buf, MBEDTLS_ASN1_CONSTRUCTED |
  339. MBEDTLS_ASN1_SEQUENCE));
  340. } else
  341. #endif /* MBEDTLS_RSA_C */
  342. #if defined(MBEDTLS_ECP_C)
  343. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
  344. mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
  345. size_t pub_len = 0, par_len = 0;
  346. /*
  347. * RFC 5915, or SEC1 Appendix C.4
  348. *
  349. * ECPrivateKey ::= SEQUENCE {
  350. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  351. * privateKey OCTET STRING,
  352. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  353. * publicKey [1] BIT STRING OPTIONAL
  354. * }
  355. */
  356. /* publicKey */
  357. MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec));
  358. if (c - buf < 1) {
  359. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  360. }
  361. *--c = 0;
  362. pub_len += 1;
  363. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
  364. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
  365. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
  366. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf,
  367. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  368. MBEDTLS_ASN1_CONSTRUCTED | 1));
  369. len += pub_len;
  370. /* parameters */
  371. MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec));
  372. MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
  373. MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf,
  374. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  375. MBEDTLS_ASN1_CONSTRUCTED | 0));
  376. len += par_len;
  377. /* privateKey */
  378. MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec));
  379. /* version */
  380. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
  381. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  382. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  383. MBEDTLS_ASN1_SEQUENCE));
  384. } else
  385. #endif /* MBEDTLS_ECP_C */
  386. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  387. return (int) len;
  388. }
  389. #if defined(MBEDTLS_PEM_WRITE_C)
  390. #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
  391. #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
  392. #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
  393. #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
  394. #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
  395. #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
  396. #define PUB_DER_MAX_BYTES \
  397. (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES ? \
  398. MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES : MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
  399. #define PRV_DER_MAX_BYTES \
  400. (MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES > MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES ? \
  401. MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES : MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES)
  402. int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  403. {
  404. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  405. unsigned char output_buf[PUB_DER_MAX_BYTES];
  406. size_t olen = 0;
  407. if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
  408. sizeof(output_buf))) < 0) {
  409. return ret;
  410. }
  411. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
  412. output_buf + sizeof(output_buf) - ret,
  413. ret, buf, size, &olen)) != 0) {
  414. return ret;
  415. }
  416. return 0;
  417. }
  418. int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  419. {
  420. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  421. unsigned char output_buf[PRV_DER_MAX_BYTES];
  422. const char *begin, *end;
  423. size_t olen = 0;
  424. if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) {
  425. return ret;
  426. }
  427. #if defined(MBEDTLS_RSA_C)
  428. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
  429. begin = PEM_BEGIN_PRIVATE_KEY_RSA;
  430. end = PEM_END_PRIVATE_KEY_RSA;
  431. } else
  432. #endif
  433. #if defined(MBEDTLS_ECP_C)
  434. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
  435. begin = PEM_BEGIN_PRIVATE_KEY_EC;
  436. end = PEM_END_PRIVATE_KEY_EC;
  437. } else
  438. #endif
  439. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  440. if ((ret = mbedtls_pem_write_buffer(begin, end,
  441. output_buf + sizeof(output_buf) - ret,
  442. ret, buf, size, &olen)) != 0) {
  443. return ret;
  444. }
  445. return 0;
  446. }
  447. #endif /* MBEDTLS_PEM_WRITE_C */
  448. #endif /* MBEDTLS_PK_WRITE_C */