Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/net/ssl/SNIServerName.java
41159 views
1
/*
2
* Copyright (c) 2012, 2020, 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 javax.net.ssl;
27
28
import java.util.Arrays;
29
import java.util.HexFormat;
30
31
/**
32
* Instances of this class represent a server name in a Server Name
33
* Indication (SNI) extension.
34
* <P>
35
* The SNI extension is a feature that extends the SSL/TLS/DTLS protocols to
36
* indicate what server name the client is attempting to connect to during
37
* handshaking. See section 3, "Server Name Indication", of <A
38
* HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions (RFC 6066)</A>.
39
* <P>
40
* {@code SNIServerName} objects are immutable. Subclasses should not provide
41
* methods that can change the state of an instance once it has been created.
42
*
43
* @see SSLParameters#getServerNames()
44
* @see SSLParameters#setServerNames(List)
45
*
46
* @since 1.8
47
*/
48
public abstract class SNIServerName {
49
50
// the type of the server name
51
private final int type;
52
53
// the encoded value of the server name
54
private final byte[] encoded;
55
56
/**
57
* Creates an {@code SNIServerName} using the specified name type and
58
* encoded value.
59
* <P>
60
* Note that the {@code encoded} byte array is cloned to protect against
61
* subsequent modification.
62
*
63
* @param type
64
* the type of the server name
65
* @param encoded
66
* the encoded value of the server name
67
*
68
* @throws IllegalArgumentException if {@code type} is not in the range
69
* of 0 to 255, inclusive.
70
* @throws NullPointerException if {@code encoded} is null
71
*/
72
protected SNIServerName(int type, byte[] encoded) {
73
if (type < 0) {
74
throw new IllegalArgumentException(
75
"Server name type cannot be less than zero");
76
} else if (type > 255) {
77
throw new IllegalArgumentException(
78
"Server name type cannot be greater than 255");
79
}
80
this.type = type;
81
82
if (encoded == null) {
83
throw new NullPointerException(
84
"Server name encoded value cannot be null");
85
}
86
this.encoded = encoded.clone();
87
}
88
89
90
/**
91
* Returns the name type of this server name.
92
*
93
* @return the name type of this server name
94
*/
95
public final int getType() {
96
return type;
97
}
98
99
/**
100
* Returns a copy of the encoded server name value of this server name.
101
*
102
* @return a copy of the encoded server name value of this server name
103
*/
104
public final byte[] getEncoded() {
105
return encoded.clone();
106
}
107
108
/**
109
* Indicates whether some other object is "equal to" this server name.
110
*
111
* @return true if, and only if, {@code other} is of the same class
112
* of this object, and has the same name type and
113
* encoded value as this server name.
114
*/
115
@Override
116
public boolean equals(Object other) {
117
if (this == other) {
118
return true;
119
}
120
121
if (this.getClass() != other.getClass()) {
122
return false;
123
}
124
125
SNIServerName that = (SNIServerName)other;
126
return (this.type == that.type) &&
127
Arrays.equals(this.encoded, that.encoded);
128
}
129
130
/**
131
* Returns a hash code value for this server name.
132
* <P>
133
* The hash code value is generated using the name type and encoded
134
* value of this server name.
135
*
136
* @return a hash code value for this server name.
137
*/
138
@Override
139
public int hashCode() {
140
int result = 17; // 17/31: prime number to decrease collisions
141
result = 31 * result + type;
142
result = 31 * result + Arrays.hashCode(encoded);
143
144
return result;
145
}
146
147
/**
148
* Returns a string representation of this server name, including the server
149
* name type and the encoded server name value in this
150
* {@code SNIServerName} object.
151
* <P>
152
* The exact details of the representation are unspecified and subject
153
* to change, but the following may be regarded as typical:
154
* <pre>
155
* "type={@literal <name type>}, value={@literal <name value>}"
156
* </pre>
157
* <P>
158
* In this class, the format of "{@literal <name type>}" is
159
* "[LITERAL] (INTEGER)", where the optional "LITERAL" is the literal
160
* name, and INTEGER is the integer value of the name type. The format
161
* of "{@literal <name value>}" is "XX:...:XX", where "XX" is the
162
* hexadecimal digit representation of a byte value. For example, a
163
* returned value of an pseudo server name may look like:
164
* <pre>
165
* "type=(31), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"
166
* </pre>
167
* or
168
* <pre>
169
* "type=host_name (0), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"
170
* </pre>
171
*
172
* <P>
173
* Please NOTE that the exact details of the representation are unspecified
174
* and subject to change, and subclasses may override the method with
175
* their own formats.
176
*
177
* @return a string representation of this server name
178
*/
179
@Override
180
public String toString() {
181
if (type == StandardConstants.SNI_HOST_NAME) {
182
return "type=host_name (0), value=" + toHexString(encoded);
183
} else {
184
return "type=(" + type + "), value=" + toHexString(encoded);
185
}
186
}
187
188
// convert byte array to hex string
189
private static String toHexString(byte[] bytes) {
190
if (bytes.length == 0) {
191
return "(empty)";
192
}
193
return HexFormat.ofDelimiter(":").withUpperCase().formatHex(bytes);
194
}
195
}
196
197
198