Skip to content

Commit 0e53815

Browse files
committed
initial commit of java 10 src from label jdk-10+46 of the JSSE source from sun.security.ssl (most of it anyway - the files directly under src/java.base/share/classes/sun/security/ssl)as a clean baseline from which to make changes (that will likely include the changes for the work previously done against java 9)
0 parents  commit 0e53815

File tree

90 files changed

+40441
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+40441
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2015, 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.io.IOException;
29+
import java.nio.charset.*;
30+
import java.util.*;
31+
32+
import javax.net.ssl.*;
33+
34+
/*
35+
* [RFC 7301]
36+
* This TLS extension facilitates the negotiation of application-layer protocols
37+
* within the TLS handshake. Clients MAY include an extension of type
38+
* "application_layer_protocol_negotiation" in the (extended) ClientHello
39+
* message. The "extension_data" field of this extension SHALL contain a
40+
* "ProtocolNameList" value:
41+
*
42+
* enum {
43+
* application_layer_protocol_negotiation(16), (65535)
44+
* } ExtensionType;
45+
*
46+
* opaque ProtocolName<1..2^8-1>;
47+
*
48+
* struct {
49+
* ProtocolName protocol_name_list<2..2^16-1>
50+
* } ProtocolNameList;
51+
*/
52+
final class ALPNExtension extends HelloExtension {
53+
54+
final static int ALPN_HEADER_LENGTH = 1;
55+
final static int MAX_APPLICATION_PROTOCOL_LENGTH = 255;
56+
final static int MAX_APPLICATION_PROTOCOL_LIST_LENGTH = 65535;
57+
private int listLength = 0; // ProtocolNameList length
58+
private List<String> protocolNames = null;
59+
60+
// constructor for ServerHello
61+
ALPNExtension(String protocolName) throws SSLException {
62+
this(new String[]{ protocolName });
63+
}
64+
65+
// constructor for ClientHello
66+
ALPNExtension(String[] protocolNames) throws SSLException {
67+
super(ExtensionType.EXT_ALPN);
68+
if (protocolNames.length == 0) { // never null, never empty
69+
throw new IllegalArgumentException(
70+
"The list of application protocols cannot be empty");
71+
}
72+
this.protocolNames = Arrays.asList(protocolNames);
73+
for (String p : protocolNames) {
74+
int length = p.getBytes(StandardCharsets.UTF_8).length;
75+
if (length == 0) {
76+
throw new SSLProtocolException(
77+
"Application protocol name is empty");
78+
}
79+
if (length <= MAX_APPLICATION_PROTOCOL_LENGTH) {
80+
listLength += length + ALPN_HEADER_LENGTH;
81+
} else {
82+
throw new SSLProtocolException(
83+
"Application protocol name is too long: " + p);
84+
}
85+
if (listLength > MAX_APPLICATION_PROTOCOL_LIST_LENGTH) {
86+
throw new SSLProtocolException(
87+
"Application protocol name list is too long");
88+
}
89+
}
90+
}
91+
92+
// constructor for ServerHello for parsing ALPN extension
93+
ALPNExtension(HandshakeInStream s, int len) throws IOException {
94+
super(ExtensionType.EXT_ALPN);
95+
96+
if (len >= 2) {
97+
listLength = s.getInt16(); // list length
98+
if (listLength < 2 || listLength + 2 != len) {
99+
throw new SSLProtocolException(
100+
"Invalid " + type + " extension: incorrect list length " +
101+
"(length=" + listLength + ")");
102+
}
103+
} else {
104+
throw new SSLProtocolException(
105+
"Invalid " + type + " extension: insufficient data " +
106+
"(length=" + len + ")");
107+
}
108+
109+
int remaining = listLength;
110+
this.protocolNames = new ArrayList<>();
111+
while (remaining > 0) {
112+
// opaque ProtocolName<1..2^8-1>; // RFC 7301
113+
byte[] bytes = s.getBytes8();
114+
if (bytes.length == 0) {
115+
throw new SSLProtocolException("Invalid " + type +
116+
" extension: empty application protocol name");
117+
}
118+
String p =
119+
new String(bytes, StandardCharsets.UTF_8); // app protocol
120+
protocolNames.add(p);
121+
remaining -= bytes.length + ALPN_HEADER_LENGTH;
122+
}
123+
124+
if (remaining != 0) {
125+
throw new SSLProtocolException(
126+
"Invalid " + type + " extension: extra data " +
127+
"(length=" + remaining + ")");
128+
}
129+
}
130+
131+
List<String> getPeerAPs() {
132+
return protocolNames;
133+
}
134+
135+
/*
136+
* Return the length in bytes, including extension type and length fields.
137+
*/
138+
@Override
139+
int length() {
140+
return 6 + listLength;
141+
}
142+
143+
@Override
144+
void send(HandshakeOutStream s) throws IOException {
145+
s.putInt16(type.id);
146+
s.putInt16(listLength + 2); // length of extension_data
147+
s.putInt16(listLength); // length of ProtocolNameList
148+
149+
for (String p : protocolNames) {
150+
s.putBytes8(p.getBytes(StandardCharsets.UTF_8));
151+
}
152+
}
153+
154+
@Override
155+
public String toString() {
156+
StringBuilder sb = new StringBuilder();
157+
if (protocolNames == null || protocolNames.isEmpty()) {
158+
sb.append("<empty>");
159+
} else {
160+
for (String protocolName : protocolNames) {
161+
sb.append("[" + protocolName + "]");
162+
}
163+
}
164+
165+
return "Extension " + type +
166+
", protocol names: " + sb;
167+
}
168+
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/*
2+
* Copyright (c) 2003, 2015, 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 javax.net.ssl.*;
29+
30+
/*
31+
* A simple class to congregate alerts, their definitions, and common
32+
* support methods.
33+
*/
34+
35+
final class Alerts {
36+
37+
/*
38+
* Alerts are always a fixed two byte format (level/description).
39+
*/
40+
41+
// warnings and fatal errors are package private facilities/constants
42+
43+
// Alert levels (enum AlertLevel)
44+
static final byte alert_warning = 1;
45+
static final byte alert_fatal = 2;
46+
47+
/*
48+
* Alert descriptions (enum AlertDescription)
49+
*
50+
* We may not use them all in our processing, but if someone
51+
* sends us one, we can at least convert it to a string for the
52+
* user.
53+
*/
54+
static final byte alert_close_notify = 0;
55+
static final byte alert_unexpected_message = 10;
56+
static final byte alert_bad_record_mac = 20;
57+
static final byte alert_decryption_failed = 21;
58+
static final byte alert_record_overflow = 22;
59+
static final byte alert_decompression_failure = 30;
60+
static final byte alert_handshake_failure = 40;
61+
static final byte alert_no_certificate = 41;
62+
static final byte alert_bad_certificate = 42;
63+
static final byte alert_unsupported_certificate = 43;
64+
static final byte alert_certificate_revoked = 44;
65+
static final byte alert_certificate_expired = 45;
66+
static final byte alert_certificate_unknown = 46;
67+
static final byte alert_illegal_parameter = 47;
68+
static final byte alert_unknown_ca = 48;
69+
static final byte alert_access_denied = 49;
70+
static final byte alert_decode_error = 50;
71+
static final byte alert_decrypt_error = 51;
72+
static final byte alert_export_restriction = 60;
73+
static final byte alert_protocol_version = 70;
74+
static final byte alert_insufficient_security = 71;
75+
static final byte alert_internal_error = 80;
76+
static final byte alert_user_canceled = 90;
77+
static final byte alert_no_renegotiation = 100;
78+
79+
// from RFC 3546 (TLS Extensions)
80+
static final byte alert_unsupported_extension = 110;
81+
static final byte alert_certificate_unobtainable = 111;
82+
static final byte alert_unrecognized_name = 112;
83+
static final byte alert_bad_certificate_status_response = 113;
84+
static final byte alert_bad_certificate_hash_value = 114;
85+
86+
// from RFC 7301 (TLS ALPN Extension)
87+
static final byte alert_no_application_protocol = 120;
88+
89+
static String alertDescription(byte code) {
90+
switch (code) {
91+
92+
case alert_close_notify:
93+
return "close_notify";
94+
case alert_unexpected_message:
95+
return "unexpected_message";
96+
case alert_bad_record_mac:
97+
return "bad_record_mac";
98+
case alert_decryption_failed:
99+
return "decryption_failed";
100+
case alert_record_overflow:
101+
return "record_overflow";
102+
case alert_decompression_failure:
103+
return "decompression_failure";
104+
case alert_handshake_failure:
105+
return "handshake_failure";
106+
case alert_no_certificate:
107+
return "no_certificate";
108+
case alert_bad_certificate:
109+
return "bad_certificate";
110+
case alert_unsupported_certificate:
111+
return "unsupported_certificate";
112+
case alert_certificate_revoked:
113+
return "certificate_revoked";
114+
case alert_certificate_expired:
115+
return "certificate_expired";
116+
case alert_certificate_unknown:
117+
return "certificate_unknown";
118+
case alert_illegal_parameter:
119+
return "illegal_parameter";
120+
case alert_unknown_ca:
121+
return "unknown_ca";
122+
case alert_access_denied:
123+
return "access_denied";
124+
case alert_decode_error:
125+
return "decode_error";
126+
case alert_decrypt_error:
127+
return "decrypt_error";
128+
case alert_export_restriction:
129+
return "export_restriction";
130+
case alert_protocol_version:
131+
return "protocol_version";
132+
case alert_insufficient_security:
133+
return "insufficient_security";
134+
case alert_internal_error:
135+
return "internal_error";
136+
case alert_user_canceled:
137+
return "user_canceled";
138+
case alert_no_renegotiation:
139+
return "no_renegotiation";
140+
case alert_unsupported_extension:
141+
return "unsupported_extension";
142+
case alert_certificate_unobtainable:
143+
return "certificate_unobtainable";
144+
case alert_unrecognized_name:
145+
return "unrecognized_name";
146+
case alert_bad_certificate_status_response:
147+
return "bad_certificate_status_response";
148+
case alert_bad_certificate_hash_value:
149+
return "bad_certificate_hash_value";
150+
case alert_no_application_protocol:
151+
return "no_application_protocol";
152+
153+
default:
154+
return "<UNKNOWN ALERT: " + (code & 0x0ff) + ">";
155+
}
156+
}
157+
158+
static SSLException getSSLException(byte description, String reason) {
159+
return getSSLException(description, null, reason);
160+
}
161+
162+
/*
163+
* Try to be a little more specific in our choice of
164+
* exceptions to throw.
165+
*/
166+
static SSLException getSSLException(byte description, Throwable cause,
167+
String reason) {
168+
169+
SSLException e;
170+
// the SSLException classes do not have a no-args constructor
171+
// make up a message if there is none
172+
if (reason == null) {
173+
if (cause != null) {
174+
reason = cause.toString();
175+
} else {
176+
reason = "";
177+
}
178+
}
179+
switch (description) {
180+
case alert_handshake_failure:
181+
case alert_no_certificate:
182+
case alert_bad_certificate:
183+
case alert_unsupported_certificate:
184+
case alert_certificate_revoked:
185+
case alert_certificate_expired:
186+
case alert_certificate_unknown:
187+
case alert_unknown_ca:
188+
case alert_access_denied:
189+
case alert_decrypt_error:
190+
case alert_export_restriction:
191+
case alert_insufficient_security:
192+
case alert_unsupported_extension:
193+
case alert_certificate_unobtainable:
194+
case alert_unrecognized_name:
195+
case alert_bad_certificate_status_response:
196+
case alert_bad_certificate_hash_value:
197+
case alert_no_application_protocol:
198+
e = new SSLHandshakeException(reason);
199+
break;
200+
201+
case alert_close_notify:
202+
case alert_unexpected_message:
203+
case alert_bad_record_mac:
204+
case alert_decryption_failed:
205+
case alert_record_overflow:
206+
case alert_decompression_failure:
207+
case alert_illegal_parameter:
208+
case alert_decode_error:
209+
case alert_protocol_version:
210+
case alert_internal_error:
211+
case alert_user_canceled:
212+
case alert_no_renegotiation:
213+
default:
214+
e = new SSLException(reason);
215+
break;
216+
}
217+
218+
if (cause != null) {
219+
e.initCause(cause);
220+
}
221+
return e;
222+
}
223+
}

0 commit comments

Comments
 (0)