d6323bbc19d601d546dccbe7a15aa56c6ee4daa0
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / EasyX509TrustManager.java
1 package eu.alefzero.owncloud.authenticator;
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 * @since 1.2.3
36 */
37 public class EasyX509TrustManager
38 implements X509TrustManager
39 {
40
41 private X509TrustManager standardTrustManager = null;
42
43 /**
44 * Constructor for EasyX509TrustManager.
45 */
46 public EasyX509TrustManager( KeyStore keystore )
47 throws NoSuchAlgorithmException, KeyStoreException
48 {
49 super();
50 TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
51 factory.init( keystore );
52 TrustManager[] trustmanagers = factory.getTrustManagers();
53 if ( trustmanagers.length == 0 )
54 {
55 throw new NoSuchAlgorithmException( "no trust manager found" );
56 }
57 this.standardTrustManager = (X509TrustManager) trustmanagers[0];
58 }
59
60 /**
61 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
62 */
63 public void checkClientTrusted( X509Certificate[] certificates, String authType )
64 throws CertificateException
65 {
66 standardTrustManager.checkClientTrusted( certificates, authType );
67 }
68
69 /**
70 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
71 */
72 public void checkServerTrusted( X509Certificate[] certificates, String authType )
73 throws CertificateException
74 {
75 if ( ( certificates != null ) && ( certificates.length == 1 ) )
76 {
77 certificates[0].checkValidity();
78 }
79 else
80 {
81 //standardTrustManager.checkServerTrusted( certificates, authType );
82 }
83 }
84
85 /**
86 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
87 */
88 public X509Certificate[] getAcceptedIssuers()
89 {
90 return this.standardTrustManager.getAcceptedIssuers();
91 }
92
93 }