test/jdk/javax/net/ssl/ALPN/SSLEngineAlpnTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 42706 jdk/test/javax/net/ssl/ALPN/SSLEngineAlpnTest.java@796cf076d69b
child 51407 910f7b56592f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     1
/*
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
     2
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     4
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     8
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    13
 * accompanied this code).
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    14
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    18
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    21
 * questions.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    22
 */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    23
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    25
// system properties in samevm/agentvm mode.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    26
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    27
/*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    28
 * @test
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    29
 * @bug 8051498 8145849 8170282
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    30
 * @summary JEP 244: TLS Application-Layer Protocol Negotiation Extension
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
    31
 * @compile MyX509ExtendedKeyManager.java
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    32
 *
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    33
 * @run main/othervm SSLEngineAlpnTest h2          UNUSED   h2          h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    34
 * @run main/othervm SSLEngineAlpnTest h2          UNUSED   h2,http/1.1 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    35
 * @run main/othervm SSLEngineAlpnTest h2,http/1.1 UNUSED   h2,http/1.1 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    36
 * @run main/othervm SSLEngineAlpnTest http/1.1,h2 UNUSED   h2,http/1.1 http/1.1
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    37
 * @run main/othervm SSLEngineAlpnTest h4,h3,h2    UNUSED   h1,h2       h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    38
 * @run main/othervm SSLEngineAlpnTest EMPTY       UNUSED   h2,http/1.1 NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    39
 * @run main/othervm SSLEngineAlpnTest h2          UNUSED   EMPTY       NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    40
 * @run main/othervm SSLEngineAlpnTest H2          UNUSED   h2          ERROR
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    41
 * @run main/othervm SSLEngineAlpnTest h2          UNUSED   http/1.1    ERROR
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    42
 *
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    43
 * @run main/othervm SSLEngineAlpnTest UNUSED      h2       h2          h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    44
 * @run main/othervm SSLEngineAlpnTest UNUSED      h2       h2,http/1.1 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    45
 * @run main/othervm SSLEngineAlpnTest UNUSED      h2       http/1.1,h2 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    46
 * @run main/othervm SSLEngineAlpnTest UNUSED      http/1.1 h2,http/1.1 http/1.1
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    47
 * @run main/othervm SSLEngineAlpnTest UNUSED      EMPTY    h2,http/1.1 NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    48
 * @run main/othervm SSLEngineAlpnTest UNUSED      h2       EMPTY       NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    49
 * @run main/othervm SSLEngineAlpnTest UNUSED      H2       h2          ERROR
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    50
 * @run main/othervm SSLEngineAlpnTest UNUSED      h2       http/1.1    ERROR
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    51
 *
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    52
 * @run main/othervm SSLEngineAlpnTest h2          h2       h2          h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    53
 * @run main/othervm SSLEngineAlpnTest H2          h2       h2,http/1.1 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    54
 * @run main/othervm SSLEngineAlpnTest h2,http/1.1 http/1.1 h2,http/1.1 http/1.1
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    55
 * @run main/othervm SSLEngineAlpnTest http/1.1,h2 h2       h2,http/1.1 h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    56
 * @run main/othervm SSLEngineAlpnTest EMPTY       h2       h2          h2
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    57
 * @run main/othervm SSLEngineAlpnTest h2,http/1.1 EMPTY    http/1.1    NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    58
 * @run main/othervm SSLEngineAlpnTest h2,http/1.1 h2       EMPTY       NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    59
 * @run main/othervm SSLEngineAlpnTest UNUSED      UNUSED   http/1.1,h2 NONE
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    60
 * @run main/othervm SSLEngineAlpnTest h2          h2       http/1.1    ERROR
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    61
 * @run main/othervm SSLEngineAlpnTest h2,http/1.1 H2       http/1.1    ERROR
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    62
 */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    63
