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
73 changes: 66 additions & 7 deletions src/wp_rsa_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,18 @@ static int wp_rsa_verify_x931(wp_RsaSigCtx* ctx, const unsigned char* sig,
return ok;
}

/* Restore leading zero bytes stripped from a nonempty, short signature. */
static unsigned char* wp_rsa_pad_short_sig(const unsigned char* sig,
size_t sigLen, size_t keyLen)
{
unsigned char* padded = (unsigned char*)OPENSSL_zalloc(keyLen);

if (padded != NULL) {
XMEMCPY(padded + keyLen - sigLen, sig, sigLen);
}
return padded;
}

/**
* Verify an RSA signature.
*
Expand All @@ -1480,11 +1492,39 @@ static int wp_rsa_verify(wp_RsaSigCtx* ctx, const unsigned char* sig,
ok = 0;
}
else {
unsigned char* decryptedSig = (unsigned char*)OPENSSL_malloc(sigLen);
unsigned char* decryptedSig = NULL;
unsigned char* padded = NULL;
int keyLen = wc_RsaEncryptSize(wp_rsa_get_key(ctx->rsa));

if (decryptedSig == NULL) {
if ((sig == NULL) || (sigLen == 0) || (keyLen <= 0) ||
(sigLen > (size_t)keyLen)) {
ok = 0;
}
/* Like RSA_public_decrypt, accept short signatures without a digest
* for PKCS#1 v1.5 and no padding. */
else if (((ctx->padMode == RSA_PKCS1_PADDING) ||
(ctx->padMode == RSA_NO_PADDING)) &&
#if LIBWOLFSSL_VERSION_HEX >= 0x05007004
Comment thread
aidangarske marked this conversation as resolved.
Resolved
(ctx->hash.type == WC_HASH_TYPE_NONE) &&
#else
(ctx->hashType == WC_HASH_TYPE_NONE) &&
#endif
(sigLen < (size_t)keyLen)) {
padded = wp_rsa_pad_short_sig(sig, sigLen, (size_t)keyLen);
if (padded == NULL) {
ok = 0;
}
else {
sig = padded;
sigLen = (size_t)keyLen;
}
}
if (ok) {
decryptedSig = (unsigned char*)OPENSSL_malloc(sigLen);
if (decryptedSig == NULL) {
ok = 0;
}
}
if (ok) {
if (ctx->padMode == RSA_PKCS1_PADDING) {
ok = wp_rsa_verify_pkcs1(ctx, sig, sigLen, tbs, tbsLen,
Expand All @@ -1507,6 +1547,7 @@ static int wp_rsa_verify(wp_RsaSigCtx* ctx, const unsigned char* sig,
}
}
OPENSSL_free(decryptedSig);
OPENSSL_free(padded);
}

WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
Expand Down Expand Up @@ -1565,6 +1606,7 @@ static int wp_rsa_verify_recover(wp_RsaSigCtx* ctx, unsigned char* rout,
enum wc_HashType hashType = WC_HASH_TYPE_NONE;
unsigned char* out = rout;
unsigned char* decrypted = NULL;
unsigned char* padded = NULL;
unsigned char encoded[MAX_DER_DIGEST_SZ];

WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_verify_recover");
Expand Down Expand Up @@ -1601,18 +1643,34 @@ static int wp_rsa_verify_recover(wp_RsaSigCtx* ctx, unsigned char* rout,
}
}
if (ok && (rout != NULL) && ((sig == NULL) ||
(!WP_FITS_WORD32(sigLen)) || (!WP_FITS_WORD32(routsize)))) {
(sigLen == 0) || (sigLen > (size_t)keyLen) ||
(!WP_FITS_WORD32(routsize)))) {
ok = 0;
}
if (ok && (rout != NULL)) {
outLen = (word32)routsize;
if (hashType != WC_HASH_TYPE_NONE) {
decrypted = (unsigned char*)OPENSSL_malloc(keyLen);
if (decrypted == NULL) {
if (sigLen != (size_t)keyLen) {
ok = 0;
}
if (ok) {
decrypted = (unsigned char*)OPENSSL_malloc(keyLen);
if (decrypted == NULL) {
ok = 0;
}
out = decrypted;
outLen = (word32)keyLen;
}
}
else if (sigLen < (size_t)keyLen) {
padded = wp_rsa_pad_short_sig(sig, sigLen, (size_t)keyLen);
if (padded == NULL) {
ok = 0;
}
out = decrypted;
outLen = (word32)keyLen;
else {
sig = padded;
sigLen = (size_t)keyLen;
}
}
}
if (ok && (rout != NULL) && (wp_lock(wp_rsa_get_mutex(ctx->rsa)) != 1)) {
Expand Down Expand Up @@ -1653,6 +1711,7 @@ static int wp_rsa_verify_recover(wp_RsaSigCtx* ctx, unsigned char* rout,
}

OPENSSL_free(decrypted);
OPENSSL_free(padded);
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
Expand Down
100 changes: 100 additions & 0 deletions test/test_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,106 @@ int test_rsa_sign_verify_x931(void *data)
return err;
}

int test_rsa_verify_short_sig(void *data)
{
int err;
static const unsigned char sha256Prefix[] = {
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
};
/* SHA-256 PKCS#1 v1.5 signature of msg by rsa_key_der_2048 starts 0x00. */
unsigned char msg[] = { 0x00, 0x65 };
unsigned char tbs[sizeof(sha256Prefix) + 32];
unsigned char sig[256];
unsigned char raw[256], recovered[256];
size_t sigLen = sizeof(sig);
const unsigned char *p = rsa_key_der_2048;
OSSL_LIB_CTX *libCtxs[] = { osslLibCtx, wpLibCtx };
EVP_PKEY *pkey = NULL;
int i;

(void)data;

pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, sizeof(rsa_key_der_2048));
err = pkey == NULL;
if (err == 0) {
memcpy(tbs, sha256Prefix, sizeof(sha256Prefix));
err = EVP_Digest(msg, sizeof(msg), tbs + sizeof(sha256Prefix), NULL,
EVP_sha256(), NULL) != 1;
}
if (err == 0) {
err = test_digest_sign(pkey, osslLibCtx, msg, sizeof(msg), "SHA256",
NULL, sig, &sigLen, RSA_PKCS1_PADDING, 0);
}
if (err == 0) {
err = (sigLen != sizeof(sig)) || (sig[0] != 0x00);
}
if (err == 0) {
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(osslLibCtx, pkey, NULL);
size_t len = sizeof(raw);

err = (ctx == NULL) || (EVP_PKEY_verify_recover_init(ctx) != 1) ||
(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING) <= 0) ||
(EVP_PKEY_verify_recover(ctx, raw, &len, sig, sigLen) != 1) ||
(len != sizeof(raw));
EVP_PKEY_CTX_free(ctx);
}
for (i = 0; (err == 0) && (i < 2); i++) {
/* No digest, DigestInfo as tbs, leading zero stripped (OpenPGP). */
err = test_pkey_verify(pkey, libCtxs[i], tbs, sizeof(tbs), sig + 1,
sigLen - 1, RSA_PKCS1_PADDING, NULL, NULL);
if (err == 0) {
sig[sigLen / 2] ^= 0x80;
err = test_pkey_verify(pkey, libCtxs[i], tbs, sizeof(tbs),
sig + 1, sigLen - 1, RSA_PKCS1_PADDING, NULL, NULL) != 1;
sig[sigLen / 2] ^= 0x80;
}
/* With a digest the signature must be full length. */
if (err == 0) {
err = test_digest_verify(pkey, libCtxs[i], msg, sizeof(msg),
"SHA256", NULL, sig, sigLen, RSA_PKCS1_PADDING, 0);
}
if (err == 0) {
err = test_digest_verify(pkey, libCtxs[i], msg, sizeof(msg),
"SHA256", NULL, sig + 1, sigLen - 1, RSA_PKCS1_PADDING,
0) != 1;
}
if (err == 0) {
err = test_pkey_verify(pkey, libCtxs[i], raw, sizeof(raw), sig + 1,
sigLen - 1, RSA_NO_PADDING, NULL, NULL);
}
if (err == 0) {
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libCtxs[i], pkey,
NULL);
size_t len = sizeof(recovered);

err = (ctx == NULL) || (EVP_PKEY_verify_recover_init(ctx) != 1) ||
(EVP_PKEY_verify_recover(ctx, recovered, &len, sig + 1,
sigLen - 1) != 1) || (len != sizeof(tbs)) ||
(memcmp(recovered, tbs, sizeof(tbs)) != 0);
if (err == 0) {
len = sizeof(recovered);
err = (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING) <= 0) ||
(EVP_PKEY_verify_recover(ctx, recovered, &len, sig + 1,
sigLen - 1) != 1) || (len != sizeof(raw)) ||
(memcmp(recovered, raw, sizeof(raw)) != 0);
}
if (err == 0) {
len = sizeof(recovered);
err = (EVP_PKEY_verify_recover_init(ctx) != 1) ||
(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0) ||
(EVP_PKEY_verify_recover(ctx, recovered, &len, sig + 1,
sigLen - 1) > 0);
ERR_clear_error();
}
EVP_PKEY_CTX_free(ctx);
}
}

