View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.bueller;
23  
24  import javax.net.ssl.TrustManager;
25  import javax.net.ssl.X509TrustManager;
26  import java.security.cert.X509Certificate;
27  
28  import javax.net.ssl.X509TrustManager;
29  import java.security.cert.CertificateException;
30  import org.apache.commons.logging.Log; 
31  import org.apache.commons.logging.LogFactory;
32  /**
33   *
34   * @author AO
35   */
36  class AuthSSLX509TrustManager implements TrustManager {
37  
38  private X509TrustManager defaultTrustManager = null;
39  
40      /** Log object for this class. */
41      private static final Log LOG = LogFactory.getLog(AuthSSLX509TrustManager.class);
42  
43      /**
44       * Constructor for AuthSSLX509TrustManager.
45       */
46      public AuthSSLX509TrustManager(final X509TrustManager defaultTrustManager) {
47          super();
48          if (defaultTrustManager == null) {
49              throw new IllegalArgumentException("Trust manager may not be null");
50          }
51          this.defaultTrustManager = defaultTrustManager;
52      }
53  
54      /**
55       * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
56       */
57      public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
58          if (LOG.isInfoEnabled() && certificates != null) {
59              for (int c = 0; c < certificates.length; c++) {
60                  X509Certificate cert = certificates[c];
61                  LOG.info(" Client certificate " + (c + 1) + ":");
62                  LOG.info("  Subject DN: " + cert.getSubjectDN());
63                  LOG.info("  Signature Algorithm: " + cert.getSigAlgName());
64                  LOG.info("  Valid from: " + cert.getNotBefore() );
65                  LOG.info("  Valid until: " + cert.getNotAfter());
66                  LOG.info("  Issuer: " + cert.getIssuerDN());
67              }
68          }
69          defaultTrustManager.checkClientTrusted(certificates,authType);
70      }
71  
72      /**
73       * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
74       */
75      public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
76          if (LOG.isInfoEnabled() && certificates != null) {
77              for (int c = 0; c < certificates.length; c++) {
78                  X509Certificate cert = certificates[c];
79                  LOG.info(" Server certificate " + (c + 1) + ":");
80                  LOG.info("  Subject DN: " + cert.getSubjectDN());
81                  LOG.info("  Signature Algorithm: " + cert.getSigAlgName());
82                  LOG.info("  Valid from: " + cert.getNotBefore() );
83                  LOG.info("  Valid until: " + cert.getNotAfter());
84                  LOG.info("  Issuer: " + cert.getIssuerDN());
85              }
86          }
87          defaultTrustManager.checkServerTrusted(certificates,authType);
88      }
89  
90      /**
91       * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
92       */
93      public X509Certificate[] getAcceptedIssuers() {
94          return this.defaultTrustManager.getAcceptedIssuers();
95      }
96      
97  }