blob: c91ffe915a63022a5217b921204a7a1e87ddaf92 [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024# include "mbedtls/pk.h"
25# include "mbedtls/asn1write.h"
26# include "mbedtls/oid.h"
27# include "mbedtls/platform_util.h"
28# include "mbedtls/error.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020029
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020030# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# if defined(MBEDTLS_RSA_C)
33# include "mbedtls/rsa.h"
34# endif
35# if defined(MBEDTLS_ECP_C)
36# include "mbedtls/bignum.h"
37# include "mbedtls/ecp.h"
38# include "mbedtls/platform_util.h"
39# endif
40# if defined(MBEDTLS_ECDSA_C)
41# include "mbedtls/ecdsa.h"
42# endif
43# if defined(MBEDTLS_PEM_WRITE_C)
44# include "mbedtls/pem.h"
45# endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020046
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# if defined(MBEDTLS_USE_PSA_CRYPTO)
48# include "psa/crypto.h"
49# include "mbedtls/psa_util.h"
50# endif
51# if defined(MBEDTLS_PLATFORM_C)
52# include "mbedtls/platform.h"
53# else
54# include <stdlib.h>
55# define mbedtls_calloc calloc
56# define mbedtls_free free
57# endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050059/* Parameter validation macros based on platform_util.h */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# define PK_VALIDATE_RET(cond) \
61 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA)
62# define PK_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020065/*
66 * RSAPublicKey ::= SEQUENCE {
67 * modulus INTEGER, -- n
68 * publicExponent INTEGER -- e
69 * }
70 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071static int pk_write_rsa_pubkey(unsigned char **p,
72 unsigned char *start,
73 mbedtls_rsa_context *rsa)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020074{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020076 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010077 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020078
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +010080
81 /* Export E */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
83 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +010084 goto end_of_export;
85 len += ret;
86
87 /* Export N */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020088 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
89 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +010090 goto end_of_export;
91 len += ret;
92
93end_of_export:
94
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095 mbedtls_mpi_free(&T);
96 if (ret < 0)
97 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
100 MBEDTLS_ASN1_CHK_ADD(
101 len, mbedtls_asn1_write_tag(
102 p, start, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106# endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108# if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109/*
110 * EC public key is an EC point
111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112static int pk_write_ec_pubkey(unsigned char **p,
113 unsigned char *start,
114 mbedtls_ecp_keypair *ec)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200115{
Janos Follath24eed8d2019-11-22 13:21:35 +0000116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200117 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200119
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
121 MBEDTLS_ECP_PF_UNCOMPRESSED, &len,
122 buf, sizeof(buf))) != 0) {
123 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124 }
125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126 if (*p < start || (size_t)(*p - start) < len)
127 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200128
129 *p -= len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 memcpy(*p, buf, len);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200133}
134
135/*
136 * ECParameters ::= CHOICE {
137 * namedCurve OBJECT IDENTIFIER
138 * }
139 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140static int pk_write_ec_param(unsigned char **p,
141 unsigned char *start,
142 mbedtls_ecp_keypair *ec)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200143{
Janos Follath24eed8d2019-11-22 13:21:35 +0000144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200145 size_t len = 0;
146 const char *oid;
147 size_t oid_len;
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 if ((ret = mbedtls_oid_get_oid_by_ec_grp(ec->grp.id, &oid, &oid_len)) != 0)
150 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200151
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200152 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200155}
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200156
157/*
158 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160static int pk_write_ec_private(unsigned char **p,
161 unsigned char *start,
162 mbedtls_ecp_keypair *ec)
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200163{
Janos Follath24eed8d2019-11-22 13:21:35 +0000164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165 size_t byte_length = (ec->grp.pbits + 7) / 8;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200166 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
167
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168 ret = mbedtls_ecp_write_key(ec, tmp, byte_length);
169 if (ret != 0)
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200170 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171 ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length);
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200172
173exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200174 mbedtls_platform_zeroize(tmp, byte_length);
175 return ret;
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200176}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177# endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200178
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179int mbedtls_pk_write_pubkey(unsigned char **p,
180 unsigned char *start,
181 const mbedtls_pk_context *key)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182{
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200184 size_t len = 0;
185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 PK_VALIDATE_RET(p != NULL);
187 PK_VALIDATE_RET(*p != NULL);
188 PK_VALIDATE_RET(start != NULL);
189 PK_VALIDATE_RET(key != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191# if defined(MBEDTLS_RSA_C)
192 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA)
193 MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start,
194 mbedtls_pk_rsa(*key)));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200195 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196# endif
197# if defined(MBEDTLS_ECP_C)
198 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY)
199 MBEDTLS_ASN1_CHK_ADD(len,
200 pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key)));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200201 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200202# endif
203# if defined(MBEDTLS_USE_PSA_CRYPTO)
204 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) {
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500205 size_t buffer_size;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 psa_key_id_t *key_id = (psa_key_id_t *)key->pk_ctx;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500207
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200208 if (*p < start)
209 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500210
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 buffer_size = (size_t)(*p - start);
212 if (psa_export_public_key(*key_id, start, buffer_size, &len) !=
213 PSA_SUCCESS) {
214 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
215 } else {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000216 *p -= len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 memmove(*p, start, len);
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500218 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 } else
220# endif /* MBEDTLS_USE_PSA_CRYPTO */
221 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200224}
225
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key,
227 unsigned char *buf,
228 size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200229{
Janos Follath24eed8d2019-11-22 13:21:35 +0000230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200231 unsigned char *c;
232 size_t len = 0, par_len = 0, oid_len;
Hanno Becker493c1712019-02-01 10:07:07 +0000233 mbedtls_pk_type_t pk_type;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234 const char *oid;
235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 PK_VALIDATE_RET(key != NULL);
237 if (size == 0)
238 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
239 PK_VALIDATE_RET(buf != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200241 c = buf + size;
242
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245 if (c - buf < 1)
246 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200247
248 /*
249 * SubjectPublicKeyInfo ::= SEQUENCE {
250 * algorithm AlgorithmIdentifier,
251 * subjectPublicKey BIT STRING }
252 */
253 *--c = 0;
254 len += 1;
255
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
257 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
258 MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 pk_type = mbedtls_pk_get_type(key);
261# if defined(MBEDTLS_ECP_C)
262 if (pk_type == MBEDTLS_PK_ECKEY) {
263 MBEDTLS_ASN1_CHK_ADD(par_len,
264 pk_write_ec_param(&c, buf, mbedtls_pk_ec(*key)));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200265 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266# endif
267# if defined(MBEDTLS_USE_PSA_CRYPTO)
268 if (pk_type == MBEDTLS_PK_OPAQUE) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Becker493c1712019-02-01 10:07:07 +0000270 psa_key_type_t key_type;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200271 psa_key_id_t key_id;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100272 psa_ecc_family_t curve;
Gilles Peskine89177e82019-12-03 21:19:09 +0100273 size_t bits;
Hanno Becker493c1712019-02-01 10:07:07 +0000274
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200275 key_id = *((psa_key_id_t *)key->pk_ctx);
276 if (PSA_SUCCESS != psa_get_key_attributes(key_id, &attributes))
277 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
278 key_type = psa_get_key_type(&attributes);
279 bits = psa_get_key_bits(&attributes);
280 psa_reset_key_attributes(&attributes);
Hanno Becker493c1712019-02-01 10:07:07 +0000281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
283 if (curve == 0)
284 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker493c1712019-02-01 10:07:07 +0000285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 ret = mbedtls_psa_get_ecc_oid_from_id(curve, bits, &oid, &oid_len);
287 if (ret != 0)
288 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker493c1712019-02-01 10:07:07 +0000289
290 /* Write EC algorithm parameters; that's akin
291 * to pk_write_ec_param() above. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 MBEDTLS_ASN1_CHK_ADD(par_len,
293 mbedtls_asn1_write_oid(&c, buf, oid, oid_len));
Hanno Becker493c1712019-02-01 10:07:07 +0000294
295 /* The rest of the function works as for legacy EC contexts. */
296 pk_type = MBEDTLS_PK_ECKEY;
297 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298# endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker493c1712019-02-01 10:07:07 +0000299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid, &oid_len)) != 0) {
301 return ret;
Hanno Becker493c1712019-02-01 10:07:07 +0000302 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200303
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200304 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(
305 &c, buf, oid, oid_len, par_len));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200306
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200307 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
308 MBEDTLS_ASN1_CHK_ADD(
309 len, mbedtls_asn1_write_tag(
310 &c, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200311
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200312 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200313}
314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315int mbedtls_pk_write_key_der(const mbedtls_pk_context *key,
316 unsigned char *buf,
317 size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200318{
Janos Follath24eed8d2019-11-22 13:21:35 +0000319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200321 size_t len = 0;
322
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200323 PK_VALIDATE_RET(key != NULL);
324 if (size == 0)
325 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
326 PK_VALIDATE_RET(buf != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327
328 c = buf + size;
329
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200330# if defined(MBEDTLS_RSA_C)
331 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100332 mbedtls_mpi T; /* Temporary holding the exported parameters */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200334
Hanno Becker15f81fa2017-08-23 12:38:27 +0100335 /*
336 * Export the parameters one after another to avoid simultaneous copies.
337 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200338
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 mbedtls_mpi_init(&T);
Hanno Becker15f81fa2017-08-23 12:38:27 +0100340
341 /* Export QP */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
343 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100344 goto end_of_export;
345 len += ret;
346
347 /* Export DQ */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
349 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100350 goto end_of_export;
351 len += ret;
352
353 /* Export DP */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200354 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
355 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100356 goto end_of_export;
357 len += ret;
358
359 /* Export Q */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
361 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100362 goto end_of_export;
363 len += ret;
364
365 /* Export P */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
367 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100368 goto end_of_export;
369 len += ret;
370
371 /* Export D */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200372 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
373 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100374 goto end_of_export;
375 len += ret;
376
377 /* Export E */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
379 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100380 goto end_of_export;
381 len += ret;
382
383 /* Export N */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200384 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
385 (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
Hanno Becker15f81fa2017-08-23 12:38:27 +0100386 goto end_of_export;
387 len += ret;
388
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200389end_of_export:
Hanno Becker15f81fa2017-08-23 12:38:27 +0100390
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391 mbedtls_mpi_free(&T);
392 if (ret < 0)
393 return ret;
Hanno Becker15f81fa2017-08-23 12:38:27 +0100394
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200395 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
396 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
397 MBEDTLS_ASN1_CHK_ADD(len,
398 mbedtls_asn1_write_tag(&c, buf,
399 MBEDTLS_ASN1_CONSTRUCTED |
400 MBEDTLS_ASN1_SEQUENCE));
401 } else
402# endif /* MBEDTLS_RSA_C */
403# if defined(MBEDTLS_ECP_C)
404 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
405 mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200406 size_t pub_len = 0, par_len = 0;
407
408 /*
409 * RFC 5915, or SEC1 Appendix C.4
410 *
411 * ECPrivateKey ::= SEQUENCE {
412 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
413 * privateKey OCTET STRING,
414 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
415 * publicKey [1] BIT STRING OPTIONAL
416 * }
417 */
418
419 /* publicKey */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200420 MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200421
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422 if (c - buf < 1)
423 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200424 *--c = 0;
425 pub_len += 1;
426
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200427 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
428 MBEDTLS_ASN1_CHK_ADD(
429 pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200430
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200431 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
432 MBEDTLS_ASN1_CHK_ADD(
433 pub_len, mbedtls_asn1_write_tag(&c, buf,
434 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
435 MBEDTLS_ASN1_CONSTRUCTED | 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200436 len += pub_len;
437
438 /* parameters */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200439 MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200440
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200441 MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
442 MBEDTLS_ASN1_CHK_ADD(
443 par_len, mbedtls_asn1_write_tag(&c, buf,
444 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
445 MBEDTLS_ASN1_CONSTRUCTED | 0));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200446 len += par_len;
447
Gilles Peskine2700cfb2018-08-11 00:48:44 +0200448 /* privateKey */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200449 MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200450
451 /* version */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200453
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200454 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
455 MBEDTLS_ASN1_CHK_ADD(len,
456 mbedtls_asn1_write_tag(&c, buf,
457 MBEDTLS_ASN1_CONSTRUCTED |
458 MBEDTLS_ASN1_SEQUENCE));
459 } else
460# endif /* MBEDTLS_ECP_C */
461 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200462
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463 return (int)len;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200464}
465
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200466# if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200467
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200468# define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
469# define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471# define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
472# define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
473# define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
474# define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200475
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200476/*
477 * Max sizes of key per types. Shown as tag + len (+ content).
478 */
479
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200480# if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200481/*
482 * RSA public keys:
483 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
484 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
485 * + 1 + 1 + 9 (rsa oid)
486 * + 1 + 1 (params null)
487 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
488 * RSAPublicKey ::= SEQUENCE { 1 + 3
489 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
490 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
491 * }
492 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200493# define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200494
495/*
496 * RSA private keys:
497 * RSAPrivateKey ::= SEQUENCE { 1 + 3
498 * version Version, 1 + 1 + 1
499 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
500 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
501 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
502 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
503 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
504 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
505 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
506 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
507 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
508 * }
509 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200510# define MPI_MAX_SIZE_2 \
511 (MBEDTLS_MPI_MAX_SIZE / 2 + MBEDTLS_MPI_MAX_SIZE % 2)
512# define RSA_PRV_DER_MAX_BYTES \
513 (47 + 3 * MBEDTLS_MPI_MAX_SIZE + 5 * MPI_MAX_SIZE_2)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200514
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200515# else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200516
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200517# define RSA_PUB_DER_MAX_BYTES 0
518# define RSA_PRV_DER_MAX_BYTES 0
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200519
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200520# endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200521
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522# if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200523/*
524 * EC public keys:
525 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
526 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
527 * + 1 + 1 + 7 (ec oid)
528 * + 1 + 1 + 9 (namedCurve oid)
529 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
530 * + 1 (point format) [1]
531 * + 2 * ECP_MAX (coords) [1]
532 * }
533 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200534# define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200535
536/*
537 * EC private keys:
538 * ECPrivateKey ::= SEQUENCE { 1 + 2
539 * version INTEGER , 1 + 1 + 1
540 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
541 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
542 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
543 * }
544 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200545# define ECP_PRV_DER_MAX_BYTES (29 + 3 * MBEDTLS_ECP_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200546
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200547# else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200548
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200549# define ECP_PUB_DER_MAX_BYTES 0
550# define ECP_PRV_DER_MAX_BYTES 0
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200551
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200552# endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200553
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200554# define PUB_DER_MAX_BYTES \
555 (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
556 RSA_PUB_DER_MAX_BYTES : \
557 ECP_PUB_DER_MAX_BYTES)
558# define PRV_DER_MAX_BYTES \
559 (RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
560 RSA_PRV_DER_MAX_BYTES : \
561 ECP_PRV_DER_MAX_BYTES)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200562
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200563int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *key,
564 unsigned char *buf,
565 size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200566{
Janos Follath24eed8d2019-11-22 13:21:35 +0000567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200568 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200569 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200570
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200571 PK_VALIDATE_RET(key != NULL);
572 PK_VALIDATE_RET(buf != NULL || size == 0);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500573
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200574 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
575 sizeof(output_buf))) < 0) {
576 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200577 }
578
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200579 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY,
580 PEM_END_PUBLIC_KEY,
581 output_buf + sizeof(output_buf) - ret,
582 ret, buf, size, &olen)) != 0) {
583 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200584 }
585
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200586 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200587}
588
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200589int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key,
590 unsigned char *buf,
591 size_t size)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592{
Janos Follath24eed8d2019-11-22 13:21:35 +0000593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200594 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200595 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200596 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200597
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200598 PK_VALIDATE_RET(key != NULL);
599 PK_VALIDATE_RET(buf != NULL || size == 0);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500600
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200601 if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) <
602 0)
603 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200604
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200605# if defined(MBEDTLS_RSA_C)
606 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200607 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
608 end = PEM_END_PRIVATE_KEY_RSA;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609 } else
610# endif
611# if defined(MBEDTLS_ECP_C)
612 if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200613 begin = PEM_BEGIN_PRIVATE_KEY_EC;
614 end = PEM_END_PRIVATE_KEY_EC;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615 } else
616# endif
617 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200618
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200619 if ((ret = mbedtls_pem_write_buffer(begin, end,
620 output_buf + sizeof(output_buf) - ret,
621 ret, buf, size, &olen)) != 0) {
622 return ret;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200623 }
624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625 return 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200626}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200627# endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#endif /* MBEDTLS_PK_WRITE_C */