|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package android.net.http; |
| 18 | + |
| 19 | +import java.security.cert.CertificateException; |
| 20 | +import java.security.cert.X509Certificate; |
| 21 | +import java.security.KeyManagementException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import javax.net.ssl.X509TrustManager; |
| 25 | + |
| 26 | +import org.apache.harmony.xnet.provider.jsse.TrustManagerImpl; |
| 27 | + |
| 28 | +/** |
| 29 | + * X509TrustManager wrapper exposing Android-added features. |
| 30 | + * |
| 31 | + * <p> The checkServerTrusted method allows callers to perform additional |
| 32 | + * verification of certificate chains after they have been successfully |
| 33 | + * verified by the platform.</p> |
| 34 | + */ |
| 35 | +public class X509TrustManagerExtensions { |
| 36 | + |
| 37 | + TrustManagerImpl mDelegate; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructs a new X509TrustManagerExtensions wrapper. |
| 41 | + * |
| 42 | + * @param tm A {@link X509TrustManager} as returned by TrustManagerFactory.getInstance(); |
| 43 | + * @throws IllegalArgumentException If tm is an unsupported TrustManager type. |
| 44 | + */ |
| 45 | + public X509TrustManagerExtensions(X509TrustManager tm) throws IllegalArgumentException { |
| 46 | + if (mDelegate instanceof TrustManagerImpl) { |
| 47 | + mDelegate = (TrustManagerImpl) tm; |
| 48 | + } else { |
| 49 | + throw new IllegalArgumentException("tm is not a supported type of X509TrustManager"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Verifies the given certificate chain. |
| 55 | + * |
| 56 | + * <p>See {@link X509TrustManager#checkServerTrusted(X509Certificate[], String)} for a |
| 57 | + * description of the chain and authType parameters. The final parameter, host, should be the |
| 58 | + * hostname of the server.</p> |
| 59 | + * |
| 60 | + * @throws CertificateException if the chain does not verify correctly. |
| 61 | + * @return the properly ordered chain used for verification as a list of X509Certificates. |
| 62 | + */ |
| 63 | + public List<X509Certificate> checkServerTrusted(X509Certificate[] chain, String authType, |
| 64 | + String host) throws CertificateException { |
| 65 | + return mDelegate.checkServerTrusted(chain, authType, host); |
| 66 | + } |
| 67 | +} |
0 commit comments