src/java.base/share/classes/sun/security/ssl/SSLSocketImpl.java
branchJDK-8145252-TLS13-branch
changeset 56712 bad9f4c7eeec
parent 56611 f8f7e604e1f8
child 56718 da9979451b7a
equal deleted inserted replaced
56711:ad4c1c488574 56712:bad9f4c7eeec
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.security.ssl;
    26 package sun.security.ssl;
    27 
    27 
       
    28 import java.io.EOFException;
    28 import java.io.IOException;
    29 import java.io.IOException;
    29 import java.io.InputStream;
    30 import java.io.InputStream;
    30 import java.io.InterruptedIOException;
    31 import java.io.InterruptedIOException;
    31 import java.io.OutputStream;
    32 import java.io.OutputStream;
    32 import java.net.InetAddress;
    33 import java.net.InetAddress;
   575                     buffer.clear();
   576                     buffer.clear();
   576 
   577 
   577                     // grow the buffer if needed
   578                     // grow the buffer if needed
   578                     int inLen = conContext.inputRecord.bytesInCompletePacket();
   579                     int inLen = conContext.inputRecord.bytesInCompletePacket();
   579                     if (inLen < 0) {    // EOF
   580                     if (inLen < 0) {    // EOF
   580                         // treat like receiving a close_notify warning message.
   581                         handleEOF(null);
   581                         conContext.isInputCloseNotified = true;
   582 
   582                         conContext.closeInbound();
   583                         // if no exceptio thrown
   583                         return -1;
   584                         return -1;
   584                     }
   585                     }
   585 
   586 
   586                     // Is this packet bigger than SSL/TLS normally allows?
   587                     // Is this packet bigger than SSL/TLS normally allows?
   587                     if (inLen > SSLRecord.maxLargeRecordSize) {
   588                     if (inLen > SSLRecord.maxLargeRecordSize) {
   802                     return 0;
   803                     return 0;
   803                 }
   804                 }
   804             } catch (SSLException ssle) {
   805             } catch (SSLException ssle) {
   805                 throw ssle;
   806                 throw ssle;
   806             } catch (IOException ioe) {
   807             } catch (IOException ioe) {
   807                 throw new SSLException("readRecord", ioe);
   808                 if (!(ioe instanceof SSLException)) {
       
   809                     throw new SSLException("readRecord", ioe);
       
   810                 } else {
       
   811                     throw ioe;
       
   812                 }
   808             }
   813             }
   809         }
   814         }
   810 
   815 
   811         return -1;
   816         return -1;
   812     }
   817     }
   821              * give the caller the chance to adjust the buffer.
   826              * give the caller the chance to adjust the buffer.
   822              */
   827              */
   823             buffer.clear();
   828             buffer.clear();
   824             int inLen = conContext.inputRecord.bytesInCompletePacket();
   829             int inLen = conContext.inputRecord.bytesInCompletePacket();
   825             if (inLen < 0) {    // EOF
   830             if (inLen < 0) {    // EOF
       
   831                 handleEOF(null);
       
   832 
       
   833                 // if no exceptio thrown
   826                 return -1;
   834                 return -1;
   827             }
   835             }
   828 
   836 
   829             if (buffer.remaining() < inLen) {
   837             if (buffer.remaining() < inLen) {
   830                 return 0;
   838                 return 0;
   836                     return buffer.position();
   844                     return buffer.position();
   837                 }
   845                 }
   838             } catch (SSLException ssle) {
   846             } catch (SSLException ssle) {
   839                 throw ssle;
   847                 throw ssle;
   840             } catch (IOException ioe) {
   848             } catch (IOException ioe) {
   841                 throw new SSLException("readRecord", ioe);
   849                 if (!(ioe instanceof SSLException)) {
       
   850                     throw new SSLException("readRecord", ioe);
       
   851                 } else {
       
   852                     throw ioe;
       
   853                 }
   842             }
   854             }
   843         }
   855         }
   844 
   856 
   845         //
   857         //
   846         // couldn't read, due to some kind of error
   858         // couldn't read, due to some kind of error
   848         return -1;
   860         return -1;
   849     }
   861     }
   850 
   862 
   851     private Plaintext decode(ByteBuffer destination) throws IOException {
   863     private Plaintext decode(ByteBuffer destination) throws IOException {
   852         Plaintext plainText;
   864         Plaintext plainText;
   853         if (destination == null) {
   865         try {
   854             plainText = SSLTransport.decode(conContext,
   866             if (destination == null) {
   855                     null, 0, 0, null, 0, 0);
   867                 plainText = SSLTransport.decode(conContext,
   856         } else {
   868                         null, 0, 0, null, 0, 0);
   857             plainText = SSLTransport.decode(conContext,
   869             } else {
   858                     null, 0, 0, new ByteBuffer[]{destination}, 0, 1);
   870                 plainText = SSLTransport.decode(conContext,
       
   871                         null, 0, 0, new ByteBuffer[]{destination}, 0, 1);
       
   872             }
       
   873         } catch (EOFException eofe) {
       
   874             // EOFException is special as it is related to close_notify.
       
   875             plainText = handleEOF(eofe);
   859         }
   876         }
   860 
   877 
   861         // Is the sequence number is nearly overflow?
   878         // Is the sequence number is nearly overflow?
   862         if (plainText != Plaintext.PLAINTEXT_NULL &&
   879         if (plainText != Plaintext.PLAINTEXT_NULL &&
   863                 conContext.inputRecord.seqNumIsHuge()) {
   880                 conContext.inputRecord.seqNumIsHuge()) {
  1086             }
  1103             }
  1087         }
  1104         }
  1088         conContext.fatal(alert, cause);
  1105         conContext.fatal(alert, cause);
  1089     }
  1106     }
  1090 
  1107 
       
  1108     private Plaintext handleEOF(EOFException eofe) throws IOException {
       
  1109         if (requireCloseNotify || conContext.handshakeContext != null) {
       
  1110             SSLException ssle;
       
  1111             if (conContext.handshakeContext != null) {
       
  1112                 ssle = new SSLHandshakeException(
       
  1113                         "Remote host terminated the handshake");
       
  1114             } else {
       
  1115                 ssle = new SSLProtocolException(
       
  1116                         "Remote host terminated the connection");
       
  1117             }
       
  1118 
       
  1119             if (eofe != null) {
       
  1120                 ssle.initCause(eofe);
       
  1121             }
       
  1122             throw ssle;
       
  1123         } else {
       
  1124             // treat as if we had received a close_notify
       
  1125             conContext.isInputCloseNotified = true;
       
  1126             conContext.transport.shutdown();
       
  1127 
       
  1128             return Plaintext.PLAINTEXT_NULL;
       
  1129         }
       
  1130     }
       
  1131 
       
  1132 
  1091     @Override
  1133     @Override
  1092     public String getPeerHost() {
  1134     public String getPeerHost() {
  1093         return peerHost;
  1135         return peerHost;
  1094     }
  1136     }
  1095 
  1137