12860
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package sun.security.provider.certpath;
|
|
26 |
|
|
27 |
import java.security.InvalidAlgorithmParameterException;
|
|
28 |
import java.security.KeyStore;
|
|
29 |
import java.security.cert.*;
|
|
30 |
import java.util.*;
|
|
31 |
import javax.security.auth.x500.X500Principal;
|
|
32 |
|
|
33 |
import sun.security.util.Debug;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Common utility methods and classes used by the PKIX CertPathValidator and
|
|
37 |
* CertPathBuilder implementation.
|
|
38 |
*/
|
|
39 |
class PKIX {
|
|
40 |
|
|
41 |
private static final Debug debug = Debug.getInstance("certpath");
|
|
42 |
|
|
43 |
private PKIX() { }
|
|
44 |
|
|
45 |
static ValidatorParams checkParams(CertPath cp, CertPathParameters params)
|
|
46 |
throws InvalidAlgorithmParameterException
|
|
47 |
{
|
|
48 |
if (!(params instanceof PKIXParameters)) {
|
|
49 |
throw new InvalidAlgorithmParameterException("inappropriate "
|
|
50 |
+ "params, must be an instance of PKIXParameters");
|
|
51 |
}
|
|
52 |
return new ValidatorParams(cp, (PKIXParameters)params);
|
|
53 |
}
|
|
54 |
|
|
55 |
static BuilderParams checkBuilderParams(CertPathParameters params)
|
|
56 |
throws InvalidAlgorithmParameterException
|
|
57 |
{
|
|
58 |
if (!(params instanceof PKIXBuilderParameters)) {
|
|
59 |
throw new InvalidAlgorithmParameterException("inappropriate "
|
|
60 |
+ "params, must be an instance of PKIXBuilderParameters");
|
|
61 |
}
|
|
62 |
return new BuilderParams((PKIXBuilderParameters)params);
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* PKIXParameters that are shared by the PKIX CertPathValidator
|
|
67 |
* implementation. Provides additional functionality and avoids
|
|
68 |
* unnecessary cloning.
|
|
69 |
*/
|
|
70 |
static class ValidatorParams {
|
|
71 |
private final PKIXParameters params;
|
|
72 |
private CertPath certPath;
|
|
73 |
private List<PKIXCertPathChecker> checkers;
|
|
74 |
private List<CertStore> stores;
|
|
75 |
private boolean gotDate;
|
|
76 |
private Date date;
|
|
77 |
private Set<String> policies;
|
|
78 |
private boolean gotConstraints;
|
|
79 |
private CertSelector constraints;
|
|
80 |
private Set<TrustAnchor> anchors;
|
|
81 |
private List<X509Certificate> certs;
|
|
82 |
|
|
83 |
ValidatorParams(CertPath cp, PKIXParameters params)
|
|
84 |
throws InvalidAlgorithmParameterException
|
|
85 |
{
|
|
86 |
this(params);
|
|
87 |
if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) {
|
|
88 |
throw new InvalidAlgorithmParameterException("inappropriate "
|
|
89 |
+ "CertPath type specified, must be X.509 or X509");
|
|
90 |
}
|
|
91 |
this.certPath = cp;
|
|
92 |
}
|
|
93 |
|
|
94 |
ValidatorParams(PKIXParameters params)
|
|
95 |
throws InvalidAlgorithmParameterException
|
|
96 |
{
|
|
97 |
this.anchors = params.getTrustAnchors();
|
|
98 |
// Make sure that none of the trust anchors include name constraints
|
|
99 |
// (not supported).
|
|
100 |
for (TrustAnchor anchor : this.anchors) {
|
|
101 |
if (anchor.getNameConstraints() != null) {
|
|
102 |
throw new InvalidAlgorithmParameterException
|
|
103 |
("name constraints in trust anchor not supported");
|
|
104 |
}
|
|
105 |
}
|
|
106 |
this.params = params;
|
|
107 |
}
|
|
108 |
|
|
109 |
CertPath certPath() {
|
|
110 |
return certPath;
|
|
111 |
}
|
|
112 |
// called by CertPathBuilder after path has been built
|
|
113 |
void setCertPath(CertPath cp) {
|
|
114 |
this.certPath = cp;
|
|
115 |
}
|
|
116 |
List<X509Certificate> certificates() {
|
|
117 |
if (certs == null) {
|
|
118 |
if (certPath == null) {
|
|
119 |
certs = Collections.emptyList();
|
|
120 |
} else {
|
|
121 |
// Reverse the ordering for validation so that the target
|
|
122 |
// cert is the last certificate
|
|
123 |
@SuppressWarnings("unchecked")
|
|
124 |
List<X509Certificate> xc = new ArrayList<>
|
|
125 |
((List<X509Certificate>)certPath.getCertificates());
|
|
126 |
Collections.reverse(xc);
|
|
127 |
certs = xc;
|
|
128 |
}
|
|
129 |
}
|
|
130 |
return certs;
|
|
131 |
}
|
|
132 |
List<PKIXCertPathChecker> certPathCheckers() {
|
|
133 |
if (checkers == null)
|
|
134 |
checkers = params.getCertPathCheckers();
|
|
135 |
return checkers;
|
|
136 |
}
|
|
137 |
List<CertStore> certStores() {
|
|
138 |
if (stores == null)
|
|
139 |
stores = params.getCertStores();
|
|
140 |
return stores;
|
|
141 |
}
|
|
142 |
Date date() {
|
|
143 |
if (!gotDate) {
|
|
144 |
date = params.getDate();
|
|
145 |
if (date == null)
|
|
146 |
date = new Date();
|
|
147 |
gotDate = true;
|
|
148 |
}
|
|
149 |
return date;
|
|
150 |
}
|
|
151 |
Set<String> initialPolicies() {
|
|
152 |
if (policies == null)
|
|
153 |
policies = params.getInitialPolicies();
|
|
154 |
return policies;
|
|
155 |
}
|
|
156 |
CertSelector targetCertConstraints() {
|
|
157 |
if (!gotConstraints) {
|
|
158 |
constraints = params.getTargetCertConstraints();
|
|
159 |
gotConstraints = true;
|
|
160 |
}
|
|
161 |
return constraints;
|
|
162 |
}
|
|
163 |
Set<TrustAnchor> trustAnchors() {
|
|
164 |
return anchors;
|
|
165 |
}
|
|
166 |
boolean revocationEnabled() {
|
|
167 |
return params.isRevocationEnabled();
|
|
168 |
}
|
|
169 |
boolean policyMappingInhibited() {
|
|
170 |
return params.isPolicyMappingInhibited();
|
|
171 |
}
|
|
172 |
boolean explicitPolicyRequired() {
|
|
173 |
return params.isExplicitPolicyRequired();
|
|
174 |
}
|
|
175 |
boolean policyQualifiersRejected() {
|
|
176 |
return params.getPolicyQualifiersRejected();
|
|
177 |
}
|
|
178 |
String sigProvider() { return params.getSigProvider(); }
|
|
179 |
boolean anyPolicyInhibited() { return params.isAnyPolicyInhibited(); }
|
|
180 |
|
|
181 |
// in rare cases we need access to the original params, for example
|
|
182 |
// in order to clone CertPathCheckers before building a new chain
|
|
183 |
PKIXParameters getPKIXParameters() {
|
|
184 |
return params;
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
static class BuilderParams extends ValidatorParams {
|
|
189 |
private PKIXBuilderParameters params;
|
|
190 |
private boolean buildForward = true;
|
|
191 |
private List<CertStore> stores;
|
|
192 |
private X500Principal targetSubject;
|
|
193 |
|
|
194 |
BuilderParams(PKIXBuilderParameters params)
|
|
195 |
throws InvalidAlgorithmParameterException
|
|
196 |
{
|
|
197 |
super(params);
|
|
198 |
checkParams(params);
|
|
199 |
}
|
|
200 |
private void checkParams(PKIXBuilderParameters params)
|
|
201 |
throws InvalidAlgorithmParameterException
|
|
202 |
{
|
|
203 |
CertSelector sel = targetCertConstraints();
|
|
204 |
if (!(sel instanceof X509CertSelector)) {
|
|
205 |
throw new InvalidAlgorithmParameterException("the "
|
|
206 |
+ "targetCertConstraints parameter must be an "
|
|
207 |
+ "X509CertSelector");
|
|
208 |
}
|
|
209 |
if (params instanceof SunCertPathBuilderParameters) {
|
|
210 |
buildForward =
|
|
211 |
((SunCertPathBuilderParameters)params).getBuildForward();
|
|
212 |
}
|
|
213 |
this.params = params;
|
|
214 |
this.targetSubject = getTargetSubject(
|
|
215 |
certStores(), (X509CertSelector)targetCertConstraints());
|
|
216 |
}
|
|
217 |
@Override List<CertStore> certStores() {
|
|
218 |
if (stores == null) {
|
|
219 |
// reorder CertStores so that local CertStores are tried first
|
|
220 |
stores = new ArrayList<>(params.getCertStores());
|
|
221 |
Collections.sort(stores, new CertStoreComparator());
|
|
222 |
}
|
|
223 |
return stores;
|
|
224 |
}
|
|
225 |
int maxPathLength() { return params.getMaxPathLength(); }
|
|
226 |
boolean buildForward() { return buildForward; }
|
|
227 |
PKIXBuilderParameters params() { return params; }
|
|
228 |
X500Principal targetSubject() { return targetSubject; }
|
|
229 |
|
|
230 |
/**
|
|
231 |
* Returns the target subject DN from the first X509Certificate that
|
|
232 |
* is fetched that matches the specified X509CertSelector.
|
|
233 |
*/
|
|
234 |
private static X500Principal getTargetSubject(List<CertStore> stores,
|
|
235 |
X509CertSelector sel)
|
|
236 |
throws InvalidAlgorithmParameterException
|
|
237 |
{
|
|
238 |
X500Principal subject = sel.getSubject();
|
|
239 |
if (subject != null) {
|
|
240 |
return subject;
|
|
241 |
}
|
|
242 |
X509Certificate cert = sel.getCertificate();
|
|
243 |
if (cert != null) {
|
|
244 |
subject = cert.getSubjectX500Principal();
|
|
245 |
}
|
|
246 |
if (subject != null) {
|
|
247 |
return subject;
|
|
248 |
}
|
|
249 |
for (CertStore store : stores) {
|
|
250 |
try {
|
|
251 |
Collection<? extends Certificate> certs =
|
|
252 |
(Collection<? extends Certificate>)
|
|
253 |
store.getCertificates(sel);
|
|
254 |
if (!certs.isEmpty()) {
|
|
255 |
X509Certificate xc =
|
|
256 |
(X509Certificate)certs.iterator().next();
|
|
257 |
return xc.getSubjectX500Principal();
|
|
258 |
}
|
|
259 |
} catch (CertStoreException e) {
|
|
260 |
// ignore but log it
|
|
261 |
if (debug != null) {
|
|
262 |
debug.println("BuilderParams.getTargetSubjectDN: " +
|
|
263 |
"non-fatal exception retrieving certs: " + e);
|
|
264 |
e.printStackTrace();
|
|
265 |
}
|
|
266 |
}
|
|
267 |
}
|
|
268 |
throw new InvalidAlgorithmParameterException
|
|
269 |
("Could not determine unique target subject");
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Comparator that orders CertStores so that local CertStores come before
|
|
275 |
* remote CertStores.
|
|
276 |
*/
|
|
277 |
private static class CertStoreComparator implements Comparator<CertStore> {
|
|
278 |
@Override
|
|
279 |
public int compare(CertStore store1, CertStore store2) {
|
|
280 |
if (store1.getType().equals("Collection") ||
|
|
281 |
store1.getCertStoreParameters() instanceof
|
|
282 |
CollectionCertStoreParameters) {
|
|
283 |
return -1;
|
|
284 |
} else {
|
|
285 |
return 1;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
}
|
|
289 |
}
|