Path: blob/master/test/jdk/java/security/cert/CertPathValidator/OCSP/AIACheck.java
41161 views
/*1* Copyright (c) 2004, 2015, 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// Security properties, once set, cannot revert to unset. To avoid25// conflicts with tests running in the same VM isolate this test by26// running it in otherVM mode.27//2829/**30* @test31* @bug 507295332* @summary Verify that the URL for an OCSP responder can be extracted from a33* certificate's AuthorityInfoAccess extension when OCSP certifiate34* validation has been enabled.35* @run main/othervm AIACheck36*/3738import java.io.*;39import java.net.*;40import java.util.*;41import java.security.Security;42import java.security.cert.*;4344public class AIACheck {4546private final static File baseDir =47new File(System.getProperty("test.src", "."));4849private static X509Certificate loadCertificate(String name)50throws Exception51{52File certFile = new File(baseDir, name);53InputStream in = new FileInputStream(certFile);54CertificateFactory cf = CertificateFactory.getInstance("X.509");55X509Certificate cert = (X509Certificate)cf.generateCertificate(in);56return cert;57}5859public static void main(String args[]) throws Exception {60// MD5 is used in this test case, don't disable MD5 algorithm.61Security.setProperty(62"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");6364X509Certificate aiaCert = loadCertificate("AIACert.pem");65X509Certificate rootCert = loadCertificate("RootCert.pem");6667List<X509Certificate> list =68//Arrays.asList(new X509Certificate[] {aiaCert, rootCert});69Arrays.asList(new X509Certificate[] {aiaCert});70CertificateFactory cf = CertificateFactory.getInstance("X.509");71CertPath path = cf.generateCertPath(list);7273TrustAnchor anchor = new TrustAnchor(rootCert, null);74Set<TrustAnchor> anchors = Collections.singleton(anchor);7576PKIXParameters params = new PKIXParameters(anchors);77// Activate certificate revocation checking78params.setRevocationEnabled(true);7980// Activate OCSP81Security.setProperty("ocsp.enable", "true");8283// Ensure that the ocsp.responderURL property is not set.84if (Security.getProperty("ocsp.responderURL") != null) {85throw new86Exception("The ocsp.responderURL property must not be set");87}8889CertPathValidator validator = CertPathValidator.getInstance("PKIX");9091try {92validator.validate(path, params);93throw new Exception("Successfully validated an invalid path");9495} catch (CertPathValidatorException e ) {96Throwable rootCause = e.getCause();97if (!(rootCause instanceof SocketException ||98rootCause instanceof SocketTimeoutException)) {99throw e;100}101102// Success - client located OCSP responder in AIA extension103// and attempted to connect.104System.out.println("Extracted the URL of the OCSP responder from " +105"the certificate's AuthorityInfoAccess extension.");106}107}108}109110111