Path: blob/master/test/jdk/javax/net/ssl/ALPN/MyX509ExtendedKeyManager.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.Socket;24import java.security.Principal;25import java.security.PrivateKey;26import java.security.cert.X509Certificate;27import javax.net.ssl.SSLEngine;28import javax.net.ssl.SSLSocket;29import javax.net.ssl.X509ExtendedKeyManager;3031public class MyX509ExtendedKeyManager extends X509ExtendedKeyManager {3233static final String ERROR = "ERROR";34X509ExtendedKeyManager akm;35String expectedAP;36boolean doCheck = true;3738MyX509ExtendedKeyManager(X509ExtendedKeyManager akm) {39this.akm = akm;40}4142public MyX509ExtendedKeyManager(43X509ExtendedKeyManager akm, String expectedAP, boolean doCheck) {44this.akm = akm;45this.expectedAP = expectedAP;46this.doCheck = doCheck;4748}4950@Override51public String[] getClientAliases(String keyType, Principal[] issuers) {52return akm.getClientAliases(keyType, issuers);53}5455@Override56public String chooseClientAlias(String[] keyType, Principal[] issuers,57Socket socket) {58String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();59checkALPN(nap);6061return akm.chooseClientAlias(keyType, issuers, socket);62}6364@Override65public String[] getServerAliases(String keyType, Principal[] issuers) {66return akm.getServerAliases(keyType, issuers);67}6869@Override70public String chooseServerAlias(String keyType, Principal[] issuers,71Socket socket) {72String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();73checkALPN(nap);7475return akm.chooseServerAlias(keyType, issuers, socket);76}7778@Override79public X509Certificate[] getCertificateChain(String alias) {80return akm.getCertificateChain(alias);81}8283@Override84public PrivateKey getPrivateKey(String alias) {85return akm.getPrivateKey(alias);86}8788@Override89public String chooseEngineClientAlias(String[] keyType, Principal[] issuers,90SSLEngine engine) {91String nap = engine.getHandshakeApplicationProtocol();92checkALPN(nap);9394return akm.chooseEngineClientAlias(keyType, issuers, engine);95}9697@Override98public String chooseEngineServerAlias(String keyType, Principal[] issuers,99SSLEngine engine) {100String nap = engine.getHandshakeApplicationProtocol();101checkALPN(nap);102103return akm.chooseEngineServerAlias(keyType, issuers, engine);104}105106private void checkALPN(String ap) {107108if (!doCheck) {109System.out.println("Skipping KeyManager checks " +110"because a callback has been registered");111return;112}113114if (ERROR.equals(expectedAP)) {115throw new RuntimeException("Should not reach here");116}117118System.out.println("Expected ALPN value: " + expectedAP119+ " Got: " + ap);120121if (ap == null) {122throw new RuntimeException(123"ALPN should be negotiated, but null was received");124}125if (expectedAP.equals("NONE")) {126if (!ap.isEmpty()) {127throw new RuntimeException("Expected no ALPN value");128} else {129System.out.println("No ALPN value negotiated, as expected");130}131} else if (!expectedAP.equals(ap)) {132throw new RuntimeException(expectedAP133+ " ALPN value not available on negotiated connection");134}135136}137}138139140