Path: blob/master/test/jdk/sun/security/pkcs11/rsa/TestCACerts.java
41154 views
/*1* Copyright (c) 2003, 2020, 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 485696626* @summary Test the new RSA provider can verify all the RSA certs in the cacerts file27* @author Andreas Sterbenz28* @library /test/lib ..29* @library ../../../../java/security/testlibrary30* @modules jdk.crypto.cryptoki31* @run main/othervm TestCACerts32* @run main/othervm -Djava.security.manager=allow TestCACerts sm TestCACerts.policy33*/3435// this test serves as our known answer test3637import java.io.FileInputStream;38import java.io.InputStream;39import java.security.KeyStore;40import java.security.Provider;41import java.security.PublicKey;42import java.security.Security;43import java.security.cert.X509Certificate;44import java.util.Enumeration;4546public class TestCACerts extends PKCS11Test {4748public static void main(String[] args) throws Exception {49main(new TestCACerts(), args);50}5152@Override53public void main(Provider p) throws Exception {5455long start = System.currentTimeMillis();56Providers.setAt(p, 1);57try {58String PROVIDER = p.getName();59String javaHome = props.getProperty("java.home");60String caCerts = javaHome + SEP + "lib" + SEP + "security" + SEP + "cacerts";61KeyStore ks;62try (InputStream in = new FileInputStream(caCerts)) {63ks = KeyStore.getInstance(KeyStore.getDefaultType());64ks.load(in, null);65}66for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {67String alias = (String)e.nextElement();68if (ks.isCertificateEntry(alias)) {69System.out.println("* Testing " + alias + "...");70X509Certificate cert = (X509Certificate)ks.getCertificate(alias);71PublicKey key = cert.getPublicKey();72String alg = key.getAlgorithm();73if (alg.equals("RSA")) {74System.out.println("Signature algorithm: " + cert.getSigAlgName());75cert.verify(key, PROVIDER);76} else {77System.out.println("Skipping cert with key: " + alg);78}79} else {80System.out.println("Skipping alias " + alias);81}82}83long stop = System.currentTimeMillis();84System.out.println("All tests passed (" + (stop - start) + " ms).");85} finally {86Security.removeProvider(p.getName());87}88}89}909192