EVP_PKEY_free(pkey);
return err;
}

static int test_rsa_enc_dec(const unsigned char *der, size_t derLen,
int padMode, const EVP_MD *rsaMd,
const EVP_MD *rsaMgf1Md)
Expand Down
1 change: 1 addition & 0 deletions test/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ TEST_CASE test_case[] = {
TEST_DECL(test_rsa_verify_recover, NULL),
TEST_DECL(test_rsa_sign_verify_pss, NULL),
TEST_DECL(test_rsa_sign_verify_x931, NULL),
TEST_DECL(test_rsa_verify_short_sig, NULL),
TEST_DECL(test_rsa_enc_dec_pkcs1, NULL),
TEST_DECL(test_rsa_enc_dec_oaep, NULL),
TEST_DECL(test_rsa_enc_dec_nopad, NULL),
Expand Down
1 change: 1 addition & 0 deletions test/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ int test_rsa_sign_verify_recover_pkcs1(void *data);
int test_rsa_verify_recover(void *data);
int test_rsa_sign_verify_pss(void *data);
int test_rsa_sign_verify_x931(void *data);
int test_rsa_verify_short_sig(void *data);
int test_rsa_enc_dec_pkcs1(void *data);
int test_rsa_enc_dec_oaep(void *data);
int test_rsa_enc_dec_nopad(void *data);
Expand Down
Loading