Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/ssl/BaseSSLSocketImpl.java
41159 views
1
/*
2
* Copyright (c) 2002, 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 sun.security.ssl;
27
28
import java.io.*;
29
import java.net.*;
30
import java.nio.channels.SocketChannel;
31
import java.util.Set;
32
import javax.net.ssl.*;
33
34
/**
35
* Abstract base class for SSLSocketImpl.
36
*
37
* Its purpose is to house code with no SSL related logic (or no logic at all).
38
* This makes SSLSocketImpl shorter and easier to read. It contains a few
39
* constants and static methods plus overridden java.net.Socket methods.
40
*
41
* Methods are defined final to ensure that they are not accidentally
42
* overridden in SSLSocketImpl.
43
*
44
* @see javax.net.ssl.SSLSocket
45
* @see SSLSocketImpl
46
*/
47
abstract class BaseSSLSocketImpl extends SSLSocket {
48
49
/*
50
* Normally "self" is "this" ... but not when this connection is
51
* layered over a preexisting socket. If we're using an existing
52
* socket, we delegate some actions to it. Else, we delegate
53
* instead to "super". This is important to ensure that we don't
54
* recurse infinitely ... e.g. close() calling itself, or doing
55
* I/O in terms of our own streams.
56
*/
57
private final Socket self;
58
private final InputStream consumedInput;
59
60
BaseSSLSocketImpl() {
61
super();
62
this.self = this;
63
this.consumedInput = null;
64
}
65
66
BaseSSLSocketImpl(Socket socket) {
67
super();
68
this.self = socket;
69
this.consumedInput = null;
70
}
71
72
BaseSSLSocketImpl(Socket socket, InputStream consumed) {
73
super();
74
this.self = socket;
75
this.consumedInput = consumed;
76
}
77
78
//
79
// CONSTANTS AND STATIC METHODS
80
//
81
82
/**
83
* TLS requires that a close_notify warning alert is sent before the
84
* connection is closed in order to avoid truncation attacks. Some
85
* implementations (MS IIS and others) don't do that. The property
86
* below controls whether we accept that or treat it as an error.
87
*
88
* The default is "false", i.e. tolerate the broken behavior.
89
*/
90
private static final String PROP_NAME =
91
"com.sun.net.ssl.requireCloseNotify";
92
93
static final boolean requireCloseNotify =
94
Utilities.getBooleanProperty(PROP_NAME, false);
95
96
//
97
// MISC SOCKET METHODS
98
//
99
100
/**
101
* Returns the unique {@link java.nio.channels.SocketChannel SocketChannel}
102
* object associated with this socket, if any.
103
*
104
* @see java.net.Socket#getChannel
105
*/
106
@Override
107
public final SocketChannel getChannel() {
108
if (self == this) {
109
return super.getChannel();
110
} else {
111
return self.getChannel();
112
}
113
}
114
115
/**
116
* Binds the address to the socket.
117
* @see java.net.Socket#bind
118
*/
119
@Override
120
public void bind(SocketAddress bindpoint) throws IOException {
121
/*
122
* Bind to this socket
123
*/
124
if (self == this) {
125
super.bind(bindpoint);
126
} else {
127
// If we're binding on a layered socket...
128
throw new IOException(
129
"Underlying socket should already be connected");
130
}
131
}
132
133
/**
134
* Returns the address of the endpoint this socket is connected to
135
* @see java.net.Socket#getLocalSocketAddress
136
*/
137
@Override
138
public SocketAddress getLocalSocketAddress() {
139
if (self == this) {
140
return super.getLocalSocketAddress();
141
} else {
142
return self.getLocalSocketAddress();
143
}
144
}
145
146
/**
147
* Returns the address of the endpoint this socket is connected to
148
* @see java.net.Socket#getRemoteSocketAddress
149
*/
150
@Override
151
public SocketAddress getRemoteSocketAddress() {
152
if (self == this) {
153
return super.getRemoteSocketAddress();
154
} else {
155
return self.getRemoteSocketAddress();
156
}
157
}
158
159
/**
160
* Connects this socket to the server.
161
*
162
* This method is either called on an unconnected SSLSocketImpl by the
163
* application, or it is called in the constructor of a regular
164
* SSLSocketImpl. If we are layering on top on another socket, then
165
* this method should not be called, because we assume that the
166
* underlying socket is already connected by the time it is passed to
167
* us.
168
*
169
* @param endpoint the <code>SocketAddress</code>
170
* @throws IOException if an error occurs during the connection
171
*/
172
@Override
173
public final void connect(SocketAddress endpoint) throws IOException {
174
connect(endpoint, 0);
175
}
176
177
/**
178
* Returns the connection state of the socket.
179
* @see java.net.Socket#isConnected
180
*/
181
@Override
182
public final boolean isConnected() {
183
if (self == this) {
184
return super.isConnected();
185
} else {
186
return self.isConnected();
187
}
188
}
189
190
/**
191
* Returns the binding state of the socket.
192
* @see java.net.Socket#isBound
193
*/
194
@Override
195
public final boolean isBound() {
196
if (self == this) {
197
return super.isBound();
198
} else {
199
return self.isBound();
200
}
201
}
202
203
//
204
// CLOSE RELATED METHODS
205
//
206
207
/**
208
* Places the input stream for this socket at "end of stream". Any data
209
* sent to the input stream side of the socket is acknowledged and then
210
* silently discarded.
211
*
212
* @see java.net.Socket#shutdownInput
213
*/
214
@Override
215
public void shutdownInput() throws IOException {
216
if (self == this) {
217
super.shutdownInput();
218
} else {
219
self.shutdownInput();
220
}
221
}
222
223
/**
224
* Disables the output stream for this socket. For a TCP socket, any
225
* previously written data will be sent followed by TCP's normal
226
* connection termination sequence.
227
*
228
* @see java.net.Socket#shutdownOutput
229
*/
230
@Override
231
public void shutdownOutput() throws IOException {
232
if (self == this) {
233
super.shutdownOutput();
234
} else {
235
self.shutdownOutput();
236
}
237
}
238
239
/**
240
* Returns the input state of the socket
241
* @see java.net.Socket#isInputShutdown
242
*/
243
@Override
244
public boolean isInputShutdown() {
245
if (self == this) {
246
return super.isInputShutdown();
247
} else {
248
return self.isInputShutdown();
249
}
250
}
251
252
/**
253
* Returns the output state of the socket
254
* @see java.net.Socket#isOutputShutdown
255
*/
256
@Override
257
public boolean isOutputShutdown() {
258
if (self == this) {
259
return super.isOutputShutdown();
260
} else {
261
return self.isOutputShutdown();
262
}
263
}
264
265
/**
266
* Ensures that the SSL connection is closed down as cleanly
267
* as possible, in case the application forgets to do so.
268
* This allows SSL connections to be implicitly reclaimed,
269
* rather than forcing them to be explicitly reclaimed at
270
* the penalty of prematurly killing SSL sessions.
271
*/
272
@Override
273
@SuppressWarnings("deprecation")
274
protected final void finalize() throws Throwable {
275
try {
276
close();
277
} catch (IOException e1) {
278
try {
279
if (self == this) {
280
super.close();
281
}
282
} catch (IOException e2) {
283
// ignore
284
}
285
} finally {
286
// We called close on the underlying socket above to
287
// make doubly sure all resources got released. We
288
// don't finalize self in the case of overlain sockets,
289
// that's a different object which the GC will finalize
290
// separately.
291
292
super.finalize();
293
}
294
}
295
296
//
297
// GET ADDRESS METHODS
298
//
299
300
/**
301
* Returns the address of the remote peer for this connection.
302
*/
303
@Override
304
public final InetAddress getInetAddress() {
305
if (self == this) {
306
return super.getInetAddress();
307
} else {
308
return self.getInetAddress();
309
}
310
}
311
312
/**
313
* Gets the local address to which the socket is bound.
314
*
315
* @return the local address to which the socket is bound.
316
* @since 1.1
317
*/
318
@Override
319
public final InetAddress getLocalAddress() {
320
if (self == this) {
321
return super.getLocalAddress();
322
} else {
323
return self.getLocalAddress();
324
}
325
}
326
327
/**
328
* Returns the number of the remote port that this connection uses.
329
*/
330
@Override
331
public final int getPort() {
332
if (self == this) {
333
return super.getPort();
334
} else {
335
return self.getPort();
336
}
337
}
338
339
/**
340
* Returns the number of the local port that this connection uses.
341
*/
342
@Override
343
public final int getLocalPort() {
344
if (self == this) {
345
return super.getLocalPort();
346
} else {
347
return self.getLocalPort();
348
}
349
}
350
351
//
352
// SOCKET OPTION METHODS
353
//
354
355
/**
356
* Enables or disables the Nagle optimization.
357
* @see java.net.Socket#setTcpNoDelay
358
*/
359
@Override
360
public final void setTcpNoDelay(boolean value) throws SocketException {
361
if (self == this) {
362
super.setTcpNoDelay(value);
363
} else {
364
self.setTcpNoDelay(value);
365
}
366
}
367
368
/**
369
* Returns true if the Nagle optimization is disabled. This
370
* relates to low-level buffering of TCP traffic, delaying the
371
* traffic to promote better throughput.
372
*
373
* @see java.net.Socket#getTcpNoDelay
374
*/
375
@Override
376
public final boolean getTcpNoDelay() throws SocketException {
377
if (self == this) {
378
return super.getTcpNoDelay();
379
} else {
380
return self.getTcpNoDelay();
381
}
382
}
383
384
/**
385
* Assigns the socket's linger timeout.
386
* @see java.net.Socket#setSoLinger
387
*/
388
@Override
389
public final void setSoLinger(boolean flag, int linger)
390
throws SocketException {
391
if (self == this) {
392
super.setSoLinger(flag, linger);
393
} else {
394
self.setSoLinger(flag, linger);
395
}
396
}
397
398
/**
399
* Returns the socket's linger timeout.
400
* @see java.net.Socket#getSoLinger
401
*/
402
@Override
403
public final int getSoLinger() throws SocketException {
404
if (self == this) {
405
return super.getSoLinger();
406
} else {
407
return self.getSoLinger();
408
}
409
}
410
411
/**
412
* Send one byte of urgent data on the socket.
413
* @see java.net.Socket#sendUrgentData
414
* At this point, there seems to be no specific requirement to support
415
* this for an SSLSocket. An implementation can be provided if a need
416
* arises in future.
417
*/
418
@Override
419
public final void sendUrgentData(int data) throws SocketException {
420
throw new SocketException("This method is not supported "
421
+ "by SSLSockets");
422
}
423
424
/**
425
* Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this
426
* option is disabled and TCP urgent data received on a socket is silently
427
* discarded.
428
* @see java.net.Socket#setOOBInline
429
* Setting OOBInline does not have any effect on SSLSocket,
430
* since currently we don't support sending urgent data.
431
*/
432
@Override
433
public final void setOOBInline(boolean on) throws SocketException {
434
throw new SocketException("This method is ineffective, since"
435
+ " sending urgent data is not supported by SSLSockets");
436
}
437
438
/**
439
* Tests if OOBINLINE is enabled.
440
* @see java.net.Socket#getOOBInline
441
*/
442
@Override
443
public final boolean getOOBInline() throws SocketException {
444
throw new SocketException("This method is ineffective, since"
445
+ " sending urgent data is not supported by SSLSockets");
446
}
447
448
/**
449
* Returns the socket timeout.
450
* @see java.net.Socket#getSoTimeout
451
*/
452
@Override
453
public final int getSoTimeout() throws SocketException {
454
if (self == this) {
455
return super.getSoTimeout();
456
} else {
457
return self.getSoTimeout();
458
}
459
}
460
461
@Override
462
public final void setSendBufferSize(int size) throws SocketException {
463
if (self == this) {
464
super.setSendBufferSize(size);
465
} else {
466
self.setSendBufferSize(size);
467
}
468
}
469
470
@Override
471
public final int getSendBufferSize() throws SocketException {
472
if (self == this) {
473
return super.getSendBufferSize();
474
} else {
475
return self.getSendBufferSize();
476
}
477
}
478
479
@Override
480
public final void setReceiveBufferSize(int size) throws SocketException {
481
if (self == this) {
482
super.setReceiveBufferSize(size);
483
} else {
484
self.setReceiveBufferSize(size);
485
}
486
}
487
488
@Override
489
public final int getReceiveBufferSize() throws SocketException {
490
if (self == this) {
491
return super.getReceiveBufferSize();
492
} else {
493
return self.getReceiveBufferSize();
494
}
495
}
496
497
/**
498
* Enable/disable SO_KEEPALIVE.
499
* @see java.net.Socket#setKeepAlive
500
*/
501
@Override
502
public final void setKeepAlive(boolean on) throws SocketException {
503
if (self == this) {
504
super.setKeepAlive(on);
505
} else {
506
self.setKeepAlive(on);
507
}
508
}
509
510
/**
511
* Tests if SO_KEEPALIVE is enabled.
512
* @see java.net.Socket#getKeepAlive
513
*/
514
@Override
515
public final boolean getKeepAlive() throws SocketException {
516
if (self == this) {
517
return super.getKeepAlive();
518
} else {
519
return self.getKeepAlive();
520
}
521
}
522
523
/**
524
* Sets traffic class or type-of-service octet in the IP header for
525
* packets sent from this Socket.
526
* @see java.net.Socket#setTrafficClass
527
*/
528
@Override
529
public final void setTrafficClass(int tc) throws SocketException {
530
if (self == this) {
531
super.setTrafficClass(tc);
532
} else {
533
self.setTrafficClass(tc);
534
}
535
}
536
537
/**
538
* Gets traffic class or type-of-service in the IP header for packets
539
* sent from this Socket.
540
* @see java.net.Socket#getTrafficClass
541
*/
542
@Override
543
public final int getTrafficClass() throws SocketException {
544
if (self == this) {
545
return super.getTrafficClass();
546
} else {
547
return self.getTrafficClass();
548
}
549
}
550
551
/**
552
* Enable/disable SO_REUSEADDR.
553
* @see java.net.Socket#setReuseAddress
554
*/
555
@Override
556
public final void setReuseAddress(boolean on) throws SocketException {
557
if (self == this) {
558
super.setReuseAddress(on);
559
} else {
560
self.setReuseAddress(on);
561
}
562
}
563
564
/**
565
* Tests if SO_REUSEADDR is enabled.
566
* @see java.net.Socket#getReuseAddress
567
*/
568
@Override
569
public final boolean getReuseAddress() throws SocketException {
570
if (self == this) {
571
return super.getReuseAddress();
572
} else {
573
return self.getReuseAddress();
574
}
575
}
576
577
/**
578
* Sets performance preferences for this socket.
579
*
580
* @see java.net.Socket#setPerformancePreferences(int, int, int)
581
*/
582
@Override
583
public void setPerformancePreferences(int connectionTime,
584
int latency, int bandwidth) {
585
if (self == this) {
586
super.setPerformancePreferences(
587
connectionTime, latency, bandwidth);
588
} else {
589
self.setPerformancePreferences(
590
connectionTime, latency, bandwidth);
591
}
592
}
593
594
@Override
595
public String toString() {
596
if (self == this) {
597
return super.toString();
598
}
599
600
return self.toString();
601
}
602
603
@Override
604
public InputStream getInputStream() throws IOException {
605
if (self == this) {
606
return super.getInputStream();
607
}
608
609
if (consumedInput != null) {
610
return new SequenceInputStream(consumedInput,
611
self.getInputStream());
612
}
613
614
return self.getInputStream();
615
}
616
617
@Override
618
public OutputStream getOutputStream() throws IOException {
619
if (self == this) {
620
return super.getOutputStream();
621
}
622
623
return self.getOutputStream();
624
}
625
626
@Override
627
public void close() throws IOException {
628
if (self == this) {
629
super.close();
630
} else {
631
self.close();
632
}
633
}
634
635
@Override
636
public void setSoTimeout(int timeout) throws SocketException {
637
if (self == this) {
638
super.setSoTimeout(timeout);
639
} else {
640
self.setSoTimeout(timeout);
641
}
642
}
643
644
@Override
645
public <T> Socket setOption(SocketOption<T> name,
646
T value) throws IOException {
647
if (self == this) {
648
return super.setOption(name, value);
649
} else {
650
return self.setOption(name, value);
651
}
652
}
653
654
@Override
655
public <T> T getOption(SocketOption<T> name) throws IOException {
656
if (self == this) {
657
return super.getOption(name);
658
} else {
659
return self.getOption(name);
660
}
661
}
662
663
@Override
664
public Set<SocketOption<?>> supportedOptions() {
665
if (self == this) {
666
return super.supportedOptions();
667
} else {
668
return self.supportedOptions();
669
}
670
}
671
672
boolean isLayered() {
673
return (self != this);
674
}
675
}
676
677