2
|
1 |
/*
|
|
2 |
* Copyright 2001 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 4519462
|
|
27 |
* @summary Verify Sun CertPathBuilder implementation handles certificates with no extensions
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.security.cert.X509Certificate;
|
|
31 |
import java.security.cert.TrustAnchor;
|
|
32 |
import java.security.cert.CollectionCertStoreParameters;
|
|
33 |
import java.security.cert.CertStore;
|
|
34 |
import java.security.cert.X509CertSelector;
|
|
35 |
import java.security.cert.CertPathBuilder;
|
|
36 |
import java.security.cert.PKIXBuilderParameters;
|
|
37 |
import java.security.cert.CertPathBuilderResult;
|
|
38 |
import java.security.cert.CertificateFactory;
|
|
39 |
import java.security.cert.CRL;
|
|
40 |
import java.security.cert.CertPath;
|
|
41 |
import java.util.HashSet;
|
|
42 |
import java.util.ArrayList;
|
|
43 |
import java.io.ByteArrayInputStream;
|
|
44 |
|
|
45 |
// Test based on user code submitted with bug by daniel.boggs@compass.net
|
|
46 |
public class NoExtensions {
|
|
47 |
|
|
48 |
public static void main(String[] args) {
|
|
49 |
try {
|
|
50 |
NoExtensions certs = new NoExtensions();
|
|
51 |
|
|
52 |
// the first certificate has the Authority Key Identifier extension
|
|
53 |
certs.doBuild(getUserCertificate1());
|
|
54 |
System.out.println("successfully built path for the first certificate");
|
|
55 |
|
|
56 |
// the second certificate does not have the Authority Key Identifier extension
|
|
57 |
// this will not succeed
|
|
58 |
certs.doBuild(getUserCertificate2());
|
|
59 |
System.out.println("successfully built path for the second certificate");
|
|
60 |
} catch (Exception ex) {
|
|
61 |
ex.printStackTrace();
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
private void doBuild(X509Certificate userCert) throws Exception {
|
|
66 |
// get the set of trusted CA certificates (only one in this instance)
|
|
67 |
HashSet trustAnchors = new HashSet();
|
|
68 |
X509Certificate trustedCert = getTrustedCertificate();
|
|
69 |
trustAnchors.add(new TrustAnchor(trustedCert, null));
|
|
70 |
|
|
71 |
// put together a CertStore (repository of the certificates and CRLs)
|
|
72 |
ArrayList certs = new ArrayList();
|
|
73 |
certs.add(trustedCert);
|
|
74 |
certs.add(userCert);
|
|
75 |
CollectionCertStoreParameters certStoreParams = new CollectionCertStoreParameters(certs);
|
|
76 |
CertStore certStore = CertStore.getInstance("Collection", certStoreParams);
|
|
77 |
|
|
78 |
// specify the target certificate via a CertSelector
|
|
79 |
X509CertSelector certSelector = new X509CertSelector();
|
|
80 |
certSelector.setCertificate(userCert);
|
|
81 |
certSelector.setSubject(userCert.getSubjectDN().getName()); // seems to be required
|
|
82 |
|
|
83 |
// build a valid cerificate path
|
|
84 |
CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", "SUN");
|
|
85 |
PKIXBuilderParameters certPathBuilderParams = new PKIXBuilderParameters(trustAnchors, certSelector);
|
|
86 |
certPathBuilderParams.addCertStore(certStore);
|
|
87 |
certPathBuilderParams.setRevocationEnabled(false);
|
|
88 |
CertPathBuilderResult result = certPathBuilder.build(certPathBuilderParams);
|
|
89 |
|
|
90 |
// get and show cert path
|
|
91 |
CertPath certPath = result.getCertPath();
|
|
92 |
// System.out.println(certPath.toString());
|
|
93 |
}
|
|
94 |
|
|
95 |
private static X509Certificate getTrustedCertificate() throws Exception {
|
|
96 |
String sCert =
|
|
97 |
"-----BEGIN CERTIFICATE-----\n"
|
|
98 |
+ "MIIBezCCASWgAwIBAgIQyWD8dLUoqpJFyDxrfRlrsTANBgkqhkiG9w0BAQQFADAW\n"
|
|
99 |
+ "MRQwEgYDVQQDEwtSb290IEFnZW5jeTAeFw0wMTEwMTkxMjU5MjZaFw0zOTEyMzEy\n"
|
|
100 |
+ "MzU5NTlaMBoxGDAWBgNVBAMTD1Jvb3RDZXJ0aWZpY2F0ZTBcMA0GCSqGSIb3DQEB\n"
|
|
101 |
+ "AQUAA0sAMEgCQQC+NFKszPjatUZKWmyWaFjir1wB93FX2u5SL+GMjgUsMs1JcTKQ\n"
|
|
102 |
+ "Kh0cnnQKknNkV4cTW4NPn31YCoB1+0KA3mknAgMBAAGjSzBJMEcGA1UdAQRAMD6A\n"
|
|
103 |
+ "EBLkCS0GHR1PAI1hIdwWZGOhGDAWMRQwEgYDVQQDEwtSb290IEFnZW5jeYIQBjds\n"
|
|
104 |
+ "AKoAZIoRz7jUqlw19DANBgkqhkiG9w0BAQQFAANBACJxAfP57yqaT9N+nRgAOugM\n"
|
|
105 |
+ "JG0aN3/peCIvL3p29epRL2xoWFvxpUUlsH2I39OZ6b8+twWCebhkv1I62segXAk=\n"
|
|
106 |
+ "-----END CERTIFICATE-----";
|
|
107 |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
|
108 |
ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
|
|
109 |
return (X509Certificate)certFactory.generateCertificate(bytes);
|
|
110 |
}
|
|
111 |
|
|
112 |
private static X509Certificate getUserCertificate1() throws Exception {
|
|
113 |
// this certificate includes an extension
|
|
114 |
String sCert =
|
|
115 |
"-----BEGIN CERTIFICATE-----\n"
|
|
116 |
+ "MIIBfzCCASmgAwIBAgIQWFSKzCWO2ptOAc2F3MKZSzANBgkqhkiG9w0BAQQFADAa\n"
|
|
117 |
+ "MRgwFgYDVQQDEw9Sb290Q2VydGlmaWNhdGUwHhcNMDExMDE5MTMwNzQxWhcNMzkx\n"
|
|
118 |
+ "MjMxMjM1OTU5WjAaMRgwFgYDVQQDEw9Vc2VyQ2VydGlmaWNhdGUwXDANBgkqhkiG\n"
|
|
119 |
+ "9w0BAQEFAANLADBIAkEA24gypa2YFGZHKznEWWbqIWNVXCM35W7RwJwhGpNsuBCj\n"
|
|
120 |
+ "NT6KEo66F+OOMgZmb0KrEZHBJASJ3n4Cqbt4aHm/2wIDAQABo0swSTBHBgNVHQEE\n"
|
|
121 |
+ "QDA+gBBch+eYzOPgVRbMq5vGpVWooRgwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3mC\n"
|
|
122 |
+ "EMlg/HS1KKqSRcg8a30Za7EwDQYJKoZIhvcNAQEEBQADQQCYBIHBqQQJePi5Hzfo\n"
|
|
123 |
+ "CxeUaYlXmvbxVNkxM65Pplsj3h4ntfZaynmlhahH3YsnnA8wk6xPt04LjSId12RB\n"
|
|
124 |
+ "PeuO\n"
|
|
125 |
+ "-----END CERTIFICATE-----";
|
|
126 |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
|
127 |
ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
|
|
128 |
return (X509Certificate)certFactory.generateCertificate(bytes);
|
|
129 |
}
|
|
130 |
|
|
131 |
private static X509Certificate getUserCertificate2() throws Exception {
|
|
132 |
// this certificate does not include any extensions
|
|
133 |
String sCert =
|
|
134 |
"-----BEGIN CERTIFICATE-----\n"
|
|
135 |
+ "MIIBMjCB3aADAgECAhB6225ckZVssEukPuvk1U1PMA0GCSqGSIb3DQEBBAUAMBox\n"
|
|
136 |
+ "GDAWBgNVBAMTD1Jvb3RDZXJ0aWZpY2F0ZTAeFw0wMTEwMTkxNjA5NTZaFw0wMjEw\n"
|
|
137 |
+ "MTkyMjA5NTZaMBsxGTAXBgNVBAMTEFVzZXJDZXJ0aWZpY2F0ZTIwXDANBgkqhkiG\n"
|
|
138 |
+ "9w0BAQEFAANLADBIAkEAzicGiW9aUlUoQIZnLy1l8MMV5OvA+4VJ4T/xo/PpN8Oq\n"
|
|
139 |
+ "WgZVGKeEp6JCzMlXEJk3TGLfpXL4Ytw+Ldhv0QPhLwIDAnMpMA0GCSqGSIb3DQEB\n"
|
|
140 |
+ "BAUAA0EAQmj9SFHEx66JyAps3ew4pcSS3QvfVZ/6qsNUYCG75rFGcTUPHcXKql9y\n"
|
|
141 |
+ "qBT83iNLJ//krjw5Ju0WRPg/buHSww==\n"
|
|
142 |
+ "-----END CERTIFICATE-----";
|
|
143 |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
|
144 |
ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
|
|
145 |
return (X509Certificate)certFactory.generateCertificate(bytes);
|
|
146 |
}
|
|
147 |
}
|