@@ -91,6 +91,8 @@ public class WolfSSLSocket extends SSLSocket {
9191 protected volatile boolean connectionClosed = false ;
9292 /** Flag representing if I/O callbacks have been set */
9393 private boolean ioCallbacksSet = false ;
94+ /** Flag representing if native fd has been set */
95+ private boolean fdSet = false ;
9496
9597 /* lock for handshakInitCalled and handshakeComplete */
9698 private final Object handshakeLock = new Object ();
@@ -502,23 +504,33 @@ private void checkAndInitSSLSocket() throws IOException {
502504
503505 synchronized (initLock ) {
504506
507+ /* If underlying Socket connected, set fd. Check before
508+ * initialized flag, since we may have already initialized
509+ * certs/keys but not fd in previous call */
510+ if (!this .fdSet && isConnected ()) {
511+ try {
512+ setFd ();
513+ } catch (WolfSSLException e ) {
514+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
515+ "Failed to set native fd, may try again later" );
516+ }
517+ }
518+
505519 if (isInitialized ) {
506520 return ;
507521 }
508522
509523 try {
510524 /* Load private key and cert chain from WolfSSLAuthStore */
525+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
526+ "loading private key and cert chain" );
527+
511528 if (this .socket != null ) {
512529 EngineHelper .LoadKeyAndCertChain (this .socket , null );
513530 } else {
514531 EngineHelper .LoadKeyAndCertChain (this , null );
515532 }
516533
517- /* If underlying Socket connected, set fd */
518- if (isConnected ()) {
519- setFd ();
520- }
521-
522534 isInitialized = true ;
523535
524536 } catch (WolfSSLException | CertificateEncodingException |
@@ -610,6 +622,9 @@ private void setFd() throws IllegalArgumentException, WolfSSLException {
610622 "registered Socket(this.socket) with native wolfSSL" );
611623 }
612624 }
625+
626+ /* Mark fd set */
627+ this .fdSet = true ;
613628 }
614629 }
615630
@@ -1715,7 +1730,7 @@ public synchronized InputStream getInputStream() throws IOException {
17151730 checkAndInitSSLSocket ();
17161731
17171732 if (!this .isConnected ()) {
1718- throw new IOException ("Socket is not connected" );
1733+ throw new SocketException ("Socket is not connected" );
17191734 }
17201735
17211736 if (this .isClosed ()) {
@@ -1747,7 +1762,7 @@ public synchronized OutputStream getOutputStream() throws IOException {
17471762 checkAndInitSSLSocket ();
17481763
17491764 if (!this .isConnected ()) {
1750- throw new IOException ("Socket is not connected" );
1765+ throw new SocketException ("Socket is not connected" );
17511766 }
17521767
17531768 if (this .isClosed ()) {
0 commit comments