author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
child 57950 | 4612a3cfb927 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 2004, 2018, 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.lang.ref.*; |
|
50768 | 29 |
import java.net.Socket; |
30 |
import java.security.AlgorithmConstraints; |
|
31 |
import java.security.KeyStore; |
|
32 |
import java.security.KeyStore.Builder; |
|
33 |
import java.security.KeyStore.Entry; |
|
34 |
import java.security.KeyStore.PrivateKeyEntry; |
|
35 |
import java.security.Principal; |
|
36 |
import java.security.PrivateKey; |
|
37 |
import java.security.cert.CertPathValidatorException; |
|
38 |
import java.security.cert.Certificate; |
|
39 |
import java.security.cert.CertificateException; |
|
40 |
import java.security.cert.X509Certificate; |
|
2 | 41 |
import java.util.*; |
42 |
import java.util.concurrent.atomic.AtomicLong; |
|
43 |
import javax.net.ssl.*; |
|
7043 | 44 |
import sun.security.provider.certpath.AlgorithmChecker; |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
45 |
import sun.security.validator.Validator; |
7043 | 46 |
|
2 | 47 |
/** |
48 |
* The new X509 key manager implementation. The main differences to the |
|
49 |
* old SunX509 key manager are: |
|
50 |
* . it is based around the KeyStore.Builder API. This allows it to use |
|
51 |
* other forms of KeyStore protection or password input (e.g. a |
|
52 |
* CallbackHandler) or to have keys within one KeyStore protected by |
|
53 |
* different keys. |
|
54 |
* . it can use multiple KeyStores at the same time. |
|
21278 | 55 |
* . it is explicitly designed to accommodate KeyStores that change over |
2 | 56 |
* the lifetime of the process. |
57 |
* . it makes an effort to choose the key that matches best, i.e. one that |
|
58 |
* is not expired and has the appropriate certificate extensions. |
|
59 |
* |
|
60 |
* Note that this code is not explicitly performance optimzied yet. |
|
61 |
* |
|
62 |
* @author Andreas Sterbenz |
|
63 |
*/ |
|
64 |
final class X509KeyManagerImpl extends X509ExtendedKeyManager |
|
65 |
implements X509KeyManager { |
|
66 |
||
67 |
// for unit testing only, set via privileged reflection |
|
68 |
private static Date verificationDate; |
|
69 |
||
70 |
// list of the builders |
|
71 |
private final List<Builder> builders; |
|
72 |
||
73 |
// counter to generate unique ids for the aliases |
|
74 |
private final AtomicLong uidCounter; |
|
75 |
||
76 |
// cached entries |
|
77 |
private final Map<String,Reference<PrivateKeyEntry>> entryCacheMap; |
|
78 |
||
79 |
X509KeyManagerImpl(Builder builder) { |
|
80 |
this(Collections.singletonList(builder)); |
|
81 |
} |
|
82 |
||
83 |
X509KeyManagerImpl(List<Builder> builders) { |
|
84 |
this.builders = builders; |
|
85 |
uidCounter = new AtomicLong(); |
|
86 |
entryCacheMap = Collections.synchronizedMap |
|
87 |
(new SizedMap<String,Reference<PrivateKeyEntry>>()); |
|
88 |
} |
|
89 |
||
90 |
// LinkedHashMap with a max size of 10 |
|
91 |
// see LinkedHashMap JavaDocs |
|
92 |
private static class SizedMap<K,V> extends LinkedHashMap<K,V> { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
93 |
private static final long serialVersionUID = -8211222668790986062L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
94 |
|
2 | 95 |
@Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { |
96 |
return size() > 10; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
// |
|
101 |
// public methods |
|
102 |
// |
|
103 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
104 |
@Override |
2 | 105 |
public X509Certificate[] getCertificateChain(String alias) { |
106 |
PrivateKeyEntry entry = getEntry(alias); |
|
107 |
return entry == null ? null : |
|
108 |
(X509Certificate[])entry.getCertificateChain(); |
|
109 |
} |
|
110 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
111 |
@Override |
2 | 112 |
public PrivateKey getPrivateKey(String alias) { |
113 |
PrivateKeyEntry entry = getEntry(alias); |
|
114 |
return entry == null ? null : entry.getPrivateKey(); |
|
115 |
} |
|
116 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
117 |
@Override |
2 | 118 |
public String chooseClientAlias(String[] keyTypes, Principal[] issuers, |
119 |
Socket socket) { |
|
7043 | 120 |
return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT, |
121 |
getAlgorithmConstraints(socket)); |
|
2 | 122 |
} |
123 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
124 |
@Override |
2 | 125 |
public String chooseEngineClientAlias(String[] keyTypes, |
126 |
Principal[] issuers, SSLEngine engine) { |
|
7043 | 127 |
return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT, |
128 |
getAlgorithmConstraints(engine)); |
|
2 | 129 |
} |
130 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
131 |
@Override |
2 | 132 |
public String chooseServerAlias(String keyType, |
133 |
Principal[] issuers, Socket socket) { |
|
7043 | 134 |
return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
135 |
getAlgorithmConstraints(socket), |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
136 |
X509TrustManagerImpl.getRequestedServerNames(socket), |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
137 |
"HTTPS"); // The SNI HostName is a fully qualified domain name. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
138 |
// The certificate selection scheme for SNI HostName |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
139 |
// is similar to HTTPS endpoint identification scheme |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
140 |
// implemented in this provider. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
141 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
142 |
// Using HTTPS endpoint identification scheme to guide |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
143 |
// the selection of an appropriate authentication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
144 |
// certificate according to requested SNI extension. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
145 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
146 |
// It is not a really HTTPS endpoint identification. |
2 | 147 |
} |
148 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
149 |
@Override |
2 | 150 |
public String chooseEngineServerAlias(String keyType, |
151 |
Principal[] issuers, SSLEngine engine) { |
|
7043 | 152 |
return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
153 |
getAlgorithmConstraints(engine), |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
154 |
X509TrustManagerImpl.getRequestedServerNames(engine), |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
155 |
"HTTPS"); // The SNI HostName is a fully qualified domain name. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
156 |
// The certificate selection scheme for SNI HostName |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
157 |
// is similar to HTTPS endpoint identification scheme |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
158 |
// implemented in this provider. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
159 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
160 |
// Using HTTPS endpoint identification scheme to guide |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
161 |
// the selection of an appropriate authentication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
162 |
// certificate according to requested SNI extension. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
163 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
164 |
// It is not a really HTTPS endpoint identification. |
2 | 165 |
} |
166 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
167 |
@Override |
2 | 168 |
public String[] getClientAliases(String keyType, Principal[] issuers) { |
7043 | 169 |
return getAliases(keyType, issuers, CheckType.CLIENT, null); |
2 | 170 |
} |
171 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
172 |
@Override |
2 | 173 |
public String[] getServerAliases(String keyType, Principal[] issuers) { |
7043 | 174 |
return getAliases(keyType, issuers, CheckType.SERVER, null); |
2 | 175 |
} |
176 |
||
177 |
// |
|
178 |
// implementation private methods |
|
179 |
// |
|
180 |
||
7043 | 181 |
// Gets algorithm constraints of the socket. |
182 |
private AlgorithmConstraints getAlgorithmConstraints(Socket socket) { |
|
183 |
if (socket != null && socket.isConnected() && |
|
184 |
socket instanceof SSLSocket) { |
|
185 |
||
186 |
SSLSocket sslSocket = (SSLSocket)socket; |
|
187 |
SSLSession session = sslSocket.getHandshakeSession(); |
|
188 |
||
189 |
if (session != null) { |
|
50768 | 190 |
if (ProtocolVersion.useTLS12PlusSpec(session.getProtocol())) { |
7043 | 191 |
String[] peerSupportedSignAlgs = null; |
192 |
||
193 |
if (session instanceof ExtendedSSLSession) { |
|
194 |
ExtendedSSLSession extSession = |
|
195 |
(ExtendedSSLSession)session; |
|
196 |
peerSupportedSignAlgs = |
|
197 |
extSession.getPeerSupportedSignatureAlgorithms(); |
|
198 |
} |
|
199 |
||
200 |
return new SSLAlgorithmConstraints( |
|
201 |
sslSocket, peerSupportedSignAlgs, true); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
return new SSLAlgorithmConstraints(sslSocket, true); |
|
206 |
} |
|
207 |
||
208 |
return new SSLAlgorithmConstraints((SSLSocket)null, true); |
|
209 |
} |
|
210 |
||
211 |
// Gets algorithm constraints of the engine. |
|
212 |
private AlgorithmConstraints getAlgorithmConstraints(SSLEngine engine) { |
|
213 |
if (engine != null) { |
|
214 |
SSLSession session = engine.getHandshakeSession(); |
|
215 |
if (session != null) { |
|
50768 | 216 |
if (ProtocolVersion.useTLS12PlusSpec(session.getProtocol())) { |
7043 | 217 |
String[] peerSupportedSignAlgs = null; |
218 |
||
219 |
if (session instanceof ExtendedSSLSession) { |
|
220 |
ExtendedSSLSession extSession = |
|
221 |
(ExtendedSSLSession)session; |
|
222 |
peerSupportedSignAlgs = |
|
223 |
extSession.getPeerSupportedSignatureAlgorithms(); |
|
224 |
} |
|
225 |
||
226 |
return new SSLAlgorithmConstraints( |
|
227 |
engine, peerSupportedSignAlgs, true); |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
return new SSLAlgorithmConstraints(engine, true); |
|
233 |
} |
|
234 |
||
2 | 235 |
// we construct the alias we return to JSSE as seen in the code below |
236 |
// a unique id is included to allow us to reliably cache entries |
|
237 |
// between the calls to getCertificateChain() and getPrivateKey() |
|
238 |
// even if tokens are inserted or removed |
|
239 |
private String makeAlias(EntryStatus entry) { |
|
240 |
return uidCounter.incrementAndGet() + "." + entry.builderIndex + "." |
|
241 |
+ entry.alias; |
|
242 |
} |
|
243 |
||
244 |
private PrivateKeyEntry getEntry(String alias) { |
|
245 |
// if the alias is null, return immediately |
|
246 |
if (alias == null) { |
|
247 |
return null; |
|
248 |
} |
|
249 |
||
250 |
// try to get the entry from cache |
|
251 |
Reference<PrivateKeyEntry> ref = entryCacheMap.get(alias); |
|
252 |
PrivateKeyEntry entry = (ref != null) ? ref.get() : null; |
|
253 |
if (entry != null) { |
|
254 |
return entry; |
|
255 |
} |
|
256 |
||
257 |
// parse the alias |
|
258 |
int firstDot = alias.indexOf('.'); |
|
259 |
int secondDot = alias.indexOf('.', firstDot + 1); |
|
260 |
if ((firstDot == -1) || (secondDot == firstDot)) { |
|
261 |
// invalid alias |
|
262 |
return null; |
|
263 |
} |
|
264 |
try { |
|
265 |
int builderIndex = Integer.parseInt |
|
266 |
(alias.substring(firstDot + 1, secondDot)); |
|
267 |
String keyStoreAlias = alias.substring(secondDot + 1); |
|
268 |
Builder builder = builders.get(builderIndex); |
|
269 |
KeyStore ks = builder.getKeyStore(); |
|
270 |
Entry newEntry = ks.getEntry |
|
271 |
(keyStoreAlias, builder.getProtectionParameter(alias)); |
|
272 |
if (newEntry instanceof PrivateKeyEntry == false) { |
|
273 |
// unexpected type of entry |
|
274 |
return null; |
|
275 |
} |
|
276 |
entry = (PrivateKeyEntry)newEntry; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
277 |
entryCacheMap.put(alias, new SoftReference<PrivateKeyEntry>(entry)); |
2 | 278 |
return entry; |
279 |
} catch (Exception e) { |
|
280 |
// ignore |
|
281 |
return null; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
// Class to help verify that the public key algorithm (and optionally |
|
286 |
// the signature algorithm) of a certificate matches what we need. |
|
287 |
private static class KeyType { |
|
288 |
||
289 |
final String keyAlgorithm; |
|
7043 | 290 |
|
291 |
// In TLS 1.2, the signature algorithm has been obsoleted by the |
|
292 |
// supported_signature_algorithms, and the certificate type no longer |
|
293 |
// restricts the algorithm used to sign the certificate. |
|
50768 | 294 |
// |
7043 | 295 |
// However, because we don't support certificate type checking other |
296 |
// than rsa_sign, dss_sign and ecdsa_sign, we don't have to check the |
|
297 |
// protocol version here. |
|
2 | 298 |
final String sigKeyAlgorithm; |
299 |
||
300 |
KeyType(String algorithm) { |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
301 |
int k = algorithm.indexOf('_'); |
2 | 302 |
if (k == -1) { |
303 |
keyAlgorithm = algorithm; |
|
304 |
sigKeyAlgorithm = null; |
|
305 |
} else { |
|
306 |
keyAlgorithm = algorithm.substring(0, k); |
|
307 |
sigKeyAlgorithm = algorithm.substring(k + 1); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
boolean matches(Certificate[] chain) { |
|
312 |
if (!chain[0].getPublicKey().getAlgorithm().equals(keyAlgorithm)) { |
|
313 |
return false; |
|
314 |
} |
|
315 |
if (sigKeyAlgorithm == null) { |
|
316 |
return true; |
|
317 |
} |
|
318 |
if (chain.length > 1) { |
|
319 |
// if possible, check the public key in the issuer cert |
|
7043 | 320 |
return sigKeyAlgorithm.equals( |
321 |
chain[1].getPublicKey().getAlgorithm()); |
|
2 | 322 |
} else { |
323 |
// Check the signature algorithm of the certificate itself. |
|
324 |
// Look for the "withRSA" in "SHA1withRSA", etc. |
|
325 |
X509Certificate issuer = (X509Certificate)chain[0]; |
|
50768 | 326 |
String sigAlgName = |
327 |
issuer.getSigAlgName().toUpperCase(Locale.ENGLISH); |
|
328 |
String pattern = |
|
329 |
"WITH" + sigKeyAlgorithm.toUpperCase(Locale.ENGLISH); |
|
2 | 330 |
return sigAlgName.contains(pattern); |
331 |
} |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
private static List<KeyType> getKeyTypes(String ... keyTypes) { |
|
7043 | 336 |
if ((keyTypes == null) || |
337 |
(keyTypes.length == 0) || (keyTypes[0] == null)) { |
|
2 | 338 |
return null; |
339 |
} |
|
7990 | 340 |
List<KeyType> list = new ArrayList<>(keyTypes.length); |
2 | 341 |
for (String keyType : keyTypes) { |
342 |
list.add(new KeyType(keyType)); |
|
343 |
} |
|
344 |
return list; |
|
345 |
} |
|
346 |
||
347 |
/* |
|
348 |
* Return the best alias that fits the given parameters. |
|
349 |
* The algorithm we use is: |
|
350 |
* . scan through all the aliases in all builders in order |
|
351 |
* . as soon as we find a perfect match, return |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
352 |
* (i.e. a match with a cert that has appropriate key usage, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
353 |
* qualified endpoint identity, and is not expired). |
2 | 354 |
* . if we do not find a perfect match, keep looping and remember |
355 |
* the imperfect matches |
|
356 |
* . at the end, sort the imperfect matches. we prefer expired certs |
|
357 |
* with appropriate key usage to certs with the wrong key usage. |
|
358 |
* return the first one of them. |
|
359 |
*/ |
|
7043 | 360 |
private String chooseAlias(List<KeyType> keyTypeList, Principal[] issuers, |
361 |
CheckType checkType, AlgorithmConstraints constraints) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
362 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
363 |
return chooseAlias(keyTypeList, issuers, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
364 |
checkType, constraints, null, null); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
365 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
366 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
367 |
private String chooseAlias(List<KeyType> keyTypeList, Principal[] issuers, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
368 |
CheckType checkType, AlgorithmConstraints constraints, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
369 |
List<SNIServerName> requestedServerNames, String idAlgorithm) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
370 |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
371 |
if (keyTypeList == null || keyTypeList.isEmpty()) { |
2 | 372 |
return null; |
373 |
} |
|
374 |
||
375 |
Set<Principal> issuerSet = getIssuerSet(issuers); |
|
376 |
List<EntryStatus> allResults = null; |
|
377 |
for (int i = 0, n = builders.size(); i < n; i++) { |
|
378 |
try { |
|
7043 | 379 |
List<EntryStatus> results = getAliases(i, keyTypeList, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
380 |
issuerSet, false, checkType, constraints, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
381 |
requestedServerNames, idAlgorithm); |
2 | 382 |
if (results != null) { |
383 |
// the results will either be a single perfect match |
|
384 |
// or 1 or more imperfect matches |
|
385 |
// if it's a perfect match, return immediately |
|
386 |
EntryStatus status = results.get(0); |
|
387 |
if (status.checkResult == CheckResult.OK) { |
|
50768 | 388 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
389 |
SSLLogger.fine("KeyMgr: choosing key: " + status); |
|
2 | 390 |
} |
391 |
return makeAlias(status); |
|
392 |
} |
|
393 |
if (allResults == null) { |
|
394 |
allResults = new ArrayList<EntryStatus>(); |
|
395 |
} |
|
396 |
allResults.addAll(results); |
|
397 |
} |
|
398 |
} catch (Exception e) { |
|
399 |
// ignore |
|
400 |
} |
|
401 |
} |
|
402 |
if (allResults == null) { |
|
50768 | 403 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
404 |
SSLLogger.fine("KeyMgr: no matching key found"); |
|
2 | 405 |
} |
406 |
return null; |
|
407 |
} |
|
408 |
Collections.sort(allResults); |
|
50768 | 409 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
410 |
SSLLogger.fine( |
|
411 |
"KeyMgr: no good matching key found, " |
|
412 |
+ "returning best match out of", allResults); |
|
2 | 413 |
} |
414 |
return makeAlias(allResults.get(0)); |
|
415 |
} |
|
416 |
||
417 |
/* |
|
418 |
* Return all aliases that (approximately) fit the parameters. |
|
419 |
* These are perfect matches plus imperfect matches (expired certificates |
|
420 |
* and certificates with the wrong extensions). |
|
421 |
* The perfect matches will be first in the array. |
|
422 |
*/ |
|
423 |
public String[] getAliases(String keyType, Principal[] issuers, |
|
7043 | 424 |
CheckType checkType, AlgorithmConstraints constraints) { |
2 | 425 |
if (keyType == null) { |
426 |
return null; |
|
427 |
} |
|
428 |
||
429 |
Set<Principal> issuerSet = getIssuerSet(issuers); |
|
430 |
List<KeyType> keyTypeList = getKeyTypes(keyType); |
|
431 |
List<EntryStatus> allResults = null; |
|
432 |
for (int i = 0, n = builders.size(); i < n; i++) { |
|
433 |
try { |
|
7043 | 434 |
List<EntryStatus> results = getAliases(i, keyTypeList, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
435 |
issuerSet, true, checkType, constraints, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
436 |
null, null); |
2 | 437 |
if (results != null) { |
438 |
if (allResults == null) { |
|
50768 | 439 |
allResults = new ArrayList<>(); |
2 | 440 |
} |
441 |
allResults.addAll(results); |
|
442 |
} |
|
443 |
} catch (Exception e) { |
|
444 |
// ignore |
|
445 |
} |
|
446 |
} |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
447 |
if (allResults == null || allResults.isEmpty()) { |
50768 | 448 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
449 |
SSLLogger.fine("KeyMgr: no matching alias found"); |
|
2 | 450 |
} |
451 |
return null; |
|
452 |
} |
|
453 |
Collections.sort(allResults); |
|
50768 | 454 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
455 |
SSLLogger.fine("KeyMgr: getting aliases", allResults); |
|
2 | 456 |
} |
457 |
return toAliases(allResults); |
|
458 |
} |
|
459 |
||
460 |
// turn candidate entries into unique aliases we can return to JSSE |
|
461 |
private String[] toAliases(List<EntryStatus> results) { |
|
462 |
String[] s = new String[results.size()]; |
|
463 |
int i = 0; |
|
464 |
for (EntryStatus result : results) { |
|
465 |
s[i++] = makeAlias(result); |
|
466 |
} |
|
467 |
return s; |
|
468 |
} |
|
469 |
||
470 |
// make a Set out of the array |
|
471 |
private Set<Principal> getIssuerSet(Principal[] issuers) { |
|
472 |
if ((issuers != null) && (issuers.length != 0)) { |
|
7990 | 473 |
return new HashSet<>(Arrays.asList(issuers)); |
2 | 474 |
} else { |
475 |
return null; |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
// a candidate match |
|
480 |
// identifies the entry by builder and alias |
|
481 |
// and includes the result of the certificate check |
|
482 |
private static class EntryStatus implements Comparable<EntryStatus> { |
|
483 |
||
484 |
final int builderIndex; |
|
485 |
final int keyIndex; |
|
486 |
final String alias; |
|
487 |
final CheckResult checkResult; |
|
488 |
||
489 |
EntryStatus(int builderIndex, int keyIndex, String alias, |
|
490 |
Certificate[] chain, CheckResult checkResult) { |
|
491 |
this.builderIndex = builderIndex; |
|
492 |
this.keyIndex = keyIndex; |
|
493 |
this.alias = alias; |
|
494 |
this.checkResult = checkResult; |
|
495 |
} |
|
496 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
497 |
@Override |
2 | 498 |
public int compareTo(EntryStatus other) { |
499 |
int result = this.checkResult.compareTo(other.checkResult); |
|
500 |
return (result == 0) ? (this.keyIndex - other.keyIndex) : result; |
|
501 |
} |
|
502 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
503 |
@Override |
2 | 504 |
public String toString() { |
505 |
String s = alias + " (verified: " + checkResult + ")"; |
|
506 |
if (builderIndex == 0) { |
|
507 |
return s; |
|
508 |
} else { |
|
509 |
return "Builder #" + builderIndex + ", alias: " + s; |
|
510 |
} |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
// enum for the type of certificate check we want to perform |
|
515 |
// (client or server) |
|
516 |
// also includes the check code itself |
|
517 |
private static enum CheckType { |
|
518 |
||
519 |
// enum constant for "no check" (currently not used) |
|
520 |
NONE(Collections.<String>emptySet()), |
|
521 |
||
522 |
// enum constant for "tls client" check |
|
523 |
// valid EKU for TLS client: any, tls_client |
|
524 |
CLIENT(new HashSet<String>(Arrays.asList(new String[] { |
|
525 |
"2.5.29.37.0", "1.3.6.1.5.5.7.3.2" }))), |
|
526 |
||
527 |
// enum constant for "tls server" check |
|
528 |
// valid EKU for TLS server: any, tls_server, ns_sgc, ms_sgc |
|
529 |
SERVER(new HashSet<String>(Arrays.asList(new String[] { |
|
530 |
"2.5.29.37.0", "1.3.6.1.5.5.7.3.1", "2.16.840.1.113730.4.1", |
|
531 |
"1.3.6.1.4.1.311.10.3.3" }))); |
|
532 |
||
533 |
// set of valid EKU values for this type |
|
534 |
final Set<String> validEku; |
|
535 |
||
536 |
CheckType(Set<String> validEku) { |
|
537 |
this.validEku = validEku; |
|
538 |
} |
|
539 |
||
540 |
private static boolean getBit(boolean[] keyUsage, int bit) { |
|
541 |
return (bit < keyUsage.length) && keyUsage[bit]; |
|
542 |
} |
|
543 |
||
50768 | 544 |
// Check if this certificate is appropriate for this type of use |
545 |
// first check extensions, if they match, check expiration. |
|
546 |
// |
|
547 |
// Note: we may want to move this code into the sun.security.validator |
|
2 | 548 |
// package |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
549 |
CheckResult check(X509Certificate cert, Date date, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
550 |
List<SNIServerName> serverNames, String idAlgorithm) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
551 |
|
2 | 552 |
if (this == NONE) { |
553 |
return CheckResult.OK; |
|
554 |
} |
|
555 |
||
556 |
// check extensions |
|
557 |
try { |
|
558 |
// check extended key usage |
|
559 |
List<String> certEku = cert.getExtendedKeyUsage(); |
|
7043 | 560 |
if ((certEku != null) && |
561 |
Collections.disjoint(validEku, certEku)) { |
|
2 | 562 |
// if extension present and it does not contain any of |
563 |
// the valid EKU OIDs, return extension_mismatch |
|
564 |
return CheckResult.EXTENSION_MISMATCH; |
|
565 |
} |
|
566 |
||
567 |
// check key usage |
|
568 |
boolean[] ku = cert.getKeyUsage(); |
|
569 |
if (ku != null) { |
|
570 |
String algorithm = cert.getPublicKey().getAlgorithm(); |
|
50768 | 571 |
boolean supportsDigitalSignature = getBit(ku, 0); |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
572 |
switch (algorithm) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
573 |
case "RSA": |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
574 |
// require either signature bit |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
575 |
// or if server also allow key encipherment bit |
50768 | 576 |
if (!supportsDigitalSignature) { |
577 |
if (this == CLIENT || getBit(ku, 2) == false) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
578 |
return CheckResult.EXTENSION_MISMATCH; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
579 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
580 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
581 |
break; |
50768 | 582 |
case "RSASSA-PSS": |
583 |
if (!supportsDigitalSignature && (this == SERVER)) { |
|
584 |
return CheckResult.EXTENSION_MISMATCH; |
|
585 |
} |
|
586 |
break; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
587 |
case "DSA": |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
588 |
// require signature bit |
50768 | 589 |
if (!supportsDigitalSignature) { |
2 | 590 |
return CheckResult.EXTENSION_MISMATCH; |
591 |
} |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
592 |
break; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
593 |
case "DH": |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
594 |
// require keyagreement bit |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
595 |
if (getBit(ku, 4) == false) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
596 |
return CheckResult.EXTENSION_MISMATCH; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
597 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
598 |
break; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
599 |
case "EC": |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
600 |
// require signature bit |
50768 | 601 |
if (!supportsDigitalSignature) { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
602 |
return CheckResult.EXTENSION_MISMATCH; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
603 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
604 |
// For servers, also require key agreement. |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
605 |
// This is not totally accurate as the keyAgreement |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
606 |
// bit is only necessary for static ECDH key |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
607 |
// exchange and not ephemeral ECDH. We leave it in |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
608 |
// for now until there are signs that this check |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
609 |
// causes problems for real world EC certificates. |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
610 |
if ((this == SERVER) && (getBit(ku, 4) == false)) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
611 |
return CheckResult.EXTENSION_MISMATCH; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
612 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
613 |
break; |
2 | 614 |
} |
615 |
} |
|
616 |
} catch (CertificateException e) { |
|
617 |
// extensions unparseable, return failure |
|
618 |
return CheckResult.EXTENSION_MISMATCH; |
|
619 |
} |
|
620 |
||
621 |
try { |
|
622 |
cert.checkValidity(date); |
|
623 |
} catch (CertificateException e) { |
|
624 |
return CheckResult.EXPIRED; |
|
625 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
626 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
627 |
if (serverNames != null && !serverNames.isEmpty()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
628 |
for (SNIServerName serverName : serverNames) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
629 |
if (serverName.getType() == |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
630 |
StandardConstants.SNI_HOST_NAME) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
631 |
if (!(serverName instanceof SNIHostName)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
632 |
try { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
633 |
serverName = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
634 |
new SNIHostName(serverName.getEncoded()); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
635 |
} catch (IllegalArgumentException iae) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
636 |
// unlikely to happen, just in case ... |
50768 | 637 |
if (SSLLogger.isOn && |
638 |
SSLLogger.isOn("keymanager")) { |
|
639 |
SSLLogger.fine( |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
640 |
"Illegal server name: " + serverName); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
641 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
642 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
643 |
return CheckResult.INSENSITIVE; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
644 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
645 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
646 |
String hostname = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
647 |
((SNIHostName)serverName).getAsciiName(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
648 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
649 |
try { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
650 |
X509TrustManagerImpl.checkIdentity(hostname, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
651 |
cert, idAlgorithm); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
652 |
} catch (CertificateException e) { |
50768 | 653 |
if (SSLLogger.isOn && |
654 |
SSLLogger.isOn("keymanager")) { |
|
655 |
SSLLogger.fine( |
|
656 |
"Certificate identity does not match " + |
|
657 |
"Server Name Inidication (SNI): " + |
|
658 |
hostname); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
659 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
660 |
return CheckResult.INSENSITIVE; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
661 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
662 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
663 |
break; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
664 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
665 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
666 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
667 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
668 |
return CheckResult.OK; |
2 | 669 |
} |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
670 |
|
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
671 |
public String getValidator() { |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
672 |
if (this == CLIENT) { |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
673 |
return Validator.VAR_TLS_CLIENT; |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
674 |
} else if (this == SERVER) { |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
675 |
return Validator.VAR_TLS_SERVER; |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
676 |
} |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
677 |
return Validator.VAR_GENERIC; |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
678 |
} |
2 | 679 |
} |
680 |
||
681 |
// enum for the result of the extension check |
|
682 |
// NOTE: the order of the constants is important as they are used |
|
683 |
// for sorting, i.e. OK is best, followed by EXPIRED and EXTENSION_MISMATCH |
|
684 |
private static enum CheckResult { |
|
685 |
OK, // ok or not checked |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
686 |
INSENSITIVE, // server name indication insensitive |
2 | 687 |
EXPIRED, // extensions valid but cert expired |
688 |
EXTENSION_MISMATCH, // extensions invalid (expiration not checked) |
|
689 |
} |
|
690 |
||
691 |
/* |
|
692 |
* Return a List of all candidate matches in the specified builder |
|
693 |
* that fit the parameters. |
|
694 |
* We exclude entries in the KeyStore if they are not: |
|
695 |
* . private key entries |
|
696 |
* . the certificates are not X509 certificates |
|
697 |
* . the algorithm of the key in the EE cert doesn't match one of keyTypes |
|
698 |
* . none of the certs is issued by a Principal in issuerSet |
|
699 |
* Using those entries would not be possible or they would almost |
|
700 |
* certainly be rejected by the peer. |
|
701 |
* |
|
702 |
* In addition to those checks, we also check the extensions in the EE |
|
703 |
* cert and its expiration. Even if there is a mismatch, we include |
|
704 |
* such certificates because they technically work and might be accepted |
|
705 |
* by the peer. This leads to more graceful failure and better error |
|
706 |
* messages if the cert expires from one day to the next. |
|
707 |
* |
|
708 |
* The return values are: |
|
709 |
* . null, if there are no matching entries at all |
|
710 |
* . if 'findAll' is 'false' and there is a perfect match, a List |
|
711 |
* with a single element (early return) |
|
712 |
* . if 'findAll' is 'false' and there is NO perfect match, a List |
|
713 |
* with all the imperfect matches (expired, wrong extensions) |
|
714 |
* . if 'findAll' is 'true', a List with all perfect and imperfect |
|
715 |
* matches |
|
716 |
*/ |
|
717 |
private List<EntryStatus> getAliases(int builderIndex, |
|
718 |
List<KeyType> keyTypes, Set<Principal> issuerSet, |
|
7043 | 719 |
boolean findAll, CheckType checkType, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
720 |
AlgorithmConstraints constraints, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
721 |
List<SNIServerName> requestedServerNames, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
722 |
String idAlgorithm) throws Exception { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
723 |
|
2 | 724 |
Builder builder = builders.get(builderIndex); |
725 |
KeyStore ks = builder.getKeyStore(); |
|
726 |
List<EntryStatus> results = null; |
|
727 |
Date date = verificationDate; |
|
728 |
boolean preferred = false; |
|
729 |
for (Enumeration<String> e = ks.aliases(); e.hasMoreElements(); ) { |
|
730 |
String alias = e.nextElement(); |
|
731 |
// check if it is a key entry (private key or secret key) |
|
50768 | 732 |
if (!ks.isKeyEntry(alias)) { |
2 | 733 |
continue; |
734 |
} |
|
735 |
||
736 |
Certificate[] chain = ks.getCertificateChain(alias); |
|
737 |
if ((chain == null) || (chain.length == 0)) { |
|
738 |
// must be secret key entry, ignore |
|
739 |
continue; |
|
740 |
} |
|
7043 | 741 |
|
742 |
boolean incompatible = false; |
|
743 |
for (Certificate cert : chain) { |
|
744 |
if (cert instanceof X509Certificate == false) { |
|
745 |
// not an X509Certificate, ignore this alias |
|
746 |
incompatible = true; |
|
747 |
break; |
|
748 |
} |
|
749 |
} |
|
750 |
if (incompatible) { |
|
751 |
continue; |
|
752 |
} |
|
753 |
||
2 | 754 |
// check keytype |
755 |
int keyIndex = -1; |
|
756 |
int j = 0; |
|
757 |
for (KeyType keyType : keyTypes) { |
|
758 |
if (keyType.matches(chain)) { |
|
759 |
keyIndex = j; |
|
760 |
break; |
|
761 |
} |
|
762 |
j++; |
|
763 |
} |
|
764 |
if (keyIndex == -1) { |
|
50768 | 765 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
766 |
SSLLogger.fine("Ignore alias " + alias |
|
2 | 767 |
+ ": key algorithm does not match"); |
768 |
} |
|
769 |
continue; |
|
770 |
} |
|
771 |
// check issuers |
|
772 |
if (issuerSet != null) { |
|
773 |
boolean found = false; |
|
774 |
for (Certificate cert : chain) { |
|
775 |
X509Certificate xcert = (X509Certificate)cert; |
|
776 |
if (issuerSet.contains(xcert.getIssuerX500Principal())) { |
|
777 |
found = true; |
|
778 |
break; |
|
779 |
} |
|
780 |
} |
|
781 |
if (found == false) { |
|
50768 | 782 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
783 |
SSLLogger.fine( |
|
784 |
"Ignore alias " + alias |
|
785 |
+ ": issuers do not match"); |
|
2 | 786 |
} |
787 |
continue; |
|
788 |
} |
|
789 |
} |
|
7043 | 790 |
|
791 |
// check the algorithm constraints |
|
792 |
if (constraints != null && |
|
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
793 |
!conformsToAlgorithmConstraints(constraints, chain, |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
794 |
checkType.getValidator())) { |
7043 | 795 |
|
50768 | 796 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
797 |
SSLLogger.fine("Ignore alias " + alias + |
|
7043 | 798 |
": certificate list does not conform to " + |
799 |
"algorithm constraints"); |
|
800 |
} |
|
801 |
continue; |
|
802 |
} |
|
803 |
||
2 | 804 |
if (date == null) { |
805 |
date = new Date(); |
|
806 |
} |
|
807 |
CheckResult checkResult = |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
808 |
checkType.check((X509Certificate)chain[0], date, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
809 |
requestedServerNames, idAlgorithm); |
2 | 810 |
EntryStatus status = |
811 |
new EntryStatus(builderIndex, keyIndex, |
|
812 |
alias, chain, checkResult); |
|
813 |
if (!preferred && checkResult == CheckResult.OK && keyIndex == 0) { |
|
814 |
preferred = true; |
|
815 |
} |
|
816 |
if (preferred && (findAll == false)) { |
|
817 |
// if we have a good match and do not need all matches, |
|
818 |
// return immediately |
|
819 |
return Collections.singletonList(status); |
|
820 |
} else { |
|
821 |
if (results == null) { |
|
50768 | 822 |
results = new ArrayList<>(); |
2 | 823 |
} |
824 |
results.add(status); |
|
825 |
} |
|
826 |
} |
|
827 |
return results; |
|
828 |
} |
|
829 |
||
7043 | 830 |
private static boolean conformsToAlgorithmConstraints( |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
831 |
AlgorithmConstraints constraints, Certificate[] chain, |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
43220
diff
changeset
|
832 |
String variant) { |
7043 | 833 |
|
50768 | 834 |
AlgorithmChecker checker = |
835 |
new AlgorithmChecker(constraints, null, variant); |
|
7043 | 836 |
try { |
837 |
checker.init(false); |
|
838 |
} catch (CertPathValidatorException cpve) { |
|
839 |
// unlikely to happen |
|
50768 | 840 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
841 |
SSLLogger.fine( |
|
842 |
"Cannot initialize algorithm constraints checker", cpve); |
|
43220
937cb78b2016
8173066: More verbose debug output for selection of X509 certs
xuelei
parents:
32649
diff
changeset
|
843 |
} |
937cb78b2016
8173066: More verbose debug output for selection of X509 certs
xuelei
parents:
32649
diff
changeset
|
844 |
|
7043 | 845 |
return false; |
846 |
} |
|
847 |
||
848 |
// It is a forward checker, so we need to check from trust to target. |
|
849 |
for (int i = chain.length - 1; i >= 0; i--) { |
|
850 |
Certificate cert = chain[i]; |
|
851 |
try { |
|
852 |
// We don't care about the unresolved critical extensions. |
|
853 |
checker.check(cert, Collections.<String>emptySet()); |
|
854 |
} catch (CertPathValidatorException cpve) { |
|
50768 | 855 |
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) { |
856 |
SSLLogger.fine("Certificate does not conform to " + |
|
857 |
"algorithm constraints", cert, cpve); |
|
43220
937cb78b2016
8173066: More verbose debug output for selection of X509 certs
xuelei
parents:
32649
diff
changeset
|
858 |
} |
937cb78b2016
8173066: More verbose debug output for selection of X509 certs
xuelei
parents:
32649
diff
changeset
|
859 |
|
7043 | 860 |
return false; |
861 |
} |
|
862 |
} |
|
863 |
||
864 |
return true; |
|
865 |
} |
|
2 | 866 |
} |