Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key layer for writing key files and structures |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_PK_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/pk.h" |
| 25 | #include "mbedtls/asn1write.h" |
| 26 | #include "mbedtls/oid.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 27 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 28 | #include "mbedtls/error.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 29 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/rsa.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 34 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 36 | #include "mbedtls/bignum.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecp.h" |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 38 | #include "mbedtls/platform_util.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 39 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/ecdsa.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 42 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/pem.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 47 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 48 | #include "psa/crypto.h" |
Hanno Becker | 65935d9 | 2019-02-01 11:55:03 +0000 | [diff] [blame] | 49 | #include "mbedtls/psa_util.h" |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 50 | #endif |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/platform.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 52 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 53 | /* Parameter validation macros based on platform_util.h */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | #define PK_VALIDATE_RET(cond) \ |
| 55 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA) |
| 56 | #define PK_VALIDATE(cond) \ |
| 57 | MBEDTLS_INTERNAL_VALIDATE(cond) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 60 | /* |
| 61 | * RSAPublicKey ::= SEQUENCE { |
| 62 | * modulus INTEGER, -- n |
| 63 | * publicExponent INTEGER -- e |
| 64 | * } |
| 65 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start, |
| 67 | mbedtls_rsa_context *rsa) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 68 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 69 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 70 | size_t len = 0; |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 71 | mbedtls_mpi T; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 72 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | mbedtls_mpi_init(&T); |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 74 | |
| 75 | /* Export E */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 || |
| 77 | (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 78 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 80 | len += ret; |
| 81 | |
| 82 | /* Export N */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 || |
| 84 | (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 85 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 87 | len += ret; |
| 88 | |
| 89 | end_of_export: |
| 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | mbedtls_mpi_free(&T); |
| 92 | if (ret < 0) { |
| 93 | return ret; |
| 94 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 95 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 97 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 98 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 99 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 100 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 101 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | #if defined(MBEDTLS_ECP_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 105 | /* |
| 106 | * EC public key is an EC point |
| 107 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start, |
| 109 | mbedtls_ecp_keypair *ec) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 110 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 111 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 112 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN]; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q, |
| 116 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 117 | &len, buf, sizeof(buf))) != 0) { |
| 118 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | if (*p < start || (size_t) (*p - start) < len) { |
| 122 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 123 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 124 | |
| 125 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | memcpy(*p, buf, len); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * ECParameters ::= CHOICE { |
| 133 | * namedCurve OBJECT IDENTIFIER |
| 134 | * } |
| 135 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | static int pk_write_ec_param(unsigned char **p, unsigned char *start, |
| 137 | mbedtls_ecp_keypair *ec) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 138 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 139 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 140 | size_t len = 0; |
| 141 | const char *oid; |
| 142 | size_t oid_len; |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | if ((ret = mbedtls_oid_get_oid_by_ec_grp(ec->grp.id, &oid, &oid_len)) != 0) { |
| 145 | return ret; |
| 146 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 147 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 149 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 151 | } |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * privateKey OCTET STRING -- always of length ceil(log2(n)/8) |
| 155 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | static int pk_write_ec_private(unsigned char **p, unsigned char *start, |
| 157 | mbedtls_ecp_keypair *ec) |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 158 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 159 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | size_t byte_length = (ec->grp.pbits + 7) / 8; |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 161 | unsigned char tmp[MBEDTLS_ECP_MAX_BYTES]; |
| 162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | ret = mbedtls_ecp_write_key(ec, tmp, byte_length); |
| 164 | if (ret != 0) { |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 165 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | } |
| 167 | ret = mbedtls_asn1_write_octet_string(p, start, tmp, byte_length); |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 168 | |
| 169 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | mbedtls_platform_zeroize(tmp, byte_length); |
| 171 | return ret; |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 172 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 174 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 175 | int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, |
| 176 | const mbedtls_pk_context *key) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 177 | { |
Dave Rodgman | 94210b1 | 2023-06-28 14:08:07 +0100 | [diff] [blame] | 178 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 179 | size_t len = 0; |
| 180 | |
Dave Rodgman | 1262315 | 2023-06-28 11:54:25 +0100 | [diff] [blame] | 181 | (void) p; |
| 182 | (void) start; |
| 183 | (void) key; |
Dave Rodgman | 94210b1 | 2023-06-28 14:08:07 +0100 | [diff] [blame] | 184 | (void) ret; |
Dave Rodgman | 1262315 | 2023-06-28 11:54:25 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | PK_VALIDATE_RET(p != NULL); |
| 187 | PK_VALIDATE_RET(*p != NULL); |
| 188 | PK_VALIDATE_RET(start != NULL); |
| 189 | PK_VALIDATE_RET(key != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
| 193 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key))); |
| 194 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 195 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 197 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
| 198 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key))); |
| 199 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 200 | #endif |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 201 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 158c3d1 | 2018-11-19 18:09:59 -0500 | [diff] [blame] | 203 | size_t buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | psa_key_id_t *key_id = (psa_key_id_t *) key->pk_ctx; |
Andrzej Kurek | 158c3d1 | 2018-11-19 18:09:59 -0500 | [diff] [blame] | 205 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | if (*p < start) { |
| 207 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 208 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | |
| 210 | buffer_size = (size_t) (*p - start); |
| 211 | if (psa_export_public_key(*key_id, start, buffer_size, &len) |
| 212 | != PSA_SUCCESS) { |
| 213 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 214 | } else { |
Hanno Becker | 4fb8db2 | 2019-02-01 09:57:20 +0000 | [diff] [blame] | 215 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | memmove(*p, start, len); |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 217 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | } else |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 219 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 226 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 228 | unsigned char *c; |
| 229 | size_t len = 0, par_len = 0, oid_len; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 230 | mbedtls_pk_type_t pk_type; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 231 | const char *oid; |
| 232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | PK_VALIDATE_RET(key != NULL); |
| 234 | if (size == 0) { |
| 235 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 236 | } |
| 237 | PK_VALIDATE_RET(buf != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 238 | |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 239 | c = buf + size; |
| 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | if (c - buf < 1) { |
| 244 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 245 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 249 | * algorithm AlgorithmIdentifier, |
| 250 | * subjectPublicKey BIT STRING } |
| 251 | */ |
| 252 | *--c = 0; |
| 253 | len += 1; |
| 254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 256 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | pk_type = mbedtls_pk_get_type(key); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | if (pk_type == MBEDTLS_PK_ECKEY) { |
| 261 | MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, mbedtls_pk_ec(*key))); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 262 | } |
| 263 | #endif |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 264 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | if (pk_type == MBEDTLS_PK_OPAQUE) { |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 266 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 267 | psa_key_type_t key_type; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 268 | psa_key_id_t key_id; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 269 | psa_ecc_family_t curve; |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 270 | size_t bits; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | key_id = *((psa_key_id_t *) key->pk_ctx); |
| 273 | if (PSA_SUCCESS != psa_get_key_attributes(key_id, &attributes)) { |
| 274 | return MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
| 275 | } |
| 276 | key_type = psa_get_key_type(&attributes); |
| 277 | bits = psa_get_key_bits(&attributes); |
| 278 | psa_reset_key_attributes(&attributes); |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type); |
| 281 | if (curve == 0) { |
| 282 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 283 | } |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 284 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 285 | ret = mbedtls_psa_get_ecc_oid_from_id(curve, bits, &oid, &oid_len); |
| 286 | if (ret != 0) { |
| 287 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 288 | } |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 289 | |
| 290 | /* Write EC algorithm parameters; that's akin |
| 291 | * to pk_write_ec_param() above. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_oid(&c, buf, |
| 293 | oid, oid_len)); |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 294 | |
| 295 | /* The rest of the function works as for legacy EC contexts. */ |
| 296 | pk_type = MBEDTLS_PK_ECKEY; |
| 297 | } |
| 298 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 299 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid, |
| 301 | &oid_len)) != 0) { |
| 302 | return ret; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 303 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 304 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(&c, buf, oid, oid_len, |
| 306 | par_len)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 309 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 310 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 315 | int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 316 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 317 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 318 | unsigned char *c; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 319 | size_t len = 0; |
| 320 | |
Dave Rodgman | 1262315 | 2023-06-28 11:54:25 +0100 | [diff] [blame] | 321 | (void) ret; |
| 322 | (void) c; |
| 323 | (void) key; |
| 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | PK_VALIDATE_RET(key != NULL); |
| 326 | if (size == 0) { |
| 327 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 328 | } |
| 329 | PK_VALIDATE_RET(buf != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 330 | |
| 331 | c = buf + size; |
| 332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 334 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 335 | mbedtls_mpi T; /* Temporary holding the exported parameters */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 336 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 337 | |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 338 | /* |
| 339 | * Export the parameters one after another to avoid simultaneous copies. |
| 340 | */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | mbedtls_mpi_init(&T); |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 343 | |
| 344 | /* Export QP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 345 | if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 || |
| 346 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 347 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 348 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 349 | len += ret; |
| 350 | |
| 351 | /* Export DQ */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 352 | if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 || |
| 353 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 354 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 355 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 356 | len += ret; |
| 357 | |
| 358 | /* Export DP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 || |
| 360 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 361 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 362 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 363 | len += ret; |
| 364 | |
| 365 | /* Export Q */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 366 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 367 | &T, NULL, NULL)) != 0 || |
| 368 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 369 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 370 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 371 | len += ret; |
| 372 | |
| 373 | /* Export P */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | if ((ret = mbedtls_rsa_export(rsa, NULL, &T, |
| 375 | NULL, NULL, NULL)) != 0 || |
| 376 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 377 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 379 | len += ret; |
| 380 | |
| 381 | /* Export D */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 383 | NULL, &T, NULL)) != 0 || |
| 384 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 385 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 387 | len += ret; |
| 388 | |
| 389 | /* Export E */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 391 | NULL, NULL, &T)) != 0 || |
| 392 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 393 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 395 | len += ret; |
| 396 | |
| 397 | /* Export N */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | if ((ret = mbedtls_rsa_export(rsa, &T, NULL, |
| 399 | NULL, NULL, NULL)) != 0 || |
| 400 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 401 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 403 | len += ret; |
| 404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | end_of_export: |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | mbedtls_mpi_free(&T); |
| 408 | if (ret < 0) { |
| 409 | return ret; |
| 410 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0)); |
| 413 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 414 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, |
| 415 | buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 416 | MBEDTLS_ASN1_SEQUENCE)); |
| 417 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 418 | #endif /* MBEDTLS_RSA_C */ |
| 419 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 420 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
| 421 | mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 422 | size_t pub_len = 0, par_len = 0; |
| 423 | |
| 424 | /* |
| 425 | * RFC 5915, or SEC1 Appendix C.4 |
| 426 | * |
| 427 | * ECPrivateKey ::= SEQUENCE { |
| 428 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 429 | * privateKey OCTET STRING, |
| 430 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 431 | * publicKey [1] BIT STRING OPTIONAL |
| 432 | * } |
| 433 | */ |
| 434 | |
| 435 | /* publicKey */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | if (c - buf < 1) { |
| 439 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 440 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 441 | *--c = 0; |
| 442 | pub_len += 1; |
| 443 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len)); |
| 445 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 446 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 447 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len)); |
| 448 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, |
| 449 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 450 | MBEDTLS_ASN1_CONSTRUCTED | 1)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 451 | len += pub_len; |
| 452 | |
| 453 | /* parameters */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 456 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len)); |
| 457 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf, |
| 458 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 459 | MBEDTLS_ASN1_CONSTRUCTED | 0)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 460 | len += par_len; |
| 461 | |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 462 | /* privateKey */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 463 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 464 | |
| 465 | /* version */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 468 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 469 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 470 | MBEDTLS_ASN1_SEQUENCE)); |
| 471 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 474 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 479 | |
| 480 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n" |
| 481 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n" |
| 482 | |
| 483 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n" |
| 484 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n" |
| 485 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n" |
| 486 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n" |
| 487 | |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 488 | /* |
| 489 | * Max sizes of key per types. Shown as tag + len (+ content). |
| 490 | */ |
| 491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 493 | /* |
| 494 | * RSA public keys: |
| 495 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3 |
| 496 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 497 | * + 1 + 1 + 9 (rsa oid) |
| 498 | * + 1 + 1 (params null) |
| 499 | * subjectPublicKey BIT STRING } 1 + 3 + (1 + below) |
| 500 | * RSAPublicKey ::= SEQUENCE { 1 + 3 |
| 501 | * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1 |
| 502 | * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1 |
| 503 | * } |
| 504 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 505 | #define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * RSA private keys: |
| 509 | * RSAPrivateKey ::= SEQUENCE { 1 + 3 |
| 510 | * version Version, 1 + 1 + 1 |
| 511 | * modulus INTEGER, 1 + 3 + MPI_MAX + 1 |
| 512 | * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 513 | * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 514 | * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 515 | * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 516 | * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 517 | * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 518 | * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 519 | * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported) |
| 520 | * } |
| 521 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 522 | #define MPI_MAX_SIZE_2 (MBEDTLS_MPI_MAX_SIZE / 2 + \ |
| 523 | MBEDTLS_MPI_MAX_SIZE % 2) |
| 524 | #define RSA_PRV_DER_MAX_BYTES (47 + 3 * MBEDTLS_MPI_MAX_SIZE \ |
| 525 | + 5 * MPI_MAX_SIZE_2) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | #else /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 528 | |
| 529 | #define RSA_PUB_DER_MAX_BYTES 0 |
| 530 | #define RSA_PRV_DER_MAX_BYTES 0 |
| 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 535 | /* |
| 536 | * EC public keys: |
| 537 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2 |
| 538 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 539 | * + 1 + 1 + 7 (ec oid) |
| 540 | * + 1 + 1 + 9 (namedCurve oid) |
| 541 | * subjectPublicKey BIT STRING 1 + 2 + 1 [1] |
| 542 | * + 1 (point format) [1] |
| 543 | * + 2 * ECP_MAX (coords) [1] |
| 544 | * } |
| 545 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 546 | #define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * EC private keys: |
| 550 | * ECPrivateKey ::= SEQUENCE { 1 + 2 |
| 551 | * version INTEGER , 1 + 1 + 1 |
| 552 | * privateKey OCTET STRING, 1 + 1 + ECP_MAX |
| 553 | * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9) |
| 554 | * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above |
| 555 | * } |
| 556 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 557 | #define ECP_PRV_DER_MAX_BYTES (29 + 3 * MBEDTLS_ECP_MAX_BYTES) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | #else /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 560 | |
| 561 | #define ECP_PUB_DER_MAX_BYTES 0 |
| 562 | #define ECP_PRV_DER_MAX_BYTES 0 |
| 563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 564 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 565 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 566 | #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 567 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES) |
| 568 | #define PRV_DER_MAX_BYTES (RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \ |
| 569 | RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 572 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 573 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Yanray Wang | 217416a | 2023-08-11 15:03:51 +0800 | [diff] [blame] | 574 | unsigned char *output_buf = NULL; |
| 575 | output_buf = calloc(1, PUB_DER_MAX_BYTES); |
| 576 | if (output_buf == NULL) { |
| 577 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 578 | } |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 579 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 580 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 581 | PK_VALIDATE_RET(key != NULL); |
| 582 | PK_VALIDATE_RET(buf != NULL || size == 0); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, |
Yanray Wang | 217416a | 2023-08-11 15:03:51 +0800 | [diff] [blame] | 585 | PUB_DER_MAX_BYTES)) < 0) { |
| 586 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 587 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 590 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
Yanray Wang | 217416a | 2023-08-11 15:03:51 +0800 | [diff] [blame] | 591 | output_buf + PUB_DER_MAX_BYTES - ret, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | ret, buf, size, &olen)) != 0) { |
Yanray Wang | 217416a | 2023-08-11 15:03:51 +0800 | [diff] [blame] | 593 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 594 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Yanray Wang | 217416a | 2023-08-11 15:03:51 +0800 | [diff] [blame] | 597 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 598 | return 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 601 | int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 602 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 603 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 604 | unsigned char *output_buf = NULL; |
| 605 | output_buf = calloc(1, PRV_DER_MAX_BYTES); |
| 606 | if (output_buf == NULL) { |
| 607 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 608 | } |
Paul Bakker | fcc1721 | 2013-10-11 09:36:52 +0200 | [diff] [blame] | 609 | const char *begin, *end; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 610 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 611 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 612 | PK_VALIDATE_RET(key != NULL); |
| 613 | PK_VALIDATE_RET(buf != NULL || size == 0); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 614 | |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 615 | if ((ret = mbedtls_pk_write_key_der(key, output_buf, PRV_DER_MAX_BYTES)) < 0) { |
| 616 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 617 | return ret; |
| 618 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 622 | begin = PEM_BEGIN_PRIVATE_KEY_RSA; |
| 623 | end = PEM_END_PRIVATE_KEY_RSA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 625 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 628 | begin = PEM_BEGIN_PRIVATE_KEY_EC; |
| 629 | end = PEM_END_PRIVATE_KEY_EC; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 630 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 631 | #endif |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 632 | { |
| 633 | free(output_buf); |
| 634 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 635 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 636 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | if ((ret = mbedtls_pem_write_buffer(begin, end, |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 638 | output_buf + PRV_DER_MAX_BYTES - ret, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 639 | ret, buf, size, &olen)) != 0) { |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 640 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 641 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Yanray Wang | 7bbca13 | 2023-08-11 15:33:07 +0800 | [diff] [blame^] | 644 | free(output_buf); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 645 | return 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 646 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | #endif /* MBEDTLS_PK_WRITE_C */ |