Add additional DigestInfo checks.
authorDr. Stephen Henson <steve@openssl.org>
Thu, 25 Sep 2014 22:28:48 +0000 (23:28 +0100)
committerDr. Stephen Henson <steve@openssl.org>
Mon, 29 Sep 2014 11:24:04 +0000 (12:24 +0100)
Reencode DigestInto in DER and check against the original: this
will reject any improperly encoded DigestInfo structures.

Note: this is a precautionary measure, there is no known attack
which can exploit this.

Thanks to Brian Smith for reporting this issue.
Reviewed-by: Tim Hudson <tjh@openssl.org>
CHANGES
crypto/rsa/rsa_sign.c

diff --git a/CHANGES b/CHANGES
index 9d3e4586bf9d82194914481e8d262946a974630a..1abff63983fbb4440f286f1db467276c242d9781 100644 (file)
--- a/CHANGES
+++ b/CHANGES
      bogus results, with non-infinity inputs mapped to infinity too.)
      [Bodo Moeller]
 
+ Changes between 1.0.1i and 1.0.1j [xx XXX xxxx]
+
+  *) Add additional DigestInfo checks.
+     Reencode DigestInto in DER and check against the original when
+     verifying RSA signature: this will reject any improperly encoded
+     DigestInfo structures.
+
+     Note: this is a precautionary measure and no attacks are currently known.
+
+     [Steve Henson]
+
  Changes between 1.0.1g and 1.0.1h [5 Jun 2014]
 
   *) Fix for SSL/TLS MITM flaw. An attacker using a carefully crafted
index b6f6037ae002df06a4fb078e8f8af0a0b6dd2dfb..225bcfe2dcabee2afb39cf224d9b4302b9eb180e 100644 (file)
@@ -151,6 +151,25 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
        return(ret);
        }
 
+/*
+ * Check DigestInfo structure does not contain extraneous data by reencoding
+ * using DER and checking encoding against original. 
+ */
+static int rsa_check_digestinfo(X509_SIG *sig, const unsigned char *dinfo, int dinfolen)
+       {
+       unsigned char *der = NULL;
+       int derlen;
+       int ret = 0;
+       derlen = i2d_X509_SIG(sig, &der);
+       if (derlen <= 0)
+               return 0;
+       if (derlen == dinfolen && !memcmp(dinfo, der, derlen))
+               ret = 1;
+       OPENSSL_cleanse(der, derlen);
+       OPENSSL_free(der);
+       return ret;
+       }
+
 int int_rsa_verify(int dtype, const unsigned char *m,
                          unsigned int m_len,
                          unsigned char *rm, size_t *prm_len,
@@ -228,7 +247,7 @@ int int_rsa_verify(int dtype, const unsigned char *m,
                if (sig == NULL) goto err;
 
                /* Excess data can be used to create forgeries */
-               if(p != s+i)
+               if(p != s+i || !rsa_check_digestinfo(sig, s, i))
                        {
                        RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
                        goto err;