2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4458951
|
|
27 |
* @summary Check that Sun's PKIX implementation of
|
|
28 |
* CertPathValidator.validate() and CertPathBuilder.build() throw an
|
|
29 |
* InvalidAlgorithmParameterException if any of the TrustAnchors specified
|
|
30 |
* contain nameConstraints
|
|
31 |
*/
|
|
32 |
import java.io.File;
|
|
33 |
import java.io.FileInputStream;
|
|
34 |
import java.io.IOException;
|
|
35 |
|
|
36 |
import java.security.InvalidAlgorithmParameterException;
|
|
37 |
import java.security.cert.CertificateFactory;
|
|
38 |
import java.security.cert.CertPath;
|
|
39 |
import java.security.cert.CertPathBuilder;
|
|
40 |
import java.security.cert.CertPathBuilderResult;
|
|
41 |
import java.security.cert.CertPathValidator;
|
|
42 |
import java.security.cert.CertPathValidatorResult;
|
|
43 |
import java.security.cert.PKIXParameters;
|
|
44 |
import java.security.cert.PKIXBuilderParameters;
|
|
45 |
import java.security.cert.TrustAnchor;
|
|
46 |
import java.security.cert.X509Certificate;
|
|
47 |
import java.security.cert.X509CertSelector;
|
|
48 |
|
|
49 |
import java.util.ArrayList;
|
|
50 |
import java.util.Collections;
|
|
51 |
import java.util.List;
|
|
52 |
import java.util.Set;
|
|
53 |
|
|
54 |
import sun.security.util.DerInputStream;
|
|
55 |
|
|
56 |
/**
|
|
57 |
* ValidateNC performs a validation and build of a certification path, using any
|
|
58 |
* name constraints provided in the trust anchor's certificate. Using
|
|
59 |
* Sun's provider, the validation and build should fail because name constraints
|
|
60 |
* on trust anchors are not supported.
|
|
61 |
*
|
|
62 |
* @author Steve Hanna
|
|
63 |
* @author Sean Mullan
|
|
64 |
*/
|
|
65 |
public final class ValidateNC {
|
|
66 |
|
|
67 |
private static CertPath path;
|
|
68 |
private static PKIXParameters params;
|
|
69 |
private static Set anchors;
|
|
70 |
|
|
71 |
public static void main(String[] args) throws Exception {
|
|
72 |
|
|
73 |
String[] certs = { "sun2labs2.cer", "labs2isrg2.cer" };
|
|
74 |
|
|
75 |
createPath(certs);
|
|
76 |
try {
|
|
77 |
validate(path, params);
|
|
78 |
throw new Exception("CertPathValidator should have thrown an " +
|
|
79 |
"InvalidAlgorithmParameterException");
|
|
80 |
} catch (InvalidAlgorithmParameterException iape) {
|
|
81 |
// success!
|
|
82 |
}
|
|
83 |
|
|
84 |
try {
|
|
85 |
X509CertSelector sel = new X509CertSelector();
|
|
86 |
sel.setSubject("cn=sean");
|
|
87 |
PKIXBuilderParameters bparams =
|
|
88 |
new PKIXBuilderParameters(anchors, sel);
|
|
89 |
build(bparams);
|
|
90 |
throw new Exception("CertPathBuilder should have thrown an " +
|
|
91 |
"InvalidAlgorithmParameterException");
|
|
92 |
} catch (InvalidAlgorithmParameterException iape) {
|
|
93 |
// success!
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
public static void createPath(String[] certs) throws Exception {
|
|
98 |
|
|
99 |
X509Certificate anchorCert = getCertFromFile(certs[0]);
|
|
100 |
byte [] nameConstraints = anchorCert.getExtensionValue("2.5.29.30");
|
|
101 |
if (nameConstraints != null) {
|
|
102 |
DerInputStream in = new DerInputStream(nameConstraints);
|
|
103 |
nameConstraints = in.getOctetString();
|
|
104 |
}
|
|
105 |
TrustAnchor anchor = new TrustAnchor(anchorCert, nameConstraints);
|
|
106 |
List list = new ArrayList();
|
|
107 |
for (int i = 1; i < certs.length; i++) {
|
|
108 |
list.add(0, getCertFromFile(certs[i]));
|
|
109 |
}
|
|
110 |
CertificateFactory cf = CertificateFactory.getInstance("X509");
|
|
111 |
path = cf.generateCertPath(list);
|
|
112 |
|
|
113 |
anchors = Collections.singleton(anchor);
|
|
114 |
params = new PKIXParameters(anchors);
|
|
115 |
params.setRevocationEnabled(false);
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Get a DER-encoded X.509 certificate from a file.
|
|
120 |
*
|
|
121 |
* @param certFilePath path to file containing DER-encoded certificate
|
|
122 |
* @return X509Certificate
|
|
123 |
* @throws IOException on error
|
|
124 |
*/
|
|
125 |
public static X509Certificate getCertFromFile(String certFilePath)
|
|
126 |
throws IOException {
|
|
127 |
X509Certificate cert = null;
|
|
128 |
try {
|
|
129 |
File certFile = new File(System.getProperty("test.src", "."),
|
|
130 |
certFilePath);
|
|
131 |
FileInputStream certFileInputStream =
|
|
132 |
new FileInputStream(certFile);
|
|
133 |
CertificateFactory cf = CertificateFactory.getInstance("X509");
|
|
134 |
cert = (X509Certificate)
|
|
135 |
cf.generateCertificate(certFileInputStream);
|
|
136 |
} catch (Exception e) {
|
|
137 |
e.printStackTrace();
|
|
138 |
throw new IOException("Can't construct X509Certificate: " +
|
|
139 |
e.getMessage());
|
|
140 |
}
|
|
141 |
return cert;
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Perform a PKIX validation.
|
|
146 |
*
|
|
147 |
* @param path CertPath to validate
|
|
148 |
* @param params PKIXParameters to use in validation
|
|
149 |
* @throws Exception on error
|
|
150 |
*/
|
|
151 |
public static void validate(CertPath path, PKIXParameters params)
|
|
152 |
throws Exception {
|
|
153 |
CertPathValidator validator =
|
|
154 |
CertPathValidator.getInstance("PKIX", "SUN");
|
|
155 |
CertPathValidatorResult cpvr = validator.validate(path, params);
|
|
156 |
}
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Perform a PKIX build.
|
|
160 |
*
|
|
161 |
* @param params PKIXBuilderParameters to use in the build
|
|
162 |
* @throws Exception on error
|
|
163 |
*/
|
|
164 |
public static void build(PKIXBuilderParameters params)
|
|
165 |
throws Exception {
|
|
166 |
CertPathBuilder builder =
|
|
167 |
CertPathBuilder.getInstance("PKIX", "SUN");
|
|
168 |
CertPathBuilderResult cpbr = builder.build(params);
|
|
169 |
}
|
|
170 |
}
|