|
1 /** |
|
2 * Copyright (c) 2012-2013, Gerald Garcia |
|
3 * |
|
4 * This file is part of Andoid Caldav Sync Adapter Free. |
|
5 * |
|
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute |
|
7 * it and/or modify it under the terms of the GNU General Public License |
|
8 * as published by the Free Software Foundation, either version 3 of the |
|
9 * License, or at your option any later version. |
|
10 * |
|
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that |
|
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with Andoid Caldav Sync Adapter Free. |
|
18 * If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
20 */ |
|
21 |
|
22 package org.gege.caldavsyncadapter.caldav; |
|
23 |
|
24 /* |
|
25 * Licensed to the Apache Software Foundation (ASF) under one |
|
26 * or more contributor license agreements. See the NOTICE file |
|
27 * distributed with this work for additional information |
|
28 * regarding copyright ownership. The ASF licenses this file |
|
29 * to you under the Apache License, Version 2.0 (the |
|
30 * "License"); you may not use this file except in compliance |
|
31 * with the License. You may obtain a copy of the License at |
|
32 * |
|
33 * http://www.apache.org/licenses/LICENSE-2.0 |
|
34 * |
|
35 * Unless required by applicable law or agreed to in writing, |
|
36 * software distributed under the License is distributed on an |
|
37 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
38 * KIND, either express or implied. See the License for the |
|
39 * specific language governing permissions and limitations |
|
40 * under the License. |
|
41 */ |
|
42 |
|
43 import java.security.KeyStore; |
|
44 import java.security.KeyStoreException; |
|
45 import java.security.NoSuchAlgorithmException; |
|
46 import java.security.cert.CertificateException; |
|
47 import java.security.cert.X509Certificate; |
|
48 |
|
49 import javax.net.ssl.TrustManager; |
|
50 import javax.net.ssl.TrustManagerFactory; |
|
51 import javax.net.ssl.X509TrustManager; |
|
52 |
|
53 /** |
|
54 * @author olamy |
|
55 * @version $Id: EasyX509TrustManager.java 765355 2009-04-15 20:59:07Z evenisse $ |
|
56 * @since 1.2.3 |
|
57 */ |
|
58 public class EasyX509TrustManager |
|
59 implements X509TrustManager |
|
60 { |
|
61 |
|
62 private X509TrustManager standardTrustManager = null; |
|
63 |
|
64 /** |
|
65 * Constructor for EasyX509TrustManager. |
|
66 */ |
|
67 public EasyX509TrustManager( KeyStore keystore ) |
|
68 throws NoSuchAlgorithmException, KeyStoreException |
|
69 { |
|
70 super(); |
|
71 TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); |
|
72 factory.init( keystore ); |
|
73 TrustManager[] trustmanagers = factory.getTrustManagers(); |
|
74 if ( trustmanagers.length == 0 ) |
|
75 { |
|
76 throw new NoSuchAlgorithmException( "no trust manager found" ); |
|
77 } |
|
78 this.standardTrustManager = (X509TrustManager) trustmanagers[0]; |
|
79 } |
|
80 |
|
81 /** |
|
82 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType) |
|
83 */ |
|
84 public void checkClientTrusted( X509Certificate[] certificates, String authType ) |
|
85 throws CertificateException |
|
86 { |
|
87 standardTrustManager.checkClientTrusted( certificates, authType ); |
|
88 } |
|
89 |
|
90 /** |
|
91 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType) |
|
92 */ |
|
93 public void checkServerTrusted( X509Certificate[] certificates, String authType ) |
|
94 throws CertificateException |
|
95 { |
|
96 if ( ( certificates != null ) && ( certificates.length == 1 ) ) |
|
97 { |
|
98 certificates[0].checkValidity(); |
|
99 } |
|
100 else |
|
101 { |
|
102 standardTrustManager.checkServerTrusted( certificates, authType ); |
|
103 } |
|
104 } |
|
105 |
|
106 /** |
|
107 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
|
108 */ |
|
109 public X509Certificate[] getAcceptedIssuers() |
|
110 { |
|
111 return this.standardTrustManager.getAcceptedIssuers(); |
|
112 } |
|
113 |
|
114 } |