|
|
d5b14c |
diff --git a/lib/cryptohi/secvfy.c b/lib/cryptohi/secvfy.c
|
|
|
d5b14c |
--- a/lib/cryptohi/secvfy.c
|
|
|
d5b14c |
+++ b/lib/cryptohi/secvfy.c
|
|
|
d5b14c |
@@ -164,6 +164,37 @@
|
|
|
d5b14c |
PR_FALSE /*XXX: unsafeAllowMissingParameters*/);
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
+static unsigned int
|
|
|
d5b14c |
+checkedSignatureLen(const SECKEYPublicKey *pubk)
|
|
|
d5b14c |
+{
|
|
|
d5b14c |
+ unsigned int sigLen = SECKEY_SignatureLen(pubk);
|
|
|
d5b14c |
+ if (sigLen == 0) {
|
|
|
d5b14c |
+ /* Error set by SECKEY_SignatureLen */
|
|
|
d5b14c |
+ return sigLen;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ unsigned int maxSigLen;
|
|
|
d5b14c |
+ switch (pubk->keyType) {
|
|
|
d5b14c |
+ case rsaKey:
|
|
|
d5b14c |
+ case rsaPssKey:
|
|
|
d5b14c |
+ maxSigLen = (RSA_MAX_MODULUS_BITS + 7) / 8;
|
|
|
d5b14c |
+ break;
|
|
|
d5b14c |
+ case dsaKey:
|
|
|
d5b14c |
+ maxSigLen = DSA_MAX_SIGNATURE_LEN;
|
|
|
d5b14c |
+ break;
|
|
|
d5b14c |
+ case ecKey:
|
|
|
d5b14c |
+ maxSigLen = 2 * MAX_ECKEY_LEN;
|
|
|
d5b14c |
+ break;
|
|
|
d5b14c |
+ default:
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG);
|
|
|
d5b14c |
+ return 0;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ if (sigLen > maxSigLen) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_INVALID_KEY);
|
|
|
d5b14c |
+ return 0;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ return sigLen;
|
|
|
d5b14c |
+}
|
|
|
d5b14c |
+
|
|
|
d5b14c |
/*
|
|
|
d5b14c |
* decode the ECDSA or DSA signature from it's DER wrapping.
|
|
|
d5b14c |
* The unwrapped/raw signature is placed in the buffer pointed
|
|
|
d5b14c |
@@ -174,38 +205,38 @@
|
|
|
d5b14c |
unsigned int len)
|
|
|
d5b14c |
{
|
|
|
d5b14c |
SECItem *dsasig = NULL; /* also used for ECDSA */
|
|
|
d5b14c |
- SECStatus rv = SECSuccess;
|
|
|
d5b14c |
|
|
|
d5b14c |
- if ((algid != SEC_OID_ANSIX9_DSA_SIGNATURE) &&
|
|
|
d5b14c |
- (algid != SEC_OID_ANSIX962_EC_PUBLIC_KEY)) {
|
|
|
d5b14c |
- if (sig->len != len) {
|
|
|
d5b14c |
- PORT_SetError(SEC_ERROR_BAD_DER);
|
|
|
d5b14c |
- return SECFailure;
|
|
|
d5b14c |
+ /* Safety: Ensure algId is as expected and that signature size is within maxmimums */
|
|
|
d5b14c |
+ if (algid == SEC_OID_ANSIX9_DSA_SIGNATURE) {
|
|
|
d5b14c |
+ if (len > DSA_MAX_SIGNATURE_LEN) {
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
-
|
|
|
d5b14c |
- PORT_Memcpy(dsig, sig->data, sig->len);
|
|
|
d5b14c |
- return SECSuccess;
|
|
|
d5b14c |
+ } else if (algid == SEC_OID_ANSIX962_EC_PUBLIC_KEY) {
|
|
|
d5b14c |
+ if (len > MAX_ECKEY_LEN * 2) {
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ } else {
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
- if (algid == SEC_OID_ANSIX962_EC_PUBLIC_KEY) {
|
|
|
d5b14c |
- if (len > MAX_ECKEY_LEN * 2) {
|
|
|
d5b14c |
- PORT_SetError(SEC_ERROR_BAD_DER);
|
|
|
d5b14c |
- return SECFailure;
|
|
|
d5b14c |
- }
|
|
|
d5b14c |
+ /* Decode and pad to length */
|
|
|
d5b14c |
+ dsasig = DSAU_DecodeDerSigToLen((SECItem *)sig, len);
|
|
|
d5b14c |
+ if (dsasig == NULL) {
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
- dsasig = DSAU_DecodeDerSigToLen((SECItem *)sig, len);
|
|
|
d5b14c |
-
|
|
|
d5b14c |
- if ((dsasig == NULL) || (dsasig->len != len)) {
|
|
|
d5b14c |
- rv = SECFailure;
|
|
|
d5b14c |
- } else {
|
|
|
d5b14c |
- PORT_Memcpy(dsig, dsasig->data, dsasig->len);
|
|
|
d5b14c |
+ if (dsasig->len != len) {
|
|
|
d5b14c |
+ SECITEM_FreeItem(dsasig, PR_TRUE);
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
- if (dsasig != NULL)
|
|
|
d5b14c |
- SECITEM_FreeItem(dsasig, PR_TRUE);
|
|
|
d5b14c |
- if (rv == SECFailure)
|
|
|
d5b14c |
- PORT_SetError(SEC_ERROR_BAD_DER);
|
|
|
d5b14c |
- return rv;
|
|
|
d5b14c |
+ PORT_Memcpy(dsig, dsasig->data, len);
|
|
|
d5b14c |
+ SECITEM_FreeItem(dsasig, PR_TRUE);
|
|
|
d5b14c |
+
|
|
|
d5b14c |
+ return SECSuccess;
|
|
|
d5b14c |
+
|
|
|
d5b14c |
+loser:
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_DER);
|
|
|
d5b14c |
+ return SECFailure;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
const SEC_ASN1Template hashParameterTemplate[] =
|
|
|
d5b14c |
@@ -281,7 +312,7 @@
|
|
|
d5b14c |
sec_DecodeSigAlg(const SECKEYPublicKey *key, SECOidTag sigAlg,
|
|
|
d5b14c |
const SECItem *param, SECOidTag *encalgp, SECOidTag *hashalg)
|
|
|
d5b14c |
{
|
|
|
d5b14c |
- int len;
|
|
|
d5b14c |
+ unsigned int len;
|
|
|
d5b14c |
PLArenaPool *arena;
|
|
|
d5b14c |
SECStatus rv;
|
|
|
d5b14c |
SECItem oid;
|
|
|
d5b14c |
@@ -466,48 +497,52 @@
|
|
|
d5b14c |
cx->pkcs1RSADigestInfo = NULL;
|
|
|
d5b14c |
rv = SECSuccess;
|
|
|
d5b14c |
if (sig) {
|
|
|
d5b14c |
- switch (type) {
|
|
|
d5b14c |
- case rsaKey:
|
|
|
d5b14c |
- rv = recoverPKCS1DigestInfo(hashAlg, &cx->hashAlg,
|
|
|
d5b14c |
- &cx->pkcs1RSADigestInfo,
|
|
|
d5b14c |
- &cx->pkcs1RSADigestInfoLen,
|
|
|
d5b14c |
- cx->key,
|
|
|
d5b14c |
- sig, wincx);
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
- case rsaPssKey:
|
|
|
d5b14c |
- sigLen = SECKEY_SignatureLen(key);
|
|
|
d5b14c |
- if (sigLen == 0) {
|
|
|
d5b14c |
- /* error set by SECKEY_SignatureLen */
|
|
|
d5b14c |
- rv = SECFailure;
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
+ if (type == rsaKey) {
|
|
|
d5b14c |
+ rv = recoverPKCS1DigestInfo(hashAlg, &cx->hashAlg,
|
|
|
d5b14c |
+ &cx->pkcs1RSADigestInfo,
|
|
|
d5b14c |
+ &cx->pkcs1RSADigestInfoLen,
|
|
|
d5b14c |
+ cx->key,
|
|
|
d5b14c |
+ sig, wincx);
|
|
|
d5b14c |
+ } else {
|
|
|
d5b14c |
+ sigLen = checkedSignatureLen(key);
|
|
|
d5b14c |
+ /* Check signature length is within limits */
|
|
|
d5b14c |
+ if (sigLen == 0) {
|
|
|
d5b14c |
+ /* error set by checkedSignatureLen */
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ if (sigLen > sizeof(cx->u)) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ switch (type) {
|
|
|
d5b14c |
+ case rsaPssKey:
|
|
|
d5b14c |
+ if (sig->len != sigLen) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ PORT_Memcpy(cx->u.buffer, sig->data, sigLen);
|
|
|
d5b14c |
+ rv = SECSuccess;
|
|
|
d5b14c |
break;
|
|
|
d5b14c |
- }
|
|
|
d5b14c |
- if (sig->len != sigLen) {
|
|
|
d5b14c |
- PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
+ case ecKey:
|
|
|
d5b14c |
+ case dsaKey:
|
|
|
d5b14c |
+ /* decodeECorDSASignature will check sigLen == sig->len after padding */
|
|
|
d5b14c |
+ rv = decodeECorDSASignature(encAlg, sig, cx->u.buffer, sigLen);
|
|
|
d5b14c |
+ break;
|
|
|
d5b14c |
+ default:
|
|
|
d5b14c |
+ /* Unreachable */
|
|
|
d5b14c |
rv = SECFailure;
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
- }
|
|
|
d5b14c |
- PORT_Memcpy(cx->u.buffer, sig->data, sigLen);
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
- case dsaKey:
|
|
|
d5b14c |
- case ecKey:
|
|
|
d5b14c |
- sigLen = SECKEY_SignatureLen(key);
|
|
|
d5b14c |
- if (sigLen == 0) {
|
|
|
d5b14c |
- /* error set by SECKEY_SignatureLen */
|
|
|
d5b14c |
- rv = SECFailure;
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
- }
|
|
|
d5b14c |
- rv = decodeECorDSASignature(encAlg, sig, cx->u.buffer, sigLen);
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
- default:
|
|
|
d5b14c |
- rv = SECFailure;
|
|
|
d5b14c |
- PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG);
|
|
|
d5b14c |
- break;
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ if (rv != SECSuccess) {
|
|
|
d5b14c |
+ goto loser;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
- if (rv)
|
|
|
d5b14c |
- goto loser;
|
|
|
d5b14c |
-
|
|
|
d5b14c |
/* check hash alg again, RSA may have changed it.*/
|
|
|
d5b14c |
if (HASH_GetHashTypeByOidTag(cx->hashAlg) == HASH_AlgNULL) {
|
|
|
d5b14c |
/* error set by HASH_GetHashTypeByOidTag */
|
|
|
d5b14c |
@@ -650,11 +685,16 @@
|
|
|
d5b14c |
switch (cx->key->keyType) {
|
|
|
d5b14c |
case ecKey:
|
|
|
d5b14c |
case dsaKey:
|
|
|
d5b14c |
- dsasig.data = cx->u.buffer;
|
|
|
d5b14c |
- dsasig.len = SECKEY_SignatureLen(cx->key);
|
|
|
d5b14c |
+ dsasig.len = checkedSignatureLen(cx->key);
|
|
|
d5b14c |
if (dsasig.len == 0) {
|
|
|
d5b14c |
return SECFailure;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
+ if (dsasig.len > sizeof(cx->u)) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
+ return SECFailure;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ dsasig.data = cx->u.buffer;
|
|
|
d5b14c |
+
|
|
|
d5b14c |
if (sig) {
|
|
|
d5b14c |
rv = decodeECorDSASignature(cx->encAlg, sig, dsasig.data,
|
|
|
d5b14c |
dsasig.len);
|
|
|
d5b14c |
@@ -686,8 +726,13 @@
|
|
|
d5b14c |
}
|
|
|
d5b14c |
|
|
|
d5b14c |
rsasig.data = cx->u.buffer;
|
|
|
d5b14c |
- rsasig.len = SECKEY_SignatureLen(cx->key);
|
|
|
d5b14c |
+ rsasig.len = checkedSignatureLen(cx->key);
|
|
|
d5b14c |
if (rsasig.len == 0) {
|
|
|
d5b14c |
+ /* Error set by checkedSignatureLen */
|
|
|
d5b14c |
+ return SECFailure;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ if (rsasig.len > sizeof(cx->u)) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
return SECFailure;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
if (sig) {
|
|
|
d5b14c |
@@ -749,7 +794,6 @@
|
|
|
d5b14c |
SECStatus rv;
|
|
|
d5b14c |
VFYContext *cx;
|
|
|
d5b14c |
SECItem dsasig; /* also used for ECDSA */
|
|
|
d5b14c |
-
|
|
|
d5b14c |
rv = SECFailure;
|
|
|
d5b14c |
|
|
|
d5b14c |
cx = vfy_CreateContext(key, sig, encAlg, hashAlg, NULL, wincx);
|
|
|
d5b14c |
@@ -757,19 +801,25 @@
|
|
|
d5b14c |
switch (key->keyType) {
|
|
|
d5b14c |
case rsaKey:
|
|
|
d5b14c |
rv = verifyPKCS1DigestInfo(cx, digest);
|
|
|
d5b14c |
+ /* Error (if any) set by verifyPKCS1DigestInfo */
|
|
|
d5b14c |
break;
|
|
|
d5b14c |
- case dsaKey:
|
|
|
d5b14c |
case ecKey:
|
|
|
d5b14c |
+ case dsaKey:
|
|
|
d5b14c |
dsasig.data = cx->u.buffer;
|
|
|
d5b14c |
- dsasig.len = SECKEY_SignatureLen(cx->key);
|
|
|
d5b14c |
+ dsasig.len = checkedSignatureLen(cx->key);
|
|
|
d5b14c |
if (dsasig.len == 0) {
|
|
|
d5b14c |
+ /* Error set by checkedSignatureLen */
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
break;
|
|
|
d5b14c |
}
|
|
|
d5b14c |
- if (PK11_Verify(cx->key, &dsasig, (SECItem *)digest, cx->wincx) !=
|
|
|
d5b14c |
- SECSuccess) {
|
|
|
d5b14c |
+ if (dsasig.len > sizeof(cx->u)) {
|
|
|
d5b14c |
PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
- } else {
|
|
|
d5b14c |
- rv = SECSuccess;
|
|
|
d5b14c |
+ rv = SECFailure;
|
|
|
d5b14c |
+ break;
|
|
|
d5b14c |
+ }
|
|
|
d5b14c |
+ rv = PK11_Verify(cx->key, &dsasig, (SECItem *)digest, cx->wincx);
|
|
|
d5b14c |
+ if (rv != SECSuccess) {
|
|
|
d5b14c |
+ PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
|
|
|
d5b14c |
}
|
|
|
d5b14c |
break;
|
|
|
d5b14c |
default:
|
|
|
d5b14c |
|