-
Notifications
You must be signed in to change notification settings - Fork 605
/
Copy pathSSLConnectionLink.java
1380 lines (1248 loc) · 58.8 KB
/
SSLConnectionLink.java
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
/*******************************************************************************
* Copyright (c) 1997, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*
* 092122 loriad 287316 Add local and remote host:port to handshake error message
*******************************************************************************/
package com.ibm.ws.channel.ssl.internal;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ReadOnlyBufferException;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLException;
import com.ibm.websphere.channelfw.FlowType;
import com.ibm.websphere.channelfw.osgi.CHFWBundle;
import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.ws.channel.ssl.internal.SSLAlpnNegotiator.ThirdPartyAlpnNegotiator;
import com.ibm.ws.ffdc.FFDCFilter;
import com.ibm.wsspi.bytebuffer.WsByteBuffer;
import com.ibm.wsspi.bytebuffer.WsByteBufferUtils;
import com.ibm.wsspi.channelfw.ConnectionLink;
import com.ibm.wsspi.channelfw.ConnectionReadyCallback;
import com.ibm.wsspi.channelfw.DiscriminationProcess;
import com.ibm.wsspi.channelfw.Discriminator;
import com.ibm.wsspi.channelfw.OutboundConnectionLink;
import com.ibm.wsspi.channelfw.VirtualConnection;
import com.ibm.wsspi.channelfw.base.OutboundProtocolLink;
import com.ibm.wsspi.kernel.service.utils.FrameworkState;
import com.ibm.wsspi.tcpchannel.SSLConnectionContext;
import com.ibm.wsspi.tcpchannel.TCPConnectRequestContext;
import com.ibm.wsspi.tcpchannel.TCPConnectionContext;
import com.ibm.wsspi.tcpchannel.TCPReadCompletedCallback;
import com.ibm.wsspi.tcpchannel.TCPReadRequestContext;
import com.ibm.wsspi.tcpchannel.TCPRequestContext;
import com.ibm.wsspi.tcpchannel.TCPWriteRequestContext;
import io.openliberty.wsoc.ssl.SSLContextEnabledAddress;
/**
* Main Connection Link and TCPConnectionContext interface.
*/
public class SSLConnectionLink extends OutboundProtocolLink implements ConnectionLink, TCPConnectionContext {
/** Trace component for WAS. Protect for use by inner classes. */
protected static final TraceComponent tc = Tr.register(SSLConnectionLink.class,
SSLChannelConstants.SSL_TRACE_NAME,
SSLChannelConstants.SSL_BUNDLE);
/** VC statemap key used for the connlink configuration */
public static final String LINKCONFIG = "SSLLINKCONFIG";
/** SSL channel that created this link. */
private SSLChannel sslChannel = null;
/** Config of this specific connection */
private SSLLinkConfig linkConfig = null;
/** SSL engine associated with this connection. */
private SSLEngine sslEngine = null;
/** my Read interface for channels to use. */
private SSLReadServiceContext readInterface = null;
/** my write interface for channels to use. */
private SSLWriteServiceContext writeInterface = null;
/** Device side service context. */
private TCPConnectionContext deviceServiceContext = null;
/** Device side read interface. */
protected TCPReadRequestContext deviceReadInterface = null;
/** Device side write interface. */
private TCPWriteRequestContext deviceWriteInterface = null;
/** SSL connection context queriable via the TCP interface. */
private SSLConnectionContext sslConnectionContext = null;
/** Result from discrimination if it happened. */
private SSLDiscriminatorState discState = null;
/** Flag to indicate already connected. */
private volatile boolean connected = false;
/** Flag to indicate conn link is closed. */
private volatile boolean closed = false;
/** Reference flag on whether this is an inbound connection or not */
private boolean isInbound = false;
/** Flag on whether there was a sync connect failure or not */
private boolean syncConnectFailure = false;
/** Hash code of the VC used in debug messages. */
private int vcHashCode = 0;
/** SSL Context associated with this connection. */
private SSLContext sslContext = null;
/** Target address for outbound connects. */
private TCPConnectRequestContext targetAddress = null;
/** ALPN protocol negotiated for this link */
private String alpnProtocol;
/** Keep track of HTTP/2 support on this link */
private boolean http2Enabled = false;
/** The third party ALPN negotiator used for this link */
private ThirdPartyAlpnNegotiator alpnNegotiator = null;
private final Lock cleanupLock = new ReentrantLock();
/**
* Constructor. Fields assigned here stay the same for the life of
* the connection link, even across uses from the connection pool. Before
* the link is used, the init method will be called.
*
* @param inputChannel
*/
public SSLConnectionLink(SSLChannel inputChannel) {
this.sslChannel = inputChannel;
this.isInbound = inputChannel.getConfig().isInbound();
}
/*
* @see com.ibm.wsspi.channelfw.base.OutboundProtocolLink#init(com.ibm.wsspi.channelfw.VirtualConnection)
*/
@Override
public void init(VirtualConnection inVC) {
this.vcHashCode = inVC.hashCode();
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "init, vc=" + getVCHash());
}
// Create the read and write interfaces for this connection link.
super.init(inVC);
initInterfaces(new SSLConnectionContextImpl(this, !isInbound),
new SSLReadServiceContext(this),
new SSLWriteServiceContext(this));
// Check to see if http/2 is enabled for this connection and save the result
if (CHFWBundle.getServletConfiguredHttpVersionSetting() != null) {
Boolean defaultSetting = CHFWBundle.getHttp2DefaultSetting();
if (defaultSetting != null) {
Boolean configSetting = getChannel().getUseH2ProtocolAttribute();
if (Boolean.FALSE == defaultSetting) {
if (configSetting != null && configSetting.booleanValue()) {
http2Enabled = true;
this.sslChannel.checkandInitALPN();
}
} else {
if (configSetting == null || configSetting.booleanValue()) {
http2Enabled = true;
this.sslChannel.checkandInitALPN();
}
}
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "init");
}
}
void initInterfaces(SSLConnectionContext sslConnectionContext, SSLReadServiceContext readInterface, SSLWriteServiceContext writeInterface) {
this.sslConnectionContext = sslConnectionContext;
this.readInterface = readInterface;
this.writeInterface = writeInterface;
}
/*
* @see com.ibm.wsspi.channelfw.base.OutboundProtocolLink#close(com.ibm.wsspi.channelfw.VirtualConnection, java.lang.Exception)
*/
@Override
public void close(VirtualConnection inVC, Exception e) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "close, vc=" + getVCHash());
}
// Set closed flag so that ready can't be called again in an error condition.
// This is a protective measure.
closed = true;
// Clean up the read and write interfaces as well as the SSL engine.
// cleanup has logic to avoid writing if stop(0) has been called
cleanup();
//If the channel has already processed the close signal, it is too late to try and clean up the individual connection links here.
//This race condition should not happen if channels above us are well behaved, so not using synchronize logic here, so as not to
//impact mainline performance.
if (this.sslChannel.getstop0Called() != true) {
if (getDeviceLink() != null) {
getDeviceLink().close(inVC, e);
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "close");
}
}
/*
* @see com.ibm.wsspi.channelfw.base.OutboundProtocolLink#destroy(java.lang.Exception)
*/
@Override
public void destroy(Exception e) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "destroy, vc=" + getVCHash());
}
// Clean up the read and write interfaces as well as the SSL engine.
this.connected = false;
cleanup();
getVirtualConnection().getStateMap().remove(LINKCONFIG);
if (this.syncConnectFailure) {
// sync connect failure needs cleanup below us but not above
// us on the connlink chain
super.destroy();
} else {
// otherwise use the destroy that goes up the chain
super.destroy(e);
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "destroy");
}
}
/**
* This method is called from both close and destroy to clean up local resources.
* Avoid object synchronization, but ensure only one thread does cleanup at a time.
*/
public void cleanup() {
cleanupLock.lock();
try {
// Clean up the write interface.
if (null != writeInterface) {
this.writeInterface.close();
this.writeInterface = null;
}
// Clean up the read interface.
if (null != readInterface) {
this.readInterface.close();
this.readInterface = null;
}
// Clean up the SSL engine.
if (null != getSSLEngine()) {
//If the channel has already processed the stop signal, it is too late to try and send the write handshake
if (this.sslChannel.getstop0Called() == true) {
this.connected = false;
}
SSLUtils.shutDownSSLEngine(this, isInbound, this.connected);
this.sslEngine = null;
}
// mark that we have disconnected
this.connected = false;
} finally {
cleanupLock.unlock();
}
}
/*
* @see com.ibm.wsspi.channelfw.ConnectionLink#getChannelAccessor()
*/
@Override
public Object getChannelAccessor() {
return this;
}
/*
* @see com.ibm.wsspi.channelfw.base.OutboundProtocolLink#setDeviceLink(com.ibm.wsspi.channelfw.ConnectionLink)
*/
@Override
public void setDeviceLink(ConnectionLink next) {
super.setDeviceLink(next);
this.deviceServiceContext = (TCPConnectionContext) getDeviceLink().getChannelAccessor();
this.deviceReadInterface = this.deviceServiceContext.getReadInterface();
this.deviceWriteInterface = this.deviceServiceContext.getWriteInterface();
}
/**
* This method will be called at one of two times. If this connection link
* is part of an inbound chain, then this will be called when the device side
* channel has accepted a new connection and determined that this is the next
* channel in the chain. Note, that the Discriminator may have already been
* run. The second case where this method may be called is if this connection
* link is part of an outbound chain. In that case, this method will be called
* when the initial outbound connect reports back success.
*
* @see com.ibm.wsspi.channelfw.ConnectionReadyCallback#ready(VirtualConnection)
*/
@Override
public void ready(VirtualConnection inVC) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "ready, vc=" + getVCHash());
}
// Double check for error condition where close already happened. Protective measure.
if (!closed && FrameworkState.isValid()) {
try {
// Outbound connections took care of sslContext and sslEngine creation already.
// If inbound, discrimination may have already created the engine and context
if (isInbound) {
// See if discrimination ran already. Get the state map from the VC.
Map<Object, Object> stateMap = inVC.getStateMap();
// Extract and remove result of discrimination, if it happened.
discState = (SSLDiscriminatorState) stateMap.remove(SSLChannel.SSL_DISCRIMINATOR_STATE);
if (discState != null) {
// Discrimination has happened. Save already existing sslEngine.
sslEngine = discState.getEngine();
sslContext = discState.getSSLContext();
setLinkConfig((SSLLinkConfig) stateMap.get(SSLConnectionLink.LINKCONFIG));
} else if (sslContext == null || getSSLEngine() == null) {
// Create a new SSL context based on the current properties in the ssl config.
sslContext = getChannel().getSSLContextForInboundLink(this, inVC);
// Discrimination has not happened yet. Create new SSL engine.
sslEngine = SSLUtils.getSSLEngine(sslContext,
sslChannel.getConfig().getFlowType(),
getLinkConfig(),
this);
}
} else {
// Outbound connect is ready. Ensure we have an sslContext and sslEngine.
if (sslContext == null || getSSLEngine() == null) {
// Create a new SSL context based on the current properties in the ssl config.
sslContext = getChannel().getSSLContextForOutboundLink(this, inVC, targetAddress);
// PK46069 - use engine that allows session id re-use
sslEngine = SSLUtils.getOutboundSSLEngine(
sslContext, getLinkConfig(),
targetAddress.getRemoteAddress().getHostName(),
targetAddress.getRemoteAddress().getPort(),
this);
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "SSL engine hc=" + getSSLEngine().hashCode() + " associated with vc=" + getVCHash());
}
// Flag that connection has been established.
// Need to set this to true for inbound and outbound so close will work right.
connected = true;
// Determine if this is an inbound or outbound connection.
if (isInbound) {
readyInbound(inVC);
} else {
readyOutbound(inVC, true);
}
} catch (Exception e) {
if (FrameworkState.isStopping()) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Ignoring exception during server shutdown: " + e);
}
} else {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught exception during ready, " + e, e);
}
FFDCFilter.processException(e, getClass().getName(), "238", this);
}
close(inVC, e);
}
} else {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "ready called after close so do nothing");
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "ready");
}
}
/**
* This callback is used to handle the initial SSL handshake when a
* connection is ready to be handled and an asynchronous callback
* is needed once an action is complete.
*/
class MyHandshakeCompletedCallback implements SSLHandshakeCompletedCallback {
/** Connection using this callback */
private final SSLConnectionLink connLink;
/** Buffer used at the network layer */
private WsByteBuffer netBuffer;
/** Buffer used to decrypt network data */
private WsByteBuffer decryptedNetBuffer;
/** Buffer used to encrypt application data */
private WsByteBuffer encryptedAppBuffer;
/** Inbound or outbound */
private final FlowType flowType;
/** allow other code to tell this class if they changed netBuffer */
private WsByteBuffer updatedNetBuffer = null;
/**
* Constructor.
*
* @param _connLink SSLConnectionLink associated with this callback.
* @param _netBuffer Buffer from the network / device side.
* @param _decryptedNetBuffer Buffer containing results of decrypting netbuffer
* @param _encryptedAppBuffer Encrypted buffer to be sent out through network / device side.
* @param _flowType inbound or outbound
*/
public MyHandshakeCompletedCallback(
SSLConnectionLink _connLink,
WsByteBuffer _netBuffer,
WsByteBuffer _decryptedNetBuffer,
WsByteBuffer _encryptedAppBuffer,
FlowType _flowType) {
// Copy over the variables needed during complete and error.
this.connLink = _connLink;
this.netBuffer = _netBuffer;
this.decryptedNetBuffer = _decryptedNetBuffer;
this.encryptedAppBuffer = _encryptedAppBuffer;
this.flowType = _flowType;
}
@Override
public void updateNetBuffer(WsByteBuffer newBuffer) {
netBuffer = newBuffer;
updatedNetBuffer = newBuffer;
}
@Override
public WsByteBuffer getUpdatedNetBuffer() {
return updatedNetBuffer;
}
/*
* @see com.ibm.ws.channel.ssl.internal.SSLHandshakeCompletedCallback#complete(javax.net.ssl.SSLEngineResult)
*/
@Override
public void complete(SSLEngineResult sslResult) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "complete (handshake), vc=" + getVCHash());
}
HandshakeStatus sslStatus = sslResult.getHandshakeStatus();
if (flowType == FlowType.INBOUND) {
connLink.readyInboundPostHandshake(netBuffer, decryptedNetBuffer,
encryptedAppBuffer, sslStatus);
} else {
try {
connLink.readyOutboundPostHandshake(netBuffer, decryptedNetBuffer,
encryptedAppBuffer, sslStatus, true);
} catch (IOException e) {
close(getVirtualConnection(), e);
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "complete (handshake), vc=" + getVCHash());
}
}
/*
* @see com.ibm.ws.channel.ssl.internal.SSLHandshakeCompletedCallback#error(java.io.IOException)
*/
@Override
public void error(IOException ioe) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.debug(tc, "error (handshake), vc=" + getVCHash());
}
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught exception during unwrap, " + ioe);
}
// cleanup possible ALPN resources
if (flowType == FlowType.INBOUND) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Cleanup possible ALPN resources - error callback");
}
AlpnSupportUtils.getAlpnResult(getSSLEngine(), this.connLink);
}
if (decryptedNetBuffer != null) {
decryptedNetBuffer.release();
decryptedNetBuffer = null;
}
if (netBuffer != null) {
netBuffer.release();
netBuffer = null;
getDeviceReadInterface().setBuffers(null);
}
if (encryptedAppBuffer != null) {
encryptedAppBuffer.release();
encryptedAppBuffer = null;
}
if (flowType == FlowType.INBOUND) {
close(connLink.getVirtualConnection(), ioe);
} else {
if (ioe == null) {
close(getVirtualConnection(), null);
} else {
close(getVirtualConnection(), ioe);
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "error (handshake), vc=" + getVCHash());
}
}
}
/**
* Handle work required by the ready method for inbound connections.
*
* @param inVC
*/
private void readyInbound(VirtualConnection inVC) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "readyInbound, vc=" + getVCHash());
}
// Encrypted buffer from the network.
WsByteBuffer netBuffer = getDeviceReadInterface().getBuffer();
// Verify the buffer is not null first.
if (netBuffer == null) {
// Unable to handle this condition. TCP could not get any more data
// and looked to punt to the only available channel above.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Received null buffer so closing connection.");
}
close(inVC, null);
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyInbound, vc=" + getVCHash());
}
return;
}
netBuffer.flip();
// Unencrypted buffer from the ssl engine output to be handed up to the application.
WsByteBuffer decryptedNetBuffer = null;
// Encrypted buffer from the ssl engine to be sent sent out on the network.
WsByteBuffer encryptedAppBuffer = null;
// Result output from the SSL engine.
SSLEngineResult result = null;
// Indicate that an error occurred and buffers should be cleaned up.
boolean errorOccurred = false;
if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
Tr.event(tc, "Initial read bytes: " + netBuffer.limit());
}
// Note, net and decNet buffers will always be one buffer for discrimination.
// See if discrimination happened. Init method saved discState if so.
if (discState == null) {
// Allocate a new buffer for the results of decrypting the network buffer.
decryptedNetBuffer = SSLUtils.allocateByteBuffer(
getAppBufferSize(), sslChannel.getConfig().getDecryptBuffersDirect());
} else {
// Extract the results of the call to the engine during discrimination.
result = discState.getEngineResult();
// Extract output buffer used during discrimination.
decryptedNetBuffer = discState.getDecryptedNetBuffer();
// Determine the resulting position and limit of networkBuffer after unwrap in discrimination.
netBuffer.position(discState.getNetBufferPosition());
netBuffer.limit(discState.getNetBufferLimit());
}
// Line up buffers needed for the SSL handshake. These are temporary.
encryptedAppBuffer = SSLUtils.allocateByteBuffer(getPacketBufferSize(), true);
try {
if (discState == null) {
// Since data is ready now, we can't call handleHandshake yet, unwrap first.
if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
Tr.event(tc, "Before unwrap\r\n\tnetBuf: " + SSLUtils.getBufferTraceInfo(netBuffer)
+ "\r\n\tdecBuf: " + SSLUtils.getBufferTraceInfo(decryptedNetBuffer));
}
// Should not get any app data until handshake is done
// Protect JSSE from potential SSL packet sizes that are too big.
int savedLimit = SSLUtils.adjustBufferForJSSE(netBuffer, getPacketBufferSize());
// Have the SSL engine inspect the first packet.
result = getSSLEngine().unwrap(
netBuffer.getWrappedByteBuffer(),
decryptedNetBuffer.getWrappedByteBuffer());
if (0 < result.bytesProduced()) {
decryptedNetBuffer.flip();
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
Tr.event(tc,
"After unwrap\r\n\tnetBuf: " + SSLUtils.getBufferTraceInfo(netBuffer)
+ "\r\n\tdecBuf: " + SSLUtils.getBufferTraceInfo(decryptedNetBuffer)
+ "\r\n\tstatus=" + result.getStatus()
+ " HSstatus=" + result.getHandshakeStatus()
+ " consumed=" + result.bytesConsumed()
+ " produced=" + result.bytesProduced());
}
// If adjustments were made for the JSSE, restore them.
if (-1 != savedLimit) {
netBuffer.limit(savedLimit);
}
// If all data was consumed, clear the buffer.
if (netBuffer.remaining() == 0) {
netBuffer.clear();
}
}
// Build a callback for the asynchronous SSL handshake
MyHandshakeCompletedCallback callback = new MyHandshakeCompletedCallback(this, netBuffer, decryptedNetBuffer, encryptedAppBuffer, FlowType.INBOUND);
// Continue the SSL handshake. Do this with asynchronous handShake
result = SSLUtils.handleHandshake(this, netBuffer, decryptedNetBuffer,
encryptedAppBuffer, result, callback, false);
// Check to see if the work was able to be done synchronously.
if (result != null) {
// Handshake is done.
if ((callback != null) && (callback.getUpdatedNetBuffer() != null)) {
netBuffer = callback.getUpdatedNetBuffer();
}
readyInboundPostHandshake(netBuffer, decryptedNetBuffer,
encryptedAppBuffer, result.getHandshakeStatus());
} else {
// Handshake is being done asynchronously.
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyInbound");
}
return;
}
} catch (IOException ioe) {
// no FFDC required
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught ioexception, " + ioe);
}
errorOccurred = true;
// Handle the handshake error.
getChannel().getHandshakeErrorTracker().noteHandshakeError(ioe, getRemoteAddress(), getRemotePort(), getLocalAddress(), getLocalPort());
close(inVC, ioe);
} catch (ReadOnlyBufferException robe) {
FFDCFilter.processException(robe, getClass().getName(), "359", this);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught read-only exception, " + robe);
}
errorOccurred = true;
close(inVC, robe);
}
// Clean up buffers if an error occurred. Note, connection was already closed.
if (errorOccurred) {
if (decryptedNetBuffer != null) {
decryptedNetBuffer.release();
decryptedNetBuffer = null;
}
netBuffer.release();
netBuffer = null;
getDeviceReadInterface().setBuffers(null);
if (encryptedAppBuffer != null) {
encryptedAppBuffer.release();
encryptedAppBuffer = null;
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyInbound");
}
}
/**
* This callback is used after the initial read is done inbound and after the
* SSL handshake. It is only used when there is no data beyond the handshake.
* Once data is ready, the discrimination process is kicked off from here to
* determine the next channel.
*/
public class MyReadCompletedCallback implements TCPReadCompletedCallback {
/** Buffer used for decrypted data */
private WsByteBuffer decryptedNetBuffer;
/**
* Constructor.
*
* @param _decryptedNetBuffer
*/
public MyReadCompletedCallback(WsByteBuffer _decryptedNetBuffer) {
this.decryptedNetBuffer = _decryptedNetBuffer;
}
/*
* @see com.ibm.wsspi.tcpchannel.TCPReadCompletedCallback#complete(com.ibm.wsspi.channelfw.VirtualConnection, com.ibm.wsspi.tcpchannel.TCPReadRequestContext)
*/
@Override
public void complete(VirtualConnection inVC, TCPReadRequestContext rsc) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "complete (read), vc=" + getVCHash());
}
determineNextChannel();
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "complete (read), vc=" + getVCHash());
}
}
/*
* @see com.ibm.wsspi.tcpchannel.TCPReadCompletedCallback#error(com.ibm.wsspi.channelfw.VirtualConnection, com.ibm.wsspi.tcpchannel.TCPReadRequestContext,
* java.io.IOException)
*/
@Override
public void error(VirtualConnection inVC, TCPReadRequestContext rsc, IOException ioe) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "error (read), vc=" + getVCHash());
}
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught IOException during read, " + ioe);
}
// Clean up buffer allocated for read. Note, if this happened on an early read
// where decryptedNetBuffer was null, we must protect from an NPE here.
if (decryptedNetBuffer != null) {
decryptedNetBuffer.release();
}
decryptedNetBuffer = null;
close(inVC, ioe);
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "error (read), vc=" + getVCHash());
}
}
}
/**
* This method is called after the SSL handshake has taken place.
*
* @param netBuffer
* @param decryptedNetBuffer
* @param encryptedAppBuffer
* @param hsStatus
*/
protected void readyInboundPostHandshake(
WsByteBuffer netBuffer,
WsByteBuffer decryptedNetBuffer,
WsByteBuffer encryptedAppBuffer,
HandshakeStatus hsStatus) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "readyInboundPostHandshake, vc=" + getVCHash());
}
// Release the no longer needed buffers.
encryptedAppBuffer.release();
if (hsStatus == HandshakeStatus.FINISHED) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Cleanup possible ALPN resources - handshake finished");
}
AlpnSupportUtils.getAlpnResult(getSSLEngine(), this);
// PK16095 - take certain actions when the handshake completes
getChannel().onHandshakeFinish(getSSLEngine());
// Handshake complete. Now get the request. Use our read interface so unwrap already done.
// Check if data exists in the network buffer still. This would be app data beyond handshake.
if (netBuffer.remaining() == 0 || netBuffer.position() == 0) {
// No app data. Release the netBuffer as it will no longer be used.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Releasing netBuffer: " + netBuffer.hashCode());
}
netBuffer.release();
getDeviceReadInterface().setBuffers(null);
} else {
// Found encrypted app data. Don't release the network buffer yet. Let the read decrypt it.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "App data exists in netBuffer after handshake: " + netBuffer.remaining());
}
}
readInterface.setBuffer(decryptedNetBuffer);
// No need to save number of bytes read.
MyReadCompletedCallback readCallback = new MyReadCompletedCallback(decryptedNetBuffer);
if (null != readInterface.read(1, readCallback, false, TCPRequestContext.USE_CHANNEL_TIMEOUT)) {
// Read was handled synchronously.
determineNextChannel();
}
} else {
// Unknown result from handshake. All other results should have thrown exceptions.
// Clean up buffers used during read.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Unhandled result from SSL engine: " + hsStatus);
Tr.debug(tc, "Cleanup possible ALPN resources on unhandled results");
}
AlpnSupportUtils.getAlpnResult(getSSLEngine(), this);
netBuffer.release();
getDeviceReadInterface().setBuffers(null);
decryptedNetBuffer.release();
SSLException ssle = new SSLException("Unhandled result from SSL engine: " + hsStatus);
FFDCFilter.processException(ssle, getClass().getName(), "401", this);
close(getVirtualConnection(), ssle);
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyInboundPostHandshake");
}
}
/**
* Handle work required by the ready method for outbound connections. When called, the
* outbound socket has been established. Establish the SSL connection before reporting
* to the next channel. Note, this method is called in both sync and async flows.
*
* @param inVC virtual connection associated with this request
* @param async flag for asynchronous (true) or synchronous (false)
* @throws IOException
*/
private void readyOutbound(VirtualConnection inVC, boolean async) throws IOException {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "readyOutbound, vc=" + getVCHash());
}
final SSLChannelData config = this.sslChannel.getConfig();
// Encrypted buffer from the network.
WsByteBuffer netBuffer = SSLUtils.allocateByteBuffer(getPacketBufferSize(), config.getEncryptBuffersDirect());
// Unencrypted buffer from the ssl engine output to be handed up to the application.
WsByteBuffer decryptedNetBuffer = SSLUtils.allocateByteBuffer(getAppBufferSize(), config.getDecryptBuffersDirect());
// Encrypted buffer from the ssl engine to be sent sent out on the network.
WsByteBuffer encryptedAppBuffer = SSLUtils.allocateByteBuffer(getPacketBufferSize(), true);
// Result from the SSL engine.
SSLEngineResult sslResult = null;
// Build the required callback.
MyHandshakeCompletedCallback callback = null;
// Flag if an error took place
IOException exception = null;
// Only create the callback if this is for an async request.
if (async) {
callback = new MyHandshakeCompletedCallback(this, netBuffer, decryptedNetBuffer, encryptedAppBuffer, FlowType.OUTBOUND);
}
try {
// Start the aynchronous SSL handshake.
sslResult = SSLUtils.handleHandshake(this, netBuffer, decryptedNetBuffer,
encryptedAppBuffer, sslResult, callback, false);
// Check to see if the work was able to be done synchronously.
if (sslResult != null) {
// Handshake was done synchronously.
if ((callback != null) && (callback.getUpdatedNetBuffer() != null)) {
netBuffer = callback.getUpdatedNetBuffer();
}
readyOutboundPostHandshake(netBuffer, decryptedNetBuffer,
encryptedAppBuffer, sslResult.getHandshakeStatus(), async);
}
} catch (IOException e) {
exception = e;
} catch (ReadOnlyBufferException e) {
exception = new IOException("Caught exception: " + e);
}
if (exception != null) {
FFDCFilter.processException(exception, getClass().getName(), "540", this);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Caught exception during handshake after connect, " + exception);
}
// Release the buffers.
if (netBuffer != null) {
netBuffer.release();
netBuffer = null;
getDeviceReadInterface().setBuffers(null);
}
if (decryptedNetBuffer != null) {
decryptedNetBuffer.release();
decryptedNetBuffer = null;
}
if (encryptedAppBuffer != null) {
encryptedAppBuffer.release();
encryptedAppBuffer = null;
}
if (async) {
close(inVC, exception);
} else {
this.syncConnectFailure = true;
close(inVC, exception);
throw exception;
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyOutbound");
}
return;
}
/**
* This method is called to handle the results of an SSL handshake. This may be called
* by a callback or in the same thread as the connect request.
*
* @param netBuffer buffer for data flowing in fron the net
* @param decryptedNetBuffer buffer for decrypted data from the net
* @param encryptedAppBuffer buffer for encrypted data flowing from the app
* @param hsStatus output from the last call to the SSL engine
* @param async whether this is for an async (true) or sync (false) request
* @throws IOException
*/
protected void readyOutboundPostHandshake(
WsByteBuffer netBuffer,
WsByteBuffer decryptedNetBuffer,
WsByteBuffer encryptedAppBuffer,
HandshakeStatus hsStatus,
boolean async) throws IOException {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "readyOutboundPostHandshake, vc=" + getVCHash());
}
// Exception to call destroy with in case of bad return code from SSL engine.
IOException exception = null;
if (hsStatus != HandshakeStatus.FINISHED) {
// Handshake failed.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Unexpected results of handshake after connect, " + hsStatus);
}
exception = new IOException("Unexpected results of handshake after connect, " + hsStatus);
}
// PK16095 - take certain actions when the handshake completes
getChannel().onHandshakeFinish(getSSLEngine());
// Null out the buffer references on the device side so they don't wrongly reused later.
getDeviceReadInterface().setBuffers(null);
// Clean up the buffers.
// PI48725 Start
// Handshake complete. Now get the request. Use our read interface so unwrap already done.
// Check if data exists in the network buffer still. This would be app data beyond handshake.
if (netBuffer.remaining() == 0 || netBuffer.position() == 0) {
// No app data. Release the netBuffer as it will no longer be used.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Releasing netBuffer: " + netBuffer.hashCode());
}
// Null out the buffer references on the device side so they don't wrongly reused later.
netBuffer.release();
} else {
// Found encrypted app data. Don't release the network buffer yet. Let the read decrypt it.
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "App data exists in netBuffer after handshake: " + netBuffer.remaining());
}
this.readInterface.setNetBuffer(netBuffer);
}
// PI48725 Finish
// Clean up the buffers.
decryptedNetBuffer.release();
encryptedAppBuffer.release();
// Call appropriate callback if async
if (async) {
if (exception != null) {
close(getVirtualConnection(), exception);
} else {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "Calling ready method.");
}
super.ready(getVirtualConnection());
}
} else {
if (exception != null) {
throw exception;
}
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "readyOutboundPostHandshake");
}
}
/**
* This method is called if connect or connectAsync are called redundantly, after
* the connection is already established. It cleans up the SSL engine. The connect
* methods will then pass the connect on down the chain where, eventually, a new
* socket will be established with this virtual connection.
*/
private void handleRedundantConnect() {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "handleRedundantConnect, vc=" + getVCHash());
}
// This conn link has already been connected.
// Need to shut get a new SSL engine.
cleanup();
// PK46069 - use engine that allows session id re-use
sslEngine = SSLUtils.getOutboundSSLEngine(
sslContext, getLinkConfig(),
targetAddress.getRemoteAddress().getHostName(),
targetAddress.getRemoteAddress().getPort(),
this);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "New SSL engine=" + getSSLEngine().hashCode() + " for vc=" + getVCHash());
}
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.exit(tc, "handleRedundantConnect");
}
}
/*
* @see com.ibm.wsspi.channelfw.base.OutboundProtocolLink#connectAsynch(java.lang.Object)
*/
@Override
public void connectAsynch(Object address) {
if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
Tr.entry(tc, "connectAsynch, vc=" + getVCHash());
}