View Javadoc

1   /*
2    * Copyright 2012 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  package org.jboss.netty.example.securechat;
17  
18  import java.security.InvalidAlgorithmParameterException;
19  import java.security.KeyStore;
20  import java.security.KeyStoreException;
21  import java.security.cert.CertificateException;
22  import java.security.cert.X509Certificate;
23  
24  import javax.net.ssl.ManagerFactoryParameters;
25  import javax.net.ssl.TrustManager;
26  import javax.net.ssl.TrustManagerFactorySpi;
27  import javax.net.ssl.X509TrustManager;
28  
29  /**
30   * Bogus {@link TrustManagerFactorySpi} which accepts any certificate
31   * even if it is invalid.
32   */
33  public class SecureChatTrustManagerFactory extends TrustManagerFactorySpi {
34  
35      private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {
36          public X509Certificate[] getAcceptedIssuers() {
37              return new X509Certificate[0];
38          }
39  
40          public void checkClientTrusted(
41                  X509Certificate[] chain, String authType) throws CertificateException {
42              // Always trust - it is an example.
43              // You should do something in the real world.
44              // You will reach here only if you enabled client certificate auth,
45              // as described in SecureChatSslContextFactory.
46              System.err.println(
47                      "UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN());
48          }
49  
50          public void checkServerTrusted(
51                  X509Certificate[] chain, String authType) throws CertificateException {
52              // Always trust - it is an example.
53              // You should do something in the real world.
54              System.err.println(
55                      "UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN());
56          }
57      };
58  
59      public static TrustManager[] getTrustManagers() {
60          return new TrustManager[] { DUMMY_TRUST_MANAGER };
61      }
62  
63      @Override
64      protected TrustManager[] engineGetTrustManagers() {
65          return getTrustManagers();
66      }
67  
68      @Override
69      protected void engineInit(KeyStore keystore) throws KeyStoreException {
70          // Unused
71      }
72  
73      @Override
74      protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
75              throws InvalidAlgorithmParameterException {
76          // Unused
77      }
78  }