Merge pull request #7595 from valeriosetti/deprecate_pk_ec
Set mbedtls_pk_ec() as internal function when ECP_C is not defined
This commit is contained in:
commit
b1c0afe484
12 changed files with 120 additions and 45 deletions
|
@ -23,6 +23,7 @@
|
|||
#include "mbedtls/pk.h"
|
||||
#include "pk_wrap.h"
|
||||
#include "pkwrite.h"
|
||||
#include "pk_internal.h"
|
||||
|
||||
#include "hash_info.h"
|
||||
|
||||
|
@ -879,7 +880,7 @@ int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
|
|||
psa_status_t status;
|
||||
|
||||
/* export the private key material in the format PSA wants */
|
||||
ec = mbedtls_pk_ec(*pk);
|
||||
ec = mbedtls_pk_ec_rw(*pk);
|
||||
d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
|
||||
if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
|
||||
return ret;
|
||||
|
|
68
library/pk_internal.h
Normal file
68
library/pk_internal.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* \file pk_internal.h
|
||||
*
|
||||
* \brief Public Key abstraction layer: internal (i.e. library only) functions
|
||||
* and definitions.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBEDTLS_PK_INTERNAL_H
|
||||
#define MBEDTLS_PK_INTERNAL_H
|
||||
|
||||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
#include "mbedtls/ecp.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
/**
|
||||
* Public function mbedtls_pk_ec() can be used to get direct access to the
|
||||
* wrapped ecp_keypair strucure pointed to the pk_ctx. However this is not
|
||||
* ideal because it bypasses the PK module on the control of its internal's
|
||||
* structure (pk_context) fields.
|
||||
* For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
|
||||
* we provide 2 very similar function when only ECP_LIGHT is enabled and not
|
||||
* ECP_C.
|
||||
* These variants embed the "ro" or "rw" keywords in their name to make the
|
||||
* usage of the returned pointer explicit. Of course the returned value is
|
||||
* const or non-const accordingly.
|
||||
*/
|
||||
static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
|
||||
{
|
||||
switch (mbedtls_pk_get_type(&pk)) {
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
case MBEDTLS_PK_ECKEY_DH:
|
||||
case MBEDTLS_PK_ECDSA:
|
||||
return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
|
||||
{
|
||||
switch (mbedtls_pk_get_type(&pk)) {
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
case MBEDTLS_PK_ECKEY_DH:
|
||||
case MBEDTLS_PK_ECDSA:
|
||||
return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_LIGHT */
|
||||
|
||||
#endif /* MBEDTLS_PK_INTERNAL_H */
|
|
@ -26,6 +26,7 @@
|
|||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "pk_internal.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
@ -795,14 +796,14 @@ int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
|
|||
if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) {
|
||||
#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
|
||||
if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
|
||||
ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, &mbedtls_pk_ec(*pk)->grp);
|
||||
ret = pk_use_ecparams_rfc8410(&alg_params, ec_grp_id, &mbedtls_pk_ec_rw(*pk)->grp);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp);
|
||||
ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec_rw(*pk)->grp);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk));
|
||||
ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec_rw(*pk));
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_ECP_LIGHT */
|
||||
|
@ -1231,10 +1232,10 @@ static int pk_parse_key_pkcs8_unencrypted_der(
|
|||
if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
|
||||
#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
|
||||
if (mbedtls_pk_is_rfc8410_curve(ec_grp_id)) {
|
||||
if ((ret =
|
||||
pk_use_ecparams_rfc8410(¶ms, ec_grp_id, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
|
||||
if ((ret = pk_use_ecparams_rfc8410(¶ms, ec_grp_id,
|
||||
&mbedtls_pk_ec_rw(*pk)->grp)) != 0 ||
|
||||
(ret =
|
||||
pk_parse_key_rfc8410_der(mbedtls_pk_ec(*pk), p, len, end, f_rng,
|
||||
pk_parse_key_rfc8410_der(mbedtls_pk_ec_rw(*pk), p, len, end, f_rng,
|
||||
p_rng)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
return ret;
|
||||
|
@ -1242,8 +1243,8 @@ static int pk_parse_key_pkcs8_unencrypted_der(
|
|||
} else
|
||||
#endif
|
||||
{
|
||||
if ((ret = pk_use_ecparams(¶ms, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len, f_rng, p_rng)) != 0) {
|
||||
if ((ret = pk_use_ecparams(¶ms, &mbedtls_pk_ec_rw(*pk)->grp)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk), p, len, f_rng, p_rng)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
return ret;
|
||||
}
|
||||
|
@ -1430,7 +1431,7 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
|
||||
|
||||
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk),
|
||||
pem.buf, pem.buflen,
|
||||
f_rng, p_rng)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
|
@ -1554,7 +1555,7 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
|
||||
if (mbedtls_pk_setup(pk, pk_info) == 0 &&
|
||||
pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
pk_parse_key_sec1_der(mbedtls_pk_ec_rw(*pk),
|
||||
key, keylen, f_rng, p_rng) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "pk_internal.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
@ -182,7 +183,7 @@ int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
|
|||
#endif
|
||||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
|
||||
MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key)));
|
||||
MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec_rw(*key)));
|
||||
} else
|
||||
#endif
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
|
@ -246,7 +247,7 @@ int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *key, unsigned char *bu
|
|||
pk_type = mbedtls_pk_get_type(key);
|
||||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
if (pk_type == MBEDTLS_PK_ECKEY) {
|
||||
ec_grp_id = mbedtls_pk_ec(*key)->grp.id;
|
||||
ec_grp_id = mbedtls_pk_ec_ro(*key)->grp.id;
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_LIGHT */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
|
@ -469,7 +470,7 @@ end_of_export:
|
|||
#endif /* MBEDTLS_RSA_C */
|
||||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
|
||||
mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
|
||||
mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*key);
|
||||
size_t pub_len = 0, par_len = 0;
|
||||
|
||||
#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
|
||||
|
@ -591,7 +592,7 @@ int mbedtls_pk_write_key_pem(const mbedtls_pk_context *key, unsigned char *buf,
|
|||
#if defined(MBEDTLS_ECP_LIGHT)
|
||||
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
|
||||
#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)
|
||||
if (mbedtls_pk_is_rfc8410_curve(mbedtls_pk_ec(*key)->grp.id)) {
|
||||
if (mbedtls_pk_is_rfc8410_curve(mbedtls_pk_ec_ro(*key)->grp.id)) {
|
||||
begin = PEM_BEGIN_PRIVATE_KEY_PKCS8;
|
||||
end = PEM_END_PRIVATE_KEY_PKCS8;
|
||||
} else
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
#endif
|
||||
|
||||
#include "mbedtls/pk.h"
|
||||
#include "pk_internal.h"
|
||||
#include "common.h"
|
||||
|
||||
/* Shorthand for restartable ECC */
|
||||
|
|
|
@ -7388,9 +7388,9 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
|
|||
/* and in the unlikely case the above assumption no longer holds
|
||||
* we are making sure that pk_ec() here does not return a NULL
|
||||
*/
|
||||
const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
|
||||
const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
|
||||
if (ec == NULL) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec_ro() returned NULL"));
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
|
|
|
@ -2007,7 +2007,7 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
|||
return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
peer_key = mbedtls_pk_ec(*peer_pk);
|
||||
peer_key = mbedtls_pk_ec_ro(*peer_pk);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
size_t olen = 0;
|
||||
|
|
|
@ -666,7 +666,7 @@ static int ssl_check_key_curve(mbedtls_pk_context *pk,
|
|||
uint16_t *curves_tls_id)
|
||||
{
|
||||
uint16_t *curr_tls_id = curves_tls_id;
|
||||
mbedtls_ecp_group_id grp_id = mbedtls_pk_ec(*pk)->grp.id;
|
||||
mbedtls_ecp_group_id grp_id = mbedtls_pk_ec_ro(*pk)->grp.id;
|
||||
mbedtls_ecp_group_id curr_grp_id;
|
||||
|
||||
while (*curr_tls_id != 0) {
|
||||
|
@ -2636,7 +2636,7 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
|||
case MBEDTLS_PK_ECKEY:
|
||||
case MBEDTLS_PK_ECKEY_DH:
|
||||
case MBEDTLS_PK_ECDSA:
|
||||
key = mbedtls_pk_ec(*pk);
|
||||
key = mbedtls_pk_ec_rw(*pk);
|
||||
if (key == NULL) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
@ -2704,7 +2704,7 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
|||
}
|
||||
|
||||
if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
|
||||
mbedtls_pk_ec(*mbedtls_ssl_own_key(ssl)),
|
||||
mbedtls_pk_ec_ro(*mbedtls_ssl_own_key(ssl)),
|
||||
MBEDTLS_ECDH_OURS)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
|
||||
return ret;
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#include "hash_info.h"
|
||||
#include "x509_invasive.h"
|
||||
#include "pk_internal.h"
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
|
@ -237,7 +238,7 @@ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
|
|||
if (pk_alg == MBEDTLS_PK_ECDSA ||
|
||||
pk_alg == MBEDTLS_PK_ECKEY ||
|
||||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
|
||||
const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
|
||||
const mbedtls_ecp_group_id gid = mbedtls_pk_ec_ro(*pk)->grp.id;
|
||||
|
||||
if (gid == MBEDTLS_ECP_DP_NONE) {
|
||||
return -1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue