| 1 | /** |
| 2 | * \file pk_internal.h |
| 3 | * |
| 4 | * \brief Public Key abstraction layer: internal (i.e. library only) functions |
| 5 | * and definitions. |
| 6 | */ |
| 7 | /* |
| 8 | * Copyright The Mbed TLS Contributors |
| 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 10 | */ |
| 11 | #ifndef MBEDTLS_PK_INTERNAL_H |
| 12 | #define MBEDTLS_PK_INTERNAL_H |
| 13 | |
| 14 | #include "mbedtls/pk.h" |
| 15 | |
| 16 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 17 | #include "mbedtls/ecp.h" |
| 18 | #endif |
| 19 | |
| 20 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
| 21 | #include "psa/crypto.h" |
| 22 | |
| 23 | #include "psa_util_internal.h" |
| 24 | #define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status) |
| 25 | #define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 26 | psa_to_pk_rsa_errors, \ |
| 27 | psa_pk_status_to_mbedtls) |
| 28 | #define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 29 | psa_to_pk_ecdsa_errors, \ |
| 30 | psa_pk_status_to_mbedtls) |
| 31 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
| 32 | |
| 33 | /* Headers/footers for PEM files */ |
| 34 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----" |
| 35 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----" |
| 36 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----" |
| 37 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----" |
| 38 | #define PEM_BEGIN_PUBLIC_KEY_RSA "-----BEGIN RSA PUBLIC KEY-----" |
| 39 | #define PEM_END_PUBLIC_KEY_RSA "-----END RSA PUBLIC KEY-----" |
| 40 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----" |
| 41 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----" |
| 42 | #define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----" |
| 43 | #define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----" |
| 44 | #define PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----BEGIN ENCRYPTED PRIVATE KEY-----" |
| 45 | #define PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----END ENCRYPTED PRIVATE KEY-----" |
| 46 | |
| 47 | /* |
| 48 | * We're trying to statisfy two kinds of users: |
| 49 | * - those who don't want to use the heap; |
| 50 | * - those who can't afford large stack buffers. |
| 51 | * |
| 52 | * The current compromise is that if ECC is the only key type supported in PK, |
| 53 | * then we export keys on the stack, and otherwise we use the heap. |
| 54 | * |
| 55 | * RSA can either be used directly or indirectly via opaque keys if enabled. |
| 56 | * (RSA_ALT is not relevant here as we can't export from such contexts.) |
| 57 | */ |
| 58 | #if !defined(MBEDTLS_RSA_C) && \ |
| 59 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)) |
| 60 | #define PK_EXPORT_KEYS_ON_THE_STACK |
| 61 | #endif |
| 62 | |
| 63 | #if defined(PK_EXPORT_KEYS_ON_THE_STACK) |
| 64 | /* We know for ECC, pubkey are longer than privkeys, but double check. |
| 65 | * Also, take the maximum size of legacy and PSA, as PSA might be disabled. */ |
| 66 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH |
| 67 | #if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH |
| 68 | #undef PK_EXPORT_KEY_STACK_BUFFER_SIZE |
| 69 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH |
| 70 | #endif |
| 71 | #if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_ECP_MAX_BYTES |
| 72 | #undef PK_EXPORT_KEY_STACK_BUFFER_SIZE |
| 73 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_ECP_MAX_BYTES |
| 74 | #endif |
| 75 | #if PK_EXPORT_KEY_STACK_BUFFER_SIZE < MBEDTLS_ECP_MAX_PT_LEN |
| 76 | #undef PK_EXPORT_KEY_STACK_BUFFER_SIZE |
| 77 | #define PK_EXPORT_KEY_STACK_BUFFER_SIZE MBEDTLS_ECP_MAX_PT_LEN |
| 78 | #endif |
| 79 | #endif |
| 80 | |
| 81 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 82 | /** |
| 83 | * Public function mbedtls_pk_ec() can be used to get direct access to the |
| 84 | * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not |
| 85 | * ideal because it bypasses the PK module on the control of its internal |
| 86 | * structure (pk_context) fields. |
| 87 | * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but |
| 88 | * we provide 2 very similar functions when only ECP_LIGHT is enabled and not |
| 89 | * ECP_C. |
| 90 | * These variants embed the "ro" or "rw" keywords in their name to make the |
| 91 | * usage of the returned pointer explicit. Of course the returned value is |
| 92 | * const or non-const accordingly. |
| 93 | */ |
| 94 | static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk) |
| 95 | { |
| 96 | switch (mbedtls_pk_get_type(&pk)) { |
| 97 | case MBEDTLS_PK_ECKEY: |
| 98 | case MBEDTLS_PK_ECKEY_DH: |
| 99 | case MBEDTLS_PK_ECDSA: |
| 100 | return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
| 101 | default: |
| 102 | return NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk) |
| 107 | { |
| 108 | switch (mbedtls_pk_get_type(&pk)) { |
| 109 | case MBEDTLS_PK_ECKEY: |
| 110 | case MBEDTLS_PK_ECKEY_DH: |
| 111 | case MBEDTLS_PK_ECDSA: |
| 112 | return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
| 113 | default: |
| 114 | return NULL; |
| 115 | } |
| 116 | } |
| 117 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 118 | |
| 119 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 120 | static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_context *pk) |
| 121 | { |
| 122 | mbedtls_ecp_group_id id; |
| 123 | |
| 124 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 125 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) { |
| 126 | psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT; |
| 127 | psa_key_type_t opaque_key_type; |
| 128 | psa_ecc_family_t curve; |
| 129 | |
| 130 | if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) { |
| 131 | return MBEDTLS_ECP_DP_NONE; |
| 132 | } |
| 133 | opaque_key_type = psa_get_key_type(&opaque_attrs); |
| 134 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type); |
| 135 | id = mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(&opaque_attrs)); |
| 136 | psa_reset_key_attributes(&opaque_attrs); |
| 137 | } else |
| 138 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 139 | { |
| 140 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 141 | id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits); |
| 142 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 143 | id = mbedtls_pk_ec_ro(*pk)->grp.id; |
| 144 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 145 | } |
| 146 | |
| 147 | return id; |
| 148 | } |
| 149 | |
| 150 | /* Helper for Montgomery curves */ |
| 151 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448) |
| 152 | #define MBEDTLS_PK_HAVE_RFC8410_CURVES |
| 153 | #endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */ |
| 154 | |
| 155 | #define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \ |
| 156 | ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448)) |
| 157 | |
| 158 | static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk) |
| 159 | { |
| 160 | mbedtls_ecp_group_id id = mbedtls_pk_get_ec_group_id(pk); |
| 161 | |
| 162 | return MBEDTLS_PK_IS_RFC8410_GROUP_ID(id); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Set the group used by this key. |
| 167 | * |
| 168 | * [in/out] pk: in: must have been pk_setup() to an ECC type |
| 169 | * out: will have group (curve) information set |
| 170 | * [in] grp_in: a supported group ID (not NONE) |
| 171 | */ |
| 172 | int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id); |
| 173 | |
| 174 | /* |
| 175 | * Set the private key material |
| 176 | * |
| 177 | * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group(). |
| 178 | * out: will have the private key set. |
| 179 | * [in] key, key_len: the raw private key (no ASN.1 wrapping). |
| 180 | */ |
| 181 | int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len); |
| 182 | |
| 183 | /* |
| 184 | * Set the public key. |
| 185 | * |
| 186 | * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group(). |
| 187 | * out: will have the public key set. |
| 188 | * [in] pub, pub_len: the raw public key (an ECPoint). |
| 189 | * |
| 190 | * Return: |
| 191 | * - 0 on success; |
| 192 | * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid |
| 193 | * but not supported; |
| 194 | * - another error code otherwise. |
| 195 | */ |
| 196 | int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len); |
| 197 | |
| 198 | /* |
| 199 | * Derive a public key from its private counterpart. |
| 200 | * Computationally intensive, only use when public key is not available. |
| 201 | * |
| 202 | * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key(). |
| 203 | * out: will have the public key set. |
| 204 | * [in] prv, prv_len: the raw private key (see note below). |
| 205 | * [in] f_rng, p_rng: RNG function and context. |
| 206 | * |
| 207 | * Note: the private key information is always available from pk, |
| 208 | * however for convenience the serialized version is also passed, |
| 209 | * as it's available at each calling site, and useful in some configs |
| 210 | * (as otherwise we would have to re-serialize it from the pk context). |
| 211 | * |
| 212 | * There are three implementations of this function: |
| 213 | * 1. MBEDTLS_PK_USE_PSA_EC_DATA, |
| 214 | * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA, |
| 215 | * 3. not MBEDTLS_USE_PSA_CRYPTO. |
| 216 | */ |
| 217 | int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk, |
| 218 | const unsigned char *prv, size_t prv_len, |
| 219 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
| 220 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 221 | |
| 222 | /* Helper for (deterministic) ECDSA */ |
| 223 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 224 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_DETERMINISTIC_ECDSA |
| 225 | #else |
| 226 | #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_ECDSA |
| 227 | #endif |
| 228 | |
| 229 | #if defined(MBEDTLS_TEST_HOOKS) |
| 230 | MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der( |
| 231 | mbedtls_pk_context *pk, |
| 232 | unsigned char *key, size_t keylen, |
| 233 | const unsigned char *pwd, size_t pwdlen, |
| 234 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
| 235 | #endif |
| 236 | |
| 237 | #if defined(MBEDTLS_FS_IO) |
| 238 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n); |
| 239 | #endif |
| 240 | |
| 241 | #endif /* MBEDTLS_PK_INTERNAL_H */ |
| 242 | |