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