Skip to content

Commit ad7c25b

Browse files
authored
Merge pull request #7823 from cconlon/rsaPssSignPkCallbackNoPrehashTls13
PK callbacks: add build option to give full data to TLS 1.3 RSA-PSS sign callback instead of hash
2 parents 20e2e33 + a918c0e commit ad7c25b

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/tls13.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8761,6 +8761,10 @@ typedef struct Scv13Args {
87618761
byte sigAlgo;
87628762
byte* sigData;
87638763
word16 sigDataSz;
8764+
#ifndef NO_RSA
8765+
byte* toSign; /* not allocated */
8766+
word32 toSignSz;
8767+
#endif
87648768
#ifdef WOLFSSL_DUAL_ALG_CERTS
87658769
byte altSigAlgo;
87668770
word32 altSigLen; /* Only used in the case of both native and alt. */
@@ -9315,7 +9319,17 @@ static int SendTls13CertificateVerify(WOLFSSL* ssl)
93159319
#endif /* HAVE_DILITHIUM */
93169320
#ifndef NO_RSA
93179321
if (ssl->hsType == DYNAMIC_TYPE_RSA) {
9318-
ret = RsaSign(ssl, rsaSigBuf->buffer, (word32)rsaSigBuf->length,
9322+
args->toSign = rsaSigBuf->buffer;
9323+
args->toSignSz = (word32)rsaSigBuf->length;
9324+
#if defined(HAVE_PK_CALLBACKS) && \
9325+
defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH)
9326+
/* Pass full data to sign (args->sigData), not hash of */
9327+
if (ssl->ctx->RsaPssSignCb) {
9328+
args->toSign = args->sigData;
9329+
args->toSignSz = args->sigDataSz;
9330+
}
9331+
#endif
9332+
ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz,
93199333
sigOut, &args->sigLen, args->sigAlgo,
93209334
ssl->options.hashAlgo, (RsaKey*)ssl->hsKey,
93219335
ssl->buffers.key);
@@ -9359,10 +9373,20 @@ static int SendTls13CertificateVerify(WOLFSSL* ssl)
93599373
#endif /* HAVE_ECC */
93609374
#ifndef NO_RSA
93619375
if (ssl->hsAltType == DYNAMIC_TYPE_RSA) {
9362-
ret = RsaSign(ssl, rsaSigBuf->buffer,
9363-
(word32)rsaSigBuf->length, sigOut,
9364-
&args->altSigLen, args->altSigAlgo,
9365-
ssl->options.hashAlgo, (RsaKey*)ssl->hsAltKey,
9376+
args->toSign = rsaSigBuf->buffer;
9377+
args->toSignSz = (word32)rsaSigBuf->length;
9378+
#if defined(HAVE_PK_CALLBACKS) && \
9379+
defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH)
9380+
/* Pass full data to sign (args->altSigData), not hash of */
9381+
if (ssl->ctx->RsaPssSignCb) {
9382+
args->toSign = args->altSigData;
9383+
args->toSignSz = (word32)args->altSigDataSz;
9384+
}
9385+
#endif
9386+
ret = RsaSign(ssl, (const byte*)args->toSign,
9387+
args->toSignSz, sigOut, &args->altSigLen,
9388+
args->altSigAlgo, ssl->options.hashAlgo,
9389+
(RsaKey*)ssl->hsAltKey,
93669390
ssl->buffers.altKey);
93679391

93689392
if (ret == 0) {

wolfssl/test.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,9 +3902,11 @@ static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz,
39023902
{
39033903
enum wc_HashType hashType = WC_HASH_TYPE_NONE;
39043904
WC_RNG rng;
3905-
int ret;
3905+
int ret = 0;
39063906
word32 idx = 0;
39073907
RsaKey myKey;
3908+
byte* inBuf = (byte*)in;
3909+
word32 inBufSz = inSz;
39083910
byte* keyBuf = (byte*)key;
39093911
PkCbInfo* cbInfo = (PkCbInfo*)ctx;
39103912

@@ -3942,17 +3944,40 @@ static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz,
39423944
if (ret != 0)
39433945
return ret;
39443946

3945-
ret = wc_InitRsaKey(&myKey, NULL);
3947+
#ifdef TLS13_RSA_PSS_SIGN_CB_NO_PREHASH
3948+
/* With this defined, RSA-PSS sign callback when used from TLS 1.3
3949+
* does not hash data before giving to this callback. User must
3950+
* compute hash themselves. */
3951+
if (wolfSSL_GetVersion(ssl) == WOLFSSL_TLSV1_3) {
3952+
inBufSz = wc_HashGetDigestSize(hashType);
3953+
inBuf = (byte*)XMALLOC(inBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3954+
if (inBuf == NULL) {
3955+
ret = MEMORY_E;
3956+
}
3957+
if (ret == 0) {
3958+
ret = wc_Hash(hashType, in, inSz, inBuf, inBufSz);
3959+
}
3960+
}
3961+
#endif
3962+
3963+
if (ret == 0) {
3964+
ret = wc_InitRsaKey(&myKey, NULL);
3965+
}
39463966
if (ret == 0) {
39473967
ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
39483968
if (ret == 0) {
3949-
ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, &myKey,
3950-
&rng);
3969+
ret = wc_RsaPSS_Sign(inBuf, inBufSz, out, *outSz, hashType, mgf,
3970+
&myKey, &rng);
39513971
}
39523972
if (ret > 0) { /* save and convert to 0 success */
39533973
*outSz = (word32) ret;
39543974
ret = 0;
39553975
}
3976+
#ifdef TLS13_RSA_PSS_SIGN_CB_NO_PREHASH
3977+
if ((inBuf != NULL) && (wolfSSL_GetVersion(ssl) == WOLFSSL_TLSV1_3)) {
3978+
XFREE(inBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3979+
}
3980+
#endif
39563981
wc_FreeRsaKey(&myKey);
39573982
}
39583983
wc_FreeRng(&rng);

0 commit comments

Comments
 (0)