changeset 5501:c399756623cb

7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes Reviewed-by: xuelei, vinnie
author weijun
date Mon, 16 Jul 2012 11:56:45 +0800
parents a51e0bc5a081
children 3aa687530ca9
files src/share/classes/sun/security/pkcs/SignerInfo.java test/sun/security/x509/AlgorithmId/NonStandardNames.java
diffstat 2 files changed, 105 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/pkcs/SignerInfo.java	Thu Jul 12 20:00:21 2012 +0400
+++ b/src/share/classes/sun/security/pkcs/SignerInfo.java	Mon Jul 16 11:56:45 2012 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -273,6 +273,24 @@
         return certList;
     }
 
+    // Copied from com.sun.crypto.provider.OAEPParameters.
+    private static String convertToStandardName(String internalName) {
+        if (internalName.equals("SHA")) {
+            return "SHA-1";
+        } else if (internalName.equals("SHA224")) {
+            return "SHA-224";
+        } else if (internalName.equals("SHA256")) {
+            return "SHA-256";
+        } else if (internalName.equals("SHA384")) {
+            return "SHA-384";
+        } else if (internalName.equals("SHA512")) {
+            return "SHA-512";
+        } else {
+            return internalName;
+        }
+    }
+
+
     /* Returns null if verify fails, this signerInfo if
        verify succeeds. */
     SignerInfo verify(PKCS7 block, byte[] data)
@@ -311,7 +329,8 @@
                 if (messageDigest == null) // fail if there is no message digest
                     return null;
 
-                MessageDigest md = MessageDigest.getInstance(digestAlgname);
+                MessageDigest md = MessageDigest.getInstance(
+                        convertToStandardName(digestAlgname));
                 byte[] computedMessageDigest = md.digest(data);
 
                 if (messageDigest.length != computedMessageDigest.length)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/security/x509/AlgorithmId/NonStandardNames.java	Mon Jul 16 11:56:45 2012 +0800
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7180907
+ * @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
+ */
+
+import java.security.MessageDigest;
+import java.security.Signature;
+import java.security.cert.X509Certificate;
+import sun.security.pkcs.ContentInfo;
+import sun.security.pkcs.PKCS7;
+import sun.security.pkcs.PKCS9Attribute;
+import sun.security.pkcs.PKCS9Attributes;
+import sun.security.pkcs.SignerInfo;
+import sun.security.x509.CertAndKeyGen;
+import sun.security.x509.AlgorithmId;
+import sun.security.x509.X500Name;
+
+public class NonStandardNames {
+
+    public static void main(String[] args) throws Exception {
+
+        byte[] data = "Hello".getBytes();
+        X500Name n = new X500Name("cn=Me");
+
+        CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");
+        cakg.generate(1024);
+        X509Certificate cert = cakg.getSelfCertificate(n, 1000);
+
+        MessageDigest md = MessageDigest.getInstance("SHA-256");
+        PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{
+            new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),
+            new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),
+        });
+
+        Signature s = Signature.getInstance("SHA256withRSA");
+        s.initSign(cakg.getPrivateKey());
+        s.update(authed.getDerEncoding());
+        byte[] sig = s.sign();
+
+        SignerInfo signerInfo = new SignerInfo(
+                n,
+                cert.getSerialNumber(),
+                AlgorithmId.get("SHA-256"),
+                authed,
+                AlgorithmId.get("SHA256withRSA"),
+                sig,
+                null
+                );
+
+        PKCS7 pkcs7 = new PKCS7(
+                new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},
+                new ContentInfo(data),
+                new X509Certificate[] {cert},
+                new SignerInfo[] {signerInfo});
+
+        if (pkcs7.verify(signerInfo, data) == null) {
+            throw new Exception("Not verified");
+        }
+    }
+}