Skip to content

Commit abb0b22

Browse files
authored
Merge pull request #10505 from valeriosetti/issue10453
Remove use of `pk_can_do()`
2 parents e5ba96c + c0ac4a6 commit abb0b22

File tree

8 files changed

+57
-23
lines changed

8 files changed

+57
-23
lines changed

library/mbedtls_utils.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "mbedtls/pk.h"
2+
#include "psa/crypto.h"
3+
4+
#ifndef MBEDTLS_UTILS_H
5+
#define MBEDTLS_UTILS_H
6+
7+
/* Return the PSA algorithm associated to the given combination of "sigalg" and "hash_alg". */
8+
static inline psa_algorithm_t mbedtls_psa_alg_from_pk_sigalg(mbedtls_pk_sigalg_t sigalg,
9+
psa_algorithm_t hash_alg)
10+
{
11+
switch (sigalg) {
12+
case MBEDTLS_PK_SIGALG_RSA_PKCS1V15:
13+
return PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg);
14+
case MBEDTLS_PK_SIGALG_RSA_PSS:
15+
return PSA_ALG_RSA_PSS(hash_alg);
16+
case MBEDTLS_PK_SIGALG_ECDSA:
17+
return MBEDTLS_PK_ALG_ECDSA(hash_alg);
18+
default:
19+
return PSA_ALG_NONE;
20+
}
21+
}
22+
23+
#endif /* MBEDTLS_UTILS_H */

library/ssl_tls.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5605,13 +5605,15 @@ void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
56055605
*/
56065606
unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
56075607
{
5608+
psa_key_type_t key_type = mbedtls_pk_get_key_type(pk);
5609+
56085610
#if defined(MBEDTLS_RSA_C)
5609-
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5611+
if (PSA_KEY_TYPE_IS_RSA(key_type)) {
56105612
return MBEDTLS_SSL_SIG_RSA;
56115613
}
56125614
#endif
56135615
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
5614-
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5616+
if (PSA_KEY_TYPE_IS_ECC(key_type)) {
56155617
return MBEDTLS_SSL_SIG_ECDSA;
56165618
}
56175619
#endif
@@ -8780,7 +8782,7 @@ int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
87808782
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
87818783
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
87828784
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
8783-
mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
8785+
PSA_KEY_TYPE_IS_ECC(mbedtls_pk_get_type(&chain->pk))) {
87848786
if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
87858787
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
87868788
ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;

library/ssl_tls12_client.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "debug_internal.h"
1717
#include "mbedtls/error.h"
1818
#include "mbedtls/constant_time.h"
19+
#include "mbedtls_utils.h"
1920

2021
#include "psa/crypto.h"
2122
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
@@ -1883,6 +1884,7 @@ static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
18831884
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
18841885

18851886
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1887+
psa_algorithm_t psa_hash_alg;
18861888
mbedtls_pk_sigalg_t pk_alg = MBEDTLS_PK_SIGALG_NONE;
18871889
unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
18881890
size_t params_len = (size_t) (p - params);
@@ -1921,7 +1923,10 @@ static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
19211923
}
19221924
p += 2;
19231925

