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 | { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 178 | size_t len = 0; |
| 179 | |
Dave Rodgman | 1262315 | 2023-06-28 11:54:25 +0100 | [diff] [blame^] | 180 | (void) p; |
| 181 | (void) start; |
| 182 | (void) key; |
| 183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | PK_VALIDATE_RET(p != NULL); |
| 185 | PK_VALIDATE_RET(*p != NULL); |
| 186 | PK_VALIDATE_RET(start != NULL); |
| 187 | PK_VALIDATE_RET(key != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
| 191 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key))); |
| 192 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 193 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 195 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
| 196 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key))); |
| 197 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 198 | #endif |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 199 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 158c3d1 | 2018-11-19 18:09:59 -0500 | [diff] [blame] | 201 | size_t buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | 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] | 203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | if (*p < start) { |
| 205 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 206 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | |
| 208 | buffer_size = (size_t) (*p - start); |
| 209 | if (psa_export_public_key(*key_id, start, buffer_size, &len) |
| 210 | != PSA_SUCCESS) { |
| 211 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 212 | } else { |
Hanno Becker | 4fb8db2 | 2019-02-01 09:57:20 +0000 | [diff] [blame] | 213 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | memmove(*p, start, len); |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 215 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | } else |
Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 217 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | 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] | 224 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 225 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 226 | unsigned char *c; |
| 227 | size_t len = 0, par_len = 0, oid_len; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 228 | mbedtls_pk_type_t pk_type; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 229 | const char *oid; |
| 230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | PK_VALIDATE_RET(key != NULL); |
| 232 | if (size == 0) { |
| 233 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 234 | } |
| 235 | PK_VALIDATE_RET(buf != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 236 | |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 237 | c = buf + size; |
| 238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 239 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | if (c - buf < 1) { |
| 242 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 243 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 247 | * algorithm AlgorithmIdentifier, |
| 248 | * subjectPublicKey BIT STRING } |
| 249 | */ |
| 250 | *--c = 0; |
| 251 | len += 1; |
| 252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 254 | 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] | 255 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | pk_type = mbedtls_pk_get_type(key); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | if (pk_type == MBEDTLS_PK_ECKEY) { |
| 259 | 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] | 260 | } |
| 261 | #endif |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 262 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | if (pk_type == MBEDTLS_PK_OPAQUE) { |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 265 | psa_key_type_t key_type; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 266 | psa_key_id_t key_id; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 267 | psa_ecc_family_t curve; |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 268 | size_t bits; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | key_id = *((psa_key_id_t *) key->pk_ctx); |
| 271 | if (PSA_SUCCESS != psa_get_key_attributes(key_id, &attributes)) { |
| 272 | return MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
| 273 | } |
| 274 | key_type = psa_get_key_type(&attributes); |
| 275 | bits = psa_get_key_bits(&attributes); |
| 276 | psa_reset_key_attributes(&attributes); |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 277 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type); |
| 279 | if (curve == 0) { |
| 280 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 281 | } |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 283 | ret = mbedtls_psa_get_ecc_oid_from_id(curve, bits, &oid, &oid_len); |
| 284 | if (ret != 0) { |
| 285 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 286 | } |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 287 | |
| 288 | /* Write EC algorithm parameters; that's akin |
| 289 | * to pk_write_ec_param() above. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_oid(&c, buf, |
| 291 | oid, oid_len)); |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 292 | |
| 293 | /* The rest of the function works as for legacy EC contexts. */ |
| 294 | pk_type = MBEDTLS_PK_ECKEY; |
| 295 | } |
| 296 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | if ((ret = mbedtls_oid_get_oid_by_pk_alg(pk_type, &oid, |
| 299 | &oid_len)) != 0) { |
| 300 | return ret; |
Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 301 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(&c, buf, oid, oid_len, |
| 304 | par_len)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 307 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 308 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | 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] | 314 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 316 | unsigned char *c; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 317 | size_t len = 0; |
| 318 | |
Dave Rodgman | 1262315 | 2023-06-28 11:54:25 +0100 | [diff] [blame^] | 319 | (void) ret; |
| 320 | (void) c; |
| 321 | (void) key; |
| 322 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | PK_VALIDATE_RET(key != NULL); |
| 324 | if (size == 0) { |
| 325 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 326 | } |
| 327 | PK_VALIDATE_RET(buf != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 328 | |
| 329 | c = buf + size; |
| 330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 332 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 333 | mbedtls_mpi T; /* Temporary holding the exported parameters */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 334 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 335 | |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 336 | /* |
| 337 | * Export the parameters one after another to avoid simultaneous copies. |
| 338 | */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 340 | mbedtls_mpi_init(&T); |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 341 | |
| 342 | /* Export QP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 343 | if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 || |
| 344 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 345 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 347 | len += ret; |
| 348 | |
| 349 | /* Export DQ */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 350 | if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 || |
| 351 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 352 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 354 | len += ret; |
| 355 | |
| 356 | /* Export DP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 || |
| 358 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 359 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 361 | len += ret; |
| 362 | |
| 363 | /* Export Q */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 365 | &T, NULL, NULL)) != 0 || |
| 366 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 367 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 369 | len += ret; |
| 370 | |
| 371 | /* Export P */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | if ((ret = mbedtls_rsa_export(rsa, NULL, &T, |
| 373 | NULL, NULL, NULL)) != 0 || |
| 374 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 375 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 377 | len += ret; |
| 378 | |
| 379 | /* Export D */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 381 | NULL, &T, NULL)) != 0 || |
| 382 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 383 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 385 | len += ret; |
| 386 | |
| 387 | /* Export E */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, |
| 389 | NULL, NULL, &T)) != 0 || |
| 390 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 391 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 392 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 393 | len += ret; |
| 394 | |
| 395 | /* Export N */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 396 | if ((ret = mbedtls_rsa_export(rsa, &T, NULL, |
| 397 | NULL, NULL, NULL)) != 0 || |
| 398 | (ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0) { |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 399 | goto end_of_export; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 400 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 401 | len += ret; |
| 402 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | end_of_export: |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | mbedtls_mpi_free(&T); |
| 406 | if (ret < 0) { |
| 407 | return ret; |
| 408 | } |
Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0)); |
| 411 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 412 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, |
| 413 | buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 414 | MBEDTLS_ASN1_SEQUENCE)); |
| 415 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | #endif /* MBEDTLS_RSA_C */ |
| 417 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 418 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
| 419 | mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 420 | size_t pub_len = 0, par_len = 0; |
| 421 | |
| 422 | /* |
| 423 | * RFC 5915, or SEC1 Appendix C.4 |
| 424 | * |
| 425 | * ECPrivateKey ::= SEQUENCE { |
| 426 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 427 | * privateKey OCTET STRING, |
| 428 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 429 | * publicKey [1] BIT STRING OPTIONAL |
| 430 | * } |
| 431 | */ |
| 432 | |
| 433 | /* publicKey */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | 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] | 435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | if (c - buf < 1) { |
| 437 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 438 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 439 | *--c = 0; |
| 440 | pub_len += 1; |
| 441 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len)); |
| 443 | 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] | 444 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 445 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len)); |
| 446 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, |
| 447 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 448 | MBEDTLS_ASN1_CONSTRUCTED | 1)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 449 | len += pub_len; |
| 450 | |
| 451 | /* parameters */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | 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] | 453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len)); |
| 455 | MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf, |
| 456 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 457 | MBEDTLS_ASN1_CONSTRUCTED | 0)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 458 | len += par_len; |
| 459 | |
Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 460 | /* privateKey */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_private(&c, buf, ec)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 462 | |
| 463 | /* version */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 464 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1)); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 467 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 468 | MBEDTLS_ASN1_SEQUENCE)); |
| 469 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | return (int) len; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 477 | |
| 478 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n" |
| 479 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n" |
| 480 | |
| 481 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n" |
| 482 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n" |
| 483 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n" |
| 484 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n" |
| 485 | |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 486 | /* |
| 487 | * Max sizes of key per types. Shown as tag + len (+ content). |
| 488 | */ |
| 489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 491 | /* |
| 492 | * RSA public keys: |
| 493 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3 |
| 494 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 495 | * + 1 + 1 + 9 (rsa oid) |
| 496 | * + 1 + 1 (params null) |
| 497 | * subjectPublicKey BIT STRING } 1 + 3 + (1 + below) |
| 498 | * RSAPublicKey ::= SEQUENCE { 1 + 3 |
| 499 | * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1 |
| 500 | * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1 |
| 501 | * } |
| 502 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 503 | #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] | 504 | |
| 505 | /* |
| 506 | * RSA private keys: |
| 507 | * RSAPrivateKey ::= SEQUENCE { 1 + 3 |
| 508 | * version Version, 1 + 1 + 1 |
| 509 | * modulus INTEGER, 1 + 3 + MPI_MAX + 1 |
| 510 | * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 511 | * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 512 | * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 513 | * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 514 | * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 515 | * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 516 | * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 517 | * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported) |
| 518 | * } |
| 519 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 520 | #define MPI_MAX_SIZE_2 (MBEDTLS_MPI_MAX_SIZE / 2 + \ |
| 521 | MBEDTLS_MPI_MAX_SIZE % 2) |
| 522 | #define RSA_PRV_DER_MAX_BYTES (47 + 3 * MBEDTLS_MPI_MAX_SIZE \ |
| 523 | + 5 * MPI_MAX_SIZE_2) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 524 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | #else /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 526 | |
| 527 | #define RSA_PUB_DER_MAX_BYTES 0 |
| 528 | #define RSA_PRV_DER_MAX_BYTES 0 |
| 529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 533 | /* |
| 534 | * EC public keys: |
| 535 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2 |
| 536 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 537 | * + 1 + 1 + 7 (ec oid) |
| 538 | * + 1 + 1 + 9 (namedCurve oid) |
| 539 | * subjectPublicKey BIT STRING 1 + 2 + 1 [1] |
| 540 | * + 1 (point format) [1] |
| 541 | * + 2 * ECP_MAX (coords) [1] |
| 542 | * } |
| 543 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 544 | #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] | 545 | |
| 546 | /* |
| 547 | * EC private keys: |
| 548 | * ECPrivateKey ::= SEQUENCE { 1 + 2 |
| 549 | * version INTEGER , 1 + 1 + 1 |
| 550 | * privateKey OCTET STRING, 1 + 1 + ECP_MAX |
| 551 | * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9) |
| 552 | * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above |
| 553 | * } |
| 554 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | #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] | 556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | #else /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 558 | |
| 559 | #define ECP_PUB_DER_MAX_BYTES 0 |
| 560 | #define ECP_PRV_DER_MAX_BYTES 0 |
| 561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 563 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 564 | #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 565 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES) |
| 566 | #define PRV_DER_MAX_BYTES (RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \ |
| 567 | RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES) |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 569 | 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] | 570 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 571 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 572 | unsigned char output_buf[PUB_DER_MAX_BYTES]; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 573 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 574 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 575 | PK_VALIDATE_RET(key != NULL); |
| 576 | PK_VALIDATE_RET(buf != NULL || size == 0); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 577 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 578 | if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, |
| 579 | sizeof(output_buf))) < 0) { |
| 580 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 583 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
| 584 | output_buf + sizeof(output_buf) - ret, |
| 585 | ret, buf, size, &olen)) != 0) { |
| 586 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 589 | return 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | 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] | 593 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 594 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 595 | unsigned char output_buf[PRV_DER_MAX_BYTES]; |
Paul Bakker | fcc1721 | 2013-10-11 09:36:52 +0200 | [diff] [blame] | 596 | const char *begin, *end; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 597 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 599 | PK_VALIDATE_RET(key != NULL); |
| 600 | PK_VALIDATE_RET(buf != NULL || size == 0); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 601 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 602 | if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0) { |
| 603 | return ret; |
| 604 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 607 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 608 | begin = PEM_BEGIN_PRIVATE_KEY_RSA; |
| 609 | end = PEM_END_PRIVATE_KEY_RSA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 610 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 611 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 613 | if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 614 | begin = PEM_BEGIN_PRIVATE_KEY_EC; |
| 615 | end = PEM_END_PRIVATE_KEY_EC; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 616 | } else |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 617 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 620 | if ((ret = mbedtls_pem_write_buffer(begin, end, |
| 621 | output_buf + sizeof(output_buf) - ret, |
| 622 | ret, buf, size, &olen)) != 0) { |
| 623 | return ret; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 626 | return 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 627 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_PK_WRITE_C */ |