pkwrite.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_PK_WRITE_C)
  9. #include "mbedtls/pk.h"
  10. #include "mbedtls/asn1write.h"
  11. #include "mbedtls/oid.h"
  12. #include "mbedtls/platform_util.h"
  13. #include "mbedtls/error.h"
  14. #include "pk_internal.h"
  15. #include <string.h>
  16. #if defined(MBEDTLS_ECP_C)
  17. #include "mbedtls/bignum.h"
  18. #include "mbedtls/ecp.h"
  19. #include "mbedtls/platform_util.h"
  20. #endif
  21. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  22. #include "pk_internal.h"
  23. #endif
  24. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  25. #include "pkwrite.h"
  26. #endif
  27. #if defined(MBEDTLS_PEM_WRITE_C)
  28. #include "mbedtls/pem.h"
  29. #endif
  30. #if defined(MBEDTLS_RSA_C)
  31. #include "rsa_internal.h"
  32. #endif
  33. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  34. #include "psa/crypto.h"
  35. #include "psa_util_internal.h"
  36. #endif
  37. #include "mbedtls/platform.h"
  38. /* Helpers for properly sizing buffers aimed at holding public keys or
  39. * key-pairs based on build symbols. */
  40. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  41. #define PK_MAX_EC_PUBLIC_KEY_SIZE PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
  42. #define PK_MAX_EC_KEY_PAIR_SIZE MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH
  43. #elif defined(MBEDTLS_USE_PSA_CRYPTO)
  44. #define PK_MAX_EC_PUBLIC_KEY_SIZE PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
  45. #define PK_MAX_EC_KEY_PAIR_SIZE MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH
  46. #else
  47. #define PK_MAX_EC_PUBLIC_KEY_SIZE MBEDTLS_ECP_MAX_PT_LEN
  48. #define PK_MAX_EC_KEY_PAIR_SIZE MBEDTLS_ECP_MAX_BYTES
  49. #endif
  50. /******************************************************************************
  51. * Internal functions for RSA keys.
  52. ******************************************************************************/
  53. #if defined(MBEDTLS_RSA_C)
  54. static int pk_write_rsa_der(unsigned char **p, unsigned char *buf,
  55. const mbedtls_pk_context *pk)
  56. {
  57. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  58. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  59. uint8_t tmp[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
  60. size_t len = 0, tmp_len = 0;
  61. if (psa_export_key(pk->priv_id, tmp, sizeof(tmp), &tmp_len) != PSA_SUCCESS) {
  62. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  63. }
  64. *p -= tmp_len;
  65. memcpy(*p, tmp, tmp_len);
  66. len += tmp_len;
  67. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  68. return (int) len;
  69. }
  70. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  71. return mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk), buf, p);
  72. }
  73. #endif /* MBEDTLS_RSA_C */
  74. /******************************************************************************
  75. * Internal functions for EC keys.
  76. ******************************************************************************/
  77. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  78. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  79. static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
  80. const mbedtls_pk_context *pk)
  81. {
  82. size_t len = 0;
  83. uint8_t buf[PK_MAX_EC_PUBLIC_KEY_SIZE];
  84. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  85. if (psa_export_public_key(pk->priv_id, buf, sizeof(buf), &len) != PSA_SUCCESS) {
  86. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  87. }
  88. } else {
  89. len = pk->pub_raw_len;
  90. memcpy(buf, pk->pub_raw, len);
  91. }
  92. if (*p < start || (size_t) (*p - start) < len) {
  93. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  94. }
  95. *p -= len;
  96. memcpy(*p, buf, len);
  97. return (int) len;
  98. }
  99. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  100. static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
  101. const mbedtls_pk_context *pk)
  102. {
  103. size_t len = 0;
  104. unsigned char buf[PK_MAX_EC_PUBLIC_KEY_SIZE];
  105. mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
  106. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  107. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  108. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  109. if (psa_export_public_key(pk->priv_id, buf, sizeof(buf), &len) != PSA_SUCCESS) {
  110. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  111. }
  112. *p -= len;
  113. memcpy(*p, buf, len);
  114. return (int) len;
  115. } else
  116. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  117. {
  118. if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
  119. MBEDTLS_ECP_PF_UNCOMPRESSED,
  120. &len, buf, sizeof(buf))) != 0) {
  121. return ret;
  122. }
  123. }
  124. if (*p < start || (size_t) (*p - start) < len) {
  125. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  126. }
  127. *p -= len;
  128. memcpy(*p, buf, len);
  129. return (int) len;
  130. }
  131. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  132. /*
  133. * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
  134. */
  135. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  136. static int pk_write_ec_private(unsigned char **p, unsigned char *start,
  137. const mbedtls_pk_context *pk)
  138. {
  139. size_t byte_length;
  140. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  141. unsigned char tmp[PK_MAX_EC_KEY_PAIR_SIZE];
  142. psa_status_t status;
  143. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  144. status = psa_export_key(pk->priv_id, tmp, sizeof(tmp), &byte_length);
  145. if (status != PSA_SUCCESS) {
  146. ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
  147. return ret;
  148. }
  149. } else {
  150. status = psa_export_key(pk->priv_id, tmp, sizeof(tmp), &byte_length);
  151. if (status != PSA_SUCCESS) {
  152. ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
  153. goto exit;
  154. }
  155. }
  156. ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
  157. exit:
  158. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  159. return ret;
  160. }
  161. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  162. static int pk_write_ec_private(unsigned char **p, unsigned char *start,
  163. const mbedtls_pk_context *pk)
  164. {
  165. size_t byte_length;
  166. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  167. unsigned char tmp[PK_MAX_EC_KEY_PAIR_SIZE];
  168. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  169. psa_status_t status;
  170. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  171. status = psa_export_key(pk->priv_id, tmp, sizeof(tmp), &byte_length);
  172. if (status != PSA_SUCCESS) {
  173. ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
  174. return ret;
  175. }
  176. } else
  177. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  178. {
  179. mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
  180. byte_length = (ec->grp.pbits + 7) / 8;
  181. ret = mbedtls_ecp_write_key_ext(ec, &byte_length, tmp, sizeof(tmp));
  182. if (ret != 0) {
  183. goto exit;
  184. }
  185. }
  186. ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
  187. exit:
  188. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  189. return ret;
  190. }
  191. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  192. /*
  193. * ECParameters ::= CHOICE {
  194. * namedCurve OBJECT IDENTIFIER
  195. * }
  196. */
  197. static int pk_write_ec_param(unsigned char **p, unsigned char *start,
  198. mbedtls_ecp_group_id grp_id)
  199. {
  200. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  201. size_t len = 0;
  202. const char *oid;
  203. size_t oid_len;
  204. if ((ret = mbedtls_oid_get_oid_by_ec_grp(grp_id, &oid, &oid_len)) != 0) {
  205. return ret;
  206. }
  207. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
  208. return (int) len;
  209. }
  210. #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
  211. /*
  212. * RFC8410 section 7
  213. *
  214. * OneAsymmetricKey ::= SEQUENCE {
  215. * version Version,
  216. * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  217. * privateKey PrivateKey,
  218. * attributes [0] IMPLICIT Attributes OPTIONAL,
  219. * ...,
  220. * [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
  221. * ...
  222. * }
  223. * ...
  224. * CurvePrivateKey ::= OCTET STRING
  225. */
  226. static int pk_write_ec_rfc8410_der(unsigned char **p, unsigned char *buf,
  227. const mbedtls_pk_context *pk)
  228. {
  229. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  230. size_t len = 0;
  231. size_t oid_len = 0;
  232. const char *oid;
  233. mbedtls_ecp_group_id grp_id;
  234. /* privateKey */
  235. MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(p, buf, pk));
  236. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
  237. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_OCTET_STRING));
  238. grp_id = mbedtls_pk_get_ec_group_id(pk);
  239. /* privateKeyAlgorithm */
  240. if ((ret = mbedtls_oid_get_oid_by_ec_grp_algid(grp_id, &oid, &oid_len)) != 0) {
  241. return ret;
  242. }
  243. MBEDTLS_ASN1_CHK_ADD(len,
  244. mbedtls_asn1_write_algorithm_identifier_ext(p, buf, oid, oid_len, 0, 0));
  245. /* version */
  246. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, buf, 0));
  247. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
  248. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_CONSTRUCTED |
  249. MBEDTLS_ASN1_SEQUENCE));
  250. return (int) len;
  251. }
  252. #endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
  253. /*
  254. * RFC 5915, or SEC1 Appendix C.4
  255. *
  256. * ECPrivateKey ::= SEQUENCE {
  257. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  258. * privateKey OCTET STRING,
  259. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  260. * publicKey [1] BIT STRING OPTIONAL
  261. * }
  262. */
  263. static int pk_write_ec_der(unsigned char **p, unsigned char *buf,
  264. const mbedtls_pk_context *pk)
  265. {
  266. size_t len = 0;
  267. int ret;
  268. size_t pub_len = 0, par_len = 0;
  269. mbedtls_ecp_group_id grp_id;
  270. /* publicKey */
  271. MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(p, buf, pk));
  272. if (*p - buf < 1) {
  273. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  274. }
  275. (*p)--;
  276. **p = 0;
  277. pub_len += 1;
  278. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(p, buf, pub_len));
  279. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_BIT_STRING));
  280. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(p, buf, pub_len));
  281. MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(p, buf,
  282. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  283. MBEDTLS_ASN1_CONSTRUCTED | 1));
  284. len += pub_len;
  285. /* parameters */
  286. grp_id = mbedtls_pk_get_ec_group_id(pk);
  287. MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(p, buf, grp_id));
  288. MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(p, buf, par_len));
  289. MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(p, buf,
  290. MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  291. MBEDTLS_ASN1_CONSTRUCTED | 0));
  292. len += par_len;
  293. /* privateKey */
  294. MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(p, buf, pk));
  295. /* version */
  296. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, buf, 1));
  297. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
  298. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_CONSTRUCTED |
  299. MBEDTLS_ASN1_SEQUENCE));
  300. return (int) len;
  301. }
  302. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  303. /******************************************************************************
  304. * Internal functions for Opaque keys.
  305. ******************************************************************************/
  306. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  307. static int pk_write_opaque_pubkey(unsigned char **p, unsigned char *start,
  308. const mbedtls_pk_context *pk)
  309. {
  310. size_t buffer_size;
  311. size_t len = 0;
  312. if (*p < start) {
  313. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  314. }
  315. buffer_size = (size_t) (*p - start);
  316. if (psa_export_public_key(pk->priv_id, start, buffer_size,
  317. &len) != PSA_SUCCESS) {
  318. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  319. }
  320. *p -= len;
  321. memmove(*p, start, len);
  322. return (int) len;
  323. }
  324. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  325. /******************************************************************************
  326. * Generic helpers
  327. ******************************************************************************/
  328. /* Extend the public mbedtls_pk_get_type() by getting key type also in case of
  329. * opaque keys. */
  330. static mbedtls_pk_type_t pk_get_type_ext(const mbedtls_pk_context *pk)
  331. {
  332. mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
  333. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  334. if (pk_type == MBEDTLS_PK_OPAQUE) {
  335. psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT;
  336. psa_key_type_t opaque_key_type;
  337. if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) {
  338. return MBEDTLS_PK_NONE;
  339. }
  340. opaque_key_type = psa_get_key_type(&opaque_attrs);
  341. psa_reset_key_attributes(&opaque_attrs);
  342. if (PSA_KEY_TYPE_IS_ECC(opaque_key_type)) {
  343. return MBEDTLS_PK_ECKEY;
  344. } else if (PSA_KEY_TYPE_IS_RSA(opaque_key_type)) {
  345. return MBEDTLS_PK_RSA;
  346. } else {
  347. return MBEDTLS_PK_NONE;
  348. }
  349. } else
  350. #endif
  351. return pk_type;
  352. }
  353. /******************************************************************************
  354. * Public functions for writing private/public DER keys.
  355. ******************************************************************************/
  356. int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
  357. const mbedtls_pk_context *key)
  358. {
  359. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  360. size_t len = 0;
  361. #if defined(MBEDTLS_RSA_C)
  362. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
  363. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*key), start, p));
  364. } else
  365. #endif
  366. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  367. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
  368. MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, key));
  369. } else
  370. #endif
  371. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  372. if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) {
  373. MBEDTLS_ASN1_CHK_ADD(len, pk_write_opaque_pubkey(p, start, key));
  374. } else
  375. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  376. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  377. return (int) len;
  378. }
  379. int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  380. {
  381. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  382. unsigned char *c;
  383. int has_par = 1;
  384. size_t len = 0, par_len = 0, oid_len = 0;
  385. mbedtls_pk_type_t pk_type;
  386. const char *oid = NULL;
  387. if (size == 0) {
  388. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  389. }
  390. c = buf + size;
  391. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
  392. if (c - buf < 1) {
  393. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  394. }
  395. /*
  396. * SubjectPublicKeyInfo ::= SEQUENCE {
  397. * algorithm AlgorithmIdentifier,
  398. * subjectPublicKey BIT STRING }
  399. */
  400. *--c = 0;
  401. len += 1;
  402. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  403. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
  404. pk_type = pk_get_type_ext(key);
  405. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  406. if (pk_get_type_ext(key) == MBEDTLS_PK_ECKEY) {
  407. mbedtls_ecp_group_id ec_grp_id = mbedtls_pk_get_ec_group_id(key);
  408. if (MBEDTLS_PK_IS_RFC8410_GROUP_ID(ec_grp_id)) {
  409. ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec_grp_id, &oid, &oid_len);
  410. if (ret != 0) {
  411. return ret;
  412. }
  413. has_par = 0;
  414. } else {
  415. MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec_grp_id));
  416. }
  417. }
  418. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  419. /* At this point oid_len is not null only for EC Montgomery keys. */
  420. if (oid_len == 0) {
  421. ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid, &oid_len);
  422. if (ret != 0) {
  423. return ret;
  424. }
  425. }
  426. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier_ext(&c, buf, oid, oid_len,
  427. par_len, has_par));
  428. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
  429. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  430. MBEDTLS_ASN1_SEQUENCE));
  431. return (int) len;
  432. }
  433. int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  434. {
  435. unsigned char *c;
  436. if (size == 0) {
  437. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  438. }
  439. c = buf + size;
  440. #if defined(MBEDTLS_RSA_C)
  441. if (pk_get_type_ext(key) == MBEDTLS_PK_RSA) {
  442. return pk_write_rsa_der(&c, buf, key);
  443. } else
  444. #endif /* MBEDTLS_RSA_C */
  445. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  446. if (pk_get_type_ext(key) == MBEDTLS_PK_ECKEY) {
  447. #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
  448. if (mbedtls_pk_is_rfc8410(key)) {
  449. return pk_write_ec_rfc8410_der(&c, buf, key);
  450. }
  451. #endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
  452. return pk_write_ec_der(&c, buf, key);
  453. } else
  454. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  455. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  456. }
  457. /******************************************************************************
  458. * Public functions for wrinting private/public PEM keys.
  459. ******************************************************************************/
  460. #if defined(MBEDTLS_PEM_WRITE_C)
  461. #define PUB_DER_MAX_BYTES \
  462. (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES ? \
  463. MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES : MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
  464. #define PRV_DER_MAX_BYTES \
  465. (MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES > MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES ? \
  466. MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES : MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES)
  467. int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  468. {
  469. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  470. unsigned char *output_buf = NULL;
  471. output_buf = mbedtls_calloc(1, PUB_DER_MAX_BYTES);
  472. if (output_buf == NULL) {
  473. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  474. }
  475. size_t olen = 0;
  476. if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
  477. PUB_DER_MAX_BYTES)) < 0) {
  478. goto cleanup;
  479. }
  480. if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY "\n", PEM_END_PUBLIC_KEY "\n",
  481. output_buf + PUB_DER_MAX_BYTES - ret,
  482. ret, buf, size, &olen)) != 0) {
  483. goto cleanup;
  484. }
  485. ret = 0;
  486. cleanup:
  487. mbedtls_free(output_buf);
  488. return ret;
  489. }
  490. int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
  491. {
  492. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  493. unsigned char *output_buf = NULL;
  494. output_buf = mbedtls_calloc(1, PRV_DER_MAX_BYTES);
  495. if (output_buf == NULL) {
  496. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  497. }
  498. const char *begin, *end;
  499. size_t olen = 0;
  500. if ((ret = mbedtls_pk_write_key_der(key, output_buf, PRV_DER_MAX_BYTES)) < 0) {
  501. goto cleanup;
  502. }
  503. #if defined(MBEDTLS_RSA_C)
  504. if (pk_get_type_ext(key) == MBEDTLS_PK_RSA) {
  505. begin = PEM_BEGIN_PRIVATE_KEY_RSA "\n";
  506. end = PEM_END_PRIVATE_KEY_RSA "\n";
  507. } else
  508. #endif
  509. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  510. if (pk_get_type_ext(key) == MBEDTLS_PK_ECKEY) {
  511. if (mbedtls_pk_is_rfc8410(key)) {
  512. begin = PEM_BEGIN_PRIVATE_KEY_PKCS8 "\n";
  513. end = PEM_END_PRIVATE_KEY_PKCS8 "\n";
  514. } else {
  515. begin = PEM_BEGIN_PRIVATE_KEY_EC "\n";
  516. end = PEM_END_PRIVATE_KEY_EC "\n";
  517. }
  518. } else
  519. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  520. {
  521. ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  522. goto cleanup;
  523. }
  524. if ((ret = mbedtls_pem_write_buffer(begin, end,
  525. output_buf + PRV_DER_MAX_BYTES - ret,
  526. ret, buf, size, &olen)) != 0) {
  527. goto cleanup;
  528. }
  529. ret = 0;
  530. cleanup:
  531. mbedtls_zeroize_and_free(output_buf, PRV_DER_MAX_BYTES);
  532. return ret;
  533. }
  534. #endif /* MBEDTLS_PEM_WRITE_C */
  535. #endif /* MBEDTLS_PK_WRITE_C */