/**
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    64
 * A simple SSLEngine-based client/server that demonstrates the proposed API
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    65
 * changes for JEP 244 in support of the TLS ALPN extension (RFC 7301).
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    66
 *
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    67
 * Usage:
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    68
 *     java SSLEngineAlpnTest <server-APs> <callback-AP> <client-APs> <result>
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    69
 *
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    70
 * where:
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    71
 *      EMPTY  indicates that ALPN is disabled
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    72
 *      UNUSED indicates that no ALPN values are supplied (server-side only)
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    73
 *      ERROR  indicates that an exception is expected
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    74
 *      NONE   indicates that no ALPN is expected
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
    75
 *
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    76
 * This example is based on our standard SSLEngineTemplate.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    77
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    78
 * The immediate consumer of ALPN will be HTTP/2 (RFC 7540), aka H2. The H2 IETF
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    79
 * Working Group wanted to use TLSv1.3+ as the secure transport mechanism, but
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    80
 * TLSv1.3 wasn't ready. The H2 folk agreed to a compromise that only TLSv1.2+
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    81
 * can be used, and that if TLSv1.2 was selected, non-TLSv.1.3-approved
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    82
 * ciphersuites would be blacklisted and their use discouraged.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    83
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    84
 * In order to support connections that might negotiate either HTTP/1.1 and H2,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    85
 * the guidance from the IETF Working Group is that the H2 ciphersuites be
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    86
 * prioritized/tried first.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    87
 */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    88
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    89
/*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    90
 * The original SSLEngineTemplate comments follow.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    91
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    92
 * A SSLEngine usage example which simplifies the presentation
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    93
 * by removing the I/O and multi-threading concerns.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    94
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    95
 * The test creates two SSLEngines, simulating a client and server.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    96
 * The "transport" layer consists two byte buffers:  think of them
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    97
 * as directly connected pipes.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    98
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
    99
 * Note, this is a *very* simple example: real code will be much more
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   100
 * involved.  For example, different threading and I/O models could be
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   101
 * used, transport mechanisms could close unexpectedly, and so on.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   102
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   103
 * When this application runs, notice that several messages
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   104
 * (wrap/unwrap) pass before any application data is consumed or
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   105
 * produced.  (For more information, please see the SSL/TLS
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   106
 * specifications.)  There may several steps for a successful handshake,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   107
 * so it's typical to see the following series of operations:
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   108
 *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   109
 *      client          server          message
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   110
 *      ======          ======          =======
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   111
 *      wrap()          ...             ClientHello
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   112
 *      ...             unwrap()        ClientHello
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   113
 *      ...             wrap()          ServerHello/Certificate
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   114
 *      unwrap()        ...             ServerHello/Certificate
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   115
 *      wrap()          ...             ClientKeyExchange
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   116
 *      wrap()          ...             ChangeCipherSpec
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   117
 *      wrap()          ...             Finished
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   118
 *      ...             unwrap()        ClientKeyExchange
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   119
 *      ...             unwrap()        ChangeCipherSpec
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   120
 *      ...             unwrap()        Finished
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   121
 *      ...             wrap()          ChangeCipherSpec
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   122
 *      ...             wrap()          Finished
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   123
 *      unwrap()        ...             ChangeCipherSpec
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   124
 *      unwrap()        ...             Finished
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   125
 */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   126
