16c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom/*
26c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * Copyright (C) 2010 The Android Open Source Project
36c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom *
46c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
56c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * you may not use this file except in compliance with the License.
66c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * You may obtain a copy of the License at
76c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom *
86c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
96c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom *
106c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
116c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
126c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * See the License for the specific language governing permissions and
146c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * limitations under the License.
156c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom */
166c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
176c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrompackage libcore.javax.net.ssl;
186c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
196c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.io.PrintStream;
206c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.net.Socket;
216c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.security.Principal;
226c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.security.PrivateKey;
236c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.security.cert.CertificateException;
246c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport java.security.cert.X509Certificate;
256c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport javax.net.ssl.KeyManager;
266c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport javax.net.ssl.SSLEngine;
276c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport javax.net.ssl.X509ExtendedKeyManager;
286c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstromimport libcore.java.io.NullPrintStream;
294ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstromimport libcore.java.security.StandardNames;
306c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
316c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom/**
326c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * TestKeyManager is a simple proxy class that wraps an existing
336c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * X509ExtendedKeyManager to provide debug logging and recording of
346c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom * values.
356c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom */
366c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrompublic final class TestKeyManager extends X509ExtendedKeyManager {
376c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
386c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private static final boolean LOG = false;
396c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private static final PrintStream out = LOG ? System.out : new NullPrintStream();
406c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
416c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private final X509ExtendedKeyManager keyManager;
426c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
436c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public static KeyManager[] wrap(KeyManager[] keyManagers) {
446c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        KeyManager[] result = keyManagers.clone();
456c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (int i = 0; i < result.length; i++) {
466c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            result[i] = wrap(result[i]);
476c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
486c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return result;
496c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
506c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
516c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public static KeyManager wrap(KeyManager keyManager) {
526c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        if (!(keyManager instanceof X509ExtendedKeyManager)) {
536c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            return keyManager;
546c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
556c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return new TestKeyManager((X509ExtendedKeyManager) keyManager);
566c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
576c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
586c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public TestKeyManager(X509ExtendedKeyManager keyManager) {
596c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.println("TestKeyManager.<init> keyManager=" + keyManager);
606c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        this.keyManager = keyManager;
616c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
626c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
636c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String chooseClientAlias(String[] keyTypes, Principal[] issuers, Socket socket) {
646c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.chooseClientAlias");
656c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyTypes: ");
666c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (String keyType : keyTypes) {
676c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(keyType);
686c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(' ');
696c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
706c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
716c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpSocket(socket);
724ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyTypes(keyTypes);
736c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAlias(keyManager.chooseClientAlias(keyTypes, issuers, socket));
746c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
756c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
764ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom    private void assertKeyTypes(String[] keyTypes) {
774ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        for (String keyType : keyTypes) {
784ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom            assertKeyType(keyType);
794ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        }
804ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom    }
814ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom
824ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom    private void assertKeyType(String keyType) {
834ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        if (!StandardNames.KEY_TYPES.contains(keyType)) {
844ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom            throw new AssertionError("Unexpected key type " + keyType);
854ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        }
864ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom    }
874ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom
886c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
896c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.chooseServerAlias");
906c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyType: ");
916c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(keyType);
926c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(' ');
936c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
946c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpSocket(socket);
954ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyType(keyType);
966c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAlias(keyManager.chooseServerAlias(keyType, issuers, socket));
976c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
986c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
996c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private void dumpSocket(Socket socket) {
1006c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | socket: ");
1016c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(String.valueOf(socket));
1026c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1036c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1046c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private void dumpIssuers(Principal[] issuers) {
1056c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | issuers: ");
1066c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        if (issuers == null) {
1076c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print("null");
1086c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            return;
1096c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
1106c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (Principal issuer : issuers) {
1116c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(issuer);
1126c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(' ');
1136c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
1146c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1156c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1166c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private String dumpAlias(String alias) {
1176c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" => ");
1186c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.println(alias);
1196c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return alias;
1206c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1216c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1226c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public X509Certificate[] getCertificateChain(String alias) {
1236c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.getCertificateChain");
1246c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | alias: ");
1256c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(alias);
1266c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpCerts(keyManager.getCertificateChain(alias));
1276c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1286c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1296c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private X509Certificate[] dumpCerts(X509Certificate[] certs) {
1306c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" => ");
1316c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (X509Certificate cert : certs) {
1326c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(cert.getSubjectDN());
1336c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(' ');
1346c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
1356c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.println();
1366c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return certs;
1376c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1386c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1396c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String[] getClientAliases(String keyType, Principal[] issuers) {
1406c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.getClientAliases");
1416c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyType: ");
1426c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(keyType);
1436c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
1444ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyType(keyType);
1456c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAliases(keyManager.getClientAliases(keyType, issuers));
1466c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1476c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1486c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String[] getServerAliases(String keyType, Principal[] issuers) {
1496c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.getServerAliases");
1506c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyType: ");
1516c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(keyType);
1526c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
1534ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyType(keyType);
1546c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAliases(keyManager.getServerAliases(keyType, issuers));
1556c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1566c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1576c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private String[] dumpAliases(String[] aliases) {
1586c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" => ");
1596c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (String alias : aliases) {
1606c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(alias);
1616c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(' ');
1626c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
1636c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.println();
1646c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return aliases;
1656c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1666c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1676c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public PrivateKey getPrivateKey(String alias) {
1686c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.getPrivateKey");
1696c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | alias: ");
1706c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(alias);
1716c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        PrivateKey pk = keyManager.getPrivateKey(alias);
1726c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" => ");
1736c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.println(String.valueOf(pk));
1746c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return pk;
1756c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1766c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1776c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String chooseEngineClientAlias(String[] keyTypes, Principal[] issuers, SSLEngine e) {
1786c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.chooseEngineClientAlias");
1796c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyTypes: ");
1806c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        for (String keyType : keyTypes) {
1816c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(keyType);
1826c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom            out.print(' ');
1836c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        }
1846c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
1856c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpEngine(e);
1864ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyTypes(keyTypes);
1876c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAlias(keyManager.chooseEngineClientAlias(keyTypes, issuers, e));
1886c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
1896c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
1906c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine e) {
1916c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print("TestKeyManager.chooseEngineServerAlias");
1926c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | keyType: ");
1936c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(keyType);
1946c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(' ');
1956c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpIssuers(issuers);
1966c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        dumpEngine(e);
1974ae3fd787741bfe1b808f447dcb0785250024119Brian Carlstrom        assertKeyType(keyType);
1986c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        return dumpAlias(keyManager.chooseEngineServerAlias(keyType, issuers, e));
1996c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
2006c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
2016c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    private void dumpEngine(SSLEngine engine) {
2026c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(" | engine: ");
2036c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom        out.print(String.valueOf(engine));
2046c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom    }
2056c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom}
2066c78b7b94c232063ec559436b48b33751373ecf1Brian Carlstrom
207