SSL connections update: notice about untrusted certificates and allow the user save...
[pub/Android/ownCloud.git] / src / com / owncloud / android / network / EasyX509TrustManager.java
1 package com.owncloud.android.network;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.security.KeyStore;
23 import java.security.KeyStoreException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.CertificateException;
26 import java.security.cert.X509Certificate;
27
28 import javax.net.ssl.TrustManager;
29 import javax.net.ssl.TrustManagerFactory;
30 import javax.net.ssl.X509TrustManager;
31
32 /**
33 * @author olamy
34 * @version $Id: EasyX509TrustManager.java 765355 2009-04-15 20:59:07Z evenisse
35 * $
36 * @since 1.2.3
37 */
38 public class EasyX509TrustManager implements X509TrustManager {
39
40 private X509TrustManager standardTrustManager = null;
41
42 /**
43 * Constructor for EasyX509TrustManager.
44 */
45 public EasyX509TrustManager(KeyStore keystore)
46 throws NoSuchAlgorithmException, KeyStoreException {
47 super();
48 TrustManagerFactory factory = TrustManagerFactory
49 .getInstance(TrustManagerFactory.getDefaultAlgorithm());
50 factory.init(keystore);
51 TrustManager[] trustmanagers = factory.getTrustManagers();
52 if (trustmanagers.length == 0) {
53 throw new NoSuchAlgorithmException("no trust manager found");
54 }
55 this.standardTrustManager = (X509TrustManager) trustmanagers[0];
56 }
57
58 /**
59 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],
60 * String authType)
61 */
62 public void checkClientTrusted(X509Certificate[] certificates,
63 String authType) throws CertificateException {
64 standardTrustManager.checkClientTrusted(certificates, authType);
65 }
66
67 /**
68 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
69 * String authType)
70 */
71 public void checkServerTrusted(X509Certificate[] certificates,
72 String authType) throws CertificateException {
73 if ((certificates != null) && (certificates.length == 1)) {
74 certificates[0].checkValidity();
75 } else {
76 // standardTrustManager.checkServerTrusted( certificates, authType
77 // );
78 }
79 }
80
81 /**
82 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
83 */
84 public X509Certificate[] getAcceptedIssuers() {
85 return this.standardTrustManager.getAcceptedIssuers();
86 }
87
88 }