import javax.net.ssl.*;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   127
import javax.net.ssl.SSLEngineResult.*;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   128
import java.io.*;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   129
import java.security.*;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   130
import java.nio.*;
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   131
import java.util.Arrays;
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   132
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   133
public class SSLEngineAlpnTest {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   134
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   135
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   136
     * Enables logging of the SSLEngine operations.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   137
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   138
    private static final boolean logging = true;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   139
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   140
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   141
     * Enables the JSSE system debugging system property:
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   142
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   143
     *     -Djavax.net.debug=all
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   144
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   145
     * This gives a lot of low-level information about operations underway,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   146
     * including specific handshake messages, and might be best examined
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   147
     * after gaining some familiarity with this application.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   148
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   149
    private static final boolean debug = false;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   150
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   151
    private static boolean hasServerAPs; // whether server APs are present
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   152
    private static boolean hasCallback; // whether a callback is present
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   153
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   154
    private final SSLContext sslc;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   155
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   156
    private SSLEngine clientEngine;     // client Engine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   157
    private ByteBuffer clientOut;       // write side of clientEngine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   158
    private ByteBuffer clientIn;        // read side of clientEngine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   159
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   160
    private SSLEngine serverEngine;     // server Engine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   161
    private ByteBuffer serverOut;       // write side of serverEngine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   162
    private ByteBuffer serverIn;        // read side of serverEngine
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   163
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   164
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   165
     * For data transport, this example uses local ByteBuffers.  This
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   166
     * isn't really useful, but the purpose of this example is to show
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   167
     * SSLEngine concepts, not how to do network transport.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   168
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   169
    private ByteBuffer cTOs;            // "reliable" transport client->server
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   170
    private ByteBuffer sTOc;            // "reliable" transport server->client
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   171
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   172
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   173
     * The following is to set up the keystores.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   174
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   175
    private static final String pathToStores = "../etc";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   176
    private static final String keyStoreFile = "keystore";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   177
    private static final String trustStoreFile = "truststore";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   178
    private static final String passwd = "passphrase";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   179
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   180
    private static final String keyFilename
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   181
            = System.getProperty("test.src", ".") + "/" + pathToStores
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   182
            + "/" + keyStoreFile;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   183
    private static final String trustFilename
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   184
            = System.getProperty("test.src", ".") + "/" + pathToStores
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   185
            + "/" + trustStoreFile;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   186
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   187
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   188
     * Main entry point for this test.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   189
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   190
    public static void main(String args[]) throws Exception {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   191
        if (debug) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   192
            System.setProperty("javax.net.debug", "all");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   193
        }
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   194
        System.out.println("Test args: " + Arrays.toString(args));
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   195
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   196
        // Validate parameters
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   197
        if (args.length != 4) {
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   198
            throw new Exception("Invalid number of test parameters");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   199
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   200
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   201
        hasServerAPs = !args[0].equals("UNUSED"); // are server APs being used?
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   202
        hasCallback = !args[1].equals("UNUSED"); // is callback being used?
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   203
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   204
        SSLEngineAlpnTest test = new SSLEngineAlpnTest(args[3]);
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   205
        try {
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   206
            test.runTest(convert(args[0]), args[1], convert(args[2]), args[3]);
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   207
        } catch (SSLHandshakeException she) {
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   208
            if (args[3].equals("ERROR")) {
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   209
                System.out.println("Caught the expected exception: " + she);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   210
            } else {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   211
                throw she;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   212
            }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   213
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   214
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   215
        System.out.println("Test Passed.");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   216
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   217
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   218
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   219
     * Create an initialized SSLContext to use for these tests.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   220
     */
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   221
    public SSLEngineAlpnTest(String expectedAP) throws Exception {
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   222
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   223
        KeyStore ks = KeyStore.getInstance("JKS");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   224
        KeyStore ts = KeyStore.getInstance("JKS");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   225
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   226
        char[] passphrase = "passphrase".toCharArray();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   227
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   228
        ks.load(new FileInputStream(keyFilename), passphrase);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   229
        ts.load(new FileInputStream(trustFilename), passphrase);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   230
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   231
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   232
        kmf.init(ks, passphrase);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   233
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   234
        KeyManager [] kms = kmf.getKeyManagers();
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   235
        if (!(kms[0] instanceof X509ExtendedKeyManager)) {
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   236
            throw new Exception("kms[0] not X509ExtendedKeyManager");
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   237
        }
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   238
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   239
        kms = new KeyManager[] { new MyX509ExtendedKeyManager(
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   240
                (X509ExtendedKeyManager) kms[0], expectedAP,
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   241
                !hasCallback && hasServerAPs) };
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   242
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   243
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   244
        tmf.init(ts);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   245
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   246
        SSLContext sslCtx = SSLContext.getInstance("TLS");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   247
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   248
        sslCtx.init(kms, tmf.getTrustManagers(), null);
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   249
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   250
        sslc = sslCtx;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   251
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   252
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   253
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   254
     * Convert a comma-separated list into an array of strings.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   255
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   256
    private static String[] convert(String list) {
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   257
        if (list.equals("UNUSED")) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   258
            return null;
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   259
        }
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   260
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   261
        if (list.equals("EMPTY")) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   262
            return new String[0];
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   263
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   264
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   265
        String[] strings;
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   266
        if (list.indexOf(',') > 0) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   267
            strings = list.split(",");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   268
        } else {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   269
            strings = new String[]{ list };
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   270
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   271
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   272
        return strings;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   273
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   274
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   275
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   276
     * Run the test.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   277
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   278
     * Sit in a tight loop, both engines calling wrap/unwrap regardless
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   279
     * of whether data is available or not.  We do this until both engines
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   280
     * report back they are closed.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   281
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   282
     * The main loop handles all of the I/O phases of the SSLEngine's
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   283
     * lifetime:
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   284
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   285
     *     initial handshaking
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   286
     *     application data transfer
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   287
     *     engine closing
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   288
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   289
     * One could easily separate these phases into separate
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   290
     * sections of code.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   291
     */
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   292
    private void runTest(String[] serverAPs, String callbackAP,
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   293
            String[] clientAPs, String expectedAP) throws Exception {
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   294
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   295
        boolean dataDone = false;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   296
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   297
        createSSLEngines(serverAPs, callbackAP, clientAPs);
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   298
        createBuffers();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   299
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   300
        SSLEngineResult clientResult;   // results from client's last operation
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   301
        SSLEngineResult serverResult;   // results from server's last operation
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   302
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   303
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   304
         * Examining the SSLEngineResults could be much more involved,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   305
         * and may alter the overall flow of the application.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   306
         *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   307
         * For example, if we received a BUFFER_OVERFLOW when trying
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   308
         * to write to the output pipe, we could reallocate a larger
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   309
         * pipe, but instead we wait for the peer to drain it.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   310
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   311
        while (!isEngineClosed(clientEngine)
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   312
                || !isEngineClosed(serverEngine)) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   313
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   314
            log("================");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   315
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   316
            clientResult = clientEngine.wrap(clientOut, cTOs);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   317
            log("client wrap: ", clientResult);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   318
            runDelegatedTasks(clientResult, clientEngine);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   319
            checkAPResult(clientEngine, clientResult, expectedAP);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   320
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   321
            serverResult = serverEngine.wrap(serverOut, sTOc);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   322
            log("server wrap: ", serverResult);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   323
            runDelegatedTasks(serverResult, serverEngine);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   324
            checkAPResult(serverEngine, serverResult, expectedAP);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   325
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   326
            cTOs.flip();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   327
            sTOc.flip();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   328
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   329
            log("----");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   330
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   331
            clientResult = clientEngine.unwrap(sTOc, clientIn);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   332
            log("client unwrap: ", clientResult);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   333
            runDelegatedTasks(clientResult, clientEngine);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   334
            checkAPResult(clientEngine, clientResult, expectedAP);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   335
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   336
            serverResult = serverEngine.unwrap(cTOs, serverIn);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   337
            log("server unwrap: ", serverResult);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   338
            runDelegatedTasks(serverResult, serverEngine);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   339
            checkAPResult(serverEngine, serverResult, expectedAP);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   340
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   341
            cTOs.compact();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   342
            sTOc.compact();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   343
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   344
            /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   345
             * After we've transfered all application data between the client
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   346
             * and server, we close the clientEngine's outbound stream.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   347
             * This generates a close_notify handshake message, which the
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   348
             * server engine receives and responds by closing itself.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   349
             */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   350
            if (!dataDone && (clientOut.limit() == serverIn.position())
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   351
                    && (serverOut.limit() == clientIn.position())) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   352
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   353
                /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   354
                 * A sanity check to ensure we got what was sent.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   355
                 */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   356
                checkTransfer(serverOut, clientIn);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   357
                checkTransfer(clientOut, serverIn);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   358
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   359
                log("\tClosing clientEngine's *OUTBOUND*...");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   360
                clientEngine.closeOutbound();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   361
                dataDone = true;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   362
            }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   363
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   364
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   365
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   366
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   367
     * Check that the resulting connection meets our defined ALPN
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   368
     * criteria.  If we were connecting to a non-JSSE implementation,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   369
     * the server might have negotiated something we shouldn't accept.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   370
     *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   371
     * If we were expecting an ALPN value from server, let's make sure
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   372
     * the conditions match.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   373
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   374
    private static void checkAPResult(SSLEngine engine, SSLEngineResult result,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   375
            String expectedAP) throws Exception {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   376
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   377
        if (result.getHandshakeStatus() != HandshakeStatus.FINISHED) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   378
            return;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   379
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   380
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   381
        if (engine.getHandshakeApplicationProtocol() != null) {
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   382
            throw new Exception ("getHandshakeApplicationProtocol() should "
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   383
                    + "return null after the handshake is completed");
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   384
        }
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   385
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   386
        String ap = engine.getApplicationProtocol();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   387
        System.out.println("Application Protocol: \"" + ap + "\"");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   388
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   389
        if (ap == null) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   390
            throw new Exception(
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   391
                "Handshake was completed but null was received");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   392
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   393
        if (expectedAP.equals("NONE")) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   394
            if (!ap.isEmpty()) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   395
                throw new Exception("Expected no ALPN value");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   396
            } else {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   397
                System.out.println("No ALPN value negotiated, as expected");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   398
            }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   399
        } else if (!expectedAP.equals(ap)) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   400
            throw new Exception(expectedAP +
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   401
                " ALPN value not available on negotiated connection");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   402
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   403
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   404
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   405
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   406
     * Using the SSLContext created during object creation,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   407
     * create/configure the SSLEngines we'll use for this test.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   408
     */
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   409
    private void createSSLEngines(String[] serverAPs, String callbackAP,
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   410
            String[] clientAPs) throws Exception {
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   411
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   412
         * Configure the serverEngine to act as a server in the SSL/TLS
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   413
         * handshake.  Also, require SSL client authentication.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   414
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   415
        serverEngine = sslc.createSSLEngine();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   416
        serverEngine.setUseClientMode(false);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   417
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   418
        SSLParameters sslp = serverEngine.getSSLParameters();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   419
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   420
        sslp.setNeedClientAuth(true);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   421
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   422
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   423
         * The default ciphersuite ordering from the SSLContext may not
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   424
         * reflect "h2" ciphersuites as being preferred, additionally the
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   425
         * client may not send them in an appropriate order. We could resort
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   426
         * the suite list if so desired.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   427
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   428
        String[] suites = sslp.getCipherSuites();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   429
        sslp.setCipherSuites(suites);
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   430
        if (serverAPs != null) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   431
            sslp.setApplicationProtocols(serverAPs);
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   432
        }
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   433
        sslp.setUseCipherSuitesOrder(true);  // Set server side order
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   434
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   435
        serverEngine.setSSLParameters(sslp);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   436
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   437
        // check that no callback has been registered
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   438
        if (serverEngine.getHandshakeApplicationProtocolSelector() != null) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   439
            throw new Exception("getHandshakeApplicationProtocolSelector() " +
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   440
                "should return null");
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   441
        }
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   442
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   443
        if (hasCallback) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   444
            serverEngine.setHandshakeApplicationProtocolSelector(
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   445
                (sslEngine, clientProtocols) -> {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   446
                    return callbackAP.equals("EMPTY") ? "" : callbackAP;
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   447
                });
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   448
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   449
            // check that the callback can be retrieved
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   450
            if (serverEngine.getHandshakeApplicationProtocolSelector()
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   451
                    == null) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   452
                throw new Exception("getHandshakeApplicationProtocolSelector()"
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   453
                    + " should return non-null");
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   454
            }
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   455
        }
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   456
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   457
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   458
         * Similar to above, but using client mode instead.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   459
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   460
        clientEngine = sslc.createSSLEngine("client", 80);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   461
        clientEngine.setUseClientMode(true);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   462
        sslp = clientEngine.getSSLParameters();
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   463
        if (clientAPs != null) {
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   464
            sslp.setApplicationProtocols(clientAPs);
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 34958
diff changeset
   465
        }
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   466
        clientEngine.setSSLParameters(sslp);
