Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/rsa/TestCACerts.java
38855 views
/*1* Copyright (c) 2003, 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 485696626* @summary Test the new RSA provider can verify all the RSA certs in the cacerts file27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestCACerts30* @run main/othervm TestCACerts sm TestCACerts.policy31*/3233// this test serves as our known answer test3435import java.io.FileInputStream;36import java.io.InputStream;37import java.security.KeyStore;38import java.security.Provider;39import java.security.PublicKey;40import java.security.Security;41import java.security.cert.X509Certificate;42import java.util.Enumeration;4344public class TestCACerts extends PKCS11Test {4546public static void main(String[] args) throws Exception {47main(new TestCACerts(), args);48}4950@Override51public void main(Provider p) throws Exception {52long start = System.currentTimeMillis();53Security.addProvider(p);54try {55String PROVIDER = p.getName();56String javaHome = props.getProperty("java.home");57String caCerts = javaHome + SEP + "lib" + SEP + "security" + SEP + "cacerts";58KeyStore ks;59try (InputStream in = new FileInputStream(caCerts)) {60ks = KeyStore.getInstance(KeyStore.getDefaultType());61ks.load(in, null);62}63for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {64String alias = (String)e.nextElement();65if (ks.isCertificateEntry(alias)) {66System.out.println("* Testing " + alias + "...");67X509Certificate cert = (X509Certificate)ks.getCertificate(alias);68PublicKey key = cert.getPublicKey();69String alg = key.getAlgorithm();70if (alg.equals("RSA")) {71System.out.println("Signature algorithm: " + cert.getSigAlgName());72cert.verify(key, PROVIDER);73} else {74System.out.println("Skipping cert with key: " + alg);75}76} else {77System.out.println("Skipping alias " + alias);78}79}80long stop = System.currentTimeMillis();81System.out.println("All tests passed (" + (stop - start) + " ms).");82} finally {83Security.removeProvider(p.getName());84}85}86}878889