blob: 4bb9ac15fb75091f3071097c7467de741adf7ebe [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"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Gilles Peskine2700cfb2018-08-11 00:48:44 +020036#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Gilles Peskine2700cfb2018-08-11 00:48:44 +020038#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020039#endif
Neil Armstronge0326a62022-02-25 08:57:19 +010040#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
41#include "pkwrite.h"
42#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020048#endif
49
Andrzej Kurek5fec0862018-11-19 10:07:36 -050050#if defined(MBEDTLS_USE_PSA_CRYPTO)
51#include "psa/crypto.h"
Hanno Becker65935d92019-02-01 11:55:03 +000052#include "mbedtls/psa_util.h"
Andrzej Kurek5fec0862018-11-19 10:07:36 -050053#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020057/*
58 * RSAPublicKey ::= SEQUENCE {
59 * modulus INTEGER, -- n
60 * publicExponent INTEGER -- e
61 * }
62 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
64 mbedtls_rsa_context *rsa)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020065{
Janos Follath24eed8d2019-11-22 13:21:35 +000066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020067 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010068 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +010071
72 /* Export E */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
74 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +010075 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 }
Hanno Becker15f81fa2017-08-23 12:38:27 +010077 len += ret;
78
79 /* Export N */
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
81 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +010082 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 }
Hanno Becker15f81fa2017-08-23 12:38:27 +010084 len += ret;
85
86end_of_export:
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_mpi_free(&T);
89 if (ret < 0) {
90 return ret;
91 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
94 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
95 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200100
Valerio Setti0d2980f2023-04-05 18:17:13 +0200101#if defined(MBEDTLS_ECP_LIGHT)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200102/*
103 * EC public key is an EC point
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
106 mbedtls_ecp_keypair *ec)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107{
Janos Follath24eed8d2019-11-22 13:21:35 +0000108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
113 MBEDTLS_ECP_PF_UNCOMPRESSED,
114 &len, buf, sizeof(buf))) != 0) {
115 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (*p < start || (size_t) (*p - start) < len) {
119 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
120 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200121
122 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 memcpy(*p, buf, len);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200126}
127
128/*
129 * ECParameters ::= CHOICE {
130 * namedCurve OBJECT IDENTIFIER
131 * }
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static int pk_write_ec_param(unsigned char **p, unsigned char *start,
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200134 mbedtls_ecp_group_id grp_id)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200135{
Janos Follath24eed8d2019-11-22 13:21:35 +0000136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200137 size_t len = 0;
138 const char *oid;
139 size_t oid_len;
140
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200141 if ((ret = mbedtls_oid_get_oid_by_ec_grp(grp_id, &oid, &oid_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return ret;
143 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200148}
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200149
150/*
151 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153static int pk_write_ec_private(unsigned char **p, unsigned char *start,
154 mbedtls_ecp_keypair *ec)
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200155{
Janos Follath24eed8d2019-11-22 13:21:35 +0000156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 size_t byte_length = (ec->grp.pbits + 7) / 8;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200158 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 ret = mbedtls_ecp_write_key(ec, tmp, byte_length);
161 if (ret != 0) {
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 }
164 ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200165
166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_platform_zeroize(tmp, byte_length);
168 return ret;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200169}
Valerio Setti0d2980f2023-04-05 18:17:13 +0200170#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
173 const mbedtls_pk_context *key)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200174{
Janos Follath24eed8d2019-11-22 13:21:35 +0000175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200176 size_t len = 0;
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
180 MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key)));
181 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182#endif
Valerio Setti0d2980f2023-04-05 18:17:13 +0200183#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
185 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key)));
186 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200187#endif
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500188#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) {
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500190 size_t buffer_size;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (*p < start) {
193 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195
196 buffer_size = (size_t) (*p - start);
Valerio Setti4f387ef2023-05-02 14:15:59 +0200197 if (psa_export_public_key(key->priv_id, start, buffer_size, &len)
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 != PSA_SUCCESS) {
199 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
200 } else {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000201 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memmove(*p, start, len);
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500203 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 } else
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200212{
Janos Follath24eed8d2019-11-22 13:21:35 +0000213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200214 unsigned char *c;
Jethro Beekman01672442023-04-19 14:08:14 +0200215 int has_par = 1;
216 size_t len = 0, par_len = 0, oid_len = 0;
Hanno Becker493c1712019-02-01 10:07:07 +0000217 mbedtls_pk_type_t pk_type;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200218#if defined(MBEDTLS_ECP_LIGHT)
Jethro Beekmancb706ea2023-05-04 12:28:49 +0200219 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200220#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221 const char *oid;
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (size == 0) {
224 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
225 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200227 c = buf + size;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (c - buf < 1) {
232 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
233 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234
235 /*
236 * SubjectPublicKeyInfo ::= SEQUENCE {
237 * algorithm AlgorithmIdentifier,
238 * subjectPublicKey BIT STRING }
239 */
240 *--c = 0;
241 len += 1;
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
244 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 pk_type = mbedtls_pk_get_type(key);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200247#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (pk_type == MBEDTLS_PK_ECKEY) {
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200249 ec_grp_id = mbedtls_pk_ec(*key)->grp.id;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200250 }
Valerio Setti0d2980f2023-04-05 18:17:13 +0200251#endif /* MBEDTLS_ECP_LIGHT */
Hanno Becker493c1712019-02-01 10:07:07 +0000252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (pk_type == MBEDTLS_PK_OPAQUE) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Becker493c1712019-02-01 10:07:07 +0000255 psa_key_type_t key_type;
Valerio Setti048cd442023-04-28 15:26:11 +0200256 psa_ecc_family_t curve;
257 size_t bits;
258
Valerio Setti4f387ef2023-05-02 14:15:59 +0200259 if (PSA_SUCCESS != psa_get_key_attributes(key->priv_id,
Valerio Setti048cd442023-04-28 15:26:11 +0200260 &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
262 }
263 key_type = psa_get_key_type(&attributes);
Hanno Becker493c1712019-02-01 10:07:07 +0000264
Jethro Beekman13d415c2023-05-04 10:11:58 +0200265#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200267 psa_ecc_family_t curve;
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200270 if (curve != 0) {
271 ec_grp_id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&attributes), 0);
272 if (ec_grp_id != MBEDTLS_ECP_DP_NONE) {
273 /* The rest of the function works as for legacy EC contexts. */
274 pk_type = MBEDTLS_PK_ECKEY;
275 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200277 }
Jethro Beekman13d415c2023-05-04 10:11:58 +0200278#endif /* MBEDTLS_ECP_LIGHT */
279 if (PSA_KEY_TYPE_IS_RSA(key_type)) {
Neil Armstrong295aeb12022-03-15 16:25:41 +0100280 /* The rest of the function works as for legacy RSA contexts. */
281 pk_type = MBEDTLS_PK_RSA;
Neil Armstrong295aeb12022-03-15 16:25:41 +0100282 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200283
284 psa_reset_key_attributes(&attributes);
285 }
286 /* `pk_type` will have been changed to non-opaque by here if this function can handle it */
287 if (pk_type == MBEDTLS_PK_OPAQUE) {
288 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker493c1712019-02-01 10:07:07 +0000289 }
290#endif /* MBEDTLS_USE_PSA_CRYPTO */
291
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200292#if defined(MBEDTLS_ECP_LIGHT)
293 if (pk_type == MBEDTLS_PK_ECKEY) {
294 /* Some groups have their own AlgorithmIdentifier OID, others are handled by mbedtls_oid_get_oid_by_pk_alg() below */
295 ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec_grp_id, &oid, &oid_len);
296
297 if (ret == 0) {
298 /* Currently, none of the supported algorithms that have their own AlgorithmIdentifier OID have any parameters */
299 has_par = 0;
300 } else if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
301 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec_grp_id));
302 } else {
303 return ret;
304 }
305 }
306#endif /* MBEDTLS_ECP_LIGHT */
307
Jethro Beekman01672442023-04-19 14:08:14 +0200308 if (oid_len == 0) {
309 if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid,
310 &oid_len)) != 0) {
311 return ret;
312 }
Hanno Becker493c1712019-02-01 10:07:07 +0000313 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200314
Jethro Beekman01672442023-04-19 14:08:14 +0200315 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier_ext(&c, buf, oid, oid_len,
316 par_len, has_par));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
319 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
320 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200323}
324
Jethro Beekman01672442023-04-19 14:08:14 +0200325#if defined(MBEDTLS_ECP_LIGHT)
326#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
327/*
328 * RFC8410
329 *
330 * OneAsymmetricKey ::= SEQUENCE {
331 * version Version,
332 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
333 * privateKey PrivateKey,
334 * attributes [0] IMPLICIT Attributes OPTIONAL,
335 * ...,
336 * [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
337 * ...
338 * }
339 *
340 * CurvePrivateKey ::= OCTET STRING
341 */
342static int pk_write_ec_rfc8410_der(unsigned char **p, unsigned char *buf,
343 mbedtls_ecp_keypair *ec)
344{
345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
346 size_t len = 0;
347 size_t oid_len = 0;
348 const char *oid;
349
350 /* privateKey */
351 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(p, buf, ec));
352 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
353 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_OCTET_STRING));
354
355 /* privateKeyAlgorithm */
356 if ((ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec->grp.id, &oid, &oid_len)) != 0) {
357 return ret;
358 }
359 MBEDTLS_ASN1_CHK_ADD(len,
360 mbedtls_asn1_write_algorithm_identifier_ext(p, buf, oid, oid_len, 0, 0));
361
362 /* version */
363 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, buf, 0));
364
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_CONSTRUCTED |
367 MBEDTLS_ASN1_SEQUENCE));
368
369 return (int) len;
370}
371#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
372#endif /* MBEDTLS_ECP_LIGHT */
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200375{
Janos Follath24eed8d2019-11-22 13:21:35 +0000376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200378 size_t len = 0;
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (size == 0) {
381 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
382 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383
384 c = buf + size;
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100388 mbedtls_mpi T; /* Temporary holding the exported parameters */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200390
Hanno Becker15f81fa2017-08-23 12:38:27 +0100391 /*
392 * Export the parameters one after another to avoid simultaneous copies.
393 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +0100396
397 /* Export QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
399 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100400 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100402 len += ret;
403
404 /* Export DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
406 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100407 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100409 len += ret;
410
411 /* Export DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
413 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100414 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100416 len += ret;
417
418 /* Export Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
420 &T, NULL, NULL)) != 0 ||
421 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100422 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100424 len += ret;
425
426 /* Export P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
428 NULL, NULL, NULL)) != 0 ||
429 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100430 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100432 len += ret;
433
434 /* Export D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
436 NULL, &T, NULL)) != 0 ||
437 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100438 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100440 len += ret;
441
442 /* Export E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
444 NULL, NULL, &T)) != 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 N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
452 NULL, 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100458end_of_export:
Hanno Becker15f81fa2017-08-23 12:38:27 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 mbedtls_mpi_free(&T);
461 if (ret < 0) {
462 return ret;
463 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
466 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
467 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c,
468 buf, MBEDTLS_ASN1_CONSTRUCTED |
469 MBEDTLS_ASN1_SEQUENCE));
470 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200472#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
474 mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200475 size_t pub_len = 0, par_len = 0;
476
Jethro Beekman01672442023-04-19 14:08:14 +0200477#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
478 if (mbedtls_pk_is_rfc8410_curve(ec->grp.id)) {
479 return pk_write_ec_rfc8410_der(&c, buf, ec);
480 }
481#endif
482
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200483 /*
484 * RFC 5915, or SEC1 Appendix C.4
485 *
486 * ECPrivateKey ::= SEQUENCE {
487 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
488 * privateKey OCTET STRING,
489 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
490 * publicKey [1] BIT STRING OPTIONAL
491 * }
492 */
493
494 /* publicKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (c - buf < 1) {
498 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
499 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200500 *--c = 0;
501 pub_len += 1;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
504 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
507 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf,
508 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
509 MBEDTLS_ASN1_CONSTRUCTED | 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200510 len += pub_len;
511
512 /* parameters */
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200513 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec->grp.id));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
516 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf,
517 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
518 MBEDTLS_ASN1_CONSTRUCTED | 0));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200519 len += par_len;
520
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200521 /* privateKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200523
524 /* version */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
528 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
529 MBEDTLS_ASN1_SEQUENCE));
530 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535}
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200538
539#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
540#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
541
542#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
543#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
544#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
545#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
Jethro Beekman01672442023-04-19 14:08:14 +0200546#define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----\n"
547#define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----\n"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200548
Neil Armstronge9ecd272022-03-01 10:03:21 +0100549#define PUB_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES ? \
551 MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES : MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
Neil Armstronge9ecd272022-03-01 10:03:21 +0100552#define PRV_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 (MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES > MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES ? \
554 MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES : MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200557{
Janos Follath24eed8d2019-11-22 13:21:35 +0000558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200559 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200560 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
563 sizeof(output_buf))) < 0) {
564 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200565 }
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
568 output_buf + sizeof(output_buf) - ret,
569 ret, buf, size, &olen)) != 0) {
570 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200571 }
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200574}
575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200577{
Janos Follath24eed8d2019-11-22 13:21:35 +0000578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200579 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200580 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200581 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) {
584 return ret;
585 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200589 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
590 end = PEM_END_PRIVATE_KEY_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592#endif
Valerio Setti6c496a12023-04-07 15:53:51 +0200593#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200595#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
596 if (mbedtls_pk_is_rfc8410_curve(mbedtls_pk_ec(*key)->grp.id)) {
597 begin = PEM_BEGIN_PRIVATE_KEY_PKCS8;
598 end = PEM_END_PRIVATE_KEY_PKCS8;
599 } else
600#endif
601 {
602 begin = PEM_BEGIN_PRIVATE_KEY_EC;
603 end = PEM_END_PRIVATE_KEY_EC;
604 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200606#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if ((ret = mbedtls_pem_write_buffer(begin, end,
610 output_buf + sizeof(output_buf) - ret,
611 ret, buf, size, &olen)) != 0) {
612 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200613 }
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_PK_WRITE_C */