TestSSLContext.java revision 059dbc04218144f985b20a228bbe98139d400d0c
1bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom/*
2bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Copyright (C) 2010 The Android Open Source Project
3bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
4bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * you may not use this file except in compliance with the License.
6bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * You may obtain a copy of the License at
7bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
8bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
10bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * See the License for the specific language governing permissions and
14bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * limitations under the License.
15bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom */
16bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
17bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrompackage javax.net.ssl;
18bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
19bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstromimport java.net.InetAddress;
20bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstromimport java.net.InetSocketAddress;
21bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstromimport java.security.KeyStore;
22204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstromimport java.security.Principal;
23bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstromimport java.security.SecureRandom;
249a106a63508697a6f5f02c20b7cc6b7c6152695fBrian Carlstromimport java.security.StandardNames;
25e688a4123f165ed2905878e312b074b8c825d119Brian Carlstromimport java.security.cert.Certificate;
26059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstromimport java.security.cert.CertificateException;
27bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstromimport java.security.cert.X509Certificate;
28204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstromimport java.util.Collections;
290c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstromimport junit.framework.Assert;
30bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
31bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom/**
32bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * TestSSLContext is a convenience class for other tests that
33bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * want a canned SSLContext and related state for testing so they
34bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * don't have to duplicate the logic.
35bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom */
360c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrompublic final class TestSSLContext extends Assert {
37bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
38f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes    /*
390af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom     * The RI and Android have very different default SSLSession cache behaviors.
400af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom     * The RI keeps an unlimited number of SSLSesions around for 1 day.
41f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     * Android keeps 10 SSLSessions forever.
420af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom     */
439a106a63508697a6f5f02c20b7cc6b7c6152695fBrian Carlstrom    private static final boolean IS_RI = StandardNames.IS_RI;
440af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom    public static final int EXPECTED_DEFAULT_CLIENT_SSL_SESSION_CACHE_SIZE = (IS_RI) ? 0 : 10;
450af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom    public static final int EXPECTED_DEFAULT_SERVER_SSL_SESSION_CACHE_SIZE = (IS_RI) ? 0 : 100;
460af0a7959d838c48e6b4e8dc9ac188ff6bbb6a87Brian Carlstrom    public static final int EXPECTED_DEFAULT_SSL_SESSION_CACHE_TIMEOUT = (IS_RI) ? 86400 : 0;
47bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
48bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    /**
49bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * The Android SSLSocket and SSLServerSocket implementations are
50bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * based on a version of OpenSSL which includes support for RFC
51bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * 4507 session tickets. When using session tickets, the server
52bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * does not need to keep a cache mapping session IDs to SSL
53bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * sessions for reuse. Instead, the client presents the server
54bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * with a session ticket it received from the server earlier,
55bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * which is an SSL session encrypted by the server's secret
56bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * key. Since in this case the server does not need to keep a
57bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * cache, some tests may find different results depending on
58bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * whether or not the session tickets are in use. These tests can
59bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * use this function to determine if loopback SSL connections are
60bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * expected to use session tickets and conditionalize their
61bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * results appropriately.
62bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     */
63bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public static boolean sslServerSocketSupportsSessionTickets () {
640c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrom        // Disabled session tickets for better compatability b/2682876
650c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrom        // return !IS_RI;
660c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrom        return false;
67bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
68bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
69059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final KeyStore clientKeyStore;
70059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final char[] clientKeyStorePassword;
71059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final KeyStore serverKeyStore;
72059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final char[] serverKeyStorePassword;
73059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final X509ExtendedKeyManager clientKeyManager;
74059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final X509ExtendedKeyManager serverKeyManager;
75059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final X509TrustManager clientTrustManager;
76059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final X509TrustManager serverTrustManager;
77059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final SSLContext clientContext;
78059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public final SSLContext serverContext;
79bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final SSLServerSocket serverSocket;
80bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final InetAddress host;
81bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final int port;
82bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
83059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    private TestSSLContext(KeyStore clientKeyStore,
84059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           char[] clientKeyStorePassword,
85059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           KeyStore serverKeyStore,
86059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           char[] serverKeyStorePassword,
87059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           X509ExtendedKeyManager clientKeyManager,
88059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           X509ExtendedKeyManager serverKeyManager,
89059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           X509TrustManager clientTrustManager,
90059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           X509TrustManager serverTrustManager,
91059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           SSLContext clientContext,
92059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                           SSLContext serverContext,
93bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                           SSLServerSocket serverSocket,
94bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                           InetAddress host,
95bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                           int port) {
96059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.clientKeyStore = clientKeyStore;
97059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.clientKeyStorePassword = clientKeyStorePassword;
98059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.serverKeyStore = serverKeyStore;
99059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.serverKeyStorePassword = serverKeyStorePassword;
100059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.clientKeyManager = clientKeyManager;
101059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.serverKeyManager = serverKeyManager;
102059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.clientTrustManager = clientTrustManager;
103059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.serverTrustManager = serverTrustManager;
104059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.clientContext = clientContext;
105059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        this.serverContext = serverContext;
106bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.serverSocket = serverSocket;
107bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.host = host;
108bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.port = port;
109bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
110bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
111e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom    /**
112e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     * Usual TestSSLContext creation method, creates underlying
113e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     * SSLContext with certificate and key as well as SSLServerSocket
114e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     * listening provided host and port.
115e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     */
116bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public static TestSSLContext create() {
117059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        return create(TestKeyStore.getClient(),
118059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                      TestKeyStore.getServer());
119bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
120bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
121e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom    /**
122059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom     * TestSSLContext creation method that allows separate creation of server key store
123e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     */
124059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static TestSSLContext create(TestKeyStore client, TestKeyStore server) {
125059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        return create(client.keyStore, client.keyStorePassword,
126059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                      server.keyStore, server.keyStorePassword);
127bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
128bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
129bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    /**
130059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom     * TestSSLContext creation method that allows separate creation of client and server key store
131e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom     */
132059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static TestSSLContext create(KeyStore clientKeyStore, char[] clientKeyStorePassword,
133059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                        KeyStore serverKeyStore, char[] serverKeyStorePassword) {
134e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom        try {
135059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            KeyManager[] clientKeyManagers = createKeyManagers(clientKeyStore,
136059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                               clientKeyStorePassword);
137059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            KeyManager[] serverKeyManagers = createKeyManagers(serverKeyStore,
138059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                               serverKeyStorePassword);
139e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom
140059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            TrustManager[] clientTrustManagers = createTrustManagers(clientKeyStore,
141059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                                     clientKeyStorePassword);
142059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            TrustManager[] serverTrustManagers = createTrustManagers(serverKeyStore,
143059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                                     serverKeyStorePassword);
144e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom
145059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            SSLContext clientContext = createSSLContext(clientKeyManagers, clientTrustManagers);
146059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            SSLContext serverContext = createSSLContext(serverKeyManagers, serverTrustManagers);
147e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom
148059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            SSLServerSocket serverSocket = (SSLServerSocket)
149059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                serverContext.getServerSocketFactory().createServerSocket(0);
150059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            InetSocketAddress sa = (InetSocketAddress) serverSocket.getLocalSocketAddress();
151059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            InetAddress host = sa.getAddress();
152059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            int port = sa.getPort();
153e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom
154059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            return new TestSSLContext(clientKeyStore, clientKeyStorePassword,
155059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      serverKeyStore, serverKeyStorePassword,
156059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      (X509ExtendedKeyManager) clientKeyManagers[0],
157059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      (X509ExtendedKeyManager) serverKeyManagers[0],
158059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      (X509TrustManager) clientTrustManagers[0],
159059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      (X509TrustManager) serverTrustManagers[0],
160059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      clientContext, serverContext,
161059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                      serverSocket, host, port);
162e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom        } catch (RuntimeException e) {
163e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom            throw e;
164e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom        } catch (Exception e) {
165e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom            throw new RuntimeException(e);
166e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom        }
167e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom    }
168e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom
169e688a4123f165ed2905878e312b074b8c825d119Brian Carlstrom    /**
170bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * Create a SSLContext with a KeyManager using the private key and
171bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * certificate chain from the given KeyStore and a TrustManager
172bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * using the certificates authorities from the same KeyStore.
173bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     */
174059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static final SSLContext createSSLContext(final KeyManager[] keyManagers,
175059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                    final TrustManager[] trustManagers)
176059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            throws Exception {
177059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        SSLContext context = SSLContext.getInstance("TLS");
178059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        context.init(keyManagers, trustManagers, new SecureRandom());
179059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        return context;
180059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    }
181059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom
182059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static KeyManager[] createKeyManagers(final KeyStore keyStore,
183059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                 final char[] keyStorePassword) throws Exception {
184bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        String kmfa = KeyManagerFactory.getDefaultAlgorithm();
185bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfa);
186bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        kmf.init(keyStore, keyStorePassword);
187059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        return kmf.getKeyManagers();
188059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    }
189bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
190059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static TrustManager[] createTrustManagers(final KeyStore keyStore,
191059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                   final char[] keyStorePassword) throws Exception {
192bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        String tmfa = TrustManagerFactory.getDefaultAlgorithm();
193bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfa);
194bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        tmf.init(keyStore);
195059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        return tmf.getTrustManagers();
196bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
197204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom
198204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom    public static void assertCertificateInKeyStore(Principal principal,
199204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                                                   KeyStore keyStore) throws Exception {
200204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        String subjectName = principal.getName();
201204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        boolean found = false;
202204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        for (String alias: Collections.list(keyStore.aliases())) {
203204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            if (!keyStore.isCertificateEntry(alias)) {
204204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                continue;
205204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            }
206204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            X509Certificate keyStoreCertificate = (X509Certificate) keyStore.getCertificate(alias);
207204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            if (subjectName.equals(keyStoreCertificate.getSubjectDN().getName())) {
208204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                found = true;
209204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                break;
210204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            }
211204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        }
2120c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrom        assertTrue(found);
213204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom    }
214204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom
215204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom    public static void assertCertificateInKeyStore(Certificate certificate,
216204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                                                   KeyStore keyStore) throws Exception {
217204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        boolean found = false;
218204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        for (String alias: Collections.list(keyStore.aliases())) {
219204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            if (!keyStore.isCertificateEntry(alias)) {
220204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                continue;
221204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            }
222204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            Certificate keyStoreCertificate = keyStore.getCertificate(alias);
223204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            if (certificate.equals(keyStoreCertificate)) {
224204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                found = true;
225204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                break;
226204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom            }
227204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        }
2280c131a2ca38465b7d1df4eaee63ac73ce4d5986dBrian Carlstrom        assertTrue(found);
229204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom    }
230059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom
231059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static void assertServerCertificateChain(X509TrustManager trustManager,
232059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                    Certificate[] serverChain)
233059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            throws CertificateException {
234059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        X509Certificate[] chain = (X509Certificate[]) serverChain;
235059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        trustManager.checkServerTrusted(chain, chain[0].getPublicKey().getAlgorithm());
236059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    }
237059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom
238059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    public static void assertClientCertificateChain(X509TrustManager trustManager,
239059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom                                                    Certificate[] clientChain)
240059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            throws CertificateException {
241059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        X509Certificate[] chain = (X509Certificate[]) clientChain;
242059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        trustManager.checkClientTrusted(chain, chain[0].getPublicKey().getAlgorithm());
243059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom    }
244bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom}
245