Add additional DigestInfo checks.
[openssl.git] / crypto / rsa / rsa_sign.c
index 2ccadb73d850c193389090c2f88ba22ba268c9b3..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,
@@ -199,6 +218,22 @@ int int_rsa_verify(int dtype, const unsigned char *m,
        i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
 
        if (i <= 0) goto err;
+       /* Oddball MDC2 case: signature can be OCTET STRING.
+        * check for correct tag and length octets.
+        */
+       if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10)
+               {
+               if (rm)
+                       {
+                       memcpy(rm, s + 2, 16);
+                       *prm_len = 16;
+                       ret = 1;
+                       }
+               else if(memcmp(m, s + 2, 16))
+                       RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
+               else
+                       ret = 1;
+               }
 
        /* Special case: SSL signature */
        if(dtype == NID_md5_sha1) {
@@ -212,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;