Path: blob/master/test/jdk/sun/security/pkcs/pkcs7/PKCS7VerifyTest.java
41153 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* @modules java.base/sun.security.pkcs29* @run main/othervm PKCS7VerifyTest PKCS7TEST.DSA.base6430* @run main/othervm PKCS7VerifyTest PKCS7TEST.DSA.base64 PKCS7TEST.SF31*/32import java.io.ByteArrayInputStream;33import java.io.File;34import java.io.FileInputStream;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.security.Security;39import java.security.cert.X509Certificate;40import java.util.Base64;41import java.util.HashMap;42import java.util.Map;43import sun.security.pkcs.PKCS7;44import sun.security.pkcs.SignerInfo;4546public class PKCS7VerifyTest {4748static final String TESTSRC = System.getProperty("test.src", ".");49static final String FS = File.separator;50static final String FILEPATH = TESTSRC + FS + "jarsigner" + FS + "META-INF"51+ FS;5253public static void main(String[] args) throws Exception {54if (args.length == 0) {55throw new RuntimeException("usage: java JarVerify <file1> <file2>");56}5758Security.setProperty("jdk.jar.disabledAlgorithms", "");5960// The command " java PKCS7VerifyTest file1 [file2] "61// treats file1 as containing the DER encoding of a PKCS7 signed data62// object. If file2 is absent, the program verifies that some signature63// (SignerInfo) file1 correctly signs the data contained in the64// ContentInfo component of the PKCS7 object encoded by file1. If file265// is present, the program verifies file1 contains a correct signature66// for the contents of file2.6768PKCS7 pkcs7;69byte[] data;7071// to avoid attaching binary DSA file, the DSA file was encoded72// in Base64, decode encoded Base64 DSA file below73byte[] base64Bytes = Files.readAllBytes(Paths.get(FILEPATH + args[0]));74pkcs7 = new PKCS7(new ByteArrayInputStream(75Base64.getMimeDecoder().decode(base64Bytes)));76if (args.length < 2) {77data = null;78} else {79data = Files.readAllBytes(Paths.get(FILEPATH + args[1]));8081}8283SignerInfo[] signerInfos = pkcs7.verify(data);8485if (signerInfos == null) {86throw new RuntimeException("no signers verify");87}88System.out.println("Verifying SignerInfos:");89for (SignerInfo signerInfo : signerInfos) {90System.out.println(signerInfo.toString());91}9293X509Certificate certs[] = pkcs7.getCertificates();9495HashMap<String, X509Certificate> certTable = new HashMap(certs.length);96for (X509Certificate cert : certs) {97certTable.put(cert.getSubjectDN().toString(), cert);98}99100// try to verify all the certs101for (Map.Entry<String, X509Certificate> entry : certTable.entrySet()) {102103X509Certificate cert = entry.getValue();104X509Certificate issuerCert = certTable105.get(cert.getIssuerDN().toString());106107System.out.println("Subject: " + cert.getSubjectDN());108if (issuerCert == null) {109System.out.println("Issuer certificate not found");110} else {111System.out.println("Issuer: " + cert.getIssuerDN());112cert.verify(issuerCert.getPublicKey());113System.out.println("Cert verifies.");114}115System.out.println();116}117}118119}120121122