Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/AlgorithmId/NonStandardNames.java
38853 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* @compile -XDignore.symbol.file NonStandardNames.java28* @run main NonStandardNames29*/3031import java.security.MessageDigest;32import java.security.Signature;33import java.security.cert.X509Certificate;34import sun.security.pkcs.ContentInfo;35import sun.security.pkcs.PKCS7;36import sun.security.pkcs.PKCS9Attribute;37import sun.security.pkcs.PKCS9Attributes;38import sun.security.pkcs.SignerInfo;39import sun.security.tools.keytool.CertAndKeyGen;40import sun.security.x509.AlgorithmId;41import sun.security.x509.X500Name;4243public class NonStandardNames {4445public static void main(String[] args) throws Exception {4647byte[] data = "Hello".getBytes();48X500Name n = new X500Name("cn=Me");4950CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");51cakg.generate(1024);52X509Certificate cert = cakg.getSelfCertificate(n, 1000);5354MessageDigest md = MessageDigest.getInstance("SHA-256");55PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{56new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),57new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),58});5960Signature s = Signature.getInstance("SHA256withRSA");61s.initSign(cakg.getPrivateKey());62s.update(authed.getDerEncoding());63byte[] sig = s.sign();6465SignerInfo signerInfo = new SignerInfo(66n,67cert.getSerialNumber(),68AlgorithmId.get("SHA-256"),69authed,70AlgorithmId.get("SHA256withRSA"),71sig,72null73);7475PKCS7 pkcs7 = new PKCS7(76new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},77new ContentInfo(data),78new X509Certificate[] {cert},79new SignerInfo[] {signerInfo});8081if (pkcs7.verify(signerInfo, data) == null) {82throw new Exception("Not verified");83}84}85}868788