Avoid PKCS #1 v1.5 signature attack discovered by Daniel Bleichenbacher
[oweals/openssl.git] / crypto / rsa / rsa_sign.c
index 9e7dfd1927bf55dd9a9191dd53cf2b80647fb11a..71aabeea1bd73bca46c8a1657273dbedbffad4e0 100644 (file)
@@ -76,7 +76,7 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
        const unsigned char *s = NULL;
        X509_ALGOR algor;
        ASN1_OCTET_STRING digest;
-       if(rsa->flags & RSA_FLAG_SIGN_VER)
+       if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign)
                {
                return rsa->meth->rsa_sign(type, m, m_len,
                        sigret, siglen, rsa);
@@ -155,7 +155,7 @@ int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
                return(0);
                }
 
-       if(rsa->flags & RSA_FLAG_SIGN_VER)
+       if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify)
                {
                return rsa->meth->rsa_verify(dtype, m, m_len,
                        sigbuf, siglen, rsa);
@@ -185,6 +185,23 @@ int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
                sig=d2i_X509_SIG(NULL,&p,(long)i);
 
                if (sig == NULL) goto err;
+
+               /* Excess data can be used to create forgeries */
+               if(p != s+i)
+                       {
+                       RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
+                       goto err;
+                       }
+
+               /* Parameters to the signature algorithm can also be used to
+                  create forgeries */
+               if(sig->algor->parameter
+                  && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL)
+                       {
+                       RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
+                       goto err;
+                       }
+
                sigtype=OBJ_obj2nid(sig->algor->algorithm);