jdk/test/javax/net/ssl/ALPN/MyX509ExtendedKeyManager.java
changeset 34958 82de431119cd
child 42706 796cf076d69b
equal deleted inserted replaced
34957:4a0926942b53 34958:82de431119cd
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.net.Socket;
       
    25 import java.security.Principal;
       
    26 import java.security.PrivateKey;
       
    27 import java.security.cert.X509Certificate;
       
    28 import javax.net.ssl.SSLEngine;
       
    29 import javax.net.ssl.SSLSocket;
       
    30 import javax.net.ssl.X509ExtendedKeyManager;
       
    31 
       
    32 public class MyX509ExtendedKeyManager extends X509ExtendedKeyManager {
       
    33 
       
    34     static final String ERROR = "ERROR";
       
    35     X509ExtendedKeyManager akm;
       
    36     String expectedAP;
       
    37 
       
    38     MyX509ExtendedKeyManager(X509ExtendedKeyManager akm) {
       
    39         this.akm = akm;
       
    40     }
       
    41 
       
    42     public MyX509ExtendedKeyManager(
       
    43             X509ExtendedKeyManager akm, String expectedAP) {
       
    44         this.akm = akm;
       
    45         this.expectedAP = expectedAP;
       
    46 
       
    47     }
       
    48 
       
    49     @Override
       
    50     public String[] getClientAliases(String keyType, Principal[] issuers) {
       
    51         return akm.getClientAliases(keyType, issuers);
       
    52     }
       
    53 
       
    54     @Override
       
    55     public String chooseClientAlias(String[] keyType, Principal[] issuers,
       
    56             Socket socket) {
       
    57         String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();
       
    58         checkALPN(nap);
       
    59 
       
    60         return akm.chooseClientAlias(keyType, issuers, socket);
       
    61     }
       
    62 
       
    63     @Override
       
    64     public String[] getServerAliases(String keyType, Principal[] issuers) {
       
    65         return akm.getServerAliases(keyType, issuers);
       
    66     }
       
    67 
       
    68     @Override
       
    69     public String chooseServerAlias(String keyType, Principal[] issuers,
       
    70             Socket socket) {
       
    71         String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();
       
    72         checkALPN(nap);
       
    73 
       
    74         return akm.chooseServerAlias(keyType, issuers, socket);
       
    75     }
       
    76 
       
    77     @Override
       
    78     public X509Certificate[] getCertificateChain(String alias) {
       
    79         return akm.getCertificateChain(alias);
       
    80     }
       
    81 
       
    82     @Override
       
    83     public PrivateKey getPrivateKey(String alias) {
       
    84         return akm.getPrivateKey(alias);
       
    85     }
       
    86 
       
    87     @Override
       
    88     public String chooseEngineClientAlias(String[] keyType, Principal[] issuers,
       
    89             SSLEngine engine) {
       
    90         String nap = engine.getHandshakeApplicationProtocol();
       
    91         checkALPN(nap);
       
    92 
       
    93         return akm.chooseEngineClientAlias(keyType, issuers, engine);
       
    94     }
       
    95 
       
    96     @Override
       
    97     public String chooseEngineServerAlias(String keyType, Principal[] issuers,
       
    98             SSLEngine engine) {
       
    99         String nap = engine.getHandshakeApplicationProtocol();
       
   100         checkALPN(nap);
       
   101 
       
   102         return akm.chooseEngineServerAlias(keyType, issuers, engine);
       
   103     }
       
   104 
       
   105     private void checkALPN(String ap) {
       
   106 
       
   107         if (ERROR.equals(expectedAP)) {
       
   108             throw new RuntimeException("Should not reach here");
       
   109         }
       
   110 
       
   111         System.out.println("Expected ALPN value: " + expectedAP
       
   112                 + " Got: " + ap);
       
   113 
       
   114         if (ap == null) {
       
   115             throw new RuntimeException(
       
   116                     "ALPN should be negotiated, but null was received");
       
   117         }
       
   118         if (expectedAP.equals("NONE")) {
       
   119             if (!ap.isEmpty()) {
       
   120                 throw new RuntimeException("Expected no ALPN value");
       
   121             } else {
       
   122                 System.out.println("No ALPN value negotiated, as expected");
       
   123             }
       
   124         } else if (!expectedAP.equals(ap)) {
       
   125             throw new RuntimeException(expectedAP
       
   126                     + " ALPN value not available on negotiated connection");
       
   127         }
       
   128 
       
   129     }
       
   130 }