Path: blob/master/test/jdk/sun/security/x509/AlgorithmId/NonStandardNames.java
41153 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 718090726* @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes27* @modules java.base/sun.security.pkcs28* java.base/sun.security.tools.keytool29* java.base/sun.security.util30* java.base/sun.security.x50931* @compile -XDignore.symbol.file NonStandardNames.java32* @run main NonStandardNames33*/3435import java.security.MessageDigest;36import java.security.Signature;37import java.security.cert.X509Certificate;38import sun.security.pkcs.ContentInfo;39import sun.security.pkcs.PKCS7;40import sun.security.pkcs.PKCS9Attribute;41import sun.security.pkcs.PKCS9Attributes;42import sun.security.pkcs.SignerInfo;43import sun.security.tools.keytool.CertAndKeyGen;44import sun.security.x509.AlgorithmId;45import sun.security.x509.X500Name;4647public class NonStandardNames {4849public static void main(String[] args) throws Exception {5051byte[] data = "Hello".getBytes();52X500Name n = new X500Name("cn=Me");5354CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");55cakg.generate(1024);56X509Certificate cert = cakg.getSelfCertificate(n, 1000);5758MessageDigest md = MessageDigest.getInstance("SHA-256");59PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{60new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),61new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),62});6364Signature s = Signature.getInstance("SHA256withRSA");65s.initSign(cakg.getPrivateKey());66s.update(authed.getDerEncoding());67byte[] sig = s.sign();6869SignerInfo signerInfo = new SignerInfo(70n,71cert.getSerialNumber(),72AlgorithmId.get("SHA-256"),73authed,74AlgorithmId.get("SHA256withRSA"),75sig,76null77);7879PKCS7 pkcs7 = new PKCS7(80new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},81new ContentInfo(data),82new X509Certificate[] {cert},83new SignerInfo[] {signerInfo});8485if (pkcs7.verify(signerInfo, data) == null) {86throw new Exception("Not verified");87}88}89}909192