src/org/gege/caldavsyncadapter/caldav/EasyX509TrustManager.java

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/EasyX509TrustManager.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012-2013, Gerald Garcia
     1.6 + * 
     1.7 + * This file is part of Andoid Caldav Sync Adapter Free.
     1.8 + *
     1.9 + * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.10 + * it and/or modify it under the terms of the GNU General Public License 
    1.11 + * as published by the Free Software Foundation, either version 3 of the 
    1.12 + * License, or at your option any later version.
    1.13 + *
    1.14 + * Andoid Caldav Sync Adapter Free is distributed in the hope that 
    1.15 + * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
    1.16 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with Andoid Caldav Sync Adapter Free.  
    1.21 + * If not, see <http://www.gnu.org/licenses/>.
    1.22 + * 
    1.23 + */
    1.24 +
    1.25 +package org.gege.caldavsyncadapter.caldav;
    1.26 +
    1.27 +/*
    1.28 + * Licensed to the Apache Software Foundation (ASF) under one
    1.29 + * or more contributor license agreements.  See the NOTICE file
    1.30 + * distributed with this work for additional information
    1.31 + * regarding copyright ownership.  The ASF licenses this file
    1.32 + * to you under the Apache License, Version 2.0 (the
    1.33 + * "License"); you may not use this file except in compliance
    1.34 + * with the License.  You may obtain a copy of the License at
    1.35 + *
    1.36 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.37 + *
    1.38 + * Unless required by applicable law or agreed to in writing,
    1.39 + * software distributed under the License is distributed on an
    1.40 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.41 + * KIND, either express or implied.  See the License for the
    1.42 + * specific language governing permissions and limitations
    1.43 + * under the License.
    1.44 + */
    1.45 +
    1.46 +import java.security.KeyStore;
    1.47 +import java.security.KeyStoreException;
    1.48 +import java.security.NoSuchAlgorithmException;
    1.49 +import java.security.cert.CertificateException;
    1.50 +import java.security.cert.X509Certificate;
    1.51 +
    1.52 +import javax.net.ssl.TrustManager;
    1.53 +import javax.net.ssl.TrustManagerFactory;
    1.54 +import javax.net.ssl.X509TrustManager;
    1.55 +
    1.56 +/**
    1.57 + * @author olamy
    1.58 + * @version $Id: EasyX509TrustManager.java 765355 2009-04-15 20:59:07Z evenisse $
    1.59 + * @since 1.2.3
    1.60 + */
    1.61 +public class EasyX509TrustManager
    1.62 +    implements X509TrustManager
    1.63 +{
    1.64 +
    1.65 +    private X509TrustManager standardTrustManager = null;
    1.66 +
    1.67 +    /**
    1.68 +     * Constructor for EasyX509TrustManager.
    1.69 +     */
    1.70 +    public EasyX509TrustManager( KeyStore keystore )
    1.71 +        throws NoSuchAlgorithmException, KeyStoreException
    1.72 +    {
    1.73 +        super();
    1.74 +        TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
    1.75 +        factory.init( keystore );
    1.76 +        TrustManager[] trustmanagers = factory.getTrustManagers();
    1.77 +        if ( trustmanagers.length == 0 )
    1.78 +        {
    1.79 +            throw new NoSuchAlgorithmException( "no trust manager found" );
    1.80 +        }
    1.81 +        this.standardTrustManager = (X509TrustManager) trustmanagers[0];
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
    1.86 +     */
    1.87 +    public void checkClientTrusted( X509Certificate[] certificates, String authType )
    1.88 +        throws CertificateException
    1.89 +    {
    1.90 +        standardTrustManager.checkClientTrusted( certificates, authType );
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
    1.95 +     */
    1.96 +    public void checkServerTrusted( X509Certificate[] certificates, String authType )
    1.97 +        throws CertificateException
    1.98 +    {
    1.99 +        if ( ( certificates != null ) && ( certificates.length == 1 ) )
   1.100 +        {
   1.101 +            certificates[0].checkValidity();
   1.102 +        }
   1.103 +        else
   1.104 +        {
   1.105 +            standardTrustManager.checkServerTrusted( certificates, authType );
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
   1.111 +     */
   1.112 +    public X509Certificate[] getAcceptedIssuers()
   1.113 +    {
   1.114 +        return this.standardTrustManager.getAcceptedIssuers();
   1.115 +    }
   1.116 +
   1.117 +}
   1.118 \ No newline at end of file

mercurial