View Javadoc

1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package org.jboss.netty.handler.ssl.util;
18  
19  import org.jboss.netty.logging.InternalLogger;
20  import org.jboss.netty.logging.InternalLoggerFactory;
21  import org.jboss.netty.util.internal.EmptyArrays;
22  
23  import javax.net.ssl.ManagerFactoryParameters;
24  import javax.net.ssl.TrustManager;
25  import javax.net.ssl.TrustManagerFactory;
26  import javax.net.ssl.X509TrustManager;
27  import java.security.KeyStore;
28  import java.security.cert.X509Certificate;
29  
30  /**
31   * An insecure {@link TrustManagerFactory} that trusts all X.509 certificates without any verification.
32   * <p>
33   * <strong>NOTE:</strong>
34   * Never use this {@link TrustManagerFactory} in production.
35   * It is purely for testing purposes, and thus it is very insecure.
36   * </p>
37   */
38  public final class InsecureTrustManagerFactory extends SimpleTrustManagerFactory {
39  
40      private static final InternalLogger logger = InternalLoggerFactory.getInstance(InsecureTrustManagerFactory.class);
41  
42      public static final TrustManagerFactory INSTANCE = new InsecureTrustManagerFactory();
43  
44      private static final TrustManager tm = new X509TrustManager() {
45          public void checkClientTrusted(X509Certificate[] chain, String s) {
46              logger.debug("Accepting a client certificate: " + chain[0].getSubjectDN());
47          }
48  
49          public void checkServerTrusted(X509Certificate[] chain, String s) {
50              logger.debug("Accepting a server certificate: " + chain[0].getSubjectDN());
51          }
52  
53          public X509Certificate[] getAcceptedIssuers() {
54              return EmptyArrays.EMPTY_X509_CERTIFICATES;
55          }
56      };
57  
58      private InsecureTrustManagerFactory() { }
59  
60      @Override
61      protected void engineInit(KeyStore keyStore) throws Exception { }
62  
63      @Override
64      protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception { }
65  
66      @Override
67      protected TrustManager[] engineGetTrustManagers() {
68          return new TrustManager[] { tm };
69      }
70  }