blob: e89baaf256c7c4a3c57d98b82091be3da189c921 [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/*
2 * Public Key layer for writing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1write.h"
26#include "mbedtls/oid.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Valerio Setti77a75682023-05-15 11:18:46 +020029#include "pk_internal.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020030
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <string.h>
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ECP_C)
Gilles Peskine2700cfb2018-08-11 00:48:44 +020037#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Gilles Peskine2700cfb2018-08-11 00:48:44 +020039#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020040#endif
Valerio Setti4064dbb2023-05-17 15:33:07 +020041#if defined(MBEDTLS_ECP_LIGHT)
42#include "pk_internal.h"
43#endif
44#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_LIGHT)
Neil Armstronge0326a62022-02-25 08:57:19 +010045#include "pkwrite.h"
46#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020052#endif
53
Andrzej Kurek5fec0862018-11-19 10:07:36 -050054#if defined(MBEDTLS_USE_PSA_CRYPTO)
55#include "psa/crypto.h"
Hanno Becker65935d92019-02-01 11:55:03 +000056#include "mbedtls/psa_util.h"
Andrzej Kurek5fec0862018-11-19 10:07:36 -050057#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020059
Valerio Settie0e63112023-05-18 18:48:07 +020060/* Helper for Montgomery curves */
61#if defined(MBEDTLS_ECP_LIGHT) && defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
62static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
63{
64 mbedtls_ecp_group_id id = mbedtls_pk_get_group_id(pk);
65
66#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
67 if (id == MBEDTLS_ECP_DP_CURVE25519) {
68 return 1;
69 }
70#endif
71#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
72 if (id == MBEDTLS_ECP_DP_CURVE448) {
73 return 1;
74 }
75#endif
76 return 0;
77}
78#endif /* MBEDTLS_ECP_LIGHT && MBEDTLS_PK_HAVE_RFC8410_CURVES */
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020081/*
82 * RSAPublicKey ::= SEQUENCE {
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER -- e
85 * }
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
88 mbedtls_rsa_context *rsa)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020089{
Janos Follath24eed8d2019-11-22 13:21:35 +000090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020091 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010092 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +010095
96 /* Export E */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
98 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +010099 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100101 len += ret;
102
103 /* Export N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
105 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100106 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100108 len += ret;
109
110end_of_export:
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_mpi_free(&T);
113 if (ret < 0) {
114 return ret;
115 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
118 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
119 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124
Valerio Setti0d2980f2023-04-05 18:17:13 +0200125#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100126static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
Valerio Setti4064dbb2023-05-17 15:33:07 +0200127 const mbedtls_pk_context *pk)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200128{
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200129 size_t len = 0;
Valerio Setti4064dbb2023-05-17 15:33:07 +0200130
131#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
132 len = pk->pub_raw_len;
133
134 if (*p < start || (size_t) (*p - start) < len) {
135 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
136 }
137
138 memcpy(*p - len, pk->pub_raw, len);
139 *p -= len;
140#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Valerio Setti4064dbb2023-05-17 15:33:07 +0200142 mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
146 MBEDTLS_ECP_PF_UNCOMPRESSED,
147 &len, buf, sizeof(buf))) != 0) {
148 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200149 }
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*p < start || (size_t) (*p - start) < len) {
152 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
153 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154
155 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 memcpy(*p, buf, len);
Valerio Setti4064dbb2023-05-17 15:33:07 +0200157#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200160}
161
162/*
163 * ECParameters ::= CHOICE {
164 * namedCurve OBJECT IDENTIFIER
165 * }
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static int pk_write_ec_param(unsigned char **p, unsigned char *start,
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200168 mbedtls_ecp_group_id grp_id)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169{
Janos Follath24eed8d2019-11-22 13:21:35 +0000170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200171 size_t len = 0;
172 const char *oid;
173 size_t oid_len;
174
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200175 if ((ret = mbedtls_oid_get_oid_by_ec_grp(grp_id, &oid, &oid_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return ret;
177 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182}
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200183
184/*
185 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187static int pk_write_ec_private(unsigned char **p, unsigned char *start,
188 mbedtls_ecp_keypair *ec)
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200189{
Janos Follath24eed8d2019-11-22 13:21:35 +0000190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 size_t byte_length = (ec->grp.pbits + 7) / 8;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200192 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 ret = mbedtls_ecp_write_key(ec, tmp, byte_length);
195 if (ret != 0) {
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
198 ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200199
200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_platform_zeroize(tmp, byte_length);
202 return ret;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200203}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200204#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
207 const mbedtls_pk_context *key)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200208{
Janos Follath24eed8d2019-11-22 13:21:35 +0000209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210 size_t len = 0;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
214 MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key)));
215 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200216#endif
Valerio Setti0d2980f2023-04-05 18:17:13 +0200217#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Valerio Setti4064dbb2023-05-17 15:33:07 +0200219 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, key));
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221#endif
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) {
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500224 size_t buffer_size;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (*p < start) {
227 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500228 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100229
230 buffer_size = (size_t) (*p - start);
Valerio Setti4f387ef2023-05-02 14:15:59 +0200231 if (psa_export_public_key(key->priv_id, start, buffer_size, &len)
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 != PSA_SUCCESS) {
233 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
234 } else {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000235 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 memmove(*p, start, len);
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500237 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 } else
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200243}
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200246{
Janos Follath24eed8d2019-11-22 13:21:35 +0000247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248 unsigned char *c;
Jethro Beekman01672442023-04-19 14:08:14 +0200249 int has_par = 1;
250 size_t len = 0, par_len = 0, oid_len = 0;
Hanno Becker493c1712019-02-01 10:07:07 +0000251 mbedtls_pk_type_t pk_type;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200252#if defined(MBEDTLS_ECP_LIGHT)
Jethro Beekmancb706ea2023-05-04 12:28:49 +0200253 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200254#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200255 const char *oid;
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (size == 0) {
258 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
259 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500260
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200261 c = buf + size;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (c - buf < 1) {
266 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
267 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200268
269 /*
270 * SubjectPublicKeyInfo ::= SEQUENCE {
271 * algorithm AlgorithmIdentifier,
272 * subjectPublicKey BIT STRING }
273 */
274 *--c = 0;
275 len += 1;
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
278 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 pk_type = mbedtls_pk_get_type(key);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200281#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (pk_type == MBEDTLS_PK_ECKEY) {
Valerio Setti77a75682023-05-15 11:18:46 +0200283 ec_grp_id = mbedtls_pk_ec_ro(*key)->grp.id;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200284 }
Valerio Setti0d2980f2023-04-05 18:17:13 +0200285#endif /* MBEDTLS_ECP_LIGHT */
Hanno Becker493c1712019-02-01 10:07:07 +0000286#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (pk_type == MBEDTLS_PK_OPAQUE) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Becker493c1712019-02-01 10:07:07 +0000289 psa_key_type_t key_type;
Valerio Setti048cd442023-04-28 15:26:11 +0200290
Valerio Setti4f387ef2023-05-02 14:15:59 +0200291 if (PSA_SUCCESS != psa_get_key_attributes(key->priv_id,
Valerio Setti048cd442023-04-28 15:26:11 +0200292 &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
294 }
295 key_type = psa_get_key_type(&attributes);
Hanno Becker493c1712019-02-01 10:07:07 +0000296
Jethro Beekman13d415c2023-05-04 10:11:58 +0200297#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200299 psa_ecc_family_t curve;
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200302 if (curve != 0) {
303 ec_grp_id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&attributes), 0);
304 if (ec_grp_id != MBEDTLS_ECP_DP_NONE) {
305 /* The rest of the function works as for legacy EC contexts. */
306 pk_type = MBEDTLS_PK_ECKEY;
307 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200309 }
Jethro Beekman13d415c2023-05-04 10:11:58 +0200310#endif /* MBEDTLS_ECP_LIGHT */
311 if (PSA_KEY_TYPE_IS_RSA(key_type)) {
Neil Armstrong295aeb12022-03-15 16:25:41 +0100312 /* The rest of the function works as for legacy RSA contexts. */
313 pk_type = MBEDTLS_PK_RSA;
Neil Armstrong295aeb12022-03-15 16:25:41 +0100314 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200315
316 psa_reset_key_attributes(&attributes);
317 }
318 /* `pk_type` will have been changed to non-opaque by here if this function can handle it */
319 if (pk_type == MBEDTLS_PK_OPAQUE) {
320 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker493c1712019-02-01 10:07:07 +0000321 }
322#endif /* MBEDTLS_USE_PSA_CRYPTO */
323
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200324#if defined(MBEDTLS_ECP_LIGHT)
325 if (pk_type == MBEDTLS_PK_ECKEY) {
326 /* Some groups have their own AlgorithmIdentifier OID, others are handled by mbedtls_oid_get_oid_by_pk_alg() below */
327 ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec_grp_id, &oid, &oid_len);
328
329 if (ret == 0) {
330 /* Currently, none of the supported algorithms that have their own AlgorithmIdentifier OID have any parameters */
331 has_par = 0;
332 } else if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
333 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec_grp_id));
334 } else {
335 return ret;
336 }
337 }
338#endif /* MBEDTLS_ECP_LIGHT */
339
Jethro Beekman01672442023-04-19 14:08:14 +0200340 if (oid_len == 0) {
341 if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid,
342 &oid_len)) != 0) {
343 return ret;
344 }
Hanno Becker493c1712019-02-01 10:07:07 +0000345 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200346
Jethro Beekman01672442023-04-19 14:08:14 +0200347 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier_ext(&c, buf, oid, oid_len,
348 par_len, has_par));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
351 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
352 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200355}
356
Jethro Beekman01672442023-04-19 14:08:14 +0200357#if defined(MBEDTLS_ECP_LIGHT)
358#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
359/*
Valerio Setti4064dbb2023-05-17 15:33:07 +0200360 * RFC8410 section 7
Jethro Beekman01672442023-04-19 14:08:14 +0200361 *
362 * OneAsymmetricKey ::= SEQUENCE {
363 * version Version,
364 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
365 * privateKey PrivateKey,
366 * attributes [0] IMPLICIT Attributes OPTIONAL,
367 * ...,
368 * [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
369 * ...
370 * }
Valerio Setti4064dbb2023-05-17 15:33:07 +0200371 * ...
Jethro Beekman01672442023-04-19 14:08:14 +0200372 * CurvePrivateKey ::= OCTET STRING
373 */
374static int pk_write_ec_rfc8410_der(unsigned char **p, unsigned char *buf,
375 mbedtls_ecp_keypair *ec)
376{
377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
378 size_t len = 0;
379 size_t oid_len = 0;
380 const char *oid;
381
382 /* privateKey */
383 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(p, buf, ec));
384 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
385 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_OCTET_STRING));
386
387 /* privateKeyAlgorithm */
388 if ((ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec->grp.id, &oid, &oid_len)) != 0) {
389 return ret;
390 }
391 MBEDTLS_ASN1_CHK_ADD(len,
392 mbedtls_asn1_write_algorithm_identifier_ext(p, buf, oid, oid_len, 0, 0));
393
394 /* version */
395 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, buf, 0));
396
397 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
398 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_CONSTRUCTED |
399 MBEDTLS_ASN1_SEQUENCE));
400
401 return (int) len;
402}
403#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
404#endif /* MBEDTLS_ECP_LIGHT */
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200407{
Janos Follath24eed8d2019-11-22 13:21:35 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200410 size_t len = 0;
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (size == 0) {
413 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
414 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415
416 c = buf + size;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100420 mbedtls_mpi T; /* Temporary holding the exported parameters */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200422
Hanno Becker15f81fa2017-08-23 12:38:27 +0100423 /*
424 * Export the parameters one after another to avoid simultaneous copies.
425 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +0100428
429 /* Export QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
431 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100432 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100434 len += ret;
435
436 /* Export DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
438 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100439 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100441 len += ret;
442
443 /* Export DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
445 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100446 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100448 len += ret;
449
450 /* Export Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
452 &T, NULL, NULL)) != 0 ||
453 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100454 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100456 len += ret;
457
458 /* Export P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
460 NULL, NULL, NULL)) != 0 ||
461 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100462 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100464 len += ret;
465
466 /* Export D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
468 NULL, &T, NULL)) != 0 ||
469 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100470 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100472 len += ret;
473
474 /* Export E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
476 NULL, NULL, &T)) != 0 ||
477 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100478 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100480 len += ret;
481
482 /* Export N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
484 NULL, NULL, NULL)) != 0 ||
485 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100486 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100488 len += ret;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490end_of_export:
Hanno Becker15f81fa2017-08-23 12:38:27 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_mpi_free(&T);
493 if (ret < 0) {
494 return ret;
495 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
498 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
499 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c,
500 buf, MBEDTLS_ASN1_CONSTRUCTED |
501 MBEDTLS_ASN1_SEQUENCE));
502 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200504#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Valerio Setti77a75682023-05-15 11:18:46 +0200506 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200507 size_t pub_len = 0, par_len = 0;
508
Jethro Beekman01672442023-04-19 14:08:14 +0200509#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
510 if (mbedtls_pk_is_rfc8410_curve(ec->grp.id)) {
511 return pk_write_ec_rfc8410_der(&c, buf, ec);
512 }
513#endif
514
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200515 /*
516 * RFC 5915, or SEC1 Appendix C.4
517 *
518 * ECPrivateKey ::= SEQUENCE {
519 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
520 * privateKey OCTET STRING,
521 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
522 * publicKey [1] BIT STRING OPTIONAL
523 * }
524 */
525
526 /* publicKey */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200527 MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, key));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (c - buf < 1) {
530 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
531 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200532 *--c = 0;
533 pub_len += 1;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
536 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
539 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf,
540 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
541 MBEDTLS_ASN1_CONSTRUCTED | 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200542 len += pub_len;
543
544 /* parameters */
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200545 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec->grp.id));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
548 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf,
549 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
550 MBEDTLS_ASN1_CONSTRUCTED | 0));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200551 len += par_len;
552
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200553 /* privateKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200555
556 /* version */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
560 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
561 MBEDTLS_ASN1_SEQUENCE));
562 } else
Valerio Setti4064dbb2023-05-17 15:33:07 +0200563#endif /* MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200570
571#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
572#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
573
574#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
575#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
576#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
577#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
Jethro Beekman01672442023-04-19 14:08:14 +0200578#define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----\n"
579#define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----\n"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200580
Neil Armstronge9ecd272022-03-01 10:03:21 +0100581#define PUB_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES ? \
583 MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES : MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
Neil Armstronge9ecd272022-03-01 10:03:21 +0100584#define PRV_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 (MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES > MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES ? \
586 MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES : MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200589{
Janos Follath24eed8d2019-11-22 13:21:35 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200591 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200592 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
595 sizeof(output_buf))) < 0) {
596 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200597 }
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
600 output_buf + sizeof(output_buf) - ret,
601 ret, buf, size, &olen)) != 0) {
602 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200603 }
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200606}
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200609{
Janos Follath24eed8d2019-11-22 13:21:35 +0000610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200611 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200612 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200613 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) {
616 return ret;
617 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200621 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
622 end = PEM_END_PRIVATE_KEY_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200624#endif
Valerio Setti6c496a12023-04-07 15:53:51 +0200625#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200627#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
Valerio Setti77a75682023-05-15 11:18:46 +0200628 if (mbedtls_pk_is_rfc8410_curve(mbedtls_pk_ec_ro(*key)->grp.id)) {
Jethro Beekman01672442023-04-19 14:08:14 +0200629 begin = PEM_BEGIN_PRIVATE_KEY_PKCS8;
630 end = PEM_END_PRIVATE_KEY_PKCS8;
631 } else
632#endif
633 {
634 begin = PEM_BEGIN_PRIVATE_KEY_EC;
635 end = PEM_END_PRIVATE_KEY_EC;
636 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200638#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if ((ret = mbedtls_pem_write_buffer(begin, end,
642 output_buf + sizeof(output_buf) - ret,
643 ret, buf, size, &olen)) != 0) {
644 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200645 }
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_PK_WRITE_C */