From 06209ddc1d83aa6a6370704538421a2fe7eee857 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Mon, 16 Nov 2026 19:09:55 -0800 Subject: [PATCH] Add TF-PSA-Crypto public key parsing and mbedtls_pk_get_bitlen --- .gitignore | 2 + README.md | 13 +- src/psa_tf_psa.c | 324 ++++++++++++++++++++++++++++++---- test/mbedtls_compat/pk_test.c | 291 ++++++++++++++++++++++++------ wolfpsa/mbedtls/pk.h | 10 +- 5 files changed, 548 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index 473cd44..635a1cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build +/build-*/ +!/build-test/ # gcov coverage artifacts (make cov) *.gcda *.gcno diff --git a/README.md b/README.md index ae31f75..94d55ef 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,15 @@ wolfPSA is a PSA Crypto **engine** that implements the PSA Crypto API on - wolfCrypt as the backend crypto provider. - The default make target creates a static library (`libwolfpsa.a`) built directly from wolfPSA sources plus the required wolfCrypt objects. -- TF-PSA-Crypto's `mbedtls/pk.h` for private keys: `mbedtls_pk_init`, `free`, - `parse_key`, `parse_keyfile` and `can_do_psa`. The context has the key's - DER and type. RSA and EC keys, PEM or DER, traditional or PKCS #8. - Encrypted keys and EC keys with a compressed public key are not supported. +- TF-PSA-Crypto's `mbedtls/pk.h` for private and public keys: + `mbedtls_pk_init`, `free`, `parse_key`, `parse_keyfile`, + `parse_public_key`, `parse_public_keyfile`, `get_bitlen` and `can_do_psa`. + The context has a private key's DER, or a public key's + SubjectPublicKeyInfo DER, with its type and size. Private keys are RSA + and EC, PEM or DER, traditional or PKCS #8. Public keys are RSA, EC, X25519 + and X448, PEM or DER, SubjectPublicKeyInfo or, for RSA, PKCS #1. EC keys + are on the curves the PSA engine has. Encrypted keys and EC keys with a + compressed public key are not supported. ## Repository layout diff --git a/src/psa_tf_psa.c b/src/psa_tf_psa.c index ca2e109..6c1bfbe 100644 --- a/src/psa_tf_psa.c +++ b/src/psa_tf_psa.c @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include "psa_lock.h" #include "psa_tf_psa_internal.h" @@ -78,14 +80,23 @@ void mbedtls_pk_free(mbedtls_pk_context* ctx) if (ctx != NULL) { mbedtls_platform_zeroize(ctx->der, ctx->der_len); XFREE(ctx->der, NULL, DYNAMIC_TYPE_PRIVATE_KEY); + XFREE(ctx->pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY); XMEMSET(ctx, 0, sizeof(*ctx)); } } +size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context* ctx) +{ + return (ctx == NULL) ? 0 : ctx->bits; +} + enum { PK_TYPE_NONE = 0, PK_TYPE_RSA = 1, - PK_TYPE_ECC = 2 + PK_TYPE_ECC = 2, + PK_TYPE_RSA_PUBLIC = 3, + PK_TYPE_ECC_PUBLIC = 4, + PK_TYPE_MONTGOMERY_PUBLIC = 5 }; int wolfPSA_MbedRet(int wcRet, int err) @@ -106,9 +117,49 @@ static int PkDecodeRet(int wcRet, word32 idx, word32 len) return wolfPSA_MbedRet(wcRet, MBEDTLS_ERR_PK_KEY_INVALID_FORMAT); } -/* An RSA or EC private key, traditional or PKCS #8. *type becomes PK_TYPE_*. */ -static int PkCheckKey(const byte* der, word32 len, int* type) +#ifdef HAVE_ECC +extern int wc_psa_get_ecc_curve_id(psa_key_type_t type, size_t bits); + +/* The Weierstrass curves of PSA */ +static const struct { + psa_ecc_family_t family; + size_t bits; +} pkCurves[] = { + { PSA_ECC_FAMILY_SECP_R1, 192 }, + { PSA_ECC_FAMILY_SECP_R1, 224 }, + { PSA_ECC_FAMILY_SECP_R1, 256 }, + { PSA_ECC_FAMILY_SECP_R1, 384 }, + { PSA_ECC_FAMILY_SECP_R1, 521 }, + { PSA_ECC_FAMILY_SECP_K1, 192 }, + { PSA_ECC_FAMILY_SECP_K1, 224 }, + { PSA_ECC_FAMILY_SECP_K1, 256 }, + { PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, + { PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, + { PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 } +}; + +/* The size of the key's curve, if the PSA engine has the curve */ +static int PkEccBits(const ecc_key* ecc, size_t* bits) { + size_t i; + + for (i = 0; i < sizeof(pkCurves) / sizeof(pkCurves[0]); i++) { + if (wc_psa_get_ecc_curve_id( + PSA_KEY_TYPE_ECC_PUBLIC_KEY(pkCurves[i].family), + pkCurves[i].bits) == ecc->dp->id) { + *bits = pkCurves[i].bits; + return 0; + } + } + return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE; +} +#endif /* HAVE_ECC */ + +/* An RSA or EC private key, traditional or PKCS #8 */ +static int PkCheckKey(mbedtls_pk_context* ctx) +{ + const byte* der = ctx->der; + word32 len = (word32)ctx->der_len; int ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; word32 idx; #ifndef NO_RSA @@ -118,7 +169,6 @@ static int PkCheckKey(const byte* der, word32 len, int* type) ecc_key* ecc; #endif /* HAVE_ECC */ - *type = PK_TYPE_NONE; #ifndef NO_RSA rsa = wc_NewRsaKey(NULL, INVALID_DEVID, NULL); if (rsa == NULL) { @@ -128,7 +178,8 @@ static int PkCheckKey(const byte* der, word32 len, int* type) ret = wc_RsaPrivateKeyDecode(der, &idx, rsa, len); ret = PkDecodeRet(ret, idx, len); if (ret == 0) { - *type = PK_TYPE_RSA; + ctx->type = PK_TYPE_RSA; + ctx->bits = (size_t)mp_count_bits(&rsa->n); } wc_DeleteRsaKey(rsa, &rsa); #endif /* !NO_RSA */ @@ -142,7 +193,10 @@ static int PkCheckKey(const byte* der, word32 len, int* type) ret = wc_EccPrivateKeyDecode(der, &idx, ecc, len); ret = PkDecodeRet(ret, idx, len); if (ret == 0) { - *type = PK_TYPE_ECC; + ret = PkEccBits(ecc, &ctx->bits); + } + if (ret == 0) { + ctx->type = PK_TYPE_ECC; } wc_ecc_key_free(ecc); } @@ -153,6 +207,127 @@ static int PkCheckKey(const byte* der, word32 len, int* type) return ret; } +#ifndef NO_RSA +/* Replaces pub, which can be PKCS #1, with the key's SubjectPublicKeyInfo */ +static int PkRsaSetSpki(mbedtls_pk_context* ctx, RsaKey* rsa) +{ + unsigned char* spki; + int len; + int ret; + + len = wc_RsaKeyToPublicDer(rsa, NULL, 0); + if (len < 0) { + return wolfPSA_MbedRet(len, MBEDTLS_ERR_PK_KEY_INVALID_FORMAT); + } + spki = (unsigned char*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_PUBLIC_KEY); + if (spki == NULL) { + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + ret = wc_RsaKeyToPublicDer(rsa, spki, (word32)len); + if (ret != len) { + XFREE(spki, NULL, DYNAMIC_TYPE_PUBLIC_KEY); + return wolfPSA_MbedRet(ret, MBEDTLS_ERR_PK_KEY_INVALID_FORMAT); + } + XFREE(ctx->pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY); + ctx->pub = spki; + ctx->pub_len = (size_t)len; + return 0; +} +#endif /* !NO_RSA */ + +/* A public key, SubjectPublicKeyInfo or, for RSA, also PKCS #1 */ +static int PkCheckPublicKey(mbedtls_pk_context* ctx) +{ + const byte* der = ctx->pub; + word32 len = (word32)ctx->pub_len; + int ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; + word32 idx; +#ifndef NO_RSA + RsaKey* rsa; +#endif /* !NO_RSA */ +#ifdef HAVE_ECC + ecc_key* ecc; +#endif /* HAVE_ECC */ +#ifdef HAVE_CURVE25519 + curve25519_key* x25519; +#endif /* HAVE_CURVE25519 */ +#ifdef HAVE_CURVE448 + curve448_key* x448; +#endif /* HAVE_CURVE448 */ + +#ifndef NO_RSA + rsa = wc_NewRsaKey(NULL, INVALID_DEVID, NULL); + if (rsa == NULL) { + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + idx = 0; + ret = wc_RsaPublicKeyDecode(der, &idx, rsa, len); + ret = PkDecodeRet(ret, idx, len); + if (ret == 0) { + ret = PkRsaSetSpki(ctx, rsa); + } + if (ret == 0) { + ctx->type = PK_TYPE_RSA_PUBLIC; + ctx->bits = (size_t)mp_count_bits(&rsa->n); + } + wc_DeleteRsaKey(rsa, &rsa); +#endif /* !NO_RSA */ +#ifdef HAVE_ECC + if (ret == MBEDTLS_ERR_PK_KEY_INVALID_FORMAT) { + ecc = wc_ecc_key_new(NULL); + if (ecc == NULL) { + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + idx = 0; + ret = wc_EccPublicKeyDecode(der, &idx, ecc, len); + ret = PkDecodeRet(ret, idx, len); + if (ret == 0) { + ret = PkEccBits(ecc, &ctx->bits); + } + if (ret == 0) { + ctx->type = PK_TYPE_ECC_PUBLIC; + } + wc_ecc_key_free(ecc); + } +#endif /* HAVE_ECC */ +#ifdef HAVE_CURVE25519 + if (ret == MBEDTLS_ERR_PK_KEY_INVALID_FORMAT) { + x25519 = wc_curve25519_new(NULL, INVALID_DEVID, NULL); + if (x25519 == NULL) { + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + idx = 0; + ret = wc_Curve25519PublicKeyDecode(der, &idx, x25519, len); + ret = PkDecodeRet(ret, idx, len); + if (ret == 0) { + ctx->type = PK_TYPE_MONTGOMERY_PUBLIC; + ctx->bits = 255; + } + wc_curve25519_delete(x25519, &x25519); + } +#endif /* HAVE_CURVE25519 */ +#ifdef HAVE_CURVE448 + if (ret == MBEDTLS_ERR_PK_KEY_INVALID_FORMAT) { + x448 = wc_curve448_new(NULL, INVALID_DEVID, NULL); + if (x448 == NULL) { + return PSA_ERROR_INSUFFICIENT_MEMORY; + } + idx = 0; + ret = wc_Curve448PublicKeyDecode(der, &idx, x448, len); + ret = PkDecodeRet(ret, idx, len); + if (ret == 0) { + ctx->type = PK_TYPE_MONTGOMERY_PUBLIC; + ctx->bits = 448; + } + wc_curve448_delete(x448, &x448); + } +#endif /* HAVE_CURVE448 */ + (void)der; + (void)len; + (void)idx; + return ret; +} + /* A PEM key encrypted with a password, PKCS #8 or traditional */ static int PkIsEncrypted(const unsigned char* key, size_t keylen) { @@ -175,54 +350,101 @@ static int PkIsEncrypted(const unsigned char* key, size_t keylen) return 0; } -/* PEM or DER. Encrypted keys are not supported, so pwd is not used. */ -int mbedtls_pk_parse_key(mbedtls_pk_context* ctx, const unsigned char* key, - size_t keylen, const unsigned char* pwd, size_t pwdlen) +/* An empty context and a key */ +static int PkParseArgs(const mbedtls_pk_context* ctx, const unsigned char* key, + size_t keylen) { - int ret; - - (void)pwd; - (void)pwdlen; - - if (ctx == NULL || ctx->der != NULL || key == NULL || + if (ctx == NULL || ctx->type != PK_TYPE_NONE || key == NULL || keylen > (size_t)INT_MAX) { return PSA_ERROR_INVALID_ARGUMENT; } if (keylen == 0) { return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; } - if (PkIsEncrypted(key, keylen)) { - return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; - } + return 0; +} + +/* PEM with a header of the wolfCrypt type, or DER, into a new buffer */ +static int PkPemToDer(const unsigned char* key, size_t keylen, int type, + int dynType, unsigned char** out, size_t* outLen) +{ + DerBuffer* der = NULL; + int ret; + if (!wolfPSA_CryptoReady()) { return PSA_ERROR_BAD_STATE; } - - /* Big enough for the DER of a PEM key */ - ctx->der = (unsigned char*)XMALLOC(keylen, NULL, DYNAMIC_TYPE_PRIVATE_KEY); - if (ctx->der == NULL) { - return PSA_ERROR_INSUFFICIENT_MEMORY; - } #ifdef WOLFSSL_PEM_TO_DER - ret = wc_KeyPemToDer(key, (int)keylen, ctx->der, (int)keylen, NULL); + ret = wc_PemToDer(key, (long)keylen, type, &der, NULL, NULL, NULL); #else + (void)type; ret = WC_NO_ERR_TRACE(ASN_NO_PEM_HEADER); #endif /* WOLFSSL_PEM_TO_DER */ - if (ret > 0) { - ctx->der_len = (size_t)ret; - ret = 0; + if (ret == 0) { + key = der->buffer; + keylen = der->length; } else if (ret == WC_NO_ERR_TRACE(ASN_NO_PEM_HEADER)) { - XMEMCPY(ctx->der, key, keylen); - ctx->der_len = keylen; ret = 0; } else { ret = wolfPSA_MbedRet(ret, MBEDTLS_ERR_PK_KEY_INVALID_FORMAT); } + if (ret == 0) { + *out = (unsigned char*)XMALLOC(keylen, NULL, dynType); + if (*out == NULL) { + ret = PSA_ERROR_INSUFFICIENT_MEMORY; + } + } + if (ret == 0) { + XMEMCPY(*out, key, keylen); + *outLen = keylen; + } + wc_FreeDer(&der); + return ret; +} + +/* PEM or DER. Encrypted keys are not supported, so pwd is not used. */ +int mbedtls_pk_parse_key(mbedtls_pk_context* ctx, const unsigned char* key, + size_t keylen, const unsigned char* pwd, size_t pwdlen) +{ + int ret; + + (void)pwd; + (void)pwdlen; + + ret = PkParseArgs(ctx, key, keylen); + if (ret != 0) { + return ret; + } + if (PkIsEncrypted(key, keylen)) { + return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + } + ret = PkPemToDer(key, keylen, PRIVATEKEY_TYPE, DYNAMIC_TYPE_PRIVATE_KEY, + &ctx->der, &ctx->der_len); + if (ret == 0) { + ret = PkCheckKey(ctx); + } + if (ret != 0) { + mbedtls_pk_free(ctx); + } + return ret; +} +/* PEM or DER. RSA keys can also be PKCS #1. */ +int mbedtls_pk_parse_public_key(mbedtls_pk_context* ctx, + const unsigned char* key, size_t keylen) +{ + int ret; + + ret = PkParseArgs(ctx, key, keylen); + if (ret != 0) { + return ret; + } + ret = PkPemToDer(key, keylen, PUBLICKEY_TYPE, DYNAMIC_TYPE_PUBLIC_KEY, + &ctx->pub, &ctx->pub_len); if (ret == 0) { - ret = PkCheckKey(ctx->der, (word32)ctx->der_len, &ctx->type); + ret = PkCheckPublicKey(ctx); } if (ret != 0) { mbedtls_pk_free(ctx); @@ -230,7 +452,7 @@ int mbedtls_pk_parse_key(mbedtls_pk_context* ctx, const unsigned char* key, return ret; } -/* The policy TF-PSA-Crypto gives the PSA key of a parsed private key */ +/* The usage and algorithm pairs mbedtls_pk_can_do_psa() allows per key type */ static const struct { int type; psa_key_usage_t usage; @@ -246,13 +468,29 @@ static const struct { PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH) }, { PK_TYPE_ECC, PSA_KEY_USAGE_SIGN_HASH, MBEDTLS_PK_ALG_ECDSA(PSA_ALG_ANY_HASH) }, - { PK_TYPE_ECC, PSA_KEY_USAGE_DERIVE_PUBLIC, PSA_ALG_ECDH } + { PK_TYPE_ECC, PSA_KEY_USAGE_DERIVE_PUBLIC, PSA_ALG_ECDH }, + { PK_TYPE_RSA_PUBLIC, PSA_KEY_USAGE_VERIFY_HASH, + PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH) }, + { PK_TYPE_RSA_PUBLIC, PSA_KEY_USAGE_VERIFY_HASH, + PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH) }, + { PK_TYPE_RSA_PUBLIC, PSA_KEY_USAGE_VERIFY_HASH, + PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH) }, + { PK_TYPE_RSA_PUBLIC, PSA_KEY_USAGE_ENCRYPT, PSA_ALG_RSA_PKCS1V15_CRYPT }, + { PK_TYPE_RSA_PUBLIC, PSA_KEY_USAGE_ENCRYPT, + PSA_ALG_RSA_OAEP(PSA_ALG_ANY_HASH) }, + { PK_TYPE_ECC_PUBLIC, PSA_KEY_USAGE_VERIFY_HASH, + PSA_ALG_ECDSA(PSA_ALG_ANY_HASH) }, + { PK_TYPE_ECC_PUBLIC, PSA_KEY_USAGE_VERIFY_HASH, + PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH) }, + { PK_TYPE_ECC_PUBLIC, PSA_KEY_USAGE_DERIVE_PUBLIC, PSA_ALG_ECDH }, + { PK_TYPE_MONTGOMERY_PUBLIC, PSA_KEY_USAGE_DERIVE_PUBLIC, PSA_ALG_ECDH } }; /* alg is policyAlg, or policyAlg with its PSA_ALG_ANY_HASH set to a hash */ static int PkAlgMatches(psa_algorithm_t alg, psa_algorithm_t policyAlg) { - return alg == policyAlg || (PSA_ALG_IS_SIGN_HASH(alg) && + return alg == policyAlg || + (PSA_ALG_GET_HASH(policyAlg) == PSA_ALG_ANY_HASH && (alg & ~PSA_ALG_HASH_MASK) == (policyAlg & ~PSA_ALG_HASH_MASK)); } @@ -264,6 +502,10 @@ int mbedtls_pk_can_do_psa(const mbedtls_pk_context* pk, psa_algorithm_t alg, if (pk == NULL) { return 0; } + /* A public key can also do ECDH with a KDF */ + if (pk->pub != NULL && PSA_ALG_IS_KEY_AGREEMENT(alg)) { + alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); + } for (i = 0; i < sizeof(pkPolicy) / sizeof(pkPolicy[0]); i++) { if (pkPolicy[i].type == pk->type && pkPolicy[i].usage == usage && PkAlgMatches(alg, pkPolicy[i].alg)) { @@ -308,6 +550,20 @@ int mbedtls_pk_parse_keyfile(mbedtls_pk_context* ctx, const char* path, wolfPSA_MbedFreeFile(buf, len); return ret; } + +int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context* ctx, const char* path) +{ + unsigned char* buf; + size_t len; + int ret; + + ret = wolfPSA_MbedLoadFile(path, &buf, &len); + if (ret == 0) { + ret = mbedtls_pk_parse_public_key(ctx, buf, len); + } + wolfPSA_MbedFreeFile(buf, len); + return ret; +} #endif /* MBEDTLS_FS_IO */ #if defined(MBEDTLS_THREADING_PTHREAD) diff --git a/test/mbedtls_compat/pk_test.c b/test/mbedtls_compat/pk_test.c index d5ef1ad..cf02018 100644 --- a/test/mbedtls_compat/pk_test.c +++ b/test/mbedtls_compat/pk_test.c @@ -19,94 +19,279 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -/* Private key parsing through the TF-PSA-Crypto 1.x API. The cases of - * mbedtls_pk_parse_keyfile come from TF-PSA-Crypto's - * tests/suites/test_suite_pkparse.data, those without a password. Run from - * framework/data_files in an Mbed TLS checkout. */ +/* Key parsing through the TF-PSA-Crypto 1.x API. The cases of + * mbedtls_pk_parse_keyfile and mbedtls_pk_parse_public_keyfile come from + * TF-PSA-Crypto's tests/suites/test_suite_pkparse.data, those without a + * password. Run from framework/data_files in an Mbed TLS checkout. */ #include +#include #include #include #define INVALID MBEDTLS_ERR_PK_KEY_INVALID_FORMAT #define ENCRYPTED MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE +#define NO_CURVE MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE +#define EC_KEY "ec_256_prv.pem" +#define RSA_PUB "rsa_pkcs8_2048_public.der" +#define EC_PUB "ec_256_pub.der" +#define X25519_PUB "ec_x25519_pub.der" +#define ECDH_HKDF \ + PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, PSA_ALG_HKDF(PSA_ALG_SHA_256)) -static const struct { +typedef struct { const char* file; int ret; -} cases[] = { - { "ec_256_prv.pem", 0 }, - { "ec_256_prv.der", 0 }, - { "ec_256_prv.pk8.pem", 0 }, - { "ec_256_prv.pk8.der", 0 }, - { "ec_384_prv.pem", 0 }, - { "ec_521_prv.pem", 0 }, - { "ec_prv.pk8nopub.pem", 0 }, - { "ec_prv.pk8nopubparam.pem", 0 }, - { "ec_prv.pk8param.pem", 0 }, - { "format_gen.key", 0 }, - { "rsa_pkcs1_768_clear.der", 0 }, - { "rsa_pkcs1_776_clear.der", 0 }, + size_t bits; +} ParseCase; + +static const ParseCase keyCases[] = { + { "ec_256_prv.pem", 0, 256 }, + { "ec_256_prv.der", 0, 256 }, + { "ec_256_prv.pk8.pem", 0, 256 }, + { "ec_256_prv.pk8.der", 0, 256 }, + { "ec_384_prv.pem", 0, 384 }, + { "ec_521_prv.pem", 0, 521 }, + { "ec_prv.pk8nopub.pem", 0, 256 }, + { "ec_prv.pk8nopubparam.pem", 0, 256 }, + { "ec_prv.pk8param.pem", 0, 256 }, + { "format_gen.key", 0, 1024 }, + { "rsa_pkcs1_768_clear.der", 0, 768 }, + { "rsa_pkcs1_776_clear.der", 0, 776 }, /* Not a multiple of 8 bits. Mbed TLS: INVALID */ - { "rsa_pkcs1_769_clear.der", 0 }, + { "rsa_pkcs1_769_clear.der", 0, 769 }, /* Compressed public key. Mbed TLS: 0 */ - { "ec_256_prv.comp.pem", INVALID }, + { "ec_256_prv.comp.pem", INVALID, 0 }, /* Encrypted keys are not supported. Mbed TLS: PASSWORD_REQUIRED */ - { "rsa_pkcs8_pbes2_pbkdf2_1024_aes128cbc.pem", ENCRYPTED }, + { "rsa_pkcs8_pbes2_pbkdf2_1024_aes128cbc.pem", ENCRYPTED, 0 }, + /* Not in Mbed TLS's cases */ + { "ec_224_prv.pem", NO_CURVE, 0 }, + { "server2.key", 0, 2048 }, + { "server5.crt", INVALID, 0 }, + { "missing.key", MBEDTLS_ERR_PK_FILE_IO_ERROR, 0 } +}; + +static const ParseCase publicCases[] = { + { "rsa_pkcs8_2048_public.pem", 0, 2048 }, + { "rsa_pkcs8_2048_public.der", 0, 2048 }, + { "rsa_pkcs1_2048_public.pem", 0, 2048 }, + { "rsa_pkcs1_2048_public.der", 0, 2048 }, + { "ec_256_pub.pem", 0, 256 }, + { "ec_256_pub.der", 0, 256 }, + { "ec_384_pub.pem", 0, 384 }, + { "ec_521_pub.pem", 0, 521 }, + { "ec_x25519_pub.der", 0, 255 }, + { "ec_x448_pub.der", 0, 448 }, + { "ec_x25519_pub.pem", 0, 255 }, + { "ec_x448_pub.pem", 0, 448 }, + /* Compressed public keys. Mbed TLS: 0 */ + { "ec_256_pub.comp.pem", INVALID, 0 }, + { "ec_384_pub.comp.pem", INVALID, 0 }, + { "ec_521_pub.comp.pem", INVALID, 0 }, + /* Brainpool curves are not in the build. Mbed TLS: 0 */ + { "ec_bp256_pub.pem", INVALID, 0 }, + { "ec_bp256_pub.comp.pem", INVALID, 0 }, + { "ec_bp384_pub.pem", INVALID, 0 }, + { "ec_bp384_pub.comp.pem", INVALID, 0 }, + { "ec_bp512_pub.pem", INVALID, 0 }, + { "ec_bp512_pub.comp.pem", INVALID, 0 }, /* Not in Mbed TLS's cases */ - { "server2.key", 0 }, - { "server5.crt", INVALID }, - { "missing.key", MBEDTLS_ERR_PK_FILE_IO_ERROR } + { "rsa4096_pub.pem", 0, 4096 }, + { "ec_224_pub.pem", NO_CURVE, 0 }, + { "ec_256_prv.pem", INVALID, 0 }, + { "server5.crt", INVALID, 0 }, + { "missing.pem", MBEDTLS_ERR_PK_FILE_IO_ERROR, 0 } +}; + +/* What TF-PSA-Crypto allows a public key: verify, encrypt and ECDH */ +static const struct { + const char* file; + psa_algorithm_t alg; + psa_key_usage_t usage; + int can; +} canDoCases[] = { + { RSA_PUB, PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256), + PSA_KEY_USAGE_VERIFY_HASH, 1 }, + { RSA_PUB, PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_SHA_384), + PSA_KEY_USAGE_VERIFY_HASH, 1 }, + { RSA_PUB, PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256), PSA_KEY_USAGE_ENCRYPT, 1 }, + { RSA_PUB, PSA_ALG_RSA_PKCS1V15_CRYPT, PSA_KEY_USAGE_ENCRYPT, 1 }, + { RSA_PUB, PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256), + PSA_KEY_USAGE_SIGN_HASH, 0 }, + { RSA_PUB, PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256), PSA_KEY_USAGE_DECRYPT, 0 }, + { RSA_PUB, PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256), + PSA_KEY_USAGE_VERIFY_HASH, 0 }, + { EC_PUB, PSA_ALG_ECDSA(PSA_ALG_SHA_256), PSA_KEY_USAGE_VERIFY_HASH, 1 }, + { EC_PUB, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_384), + PSA_KEY_USAGE_VERIFY_HASH, 1 }, + { EC_PUB, PSA_ALG_ECDH, PSA_KEY_USAGE_DERIVE_PUBLIC, 1 }, + { EC_PUB, ECDH_HKDF, PSA_KEY_USAGE_DERIVE_PUBLIC, 1 }, + { EC_PUB, PSA_ALG_ECDSA(PSA_ALG_SHA_256), PSA_KEY_USAGE_SIGN_HASH, 0 }, + { EC_PUB, PSA_ALG_ECDH, PSA_KEY_USAGE_DERIVE, 0 }, + { X25519_PUB, PSA_ALG_ECDH, PSA_KEY_USAGE_DERIVE_PUBLIC, 1 }, + { X25519_PUB, PSA_ALG_ECDSA(PSA_ALG_SHA_256), + PSA_KEY_USAGE_VERIFY_HASH, 0 } }; -/* An empty buffer, and a second key into one context, as in Mbed TLS */ -static int BadInput(void) +typedef int (*ParseFile)(mbedtls_pk_context* pk, const char* file); + +static int fails; + +static void Check(int ok, const char* name) { - static const unsigned char der[] = { 0x30, 0x00 }; - mbedtls_pk_context pk; - int ok; + printf("%s: %s\n", ok ? "PASS" : "FAIL", name); + if (!ok) { + fails++; + } +} - mbedtls_pk_init(&pk); - ok = mbedtls_pk_parse_key(&pk, der, 0, NULL, 0) == INVALID && - mbedtls_pk_parse_keyfile(&pk, "server2.key", NULL) == 0 && - mbedtls_pk_parse_keyfile(&pk, "server5.key", NULL) == - PSA_ERROR_INVALID_ARGUMENT; - mbedtls_pk_free(&pk); - return ok; +static int ParseKeyFile(mbedtls_pk_context* pk, const char* file) +{ + return mbedtls_pk_parse_keyfile(pk, file, NULL); } -int main(void) +static void Parse(const ParseCase* cases, size_t count, ParseFile parse, + const char* label) { mbedtls_pk_context pk; + size_t bits; size_t i; - int fails = 0; int ret; - if (psa_crypto_init() != PSA_SUCCESS) { - printf("FAIL: psa_crypto_init\n"); - return 1; - } - for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + for (i = 0; i < count; i++) { mbedtls_pk_init(&pk); - ret = mbedtls_pk_parse_keyfile(&pk, cases[i].file, NULL); - if (ret == cases[i].ret) { - printf("PASS: %s\n", cases[i].file); + ret = parse(&pk, cases[i].file); + bits = mbedtls_pk_get_bitlen(&pk); + if (ret == cases[i].ret && bits == cases[i].bits) { + printf("PASS: %s %s\n", label, cases[i].file); } else { - printf("FAIL: %s: returned %d, expected %d\n", cases[i].file, - ret, cases[i].ret); + printf("FAIL: %s %s: returned %d, %u bits, expected %d, %u bits\n", + label, cases[i].file, ret, (unsigned)bits, cases[i].ret, + (unsigned)cases[i].bits); fails++; } mbedtls_pk_free(&pk); } - if (BadInput()) { - printf("PASS: bad input\n"); +} + +static void CanDo(void) +{ + mbedtls_pk_context pk; + char name[32]; + size_t i; + + for (i = 0; i < sizeof(canDoCases) / sizeof(canDoCases[0]); i++) { + (void)snprintf(name, sizeof(name), "can_do_psa case %u", + (unsigned)i); + mbedtls_pk_init(&pk); + Check(mbedtls_pk_parse_public_keyfile(&pk, canDoCases[i].file) == 0 && + mbedtls_pk_can_do_psa(&pk, canDoCases[i].alg, + canDoCases[i].usage) == canDoCases[i].can, name); + mbedtls_pk_free(&pk); } - else { - printf("FAIL: bad input\n"); - fails++; + mbedtls_pk_init(&pk); + Check(mbedtls_pk_parse_keyfile(&pk, EC_KEY, NULL) == 0 && + mbedtls_pk_can_do_psa(&pk, ECDH_HKDF, + PSA_KEY_USAGE_DERIVE_PUBLIC) == 0, + "can_do_psa private key, ECDH with a KDF"); + mbedtls_pk_free(&pk); +} + +/* The file in buf, with room for a byte after it */ +static size_t ReadFile(const char* file, unsigned char* buf, size_t size) +{ + FILE* f; + size_t len = 0; + + f = fopen(file, "rb"); + if (f != NULL) { + len = fread(buf, 1, size - 1, f); + (void)fclose(f); + } + return len; +} + +/* The context has SubjectPublicKeyInfo DER, also from PKCS #1 */ +static void PublicDer(void) +{ + unsigned char spki[512]; + unsigned char ec[128]; + size_t spkiLen; + size_t ecLen; + mbedtls_pk_context pk; + + spkiLen = ReadFile(RSA_PUB, spki, sizeof(spki)); + ecLen = ReadFile(EC_PUB, ec, sizeof(ec)); + + mbedtls_pk_init(&pk); + Check(spkiLen > 0 && mbedtls_pk_parse_public_keyfile(&pk, + "rsa_pkcs1_2048_public.der") == 0 && pk.pub_len == spkiLen && + memcmp(pk.pub, spki, spkiLen) == 0, "PKCS #1 public key DER"); + mbedtls_pk_free(&pk); + Check(ecLen > 0 && mbedtls_pk_parse_public_key(&pk, ec, ecLen) == 0 && + pk.pub_len == ecLen && memcmp(pk.pub, ec, ecLen) == 0, + "EC public key DER"); + mbedtls_pk_free(&pk); + + /* Mbed TLS ignores data after SubjectPublicKeyInfo */ + ec[ecLen] = 0; + Check(ecLen > 0 && mbedtls_pk_parse_public_key(&pk, ec, ecLen + 1) == + INVALID, "public key DER and a NUL"); + mbedtls_pk_free(&pk); + + /* Mbed TLS: INVALID_PUBKEY */ + if (ecLen > 0) { + ec[ecLen - 1] ^= 1; + } + Check(ecLen > 0 && mbedtls_pk_parse_public_key(&pk, ec, ecLen) == + INVALID, "point not on the curve"); + mbedtls_pk_free(&pk); +} + +/* Empty buffers, NULL and empty contexts, and a second key into one context */ +static void BadInput(void) +{ + static const unsigned char der[] = { 0x30, 0x00 }; + mbedtls_pk_context pk; + + mbedtls_pk_init(&pk); + Check(mbedtls_pk_parse_key(&pk, der, 0, NULL, 0) == INVALID, + "parse_key empty"); + Check(mbedtls_pk_parse_public_key(&pk, der, 0) == INVALID, + "parse_public_key empty"); + Check(mbedtls_pk_parse_public_key(NULL, der, sizeof(der)) == + PSA_ERROR_INVALID_ARGUMENT, "parse_public_key NULL"); + Check(mbedtls_pk_get_bitlen(&pk) == 0 && mbedtls_pk_get_bitlen(NULL) == 0, + "get_bitlen without a key"); + Check(mbedtls_pk_parse_keyfile(&pk, "server2.key", NULL) == 0 && + mbedtls_pk_parse_keyfile(&pk, "server5.key", NULL) == + PSA_ERROR_INVALID_ARGUMENT && + mbedtls_pk_parse_public_keyfile(&pk, EC_PUB) == + PSA_ERROR_INVALID_ARGUMENT, "a second key after a private key"); + mbedtls_pk_free(&pk); + Check(mbedtls_pk_parse_public_keyfile(&pk, EC_PUB) == 0 && + mbedtls_pk_parse_public_keyfile(&pk, RSA_PUB) == + PSA_ERROR_INVALID_ARGUMENT && + mbedtls_pk_parse_keyfile(&pk, "server2.key", NULL) == + PSA_ERROR_INVALID_ARGUMENT, "a second key after a public key"); + mbedtls_pk_free(&pk); +} + +int main(void) +{ + if (psa_crypto_init() != PSA_SUCCESS) { + printf("FAIL: psa_crypto_init\n"); + return 1; } + Parse(keyCases, sizeof(keyCases) / sizeof(keyCases[0]), ParseKeyFile, + "parse_keyfile"); + Parse(publicCases, sizeof(publicCases) / sizeof(publicCases[0]), + mbedtls_pk_parse_public_keyfile, "parse_public_keyfile"); + CanDo(); + PublicDer(); + BadInput(); mbedtls_psa_crypto_free(); return (fails == 0) ? 0 : 1; } diff --git a/wolfpsa/mbedtls/pk.h b/wolfpsa/mbedtls/pk.h index f169024..8fc68b3 100644 --- a/wolfpsa/mbedtls/pk.h +++ b/wolfpsa/mbedtls/pk.h @@ -51,23 +51,31 @@ #define MBEDTLS_PK_ALG_ECDSA(hash_alg) PSA_ALG_ECDSA(hash_alg) #endif /* PSA_WANT_ALG_DETERMINISTIC_ECDSA */ -/* An RSA or EC private key, DER */ +/* der has a private key's DER, or pub a public key's SubjectPublicKeyInfo */ typedef struct mbedtls_pk_context { unsigned char* der; size_t der_len; + unsigned char* pub; + size_t pub_len; + size_t bits; int type; } mbedtls_pk_context; /* The supported subset of MBEDTLS_PK_C and MBEDTLS_PK_PARSE_C */ void mbedtls_pk_init(mbedtls_pk_context* ctx); void mbedtls_pk_free(mbedtls_pk_context* ctx); +size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context* ctx); int mbedtls_pk_parse_key(mbedtls_pk_context* ctx, const unsigned char* key, size_t keylen, const unsigned char* pwd, size_t pwdlen); +int mbedtls_pk_parse_public_key(mbedtls_pk_context* ctx, + const unsigned char* key, size_t keylen); int mbedtls_pk_can_do_psa(const mbedtls_pk_context* pk, psa_algorithm_t alg, psa_key_usage_t usage); #if defined(MBEDTLS_FS_IO) int mbedtls_pk_parse_keyfile(mbedtls_pk_context* ctx, const char* path, const char* password); +int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context* ctx, + const char* path); #endif /* MBEDTLS_FS_IO */ #ifdef __cplusplus