Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs/pkcs7/PKCS7VerifyTest.java
38854 views
/*1* Copyright (c) 2015, 2016, 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 804835726* @summary Read signed data in one or more PKCS7 objects from individual files,27* verify SignerInfos and certificate chain.28* @run main/othervm -Djava.security.properties=${test.src}/reenable.jar.alg.props PKCS7VerifyTest PKCS7TEST.DSA.base6429* @run main/othervm -Djava.security.properties=${test.src}/reenable.jar.alg.props PKCS7VerifyTest PKCS7TEST.DSA.base64 PKCS7TEST.SF30*/31import java.io.ByteArrayInputStream;32import java.io.File;33import java.io.FileInputStream;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.security.Security;38import java.security.cert.X509Certificate;39import java.util.Base64;40import java.util.HashMap;41import java.util.Map;42import sun.security.pkcs.PKCS7;43import sun.security.pkcs.SignerInfo;4445public class PKCS7VerifyTest {4647static final String TESTSRC = System.getProperty("test.src", ".");48static final String FS = File.separator;49static final String FILEPATH = TESTSRC + FS + "jarsigner" + FS + "META-INF"50+ FS;5152public static void main(String[] args) throws Exception {53if (args.length == 0) {54throw new RuntimeException("usage: java JarVerify <file1> <file2>");55}5657// The command " java PKCS7VerifyTest file1 [file2] "58// treats file1 as containing the DER encoding of a PKCS7 signed data59// object. If file2 is absent, the program verifies that some signature60// (SignerInfo) file1 correctly signs the data contained in the61// ContentInfo component of the PKCS7 object encoded by file1. If file262// is present, the program verifies file1 contains a correct signature63// for the contents of file2.6465PKCS7 pkcs7;66byte[] data;6768// to avoid attaching binary DSA file, the DSA file was encoded69// in Base64, decode encoded Base64 DSA file below70byte[] base64Bytes = Files.readAllBytes(Paths.get(FILEPATH + args[0]));71pkcs7 = new PKCS7(new ByteArrayInputStream(72Base64.getMimeDecoder().decode(base64Bytes)));73if (args.length < 2) {74data = null;75} else {76data = Files.readAllBytes(Paths.get(FILEPATH + args[1]));7778}7980SignerInfo[] signerInfos = pkcs7.verify(data);8182if (signerInfos == null) {83throw new RuntimeException("no signers verify");84}85System.out.println("Verifying SignerInfos:");86for (SignerInfo signerInfo : signerInfos) {87System.out.println(signerInfo.toString());88}8990X509Certificate certs[] = pkcs7.getCertificates();9192HashMap<String, X509Certificate> certTable = new HashMap(certs.length);93for (X509Certificate cert : certs) {94certTable.put(cert.getSubjectDN().toString(), cert);95}9697// try to verify all the certs98for (Map.Entry<String, X509Certificate> entry : certTable.entrySet()) {99100X509Certificate cert = entry.getValue();101X509Certificate issuerCert = certTable102.get(cert.getIssuerDN().toString());103104System.out.println("Subject: " + cert.getSubjectDN());105if (issuerCert == null) {106System.out.println("Issuer certificate not found");107} else {108System.out.println("Issuer: " + cert.getIssuerDN());109cert.verify(issuerCert.getPublicKey());110System.out.println("Cert verifies.");111}112System.out.println();113}114}115116}117118119