forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWolfSSLSocket.java
More file actions
3006 lines (2556 loc) · 102 KB
/
WolfSSLSocket.java
File metadata and controls
3006 lines (2556 loc) · 102 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* WolfSSLSocket.java
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl.provider.jsse;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.function.BiFunction;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.nio.channels.SocketChannel;
import java.security.cert.CertificateEncodingException;
import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLException;
import com.wolfssl.WolfSSL;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.WolfSSLException;
import com.wolfssl.WolfSSLIOSendCallback;
import com.wolfssl.WolfSSLIORecvCallback;
import com.wolfssl.WolfSSLALPNSelectCallback;
import com.wolfssl.WolfSSLJNIException;
import com.wolfssl.WolfSSLSession;
/**
* wolfSSL implementation of SSLSocket
*
* @author wolfSSL
*/
public class WolfSSLSocket extends SSLSocket {
private WolfSSLAuthStore authStore = null;
/* WOLFSSL_CTX reference, passed down to this class */
private com.wolfssl.WolfSSLContext ctx = null;
/* WOLFSSL reference, created in this class */
private WolfSSLSession ssl = null;
private WolfSSLParameters params = null;
private WolfSSLEngineHelper EngineHelper = null;
private Socket socket = null;
private boolean autoClose;
private WolfSSLInputStream inStream;
private WolfSSLOutputStream outStream;
/* Track active I/O operations to prevent use-after-free */
private final java.util.concurrent.atomic.AtomicInteger activeOperations =
new java.util.concurrent.atomic.AtomicInteger(0);
private ArrayList<HandshakeCompletedListener> hsListeners = null;
/** TLS handshake initialization called */
protected volatile boolean handshakeInitCalled = false;
/** TLS handshake has been started */
protected volatile boolean handshakeStarted = false;
/** TLS handshake has completed */
protected volatile boolean handshakeComplete = false;
/** Connection to peer has closed */
protected volatile boolean connectionClosed = false;
/** Flag representing if I/O callbacks have been set */
private boolean ioCallbacksSet = false;
/** Flag representing if native fd has been set */
private boolean fdSet = false;
/* lock for handshakInitCalled and handshakeComplete */
private final Object handshakeLock = new Object();
/* protect read/write/connect/accept from multiple threads simultaneously
* accessing WolfSSLSession object / WOLFSSL struct */
private final Object ioLock = new Object();
/* lock for get/set of SO timeout */
private final Object timeoutLock = new Object();
/* lock and status for WolfSSLSocket initialization */
private boolean isInitialized = false;
private final Object initLock = new Object();
/** ALPN selector callback, if set */
protected BiFunction<SSLSocket, List<String>, String> alpnSelector = null;
/* true if client, otherwise false */
private boolean isClientMode = false;
/**
* Create new WolfSSLSocket object
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode)
throws IOException {
super();
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.autoClose = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ")");
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param host InetAddress of peer hostname
* @param port port of peer
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, InetAddress host, int port)
throws IOException {
super(host, port);
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.autoClose = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", InetAddress, port: " +
port + ")");
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, port, host);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param address InetAddress of peer hostname
* @param port port of peer
* @param localAddress local InetAddress to use for SSLSocket
* @param localPort local port to use for SSLSocket
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, InetAddress address, int port,
InetAddress localAddress, int localPort)
throws IOException {
super(address, port, localAddress, localPort);
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.autoClose = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", InetAddress, port: " +
port + ", InetAddress, localPort: " + localPort + ")");
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, port, address);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param host String of peer hostname
* @param port port of peer
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, String host, int port)
throws IOException {
super(host, port);
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.autoClose = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", host: " + host + ", port: " +
port + ")");
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, port, host);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param host InetAddress of peer hostname
* @param port port of peer
* @param localHost local InetAddress to use for SSLSocket
* @param localPort local port to use for SSLSocket
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, String host, int port, InetAddress localHost,
int localPort)
throws IOException {
super(host, port, localHost, localPort);
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.autoClose = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", host: " + host + ", port: " +
port + ", InetAddress, locaPort: " + localPort + ")");
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, port, host);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object layered over an existing Socket
* connected to the named host, at the given port.
*
* host/port refer to logical peer, but Socket could be connected to
* a proxy.
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param s existing connected Socket
* @param host String with peer hostname
* @param port port of peer
* @param autoClose automatically close wrapped Socket when finished
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, Socket s, String host, int port,
boolean autoClose) throws IOException {
super();
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.socket = s;
this.autoClose = autoClose;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", Socket, host: " + host +
", port: " + port + ", autoClose: " +
String.valueOf(autoClose) + ")");
if (s == null) {
throw new NullPointerException("Socket is null");
}
/* socket should already be connected */
if (!s.isConnected()) {
throw new IOException("Socket is not connected");
}
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, port, host);
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object layered over an existing Socket.
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param clientMode true if this is a client socket, otherwise false
* @param s existing connected Socket
* @param autoClose automatically close wrapped Socket when finished
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params,
boolean clientMode, Socket s, boolean autoClose)
throws IOException {
super();
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.socket = s;
this.autoClose = autoClose;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(clientMode: " +
String.valueOf(clientMode) + ", Socket, autoClose: " +
String.valueOf(autoClose) + ")");
if (!s.isConnected()) {
throw new IOException("Socket is not connected");
}
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, s.getPort(), s.getInetAddress());
EngineHelper.setUseClientMode(clientMode);
this.isClientMode = clientMode;
} catch (WolfSSLException e) {
throw new IOException(e);
}
}
/**
* Create new WolfSSLSocket object layered over an existing Socket,
* only a server mode Socket. Use pre-consumed InputStream data
* if provided.
*
* @param context WolfSSLContext to use with this SSLSocket
* @param authStore WolfSSLAuthStore to use with this SSLSocket
* @param params WolfSSLParameters to use with this SSLSocket
* @param s existing connected Socket
* @param consumed pre-consumed Socket data to use for this SSLSocket
* @param autoClose automatically close wrapped Socket when finished
*
* @throws IOException if initialization fails
*/
public WolfSSLSocket(com.wolfssl.WolfSSLContext context,
WolfSSLAuthStore authStore, WolfSSLParameters params, Socket s,
InputStream consumed, boolean autoClose) throws IOException {
super();
this.ctx = context;
this.authStore = authStore;
this.params = params.copy();
this.socket = s;
this.autoClose = autoClose;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating new WolfSSLSocket(Socket, InputStream, " +
"autoClose: " + String.valueOf(autoClose) + ")");
if (s == null ) {
throw new NullPointerException("Socket is null");
}
if (!s.isConnected()) {
throw new IOException("Socket is not connected");
}
try {
initSSL();
EngineHelper = new WolfSSLEngineHelper(this.ssl, this.authStore,
this.params, s.getPort(), s.getInetAddress());
EngineHelper.setUseClientMode(false);
this.isClientMode = false;
/* register custom receive callback to read consumed first */
if (consumed != null) {
ConsumedRecvCallback recvCb = new ConsumedRecvCallback();
this.ssl.setIORecv(recvCb);
ConsumedRecvCtx recvCtx = new ConsumedRecvCtx(s, consumed);
this.ssl.setIOReadCtx(recvCtx);
this.ioCallbacksSet = true;
}
} catch (WolfSSLException | WolfSSLJNIException e) {
throw new IOException(e);
}
}
/**
* Create new internal WolfSSLSession object for use with this SSLSocket.
*
* @throws WolfSSLException on error creating WolfSSLSession
*/
private void initSSL() throws WolfSSLException {
/* Initialize WolfSSLSession object, wraps WOLFSSL structure. */
ssl = new WolfSSLSession(ctx);
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "created new native WOLFSSL");
}
/**
* Initialize this WolfSSLSocket.
*
* Internal method, should be called before any handshake, I/O, or
* other operations are conducted that would rely on a set up key/cert,
* file descriptor, or I/O callback.
*
* This logic is not included directly in WolfSSLSocket constructors
* to avoid possible 'this' escape before subclass is fully initialized
* when using 'this' from setFd().
*
* @throws IOException if initialization fails
*/
private void checkAndInitSSLSocket() throws IOException {
synchronized (initLock) {
/* If underlying Socket connected, set fd. Check before
* initialized flag, since we may have already initialized
* certs/keys but not fd in previous call */
if (!this.fdSet && isConnected()) {
try {
setFd();
} catch (WolfSSLException e) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Failed to set native fd, may try again later");
}
}
if (isInitialized) {
return;
}
try {
/* Load private key and cert chain from WolfSSLAuthStore */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "loading private key and cert chain");
if (this.socket != null) {
EngineHelper.LoadKeyAndCertChain(this.socket, null);
} else {
EngineHelper.LoadKeyAndCertChain(this, null);
}
isInitialized = true;
} catch (WolfSSLException | CertificateEncodingException |
IOException e) {
throw new IOException(e);
}
}
}
/**
* Register I/O callbacks with native wolfSSL which use
* Input/OutputStream of the wrapped Socket object.
*
* Called by setFd() if ssl.setFd() fails to find or set the internal
* SocketImpl file descriptor.
*
* @throws WolfSSLException if this.socket is null or setting I/O
* callbacks or ctx fails
*/
private void setIOCallbacks() throws WolfSSLException {
if (this.socket == null) {
throw new WolfSSLException(
"Internal Socket is null, unable to set I/O callbacks");
}
if (this.ioCallbacksSet) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "wolfSSL I/O callbacks already set, skipping");
return;
}
try {
/* Register send callback and context */
SocketSendCallback sendCb = new SocketSendCallback();
this.ssl.setIOSend(sendCb);
SocketSendCtx writeCtx = new SocketSendCtx(this.socket);
this.ssl.setIOWriteCtx(writeCtx);
/* Register recv callback and context */
SocketRecvCallback recvCb = new SocketRecvCallback();
this.ssl.setIORecv(recvCb);
SocketRecvCtx readCtx = new SocketRecvCtx(this.socket);
this.ssl.setIOReadCtx(readCtx);
} catch (WolfSSLJNIException e) {
throw new WolfSSLException(e);
}
}
private void setFd() throws IllegalArgumentException, WolfSSLException {
int ret;
if (ssl == null) {
throw new IllegalArgumentException("WolfSSLSession object is null");
}
/* Synchronized on ioLock to prevent read/write/connect/accept calls
* from possibly being called before descriptor or I/O callbacks
* have been set */
synchronized (ioLock) {
if (this.socket == null) {
ret = ssl.setFd(this);
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Failed to set native Socket fd");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "registered SSLSocket(this) with native wolfSSL");
} else {
ret = ssl.setFd(this.socket);
if (ret != WolfSSL.SSL_SUCCESS) {
/* Failed to find/set internal SocketImpl file descriptor.
* Try using I/O callbacks instead with
* Input/OutputStream */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Failed to set native SocketImpl fd, " +
"trying I/O callbacks");
setIOCallbacks();
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "registered underlying Socket with " +
"wolfSSL I/O callbacks");
}
else {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "registered Socket(this.socket) with " +
"native wolfSSL");
}
}
/* Mark fd set */
this.fdSet = true;
}
}
/**
* Returns unique SocketChannel object assiciated with this socket.
*/
@Override
public final SocketChannel getChannel() {
if (this.socket != null) {
return this.socket.getChannel();
} else {
return super.getChannel();
}
}
/**
* Get the address of the remote peer.
*/
@Override
public final InetAddress getInetAddress() {
if (this.socket != null) {
return this.socket.getInetAddress();
} else {
return super.getInetAddress();
}
}
/**
* Get the local address the socket is bound to.
*/
@Override
public final InetAddress getLocalAddress() {
if (this.socket != null) {
return this.socket.getLocalAddress();
} else {
return super.getLocalAddress();
}
}
/**
* Get remote port number used by this socket.
*/
@Override
public final int getPort() {
if (this.socket != null) {
return this.socket.getPort();
} else {
return super.getPort();
}
}
/**
* Get local port number used by this socket.
*/
@Override
public final int getLocalPort() {
if (this.socket != null) {
return this.socket.getLocalPort();
} else {
return super.getLocalPort();
}
}
/**
* Tests if SO_KEEPALIVE is enabled on this socket.
*/
@Override
public final boolean getKeepAlive() throws SocketException {
if (this.socket != null) {
return this.socket.getKeepAlive();
} else {
return super.getKeepAlive();
}
}
/**
* Tests if SO_REUSEADDR is enabled on this socket.
*/
@Override
public final boolean getReuseAddress() throws SocketException {
if (this.socket != null) {
return this.socket.getReuseAddress();
} else {
return super.getReuseAddress();
}
}
/**
* Return address of the endpoint that this socket is bound to,
* or null if not bound yet.
*/
@Override
public final SocketAddress getLocalSocketAddress() {
if (this.socket != null) {
return this.socket.getLocalSocketAddress();
} else {
return super.getLocalSocketAddress();
}
}
/**
* Tests if OOBINLINE is enabled.
*/
@Override
public final boolean getOOBInline() throws SocketException {
if (this.socket != null) {
return this.socket.getOOBInline();
} else {
return super.getOOBInline();
}
}
/**
* Gets the value of the SO_RCVBUF option for this socket.
*/
@Override
public final synchronized int getReceiveBufferSize()
throws SocketException {
if (this.socket != null) {
return this.socket.getReceiveBufferSize();
} else {
return super.getReceiveBufferSize();
}
}
/**
* Returns the address of the remote endpoint, or null if not connected.
*/
@Override
public final SocketAddress getRemoteSocketAddress() {
if (this.socket != null) {
return this.socket.getRemoteSocketAddress();
} else {
return super.getRemoteSocketAddress();
}
}
/**
* Gets the value of the SO_SNDBUF option for this socket.
*/
@Override
public final synchronized int getSendBufferSize()
throws SocketException {
if (this.socket != null) {
return this.socket.getSendBufferSize();
} else {
return super.getSendBufferSize();
}
}
/**
* Gets the value of the SO_SNDBUF option for this socket. This setting
* only affects socket close.
* @return -1 if option is disabled, otherwise int value
*/
@Override
public final int getSoLinger() throws SocketException {
if (this.socket != null) {
return this.socket.getSoLinger();
} else {
return super.getSoLinger();
}
}
/**
* Tests if TCP_NODELAY is enabled.
*/
@Override
public final boolean getTcpNoDelay() throws SocketException {
if (this.socket != null) {
return this.socket.getTcpNoDelay();
} else {
return super.getTcpNoDelay();
}
}
/**
* Gets traffic class or type of service in IP header.
*/
@Override
public final int getTrafficClass() throws SocketException {
if (this.socket != null) {
return this.socket.getTrafficClass();
} else {
return super.getTrafficClass();
}
}
/**
* Returns the binding state for this socket.
*/
@Override
public final boolean isBound() {
if (this.socket != null) {
return this.socket.isBound();
} else {
return super.isBound();
}
}
/**
* Returns the closed state of the socket.
*/
@Override
public final boolean isClosed() {
if (this.socket != null) {
return this.socket.isClosed();
} else {
return super.isClosed();
}
}
/**
* Returns the connection state of this socket.
*/
@Override
public final boolean isConnected() {
if (this.socket != null) {
return this.socket.isConnected();
} else {
return super.isConnected();
}
}
/**
* Returns whether the read-half of the socket connection is closed.
*/
@Override
public final boolean isInputShutdown() {
if (this.socket != null) {
return this.socket.isInputShutdown();
} else {
return super.isInputShutdown();
}
}
/**
* Returns whether the write-half of the socket connection is closed.
*/
@Override
public final boolean isOutputShutdown() {
if (this.socket != null) {
return this.socket.isOutputShutdown();
} else {
return super.isOutputShutdown();
}
}
/**
* Send one byte of urgent data on the socket.
* Not supported by SSLSockets at this point.
*/
@Override
public final void sendUrgentData(int data) throws IOException {
throw new SocketException(
"sendUrgentData() not supported by WolfSSLSocket");
}
/**
* Enable/disable SO_KEEPALIVE.
*/
@Override
public final void setKeepAlive(boolean on) throws SocketException {
if (this.socket != null) {
this.socket.setKeepAlive(on);
} else {
super.setKeepAlive(on);
}
}
/**
* Enable/disable SO_KEEPALIVE.
* Enable/disable OOBINLINE (receipt of TCP urgent data). This option
* is disabled by default. Setting OOBInline does not have any effect
* on WolfSSLSocket since SSLSocket does not support sending urgent data.
*/
@Override
public final void setOOBInline(boolean on) throws SocketException {
throw new SocketException("setOOBInline is ineffective, as sending " +
"urgent data is not supported with SSLSocket");
}
/**
* Set performance preferences for this socket.
*/
@Override
public final void setPerformancePreferences(int connectionTime,
int latency, int bandwidth) {
if (this.socket != null) {
this.socket.setPerformancePreferences(connectionTime,
latency, bandwidth);
} else {
super.setPerformancePreferences(connectionTime, latency,
bandwidth);
}
}
/**
* Sets the SO_RCVBUF option to the specified value for this Socket.
*/
@Override
public final synchronized void setReceiveBufferSize(int size)
throws SocketException {
if (this.socket != null) {
this.socket.setReceiveBufferSize(size);
} else {
super.setReceiveBufferSize(size);
}
}
/**
* Enable/disable the SO_REUSEADDR socket option.
*/
@Override
public final void setReuseAddress(boolean on) throws SocketException {
if (this.socket != null) {
this.socket.setReuseAddress(on);
} else {
super.setReuseAddress(on);
}
}
/**
* Sets the SO_SNDBUF option to the specified value for this Socket.
*/
@Override
public final synchronized void setSendBufferSize(int size)
throws SocketException {
if (this.socket != null) {
this.socket.setSendBufferSize(size);
} else {
super.setSendBufferSize(size);
}
}
/**
* Enable/disable SO_LINGER with specified linger time in seconds.
*/
@Override
public final void setSoLinger(boolean on, int linger)
throws SocketException {
if (this.socket != null) {
this.socket.setSoLinger(on, linger);
} else {
super.setSoLinger(on, linger);
}
}
/**
* Enable/disable TCP_NODELAY on this Socket.
*/
@Override
public final void setTcpNoDelay(boolean on) throws SocketException {
if (this.socket != null) {
this.socket.setTcpNoDelay(on);