34958
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   467
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   468
        if ((clientEngine.getHandshakeApplicationProtocol() != null) ||
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   469
                (serverEngine.getHandshakeApplicationProtocol() != null)) {
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   470
            throw new Exception ("getHandshakeApplicationProtocol() should "
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   471
                    + "return null before the handshake starts");
82de431119cd 8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents: 34380
diff changeset
   472
        }
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   473
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   474
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   475
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   476
     * Create and size the buffers appropriately.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   477
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   478
    private void createBuffers() {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   479
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   480
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   481
         * We'll assume the buffer sizes are the same
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   482
         * between client and server.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   483
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   484
        SSLSession session = clientEngine.getSession();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   485
        int appBufferMax = session.getApplicationBufferSize();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   486
        int netBufferMax = session.getPacketBufferSize();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   487
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   488
        /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   489
         * We'll make the input buffers a bit bigger than the max needed
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   490
         * size, so that unwrap()s following a successful data transfer
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   491
         * won't generate BUFFER_OVERFLOWS.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   492
         *
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   493
         * We'll use a mix of direct and indirect ByteBuffers for
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   494
         * tutorial purposes only.  In reality, only use direct
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   495
         * ByteBuffers when they give a clear performance enhancement.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   496
         */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   497
        clientIn = ByteBuffer.allocate(appBufferMax + 50);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   498
        serverIn = ByteBuffer.allocate(appBufferMax + 50);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   499
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   500
        cTOs = ByteBuffer.allocateDirect(netBufferMax);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   501
        sTOc = ByteBuffer.allocateDirect(netBufferMax);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   502
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   503
        clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   504
        serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   505
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   506
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   507
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   508
     * If the result indicates that we have outstanding tasks to do,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   509
     * go ahead and run them in this thread.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   510
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   511
    private static void runDelegatedTasks(SSLEngineResult result,
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   512
            SSLEngine engine) throws Exception {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   513
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   514
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   515
            Runnable runnable;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   516
            while ((runnable = engine.getDelegatedTask()) != null) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   517
                log("\trunning delegated task...");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   518
                runnable.run();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   519
            }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   520
            HandshakeStatus hsStatus = engine.getHandshakeStatus();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   521
            if (hsStatus == HandshakeStatus.NEED_TASK) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   522
                throw new Exception(
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   523
                        "handshake shouldn't need additional tasks");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   524
            }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   525
            log("\tnew HandshakeStatus: " + hsStatus);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   526
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   527
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   528
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   529
    private static boolean isEngineClosed(SSLEngine engine) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   530
        return (engine.isOutboundDone() && engine.isInboundDone());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   531
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   532
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   533
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   534
     * Simple check to make sure everything came across as expected.
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   535
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   536
    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   537
            throws Exception {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   538
        a.flip();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   539
        b.flip();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   540
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   541
        if (!a.equals(b)) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   542
            throw new Exception("Data didn't transfer cleanly");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   543
        } else {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   544
            log("\tData transferred cleanly");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   545
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   546
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   547
        a.position(a.limit());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   548
        b.position(b.limit());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   549
        a.limit(a.capacity());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   550
        b.limit(b.capacity());
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   551
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   552
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   553
    /*
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   554
     * Logging code
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   555
     */
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   556
    private static boolean resultOnce = true;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   557
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   558
    private static void log(String str, SSLEngineResult result) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   559
        if (!logging) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   560
            return;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   561
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   562
        if (resultOnce) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   563
            resultOnce = false;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   564
            System.out.println("The format of the SSLEngineResult is: \n"
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   565
                    + "\t\"getStatus() / getHandshakeStatus()\" +\n"
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   566
                    + "\t\"bytesConsumed() / bytesProduced()\"\n");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   567
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   568
        HandshakeStatus hsStatus = result.getHandshakeStatus();
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   569
        log(str
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   570
                + result.getStatus() + "/" + hsStatus + ", "
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   571
                + result.bytesConsumed() + "/" + result.bytesProduced()
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   572
                + " bytes");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   573
        if (hsStatus == HandshakeStatus.FINISHED) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   574
            log("\t...ready for application data");
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   575
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   576
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   577
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   578
    private static void log(String str) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   579
        if (logging) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   580
            System.out.println(str);
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   581
        }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   582
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
diff changeset
   583
}