1924-
if (!mbedtls_pk_can_do(peer_pk, (mbedtls_pk_type_t) pk_alg)) {
1926+
psa_hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
1927+
if (!mbedtls_pk_can_do_psa(peer_pk,
1928+
mbedtls_psa_alg_from_pk_sigalg(pk_alg, psa_hash_alg),
1929+
PSA_KEY_USAGE_VERIFY_HASH)) {
19251930
MBEDTLS_SSL_DEBUG_MSG(1,
19261931
("bad server key exchange message"));
19271932
mbedtls_ssl_send_alert_message(
@@ -1977,14 +1982,6 @@ static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
19771982
/*
19781983
* Verify signature
19791984
*/
1980-
if (!mbedtls_pk_can_do(peer_pk, (mbedtls_pk_type_t) pk_alg)) {
1981-
MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
1982-
mbedtls_ssl_send_alert_message(
1983-
ssl,
1984-
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1985-
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1986-
return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
1987-
}
19881985

19891986
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
19901987
if (ssl->handshake->ecrs_enabled) {

library/ssl_tls12_server.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mbedtls/error.h"
1717
#include "mbedtls/platform_util.h"
1818
#include "mbedtls/constant_time.h"
19+
#include "mbedtls_utils.h"
1920

2021
#include <string.h>
2122

@@ -3324,6 +3325,7 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
33243325
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
33253326
ssl->handshake->ciphersuite_info;
33263327
mbedtls_pk_context *peer_pk;
3328+
psa_algorithm_t psa_sig_alg;
33273329

33283330
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
33293331

@@ -3421,7 +3423,8 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
34213423
/*
34223424
* Check the certificate's key type matches the signature alg
34233425
*/
3424-
if (!mbedtls_pk_can_do(peer_pk, (mbedtls_pk_type_t) pk_alg)) {
3426+
psa_sig_alg = mbedtls_psa_alg_from_pk_sigalg(pk_alg, mbedtls_md_psa_alg_from_type(md_alg));
3427+
if (!mbedtls_pk_can_do_psa(peer_pk, psa_sig_alg, PSA_KEY_USAGE_VERIFY_HASH)) {
34253428
MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
34263429
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
34273430
}

library/ssl_tls13_generic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mbedtls/constant_time.h"
1919
#include "psa/crypto.h"
2020
#include "mbedtls/psa_util.h"
21+
#include "mbedtls_utils.h"
2122

2223
#include "ssl_tls13_invasive.h"
2324
#include "ssl_tls13_keys.h"
@@ -276,7 +277,9 @@ static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
276277
/*
277278
* Check the certificate's key type matches the signature alg
278279
*/
279-
if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, (mbedtls_pk_type_t) sig_alg)) {
280+
if (!mbedtls_pk_can_do_psa(&ssl->session_negotiate->peer_cert->pk,
281+
mbedtls_psa_alg_from_pk_sigalg(sig_alg, hash_alg),
282+
PSA_KEY_USAGE_VERIFY_HASH)) {
280283
MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
281284
goto error;
282285
}

library/x509_crt.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "mbedtls/threading.h"
4444
#endif
4545

46+
#include "mbedtls_utils.h"
47+
4648
#if defined(MBEDTLS_HAVE_TIME)
4749
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
4850
#ifndef WIN32_LEAN_AND_MEAN
@@ -2108,6 +2110,13 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
21082110
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
21092111
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
21102112

2113+
/* Skip expensive computation on obvious mismatch */
2114+
if (!mbedtls_pk_can_do_psa(&parent->pk,
2115+
mbedtls_psa_alg_from_pk_sigalg(child->sig_pk, hash_alg),
2116+
PSA_KEY_USAGE_VERIFY_HASH)) {
2117+
return -1;
2118+
}
2119+
21112120
status = psa_hash_compute(hash_alg,
21122121
child->tbs.p,
21132122
child->tbs.len,
@@ -2118,11 +2127,6 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
21182127
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
21192128
}
21202129

2121-
/* Skip expensive computation on obvious mismatch */
2122-
if (!mbedtls_pk_can_do(&parent->pk, (mbedtls_pk_type_t) child->sig_pk)) {
2123-
return -1;
2124-
}
2125-
21262130
#if defined(MBEDTLS_ECP_RESTARTABLE)
21272131
if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_SIGALG_ECDSA) {
21282132
return mbedtls_pk_verify_restartable(&parent->pk,

library/x509write_crt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
392392
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
393393
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
394394
psa_algorithm_t psa_algorithm;
395+
psa_key_type_t key_type = mbedtls_pk_get_key_type(ctx->issuer_key);
395396

396397
size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
397398
size_t len = 0;
@@ -407,9 +408,9 @@ int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
407408

408409
/* There's no direct way of extracting a signature algorithm
409410
* (represented as an element of mbedtls_pk_type_t) from a PK instance. */
410-
if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
411+
if (PSA_KEY_TYPE_IS_RSA(key_type)) {
411412
pk_alg = MBEDTLS_PK_SIGALG_RSA_PKCS1V15;
412-
} else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
413+
} else if (PSA_KEY_TYPE_IS_ECC(key_type)) {
413414
pk_alg = MBEDTLS_PK_SIGALG_ECDSA;
414415
} else {
415416
return MBEDTLS_ERR_X509_INVALID_ALG;

library/x509write_csr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
144144
mbedtls_pk_sigalg_t pk_alg;
145145
size_t hash_len;
146146
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
147+
psa_key_type_t key_type = mbedtls_pk_get_key_type(ctx->key);
147148

148149
/* Write the CSR backwards starting from the end of buf */
149150
c = buf + size;
@@ -217,9 +218,9 @@ static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
217218
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
218219
}
219220

220-
if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
221+
if (PSA_KEY_TYPE_IS_RSA(key_type)) {
221222
pk_alg = MBEDTLS_PK_SIGALG_RSA_PKCS1V15;
222-
} else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
223+
} else if (PSA_KEY_TYPE_IS_ECC(key_type)) {
223224
pk_alg = MBEDTLS_PK_SIGALG_ECDSA;
224225
} else {
225226
return MBEDTLS_ERR_X509_INVALID_ALG;

0 commit comments

Comments
 (0)