SunFakeTrustSocketFactory.java
Upload User: zsjinxin
Upload Date: 2013-04-21
Package Size: 18236k
Code Size: 4k
Category:

Web Server

Development Platform:

Java

  1. /*
  2.  * Copyright 2001-2004 The Apache Software Foundation.
  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. package org.apache.axis.components.net;
  17. import java.util.Hashtable;
  18. import org.apache.axis.components.logger.LogFactory;
  19. import org.apache.axis.utils.Messages;
  20. import org.apache.commons.logging.Log;
  21. import com.sun.net.ssl.SSLContext;
  22. import com.sun.net.ssl.TrustManager;
  23. import com.sun.net.ssl.X509TrustManager;
  24. /**
  25.  * Hook for Axis sender, allowing unsigned server certs
  26.  */
  27. public class SunFakeTrustSocketFactory extends SunJSSESocketFactory {
  28.     /** Field log           */
  29.     protected static Log log =
  30.             LogFactory.getLog(SunFakeTrustSocketFactory.class.getName());
  31.     /**
  32.      * Constructor FakeTrustSocketFactory
  33.      *
  34.      * @param attributes
  35.      */
  36.     public SunFakeTrustSocketFactory(Hashtable attributes) {
  37.         super(attributes);
  38.     }
  39.     /**
  40.      * Method getContext
  41.      *
  42.      * @return
  43.      *
  44.      * @throws Exception
  45.      */
  46.     protected SSLContext getContext() throws Exception {
  47.         try {
  48.             SSLContext sc = SSLContext.getInstance("SSL");
  49.             sc.init(null, // we don't need no stinkin KeyManager
  50.                     new TrustManager[]{new FakeX509TrustManager()},
  51.                     new java.security.SecureRandom());
  52.             if (log.isDebugEnabled()) {
  53.                 log.debug(Messages.getMessage("ftsf00"));
  54.             }
  55.             return sc;
  56.         } catch (Exception exc) {
  57.             log.error(Messages.getMessage("ftsf01"), exc);
  58.             throw new Exception(Messages.getMessage("ftsf02"));
  59.         }
  60.     }
  61.     /**
  62.      * Class FakeX509TrustManager
  63.      */
  64.     public static class FakeX509TrustManager implements X509TrustManager {
  65.         /** Field log           */
  66.         protected static Log log =
  67.                 LogFactory.getLog(FakeX509TrustManager.class.getName());
  68.         /**
  69.          * Method isClientTrusted
  70.          *
  71.          * @param chain
  72.          *
  73.          * @return
  74.          */
  75.         public boolean isClientTrusted(java.security.cert
  76.                 .X509Certificate[] chain) {
  77.             if (log.isDebugEnabled()) {
  78.                 log.debug(Messages.getMessage("ftsf03"));
  79.             }
  80.             return true;
  81.         }
  82.         /**
  83.          * Method isServerTrusted
  84.          *
  85.          * @param chain
  86.          *
  87.          * @return
  88.          */
  89.         public boolean isServerTrusted(java.security.cert
  90.                 .X509Certificate[] chain) {
  91.             if (log.isDebugEnabled()) {
  92.                 log.debug(Messages.getMessage("ftsf04"));
  93.             }
  94.             return true;
  95.         }
  96.         /**
  97.          * Method getAcceptedIssuers
  98.          *
  99.          * @return
  100.          */
  101.         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  102.             if (log.isDebugEnabled()) {
  103.                 log.debug(Messages.getMessage("ftsf05"));
  104.             }
  105.             return null;
  106.         }
  107.     }
  108. }