blob: c45cb75e7f0a299c005ce8d2dd68f94f431f28e7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 5072953
27 * @summary Verify that the URL for an OCSP responder can be extracted from a
28 * certificate's AuthorityInfoAccess extension when OCSP certifiate
29 * validation has been enabled.
30 */
31
32import java.io.*;
33import java.net.SocketException;
34import java.util.*;
35import java.security.Security;
36import java.security.cert.*;
37
38public class AIACheck {
39
40 private final static File baseDir =
41 new File(System.getProperty("test.src", "."));
42
43 private static X509Certificate loadCertificate(String name)
44 throws Exception
45 {
46 File certFile = new File(baseDir, name);
47 InputStream in = new FileInputStream(certFile);
48 CertificateFactory cf = CertificateFactory.getInstance("X.509");
49 X509Certificate cert = (X509Certificate)cf.generateCertificate(in);
50 return cert;
51 }
52
53 public static void main(String args[]) throws Exception {
54 X509Certificate aiaCert = loadCertificate("AIACert.pem");
55 X509Certificate rootCert = loadCertificate("RootCert.pem");
56
57 List<X509Certificate> list =
58 //Arrays.asList(new X509Certificate[] {aiaCert, rootCert});
59 Arrays.asList(new X509Certificate[] {aiaCert});
60 CertificateFactory cf = CertificateFactory.getInstance("X.509");
61 CertPath path = cf.generateCertPath(list);
62
63 TrustAnchor anchor = new TrustAnchor(rootCert, null);
64 Set<TrustAnchor> anchors = Collections.singleton(anchor);
65
66 PKIXParameters params = new PKIXParameters(anchors);
67 // Activate certificate revocation checking
68 params.setRevocationEnabled(true);
69
70 // Activate OCSP
71 Security.setProperty("ocsp.enable", "true");
72
73 // Ensure that the ocsp.responderURL property is not set.
74 if (Security.getProperty("ocsp.responderURL") != null) {
75 throw new
76 Exception("The ocsp.responderURL property must not be set");
77 }
78
79 CertPathValidator validator = CertPathValidator.getInstance("PKIX");
80
81 try {
82 validator.validate(path, params);
83 throw new Exception("Successfully validated an invalid path");
84
85 } catch (CertPathValidatorException e ) {
86 if (! (e.getCause() instanceof SocketException)) {
87 throw e;
88 }
89
90 // Success - client located OCSP responder in AIA extension
91 // and attempted to connect.
92 System.out.println("Extracted the URL of the OCSP responder from " +
93 "the certificate's AuthorityInfoAccess extension.");
94 }
95 }
96}