blob: b83a13e0a6b298df836b9ea602f7097fa8914e96 [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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_svc_key_id_t *key_id = (mbedtls_svc_key_id_t *) key->pk_ctx;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (*p < start) {
194 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500195 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100196
197 buffer_size = (size_t) (*p - start);
198 if (psa_export_public_key(*key_id, start, buffer_size, &len)
199 != PSA_SUCCESS) {
200 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
201 } else {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000202 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memmove(*p, start, len);
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500204 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 } else
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500206#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210}
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200213{
Janos Follath24eed8d2019-11-22 13:21:35 +0000214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200215 unsigned char *c;
Jethro Beekman01672442023-04-19 14:08:14 +0200216 int has_par = 1;
217 size_t len = 0, par_len = 0, oid_len = 0;
Hanno Becker493c1712019-02-01 10:07:07 +0000218 mbedtls_pk_type_t pk_type;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200219#if defined(MBEDTLS_ECP_LIGHT)
Jethro Beekmancb706ea2023-05-04 12:28:49 +0200220 mbedtls_ecp_group_id ec_grp_id = MBEDTLS_ECP_DP_NONE;
Jethro Beekman13d415c2023-05-04 10:11:58 +0200221#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200222 const char *oid;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (size == 0) {
225 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
226 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228 c = buf + size;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (c - buf < 1) {
233 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
234 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200235
236 /*
237 * SubjectPublicKeyInfo ::= SEQUENCE {
238 * algorithm AlgorithmIdentifier,
239 * subjectPublicKey BIT STRING }
240 */
241 *--c = 0;
242 len += 1;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
245 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 pk_type = mbedtls_pk_get_type(key);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200248#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (pk_type == MBEDTLS_PK_ECKEY) {
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200250 ec_grp_id = mbedtls_pk_ec(*key)->grp.id;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200251 }
Valerio Setti0d2980f2023-04-05 18:17:13 +0200252#endif /* MBEDTLS_ECP_LIGHT */
Hanno Becker493c1712019-02-01 10:07:07 +0000253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (pk_type == MBEDTLS_PK_OPAQUE) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Becker493c1712019-02-01 10:07:07 +0000256 psa_key_type_t key_type;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100257 mbedtls_svc_key_id_t key_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 key_id = *((mbedtls_svc_key_id_t *) key->pk_ctx);
259 if (PSA_SUCCESS != psa_get_key_attributes(key_id, &attributes)) {
260 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
261 }
262 key_type = psa_get_key_type(&attributes);
Hanno Becker493c1712019-02-01 10:07:07 +0000263
Jethro Beekman13d415c2023-05-04 10:11:58 +0200264#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200266 psa_ecc_family_t curve;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200269 if (curve != 0) {
270 ec_grp_id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&attributes), 0);
271 if (ec_grp_id != MBEDTLS_ECP_DP_NONE) {
272 /* The rest of the function works as for legacy EC contexts. */
273 pk_type = MBEDTLS_PK_ECKEY;
274 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200276 }
Jethro Beekman13d415c2023-05-04 10:11:58 +0200277#endif /* MBEDTLS_ECP_LIGHT */
278 if (PSA_KEY_TYPE_IS_RSA(key_type)) {
Neil Armstrong295aeb12022-03-15 16:25:41 +0100279 /* The rest of the function works as for legacy RSA contexts. */
280 pk_type = MBEDTLS_PK_RSA;
Neil Armstrong295aeb12022-03-15 16:25:41 +0100281 }
Jethro Beekmancf4545e2023-05-04 12:05:55 +0200282
283 psa_reset_key_attributes(&attributes);
284 }
285 /* `pk_type` will have been changed to non-opaque by here if this function can handle it */
286 if (pk_type == MBEDTLS_PK_OPAQUE) {
287 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker493c1712019-02-01 10:07:07 +0000288 }
289#endif /* MBEDTLS_USE_PSA_CRYPTO */
290
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200291#if defined(MBEDTLS_ECP_LIGHT)
292 if (pk_type == MBEDTLS_PK_ECKEY) {
293 /* Some groups have their own AlgorithmIdentifier OID, others are handled by mbedtls_oid_get_oid_by_pk_alg() below */
294 ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec_grp_id, &oid, &oid_len);
295
296 if (ret == 0) {
297 /* Currently, none of the supported algorithms that have their own AlgorithmIdentifier OID have any parameters */
298 has_par = 0;
299 } else if (ret == MBEDTLS_ERR_OID_NOT_FOUND) {
300 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec_grp_id));
301 } else {
302 return ret;
303 }
304 }
305#endif /* MBEDTLS_ECP_LIGHT */
306
Jethro Beekman01672442023-04-19 14:08:14 +0200307 if (oid_len == 0) {
308 if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid,
309 &oid_len)) != 0) {
310 return ret;
311 }
Hanno Becker493c1712019-02-01 10:07:07 +0000312 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200313
Jethro Beekman01672442023-04-19 14:08:14 +0200314 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier_ext(&c, buf, oid, oid_len,
315 par_len, has_par));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
318 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
319 MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200322}
323
Jethro Beekman01672442023-04-19 14:08:14 +0200324#if defined(MBEDTLS_ECP_LIGHT)
325#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
326/*
327 * RFC8410
328 *
329 * OneAsymmetricKey ::= SEQUENCE {
330 * version Version,
331 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
332 * privateKey PrivateKey,
333 * attributes [0] IMPLICIT Attributes OPTIONAL,
334 * ...,
335 * [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
336 * ...
337 * }
338 *
339 * CurvePrivateKey ::= OCTET STRING
340 */
341static int pk_write_ec_rfc8410_der(unsigned char **p, unsigned char *buf,
342 mbedtls_ecp_keypair *ec)
343{
344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
345 size_t len = 0;
346 size_t oid_len = 0;
347 const char *oid;
348
349 /* privateKey */
350 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(p, buf, ec));
351 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
352 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_OCTET_STRING));
353
354 /* privateKeyAlgorithm */
355 if ((ret = mbedtls_oid_get_oid_by_ec_grp_algid(ec->grp.id, &oid, &oid_len)) != 0) {
356 return ret;
357 }
358 MBEDTLS_ASN1_CHK_ADD(len,
359 mbedtls_asn1_write_algorithm_identifier_ext(p, buf, oid, oid_len, 0, 0));
360
361 /* version */
362 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, buf, 0));
363
364 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buf, len));
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buf, MBEDTLS_ASN1_CONSTRUCTED |
366 MBEDTLS_ASN1_SEQUENCE));
367
368 return (int) len;
369}
370#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
371#endif /* MBEDTLS_ECP_LIGHT */
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373int mbedtls_pk_write_key_der(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200374{
Janos Follath24eed8d2019-11-22 13:21:35 +0000375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377 size_t len = 0;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (size == 0) {
380 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
381 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382
383 c = buf + size;
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100387 mbedtls_mpi T; /* Temporary holding the exported parameters */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200389
Hanno Becker15f81fa2017-08-23 12:38:27 +0100390 /*
391 * Export the parameters one after another to avoid simultaneous copies.
392 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +0100395
396 /* Export QP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
398 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100399 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100401 len += ret;
402
403 /* Export DQ */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
405 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100406 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100408 len += ret;
409
410 /* Export DP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
412 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100413 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100415 len += ret;
416
417 /* Export Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
419 &T, NULL, NULL)) != 0 ||
420 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100421 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100423 len += ret;
424
425 /* Export P */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
427 NULL, NULL, NULL)) != 0 ||
428 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100429 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100431 len += ret;
432
433 /* Export D */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
435 NULL, &T, NULL)) != 0 ||
436 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100437 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100439 len += ret;
440
441 /* Export E */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
443 NULL, NULL, &T)) != 0 ||
444 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100445 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100447 len += ret;
448
449 /* Export N */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
451 NULL, NULL, NULL)) != 0 ||
452 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100453 goto end_of_export;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100455 len += ret;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457end_of_export:
Hanno Becker15f81fa2017-08-23 12:38:27 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 mbedtls_mpi_free(&T);
460 if (ret < 0) {
461 return ret;
462 }
Hanno Becker15f81fa2017-08-23 12:38:27 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
465 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
466 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c,
467 buf, MBEDTLS_ASN1_CONSTRUCTED |
468 MBEDTLS_ASN1_SEQUENCE));
469 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200471#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
473 mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200474 size_t pub_len = 0, par_len = 0;
475
Jethro Beekman01672442023-04-19 14:08:14 +0200476#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
477 if (mbedtls_pk_is_rfc8410_curve(ec->grp.id)) {
478 return pk_write_ec_rfc8410_der(&c, buf, ec);
479 }
480#endif
481
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200482 /*
483 * RFC 5915, or SEC1 Appendix C.4
484 *
485 * ECPrivateKey ::= SEQUENCE {
486 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
487 * privateKey OCTET STRING,
488 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
489 * publicKey [1] BIT STRING OPTIONAL
490 * }
491 */
492
493 /* publicKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (c - buf < 1) {
497 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
498 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200499 *--c = 0;
500 pub_len += 1;
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
503 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
506 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf,
507 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
508 MBEDTLS_ASN1_CONSTRUCTED | 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200509 len += pub_len;
510
511 /* parameters */
Jethro Beekman8e59ebb2023-05-03 13:05:33 +0200512 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec->grp.id));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
515 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf,
516 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
517 MBEDTLS_ASN1_CONSTRUCTED | 0));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200518 len += par_len;
519
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200520 /* privateKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200522
523 /* version */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
527 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
528 MBEDTLS_ASN1_SEQUENCE));
529 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return (int) len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200534}
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537
538#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
539#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
540
541#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
542#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
543#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
544#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
Jethro Beekman01672442023-04-19 14:08:14 +0200545#define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----\n"
546#define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----\n"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200547
Neil Armstronge9ecd272022-03-01 10:03:21 +0100548#define PUB_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES ? \
550 MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES : MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
Neil Armstronge9ecd272022-03-01 10:03:21 +0100551#define PRV_DER_MAX_BYTES \
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 (MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES > MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES ? \
553 MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES : MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200556{
Janos Follath24eed8d2019-11-22 13:21:35 +0000557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200558 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200559 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
562 sizeof(output_buf))) < 0) {
563 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200564 }
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
567 output_buf + sizeof(output_buf) - ret,
568 ret, buf, size, &olen)) != 0) {
569 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200570 }
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200573}
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf, size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200576{
Janos Follath24eed8d2019-11-22 13:21:35 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200578 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200579 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200580 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) {
583 return ret;
584 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200588 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
589 end = PEM_END_PRIVATE_KEY_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200591#endif
Valerio Setti6c496a12023-04-07 15:53:51 +0200592#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Jethro Beekman01672442023-04-19 14:08:14 +0200594#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
595 if (mbedtls_pk_is_rfc8410_curve(mbedtls_pk_ec(*key)->grp.id)) {
596 begin = PEM_BEGIN_PRIVATE_KEY_PKCS8;
597 end = PEM_END_PRIVATE_KEY_PKCS8;
598 } else
599#endif
600 {
601 begin = PEM_BEGIN_PRIVATE_KEY_EC;
602 end = PEM_END_PRIVATE_KEY_EC;
603 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 } else
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200605#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if ((ret = mbedtls_pem_write_buffer(begin, end,
609 output_buf + sizeof(output_buf) - ret,
610 ret, buf, size, &olen)) != 0) {
611 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200612 }
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_PK_WRITE_C */