author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45064 | jdk/src/java.base/share/classes/sun/security/ssl/CipherSuiteList.java@b1b45177051b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
2 |
* Copyright (c) 2002, 2017, 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 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.util.*; |
|
31 |
||
32 |
import javax.net.ssl.SSLException; |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
33 |
import static sun.security.ssl.NamedGroupType.*; |
2 | 34 |
|
35 |
/** |
|
36 |
* A list of CipherSuites. Also maintains the lists of supported and |
|
37 |
* default ciphersuites and supports I/O from handshake streams. |
|
38 |
* |
|
39 |
* Instances of this class are immutable. |
|
40 |
* |
|
41 |
*/ |
|
42 |
final class CipherSuiteList { |
|
43 |
||
44 |
private final Collection<CipherSuite> cipherSuites; |
|
45 |
private String[] suiteNames; |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
46 |
private final EnumSet<NamedGroupType> groupsTypes = |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
47 |
EnumSet.noneOf(NamedGroupType.class); |
2 | 48 |
|
6856 | 49 |
// for use by buildAvailableCache() and |
50 |
// Handshaker.getKickstartMessage() only |
|
51 |
CipherSuiteList(Collection<CipherSuite> cipherSuites) { |
|
2 | 52 |
this.cipherSuites = cipherSuites; |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
53 |
for (CipherSuite suite : cipherSuites) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
54 |
updateGroupTypes(suite); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
55 |
} |
2 | 56 |
} |
57 |
||
58 |
/** |
|
59 |
* Create a CipherSuiteList with a single element. |
|
60 |
*/ |
|
61 |
CipherSuiteList(CipherSuite suite) { |
|
62 |
cipherSuites = new ArrayList<CipherSuite>(1); |
|
63 |
cipherSuites.add(suite); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
64 |
updateGroupTypes(suite); |
2 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Construct a CipherSuiteList from a array of names. We don't bother |
|
69 |
* to eliminate duplicates. |
|
70 |
* |
|
71 |
* @exception IllegalArgumentException if the array or any of its elements |
|
72 |
* is null or if the ciphersuite name is unrecognized or unsupported |
|
73 |
* using currently installed providers. |
|
74 |
*/ |
|
75 |
CipherSuiteList(String[] names) { |
|
76 |
if (names == null) { |
|
77 |
throw new IllegalArgumentException("CipherSuites may not be null"); |
|
78 |
} |
|
79 |
cipherSuites = new ArrayList<CipherSuite>(names.length); |
|
80 |
for (int i = 0; i < names.length; i++) { |
|
81 |
String suiteName = names[i]; |
|
82 |
CipherSuite suite = CipherSuite.valueOf(suiteName); |
|
83 |
if (suite.isAvailable() == false) { |
|
34826 | 84 |
throw new IllegalArgumentException("Cannot support " |
85 |
+ suiteName + " with currently installed providers"); |
|
2 | 86 |
} |
87 |
cipherSuites.add(suite); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
88 |
updateGroupTypes(suite); |
2 | 89 |
} |
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Read a CipherSuiteList from a HandshakeInStream in V3 ClientHello |
|
94 |
* format. Does not check if the listed ciphersuites are known or |
|
95 |
* supported. |
|
96 |
*/ |
|
97 |
CipherSuiteList(HandshakeInStream in) throws IOException { |
|
98 |
byte[] bytes = in.getBytes16(); |
|
99 |
if ((bytes.length & 1) != 0) { |
|
100 |
throw new SSLException("Invalid ClientHello message"); |
|
101 |
} |
|
102 |
cipherSuites = new ArrayList<CipherSuite>(bytes.length >> 1); |
|
103 |
for (int i = 0; i < bytes.length; i += 2) { |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
104 |
CipherSuite suite = CipherSuite.valueOf(bytes[i], bytes[i+1]); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
105 |
cipherSuites.add(suite); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
106 |
updateGroupTypes(suite); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
107 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
108 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
109 |
|
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
110 |
// Please don't use this method except constructors. |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
111 |
private void updateGroupTypes(CipherSuite cipherSuite) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
112 |
if (cipherSuite.keyExchange != null && (!cipherSuite.exportable)) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
113 |
NamedGroupType groupType = cipherSuite.keyExchange.groupType; |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
114 |
if ((groupType != NAMED_GROUP_NONE) && |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
115 |
(!groupsTypes.contains(groupType))) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
116 |
groupsTypes.add(groupType); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
117 |
} |
2 | 118 |
} |
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Return whether this list contains the given CipherSuite. |
|
123 |
*/ |
|
124 |
boolean contains(CipherSuite suite) { |
|
125 |
return cipherSuites.contains(suite); |
|
126 |
} |
|
127 |
||
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
128 |
// Return whether this list contains cipher suites of a named group type. |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
129 |
boolean contains(NamedGroupType groupType) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
39563
diff
changeset
|
130 |
return groupsTypes.contains(groupType); |
2 | 131 |
} |
132 |
||
133 |
/** |
|
134 |
* Return an Iterator for the CipherSuites in this list. |
|
135 |
*/ |
|
136 |
Iterator<CipherSuite> iterator() { |
|
137 |
return cipherSuites.iterator(); |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Return a reference to the internal Collection of CipherSuites. |
|
142 |
* The Collection MUST NOT be modified. |
|
143 |
*/ |
|
144 |
Collection<CipherSuite> collection() { |
|
145 |
return cipherSuites; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Return the number of CipherSuites in this list. |
|
150 |
*/ |
|
151 |
int size() { |
|
152 |
return cipherSuites.size(); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Return an array with the names of the CipherSuites in this list. |
|
157 |
*/ |
|
158 |
synchronized String[] toStringArray() { |
|
159 |
if (suiteNames == null) { |
|
160 |
suiteNames = new String[cipherSuites.size()]; |
|
161 |
int i = 0; |
|
162 |
for (CipherSuite c : cipherSuites) { |
|
163 |
suiteNames[i++] = c.name; |
|
164 |
} |
|
165 |
} |
|
166 |
return suiteNames.clone(); |
|
167 |
} |
|
168 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9275
diff
changeset
|
169 |
@Override |
2 | 170 |
public String toString() { |
171 |
return cipherSuites.toString(); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Write this list to an HandshakeOutStream in V3 ClientHello format. |
|
176 |
*/ |
|
177 |
void send(HandshakeOutStream s) throws IOException { |
|
178 |
byte[] suiteBytes = new byte[cipherSuites.size() * 2]; |
|
179 |
int i = 0; |
|
180 |
for (CipherSuite c : cipherSuites) { |
|
181 |
suiteBytes[i] = (byte)(c.id >> 8); |
|
182 |
suiteBytes[i+1] = (byte)c.id; |
|
183 |
i += 2; |
|
184 |
} |
|
185 |
s.putBytes16(suiteBytes); |
|
186 |
} |
|
187 |
} |