Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/spdm_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ int wolfSPDM_ExportEphemeralPubKey(WOLFSPDM_CTX* ctx,

/* ----- ECDH Shared Secret Computation ----- */

/* Range and on-curve check; P-384 has cofactor 1, so no order check */
static int wolfSPDM_EccCheckPub(ecc_key* key)
{
#if defined(USE_ECC_B_PARAM) || \
(defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX >= 0x05009001)
return wc_ecc_point_is_on_curve(&key->pubkey, key->idx);
#else
/* Before v5.9.1 the point check needs USE_ECC_B_PARAM */
return wc_ecc_check_key(key);
#endif
}

int wolfSPDM_ComputeSharedSecret(WOLFSPDM_CTX* ctx,
const byte* peerPubKeyX, const byte* peerPubKeyY)
{
Expand Down Expand Up @@ -192,7 +204,7 @@ int wolfSPDM_ComputeSharedSecret(WOLFSPDM_CTX* ctx,
}
/* Validate peer's public key is on the curve (prevents invalid-curve attacks) */
if (rc == 0) {
rc = wc_ecc_check_key(&peerKey);
rc = wolfSPDM_EccCheckPub(&peerKey);
if (rc != 0) {
wolfSPDM_DebugPrint(ctx, "Peer public key invalid (not on curve): %d\n", rc);
}
Expand Down Expand Up @@ -450,7 +462,7 @@ int wolfSPDM_VerifySignature(WOLFSPDM_CTX* ctx, const byte* hash, word32 hashSz,
}
}
if (rc == 0) {
rc = wc_ecc_check_key(&verifyKey);
rc = wolfSPDM_EccCheckPub(&verifyKey);
if (rc != 0) {
wolfSPDM_DebugPrint(ctx, "Responder pub key invalid (not on curve): %d\n", rc);
}
Expand Down
3 changes: 2 additions & 1 deletion src/spdm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct WOLFSPDM_CTX {
word16 kexAdvKem; /* ML-KEM sets offered at SPDM 1.4 */
byte kexAdvDhe; /* offer ECDHE P-384 */
#endif
byte certChain[WOLFSPDM_MAX_CERT_CHAIN];
byte certChain[WOLFSPDM_MAX_CERT_CHAIN + 8]; /* + one response header */
byte trustedCA[WOLFSPDM_MAX_TRUSTED_CA];
#endif
#ifndef WOLFSPDM_NO_MEAS
Expand Down Expand Up @@ -609,6 +609,7 @@ WOLFSPDM_TEST_API int wolfSPDM_ParseDigests(WOLFSPDM_CTX* ctx,
WOLFSPDM_TEST_API int wolfSPDM_ParseCertificate(WOLFSPDM_CTX* ctx,
const byte* buf, word32 bufSz, word16* portionLen, word16* remainderLen);
WOLFSPDM_LOCAL int wolfSPDM_ConnectStandard(WOLFSPDM_CTX* ctx);
WOLFSPDM_LOCAL int wolfSPDM_KeyExchangeValidated(WOLFSPDM_CTX* ctx);
#endif

#if !defined(WOLFSPDM_NO_MEAS) || !defined(WOLFSPDM_NO_CHALLENGE)
Expand Down
20 changes: 18 additions & 2 deletions src/spdm_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int wolfSPDM_GetVersion(WOLFSPDM_CTX* ctx)
wolfSPDM_ParseVersion, txBuf, sizeof(txBuf), rxBuf, sizeof(rxBuf));
}

int wolfSPDM_KeyExchange(WOLFSPDM_CTX* ctx)
static int wolfSPDM_KeyExchangeEx(WOLFSPDM_CTX* ctx, int revalidate)
{
byte txBuf[WOLFSPDM_KEY_EX_TX_SZ];
byte rxBuf[WOLFSPDM_KEY_EX_RX_SZ];
Expand All @@ -92,9 +92,12 @@ int wolfSPDM_KeyExchange(WOLFSPDM_CTX* ctx)
else {
rc = WOLFSPDM_SUCCESS;
#ifndef WOLFSPDM_NO_CERT
if (!wolfSPDM_IsTcgMode(ctx) && ctx->flags.rspKeyFromCert) {
if (revalidate && !wolfSPDM_IsTcgMode(ctx) &&
ctx->flags.rspKeyFromCert) {
rc = wolfSPDM_ValidateCertChain(ctx);
}
#else
(void)revalidate;
#endif
}
if (rc == WOLFSPDM_SUCCESS) {
Expand Down Expand Up @@ -123,6 +126,19 @@ int wolfSPDM_KeyExchange(WOLFSPDM_CTX* ctx)
return rc;
}

int wolfSPDM_KeyExchange(WOLFSPDM_CTX* ctx)
{
return wolfSPDM_KeyExchangeEx(ctx, 1);
}

#ifndef WOLFSPDM_NO_CERT
/* For a caller that ran ValidateCertChain immediately before */
int wolfSPDM_KeyExchangeValidated(WOLFSPDM_CTX* ctx)
{
return wolfSPDM_KeyExchangeEx(ctx, 0);
}
#endif

/* FINISH must be sent encrypted (HANDSHAKE_IN_THE_CLEAR not negotiated) */
static int wolfSPDM_FinishXfer(WOLFSPDM_CTX* ctx, const byte* finishBuf,
word32 finishSz, byte* decBuf, word32* decSz)
Expand Down
55 changes: 41 additions & 14 deletions src/spdm_standard.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
#include <wolfssl/wolfcrypt/asn.h>

/* Largest certificate portion requested per GET_CERTIFICATE; the response
* fits our DataTransferSize so certificates are never chunked */
#if WOLFSPDM_DATA_TRANSFER_SIZE < 1024 + 8
* fills our DataTransferSize so certificates are never chunked */
#ifndef WOLFSPDM_CERT_PORTION_SZ
#define WOLFSPDM_CERT_PORTION_SZ (WOLFSPDM_DATA_TRANSFER_SIZE - 8)
#else
#define WOLFSPDM_CERT_PORTION_SZ 1024
#endif

/* ----- VCA: GET_CAPABILITIES / NEGOTIATE_ALGORITHMS ----- */
Expand Down Expand Up @@ -369,16 +367,21 @@ int wolfSPDM_ParseCertificate(WOLFSPDM_CTX* ctx, const byte* buf,
return WOLFSPDM_E_BUFFER_SMALL;
}

XMEMCPY(ctx->certChain + ctx->certChainLen, buf + 8, *portionLen);
/* buf may be the response received in place at the end of the chain */
XMEMMOVE(ctx->certChain + ctx->certChainLen, buf + 8, *portionLen);
ctx->certChainLen += *portionLen;
return WOLFSPDM_SUCCESS;
}

int wolfSPDM_GetCertificate(WOLFSPDM_CTX* ctx, int slotId)
{
byte txBuf[8];
byte rxBuf[8 + WOLFSPDM_CERT_PORTION_SZ];
#ifndef WOLFSPDM_NO_CHALLENGE
byte rspHdr[8];
#endif
byte* rsp;
word32 rxSz;
word32 room;
word16 offset = 0;
word16 portionLen = 0;
word16 remainderLen = 1;
Expand All @@ -405,23 +408,41 @@ int wolfSPDM_GetCertificate(WOLFSPDM_CTX* ctx, int slotId)
}

while (rc == WOLFSPDM_SUCCESS && remainderLen > 0) {
/* Receive in place after the chain so far: no response buffer */
rsp = ctx->certChain + ctx->certChainLen;
room = (word32)sizeof(ctx->certChain) - ctx->certChainLen - 8;
txBuf[0] = ctx->spdmVersion;
txBuf[1] = SPDM_GET_CERTIFICATE;
txBuf[2] = (byte)slotId;
txBuf[3] = 0x00;
SPDM_Set16LE(&txBuf[4], offset);
SPDM_Set16LE(&txBuf[6], reqLen);
SPDM_Set16LE(&txBuf[6], (room < reqLen) ? (word16)room : reqLen);
if (room == 0) {
rc = WOLFSPDM_E_BUFFER_SMALL;
}

rxSz = sizeof(rxBuf);
rc = wolfSPDM_ClearExchange(ctx, txBuf, sizeof(txBuf), rxBuf, &rxSz);
rxSz = room + 8;
if (rc == WOLFSPDM_SUCCESS) {
rc = wolfSPDM_ParseCertificate(ctx, rxBuf, rxSz, &portionLen,
rc = wolfSPDM_ClearExchange(ctx, txBuf, sizeof(txBuf), rsp,
&rxSz);
}
#ifndef WOLFSPDM_NO_CHALLENGE
if (rc == WOLFSPDM_SUCCESS && rxSz >= sizeof(rspHdr)) {
XMEMCPY(rspHdr, rsp, sizeof(rspHdr));
}
#endif
if (rc == WOLFSPDM_SUCCESS) {
rc = wolfSPDM_ParseCertificate(ctx, rsp, rxSz, &portionLen,
&remainderLen);
}
#ifndef WOLFSPDM_NO_CHALLENGE
/* Same bytes as the response: header, then the portion now at rsp */
if (rc == WOLFSPDM_SUCCESS) {
rc = wolfSPDM_M1Add(ctx, txBuf, sizeof(txBuf), rspHdr,
sizeof(rspHdr));
}
if (rc == WOLFSPDM_SUCCESS) {
rc = wolfSPDM_M1Add(ctx, txBuf, sizeof(txBuf), rxBuf,
8u + portionLen);
rc = wolfSPDM_M1Add(ctx, rsp, portionLen, rspHdr, 0);
}
#endif
/* Every non-final portion must make progress */
Expand Down Expand Up @@ -725,7 +746,12 @@ int wolfSPDM_ValidateCertChain(WOLFSPDM_CTX* ctx)
if (certSz == 0) {
rc = WOLFSPDM_E_CERT_PARSE;
}
if (rc == WOLFSPDM_SUCCESS && haveKey) {
/* The configured root is the anchor; its own signature adds nothing */
if (rc == WOLFSPDM_SUCCESS && haveKey &&
!(anchored && pos == WOLFSPDM_CERT_CHAIN_HDR_SZ &&
certSz == ctx->trustedCASz &&
XMEMCMP(ctx->certChain + pos, ctx->trustedCA,
certSz) == 0)) {
rc = isCA ? wolfSPDM_CertSignedBy(ctx->certChain + pos, certSz,
&key) : WOLFSPDM_E_CERT_FAIL;
}
Expand Down Expand Up @@ -853,7 +879,8 @@ int wolfSPDM_ConnectStandard(WOLFSPDM_CTX* ctx)
wolfSPDM_GetCertificate(ctx, slot));
SPDM_CONNECT_STEP(ctx, "Validating certificate chain\n",
wolfSPDM_ValidateCertChain(ctx));
SPDM_CONNECT_STEP(ctx, "KEY_EXCHANGE\n", wolfSPDM_KeyExchange(ctx));
SPDM_CONNECT_STEP(ctx, "KEY_EXCHANGE\n",
wolfSPDM_KeyExchangeValidated(ctx));
SPDM_CONNECT_STEP(ctx, "FINISH\n", wolfSPDM_Finish(ctx));

ctx->state = WOLFSPDM_STATE_CONNECTED;
Expand Down
128 changes: 128 additions & 0 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,14 @@ static int test_sign_verify_roundtrip(void)
sig[10] ^= 0x01;
TEST_ASSERT(wolfSPDM_VerifySignature(ctx, hash, 48, sig, sigSz)
!= WOLFSPDM_SUCCESS, "flipped sig should fail");
sig[10] ^= 0x01;

/* An off-curve responder key never verifies */
pubKey[95] ^= 0x01;
if (wolfSPDM_SetResponderPubKey(ctx, pubKey, 96) == WOLFSPDM_SUCCESS) {
TEST_ASSERT(wolfSPDM_VerifySignature(ctx, hash, 48, sig, sigSz)
!= WOLFSPDM_SUCCESS, "off-curve responder key must fail");
}

wc_ForceZero(privKey, sizeof(privKey));
TEST_CTX_FREE();
Expand Down Expand Up @@ -3873,6 +3881,7 @@ static int test_load_sample_chain(WOLFSPDM_CTX* ctx)
static int test_validate_cert_chain(void)
{
byte leafKey[WOLFSPDM_ECC_POINT_SIZE];
byte anchor[sizeof(test_ca_cert_der)];
TEST_CTX_SETUP_V12();
printf("test_validate_cert_chain...\n");

Expand All @@ -3890,6 +3899,26 @@ static int test_validate_cert_chain(void)
ASSERT_EQ(ctx->flags.rspKeyFromCert, 1, "key marked as cert derived");
XMEMCPY(leafKey, ctx->rspPubKey, sizeof(leafKey));

/* A root copy that differs from the anchor is still verified */
ASSERT_EQ(XMEMCMP(test_rsp_chain_der, test_ca_cert_der,
sizeof(test_ca_cert_der)), 0, "sample chain starts with the root");
ctx->certChain[WOLFSPDM_CERT_CHAIN_HDR_SZ + sizeof(test_ca_cert_der) - 2]
^= 0x01;
ASSERT_EQ(wolfSPDM_ValidateCertChain(ctx), WOLFSPDM_E_CERT_FAIL,
"root differing from the anchor must be verified");

/* The exact configured anchor is trusted without its self-signature */
XMEMCPY(anchor, test_ca_cert_der, sizeof(anchor));
anchor[sizeof(anchor) - 2] ^= 0x01;
ASSERT_SUCCESS(wolfSPDM_SetTrustedCAs(ctx, anchor, sizeof(anchor)));
ASSERT_SUCCESS(wolfSPDM_Sha384Hash(ctx->certChain + 4, anchor,
sizeof(anchor), NULL, 0, NULL, 0));
ASSERT_SUCCESS(wolfSPDM_ValidateCertChain(ctx));
ASSERT_SUCCESS(wolfSPDM_SetTrustedCAs(ctx, test_ca_cert_der,
sizeof(test_ca_cert_der)));
ASSERT_SUCCESS(test_load_sample_chain(ctx));
ASSERT_SUCCESS(wolfSPDM_ValidateCertChain(ctx));

/* A forged leaf signature breaks the chain */
ctx->certChain[ctx->certChainLen - 2] ^= 0x01;
ASSERT_EQ(wolfSPDM_ValidateCertChain(ctx), WOLFSPDM_E_CERT_FAIL,
Expand Down Expand Up @@ -3924,6 +3953,13 @@ static int test_validate_cert_chain(void)

wolfSPDM_SetIO(ctx, dummy_io_cb, NULL);
ASSERT_SUCCESS(wolfSPDM_AllowUntrustedCerts(ctx, 0));

/* Connect validates once, so its KEY_EXCHANGE does not revalidate */
TEST_ASSERT(wolfSPDM_KeyExchangeValidated(ctx) != WOLFSPDM_E_CERT_FAIL,
"validated key exchange must not recheck the chain");
ASSERT_EQ(ctx->flags.hasRspPubKey, 1,
"validated key exchange keeps the validated key");

ASSERT_EQ(wolfSPDM_KeyExchange(ctx), WOLFSPDM_E_CERT_FAIL,
"Certificate-derived key must pass the current trust policy");
ASSERT_EQ(ctx->flags.hasRspPubKey, 0,
Expand All @@ -3945,6 +3981,97 @@ static int test_validate_cert_chain(void)
TEST_CTX_FREE();
TEST_PASS();
}
/* Serves the sample chain in small CERTIFICATE portions and hashes the
* exchanged bytes the way M1 must */
typedef struct {
byte chain[WOLFSPDM_CERT_CHAIN_HDR_SZ + sizeof(test_rsp_chain_der)];
word32 chainSz;
word32 exchanges;
wc_Sha384 m1;
} TEST_CERT_RSP;

static int cert_portion_io_cb(WOLFSPDM_CTX* ctx, const byte* txBuf,
word32 txSz, byte* rxBuf, word32* rxSz, void* userCtx)
{
TEST_CERT_RSP* r = (TEST_CERT_RSP*)userCtx;
word32 offset;
word32 portion;

(void)ctx;
if (txSz != 8 || txBuf[1] != SPDM_GET_CERTIFICATE) {
return -1;
}
offset = SPDM_Get16LE(&txBuf[4]);
portion = SPDM_Get16LE(&txBuf[6]);
if (portion > 300) {
portion = 300;
}
if (portion > r->chainSz - offset) {
portion = r->chainSz - offset;
}
if (*rxSz < 8 + portion) {
return -1;
}
rxBuf[0] = txBuf[0];
rxBuf[1] = SPDM_CERTIFICATE;
rxBuf[2] = txBuf[2];
rxBuf[3] = 0;
SPDM_Set16LE(&rxBuf[4], (word16)portion);
SPDM_Set16LE(&rxBuf[6], (word16)(r->chainSz - offset - portion));
XMEMCPY(&rxBuf[8], r->chain + offset, portion);
*rxSz = 8 + portion;
r->exchanges++;
if (wc_Sha384Update(&r->m1, txBuf, txSz) != 0 ||
wc_Sha384Update(&r->m1, rxBuf, *rxSz) != 0) {
return -1;
}
return 0;
}

static int test_get_certificate_portions(void)
{
static TEST_CERT_RSP r;
#ifndef WOLFSPDM_NO_CHALLENGE
byte want[WOLFSPDM_HASH_SIZE];
byte got[WOLFSPDM_HASH_SIZE];
wc_Sha384 m1;
#endif
TEST_CTX_SETUP_V12();
printf("test_get_certificate_portions...\n");

XMEMSET(&r, 0, sizeof(r));
r.chainSz = (word32)sizeof(r.chain);
SPDM_Set16LE(r.chain, (word16)r.chainSz);
ASSERT_SUCCESS(wolfSPDM_Sha384Hash(r.chain + 4, test_ca_cert_der,
sizeof(test_ca_cert_der), NULL, 0, NULL, 0));
XMEMCPY(r.chain + WOLFSPDM_CERT_CHAIN_HDR_SZ, test_rsp_chain_der,
sizeof(test_rsp_chain_der));
ASSERT_EQ(wc_InitSha384(&r.m1), 0, "init expected M1");
#ifndef WOLFSPDM_NO_CHALLENGE
ASSERT_SUCCESS(wolfSPDM_M1Start(ctx));
#endif

wolfSPDM_SetIO(ctx, cert_portion_io_cb, &r);
ASSERT_SUCCESS(wolfSPDM_GetCertificate(ctx, 0));
TEST_ASSERT(r.exchanges > 2, "chain arrives in several portions");
ASSERT_EQ(ctx->certChainLen, r.chainSz, "whole chain assembled");
ASSERT_EQ(XMEMCMP(ctx->certChain, r.chain, r.chainSz), 0,
"assembled chain matches the responder's");

#ifndef WOLFSPDM_NO_CHALLENGE
/* M1 holds each GET_CERTIFICATE and its full CERTIFICATE response */
ASSERT_EQ(wc_Sha384Final(&r.m1, want), 0, "expected M1");
ASSERT_EQ(wc_Sha384Copy(&ctx->m1Hash, &m1), 0, "copy M1");
ASSERT_EQ(wc_Sha384Final(&m1, got), 0, "final M1");
wc_Sha384Free(&m1);
ASSERT_EQ(XMEMCMP(want, got, sizeof(want)), 0,
"M1 matches the exchanged bytes");
#endif

wc_Sha384Free(&r.m1);
TEST_CTX_FREE();
TEST_PASS();
}
#endif /* !WOLFSPDM_NO_CERT */

#if defined(WOLFSPDM_HAVE_MLDSA) || defined(WOLFSPDM_HAVE_MLKEM)
Expand Down Expand Up @@ -5866,6 +5993,7 @@ int main(void)
test_parse_certificate();
test_mutual_auth_rejected_in_standard_mode();
test_validate_cert_chain();
test_get_certificate_portions();
test_clear_exchange_dts();
#endif
#if defined(WOLFSPDM_HAVE_MLDSA) || defined(WOLFSPDM_HAVE_MLKEM)
Expand Down
Loading