|
1 /* |
|
2 * Copyright (c) 2018, 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 |
|
26 package sun.security.ssl; |
|
27 |
|
28 import java.security.PrivateKey; |
|
29 import java.security.PublicKey; |
|
30 import java.security.cert.X509Certificate; |
|
31 import java.security.interfaces.ECPublicKey; |
|
32 import java.security.spec.ECParameterSpec; |
|
33 import java.util.AbstractMap.SimpleImmutableEntry; |
|
34 import java.util.Map; |
|
35 import javax.net.ssl.SSLEngine; |
|
36 import javax.net.ssl.SSLSocket; |
|
37 import javax.net.ssl.X509ExtendedKeyManager; |
|
38 import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
|
39 import sun.security.ssl.SupportedGroupsExtension.SupportedGroups; |
|
40 |
|
41 enum X509Authentication implements SSLAuthentication { |
|
42 // Require rsaEncryption public key |
|
43 RSA ("RSA", new X509PossessionGenerator( |
|
44 new String[]{"RSA"})), |
|
45 |
|
46 // Require RSASSA-PSS public key |
|
47 RSASSA_PSS ("RSASSA-PSS", new X509PossessionGenerator( |
|
48 new String[] {"RSASSA-PSS"})), |
|
49 |
|
50 // Require rsaEncryption or RSASSA-PSS public key |
|
51 // |
|
52 // Note that this is a specifical scheme for TLS 1.2. (EC)DHE_RSA cipher |
|
53 // suites of TLS 1.2 can use either rsaEncryption or RSASSA-PSS public |
|
54 // key for authentication and handshake. |
|
55 RSA_OR_PSS ("RSA_OR_PSS", new X509PossessionGenerator( |
|
56 new String[] {"RSA", "RSASSA-PSS"})), |
|
57 |
|
58 // Require DSA public key |
|
59 DSA ("DSA", new X509PossessionGenerator( |
|
60 new String[] {"DSA"})), |
|
61 |
|
62 // Require EC public key |
|
63 EC ("EC", new X509PossessionGenerator( |
|
64 new String[] {"EC"})); |
|
65 |
|
66 final String keyType; |
|
67 final SSLPossessionGenerator possessionGenerator; |
|
68 |
|
69 X509Authentication(String keyType, |
|
70 SSLPossessionGenerator possessionGenerator) { |
|
71 this.keyType = keyType; |
|
72 this.possessionGenerator = possessionGenerator; |
|
73 } |
|
74 |
|
75 static X509Authentication valueOf(SignatureScheme signatureScheme) { |
|
76 for (X509Authentication au: X509Authentication.values()) { |
|
77 if (au.keyType.equals(signatureScheme.keyAlgorithm)) { |
|
78 return au; |
|
79 } |
|
80 } |
|
81 |
|
82 return null; |
|
83 } |
|
84 |
|
85 @Override |
|
86 public SSLPossession createPossession(HandshakeContext handshakeContext) { |
|
87 return possessionGenerator.createPossession(handshakeContext); |
|
88 } |
|
89 |
|
90 @Override |
|
91 public SSLHandshake[] getRelatedHandshakers( |
|
92 HandshakeContext handshakeContext) { |
|
93 if (!handshakeContext.negotiatedProtocol.useTLS13PlusSpec()) { |
|
94 return new SSLHandshake[] { |
|
95 SSLHandshake.CERTIFICATE, |
|
96 SSLHandshake.CERTIFICATE_REQUEST |
|
97 }; |
|
98 } // Otherwise, TLS 1.3 does not use this method. |
|
99 |
|
100 return new SSLHandshake[0]; |
|
101 } |
|
102 |
|
103 @SuppressWarnings({"unchecked", "rawtypes"}) |
|
104 @Override |
|
105 public Map.Entry<Byte, HandshakeProducer>[] getHandshakeProducers( |
|
106 HandshakeContext handshakeContext) { |
|
107 if (!handshakeContext.negotiatedProtocol.useTLS13PlusSpec()) { |
|
108 return (Map.Entry<Byte, HandshakeProducer>[])(new Map.Entry[] { |
|
109 new SimpleImmutableEntry<Byte, HandshakeProducer>( |
|
110 SSLHandshake.CERTIFICATE.id, |
|
111 SSLHandshake.CERTIFICATE |
|
112 ) |
|
113 }); |
|
114 } // Otherwise, TLS 1.3 does not use this method. |
|
115 |
|
116 return (Map.Entry<Byte, HandshakeProducer>[])(new Map.Entry[0]); |
|
117 } |
|
118 |
|
119 static final class X509Possession implements SSLPossession { |
|
120 // Proof of possession of the private key corresponding to the public |
|
121 // key for which a certificate is being provided for authentication. |
|
122 final X509Certificate[] popCerts; |
|
123 final PrivateKey popPrivateKey; |
|
124 |
|
125 X509Possession(PrivateKey popPrivateKey, |
|
126 X509Certificate[] popCerts) { |
|
127 this.popCerts = popCerts; |
|
128 this.popPrivateKey = popPrivateKey; |
|
129 } |
|
130 } |
|
131 |
|
132 static final class X509Credentials implements SSLCredentials { |
|
133 final X509Certificate[] popCerts; |
|
134 final PublicKey popPublicKey; |
|
135 |
|
136 X509Credentials(PublicKey popPublicKey, X509Certificate[] popCerts) { |
|
137 this.popCerts = popCerts; |
|
138 this.popPublicKey = popPublicKey; |
|
139 } |
|
140 } |
|
141 |
|
142 private static final |
|
143 class X509PossessionGenerator implements SSLPossessionGenerator { |
|
144 private final String[] keyTypes; |
|
145 |
|
146 private X509PossessionGenerator(String[] keyTypes) { |
|
147 this.keyTypes = keyTypes; |
|
148 } |
|
149 |
|
150 @Override |
|
151 public SSLPossession createPossession(HandshakeContext context) { |
|
152 if (context.sslConfig.isClientMode) { |
|
153 for (String keyType : keyTypes) { |
|
154 SSLPossession poss = createClientPossession( |
|
155 (ClientHandshakeContext)context, keyType); |
|
156 if (poss != null) { |
|
157 return poss; |
|
158 } |
|
159 } |
|
160 } else { |
|
161 for (String keyType : keyTypes) { |
|
162 SSLPossession poss = createServerPossession( |
|
163 (ServerHandshakeContext)context, keyType); |
|
164 if (poss != null) { |
|
165 return poss; |
|
166 } |
|
167 } |
|
168 } |
|
169 |
|
170 return null; |
|
171 } |
|
172 |
|
173 // Used by TLS 1.3 only. |
|
174 private SSLPossession createClientPossession( |
|
175 ClientHandshakeContext chc, String keyType) { |
|
176 X509ExtendedKeyManager km = chc.sslContext.getX509KeyManager(); |
|
177 String clientAlias = null; |
|
178 if (chc.conContext.transport instanceof SSLSocketImpl) { |
|
179 clientAlias = km.chooseClientAlias( |
|
180 new String[] { keyType }, |
|
181 null, (SSLSocket)chc.conContext.transport); |
|
182 } else if (chc.conContext.transport instanceof SSLEngineImpl) { |
|
183 clientAlias = km.chooseEngineClientAlias( |
|
184 new String[] { keyType }, |
|
185 null, (SSLEngine)chc.conContext.transport); |
|
186 } |
|
187 |
|
188 if (clientAlias == null) { |
|
189 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
190 SSLLogger.finest("No X.509 cert selected for " + keyType); |
|
191 } |
|
192 return null; |
|
193 } |
|
194 |
|
195 PrivateKey clientPrivateKey = km.getPrivateKey(clientAlias); |
|
196 if (clientPrivateKey == null) { |
|
197 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
198 SSLLogger.finest( |
|
199 clientAlias + " is not a private key entry"); |
|
200 } |
|
201 return null; |
|
202 } |
|
203 |
|
204 X509Certificate[] clientCerts = km.getCertificateChain(clientAlias); |
|
205 if ((clientCerts == null) || (clientCerts.length == 0)) { |
|
206 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
207 SSLLogger.finest(clientAlias + |
|
208 " is a private key entry with no cert chain stored"); |
|
209 } |
|
210 return null; |
|
211 } |
|
212 |
|
213 PublicKey clientPublicKey = clientCerts[0].getPublicKey(); |
|
214 if ((!clientPrivateKey.getAlgorithm().equals(keyType)) |
|
215 || (!clientPublicKey.getAlgorithm().equals(keyType))) { |
|
216 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
217 SSLLogger.fine( |
|
218 clientAlias + " private or public key is not of " + |
|
219 keyType + " algorithm"); |
|
220 } |
|
221 return null; |
|
222 } |
|
223 |
|
224 return new X509Possession(clientPrivateKey, clientCerts); |
|
225 } |
|
226 |
|
227 private SSLPossession createServerPossession( |
|
228 ServerHandshakeContext shc, String keyType) { |
|
229 X509ExtendedKeyManager km = shc.sslContext.getX509KeyManager(); |
|
230 String serverAlias = null; |
|
231 if (shc.conContext.transport instanceof SSLSocketImpl) { |
|
232 serverAlias = km.chooseServerAlias(keyType, |
|
233 null, (SSLSocket)shc.conContext.transport); |
|
234 } else if (shc.conContext.transport instanceof SSLEngineImpl) { |
|
235 serverAlias = km.chooseEngineServerAlias(keyType, |
|
236 null, (SSLEngine)shc.conContext.transport); |
|
237 } |
|
238 |
|
239 if (serverAlias == null) { |
|
240 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
241 SSLLogger.finest("No X.509 cert selected for " + keyType); |
|
242 } |
|
243 return null; |
|
244 } |
|
245 |
|
246 PrivateKey serverPrivateKey = km.getPrivateKey(serverAlias); |
|
247 if (serverPrivateKey == null) { |
|
248 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
249 SSLLogger.finest( |
|
250 serverAlias + " is not a private key entry"); |
|
251 } |
|
252 return null; |
|
253 } |
|
254 |
|
255 X509Certificate[] serverCerts = km.getCertificateChain(serverAlias); |
|
256 if ((serverCerts == null) || (serverCerts.length == 0)) { |
|
257 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
258 SSLLogger.finest( |
|
259 serverAlias + " is not a certificate entry"); |
|
260 } |
|
261 return null; |
|
262 } |
|
263 |
|
264 PublicKey serverPublicKey = serverCerts[0].getPublicKey(); |
|
265 if ((!serverPrivateKey.getAlgorithm().equals(keyType)) |
|
266 || (!serverPublicKey.getAlgorithm().equals(keyType))) { |
|
267 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
268 SSLLogger.fine( |
|
269 serverAlias + " private or public key is not of " + |
|
270 keyType + " algorithm"); |
|
271 } |
|
272 return null; |
|
273 } |
|
274 |
|
275 // For ECC certs, check whether we support the EC domain |
|
276 // parameters. If the client sent a SupportedEllipticCurves |
|
277 // ClientHello extension, check against that too. |
|
278 if (keyType.equals("EC")) { |
|
279 if (!(serverPublicKey instanceof ECPublicKey)) { |
|
280 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
281 SSLLogger.warning(serverAlias + |
|
282 " public key is not an instance of ECPublicKey"); |
|
283 } |
|
284 return null; |
|
285 } |
|
286 |
|
287 // For ECC certs, check whether we support the EC domain |
|
288 // parameters. If the client sent a SupportedEllipticCurves |
|
289 // ClientHello extension, check against that too. |
|
290 ECParameterSpec params = |
|
291 ((ECPublicKey)serverPublicKey).getParams(); |
|
292 NamedGroup namedGroup = NamedGroup.valueOf(params); |
|
293 if ((namedGroup == null) || |
|
294 (!SupportedGroups.isSupported(namedGroup)) || |
|
295 ((shc.clientRequestedNamedGroups != null) && |
|
296 !shc.clientRequestedNamedGroups.contains(namedGroup))) { |
|
297 |
|
298 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
299 SSLLogger.warning( |
|
300 "Unsupported named group (" + namedGroup + |
|
301 ") used in the " + serverAlias + " certificate"); |
|
302 } |
|
303 |
|
304 return null; |
|
305 } |
|
306 } |
|
307 |
|
308 return new X509Possession(serverPrivateKey, serverCerts); |
|
309 } |
|
310 } |
|
311 } |