< prev index next >

src/java.base/share/classes/sun/security/ssl/TransportContext.java

Print this page


   1 /*
   2  * Copyright (c) 2018, 2019, 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


 479 
 480         // For TLS 1.2 and prior version, it is required to respond with
 481         // a close_notify alert of its own and close down the connection
 482         // immediately, discarding any pending writes.
 483         if (!isOutboundClosed()) {
 484             boolean needCloseNotify = SSLConfiguration.acknowledgeCloseNotify;
 485             if (!needCloseNotify) {
 486                 if (isNegotiated) {
 487                     if (!protocolVersion.useTLS13PlusSpec()) {
 488                         needCloseNotify = true;
 489                     }
 490                 } else if (handshakeContext != null) {  // initial handshake
 491                     ProtocolVersion pv = handshakeContext.negotiatedProtocol;
 492                     if (pv == null || (!pv.useTLS13PlusSpec())) {
 493                         needCloseNotify = true;
 494                     }
 495                 }
 496             }
 497 
 498             if (needCloseNotify) {
 499                 outputRecord.recordLock.lock();
 500                 try {
 501                     try {
 502                         // send a close_notify alert
 503                         warning(Alert.CLOSE_NOTIFY);
 504                     } finally {
 505                         outputRecord.close();
 506                     }
 507                 } finally {
 508                     outputRecord.recordLock.unlock();
 509                 }
 510             }
 511         }
 512     }
 513 
 514     // Initiate a inbound close when the handshake is not started.
 515     private void initiateInboundClose() throws IOException {
 516         if (!isInboundClosed()) {
 517             inputRecord.close();
 518         }
 519     }
 520 
 521     // Close outbound, no more data should be received from the underlying
 522     // transportation connection.
 523     void closeOutbound() {
 524         if (isOutboundClosed()) {
 525             return;
 526         }
 527 
 528         try {
 529              initiateOutboundClose();
 530         } catch (IOException ioe) {
 531             if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
 532                 SSLLogger.warning("outbound closure failed", ioe);
 533             }
 534         }
 535     }
 536 
 537     // Initiate a close by sending a close_notify alert.
 538     private void initiateOutboundClose() throws IOException {
 539         boolean useUserCanceled = false;
 540         if (!isNegotiated && (handshakeContext != null) && !peerUserCanceled) {
 541             // initial handshake
 542             useUserCanceled = true;
 543         }
 544 
 545         // Need a lock here so that the user_canceled alert and the
 546         // close_notify alert can be delivered together.
 547         outputRecord.recordLock.lock();
 548         try {
 549             try {
 550                 // send a user_canceled alert if needed.
 551                 if (useUserCanceled) {
 552                     warning(Alert.USER_CANCELED);
 553                 }
 554 
 555                 // send a close_notify alert
 556                 warning(Alert.CLOSE_NOTIFY);
 557             } finally {
 558                 outputRecord.close();
 559             }
 560         } finally {
 561             outputRecord.recordLock.unlock();
 562         }
 563     }
 564 
 565     // Note; HandshakeStatus.FINISHED status is retrieved in other places.
 566     HandshakeStatus getHandshakeStatus() {
 567         if (!outputRecord.isEmpty()) {
 568             // If no handshaking, special case to wrap alters or
 569             // post-handshake messages.
 570             return HandshakeStatus.NEED_WRAP;
 571         } else if (isOutboundClosed() && isInboundClosed()) {
 572             return HandshakeStatus.NOT_HANDSHAKING;
 573         } else if (handshakeContext != null) {
 574             if (!handshakeContext.delegatedActions.isEmpty()) {
 575                 return HandshakeStatus.NEED_TASK;
 576             } else if (!isInboundClosed()) {
 577                 if (sslContext.isDTLS() &&
 578                         !inputRecord.isEmpty()) {
 579                     return HandshakeStatus.NEED_UNWRAP_AGAIN;
 580                 } else {
 581                     return HandshakeStatus.NEED_UNWRAP;


   1 /*
   2  * Copyright (c) 2018, 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


 479 
 480         // For TLS 1.2 and prior version, it is required to respond with
 481         // a close_notify alert of its own and close down the connection
 482         // immediately, discarding any pending writes.
 483         if (!isOutboundClosed()) {
 484             boolean needCloseNotify = SSLConfiguration.acknowledgeCloseNotify;
 485             if (!needCloseNotify) {
 486                 if (isNegotiated) {
 487                     if (!protocolVersion.useTLS13PlusSpec()) {
 488                         needCloseNotify = true;
 489                     }
 490                 } else if (handshakeContext != null) {  // initial handshake
 491                     ProtocolVersion pv = handshakeContext.negotiatedProtocol;
 492                     if (pv == null || (!pv.useTLS13PlusSpec())) {
 493                         needCloseNotify = true;
 494                     }
 495                 }
 496             }
 497 
 498             if (needCloseNotify) {
 499                 synchronized (outputRecord) {

 500                     try {
 501                         // send a close_notify alert
 502                         warning(Alert.CLOSE_NOTIFY);
 503                     } finally {
 504                         outputRecord.close();
 505                     }


 506                 }
 507             }
 508         }
 509     }
 510 
 511     // Initiate a inbound close when the handshake is not started.
 512     private void initiateInboundClose() throws IOException {
 513         if (!isInboundClosed()) {
 514             inputRecord.close();
 515         }
 516     }
 517 
 518     // Close outbound, no more data should be received from the underlying
 519     // transportation connection.
 520     void closeOutbound() {
 521         if (isOutboundClosed()) {
 522             return;
 523         }
 524 
 525         try {
 526              initiateOutboundClose();
 527         } catch (IOException ioe) {
 528             if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
 529                 SSLLogger.warning("outbound closure failed", ioe);
 530             }
 531         }
 532     }
 533 
 534     // Initiate a close by sending a close_notify alert.
 535     private void initiateOutboundClose() throws IOException {
 536         boolean useUserCanceled = false;
 537         if (!isNegotiated && (handshakeContext != null) && !peerUserCanceled) {
 538             // initial handshake
 539             useUserCanceled = true;
 540         }
 541 
 542         // Need a lock here so that the user_canceled alert and the
 543         // close_notify alert can be delivered together.
 544         synchronized (outputRecord) {

 545             try {
 546                 // send a user_canceled alert if needed.
 547                 if (useUserCanceled) {
 548                     warning(Alert.USER_CANCELED);
 549                 }
 550 
 551                 // send a close_notify alert
 552                 warning(Alert.CLOSE_NOTIFY);
 553             } finally {
 554                 outputRecord.close();
 555             }


 556         }
 557     }
 558 
 559     // Note; HandshakeStatus.FINISHED status is retrieved in other places.
 560     HandshakeStatus getHandshakeStatus() {
 561         if (!outputRecord.isEmpty()) {
 562             // If no handshaking, special case to wrap alters or
 563             // post-handshake messages.
 564             return HandshakeStatus.NEED_WRAP;
 565         } else if (isOutboundClosed() && isInboundClosed()) {
 566             return HandshakeStatus.NOT_HANDSHAKING;
 567         } else if (handshakeContext != null) {
 568             if (!handshakeContext.delegatedActions.isEmpty()) {
 569                 return HandshakeStatus.NEED_TASK;
 570             } else if (!isInboundClosed()) {
 571                 if (sslContext.isDTLS() &&
 572                         !inputRecord.isEmpty()) {
 573                     return HandshakeStatus.NEED_UNWRAP_AGAIN;
 574                 } else {
 575                     return HandshakeStatus.NEED_UNWRAP;


< prev index next >