|
Glassfish example source code file (SSLClientConfigurator.java)
The Glassfish SSLClientConfigurator.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.admin.mbeanserver.ssl; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CRL; import java.security.cert.CRLException; import java.security.cert.CertPathParameters; import java.security.cert.CertStore; import java.security.cert.CertStoreParameters; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.CollectionCertStoreParameters; import java.security.cert.PKIXBuilderParameters; import java.security.cert.X509CertSelector; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import javax.net.ssl.CertPathTrustManagerParameters; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.ManagerFactoryParameters; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; /** * This class is a utility class that would configure a client socket factory using * either the SSL defaults for GlassFish or via params supplied. * This is a singleton class. * The initial use for this class is to configure the SslRMIClientSocketFactory * for use with the JMX connector. * * @author prasads@dev.java.net */ public class SSLClientConfigurator { private SSLParams sslParams; private static SSLClientConfigurator sslCC; private SSLContext sslContext; private SSLSocketFactory sslSocketFactory; private Logger _logger = Logger.getLogger(SSLClientConfigurator.class.getName()); private String[] enabledProtocols; private String[] enabledCipherSuites; // Private constructor private SSLClientConfigurator() { } public static SSLClientConfigurator getInstance() { if(sslCC == null ) { sslCC = new SSLClientConfigurator(); return sslCC; } else { return sslCC; } } public void setSSLParams(SSLParams sslParams) { this.sslParams = sslParams; } /** * This method creates an SSLContext based on the default provider and then * created TrustManagers, KeyManagers and initializes the SSLContext with * the TrustManager, KeyManager * * @return SSLContext */ public SSLContext configure(SSLParams sslParams) { this.sslParams = sslParams; // get the protocol and the SSLContext. String protocol = sslParams.getProtocol(); try { sslContext = SSLContext.getInstance(protocol); } catch (NoSuchAlgorithmException ex) { _logger.log(Level.SEVERE, null, ex); } configureCiphersAndProtocols(); // get the TrustManagers String trustAlgorithm = sslParams.getTrustAlgorithm(); if (trustAlgorithm == null) { trustAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); } // Certificate encoding algorithm (e.g., SunX509) String algorithm = sslParams.getKeyAlgorithm(); if (algorithm == null) { algorithm = "SunX509"; } String keyAlias = sslParams.getCertNickname(); if (keyAlias == null) { keyAlias = "s1as"; } // Initialize the SSLContext try { sslContext.init(getKeyManagers(algorithm, keyAlias), getTrustManagers(trustAlgorithm), new SecureRandom()); } catch (Exception ex) { _logger.log(Level.SEVERE, null, ex); } return sslContext; } /** * Gets a list of Enabled Protocols * @return */ public String[] getEnabledProtocols() { if(enabledProtocols == null ) { configureCiphersAndProtocols(); } return enabledProtocols; } /** * Returns the list of Enabled Protocols as a comma separated String * @return */ public String getEnabledProtocolsAsString() { if(getEnabledProtocols() != null && getEnabledProtocols().length >0) { return toCommaSeparatedString(getEnabledProtocols()); } else { return null ; } } /** * gets a list of Enabled Cipher Suites * @return */ public String[] getEnabledCipherSuites() { if(enabledCipherSuites == null) { configureCiphersAndProtocols(); } return enabledCipherSuites; } /** * Returns a list of Enabled Cipher Suites as a String * @return */ public String getEnabledCipherSuitesAsString() { if(getEnabledCipherSuites() != null && getEnabledCipherSuites().length > 0) { return toCommaSeparatedString(getEnabledCipherSuites()); } else { return null; } } /** * Gets the initialized key managers. */ protected KeyManager[] getKeyManagers(String algorithm, String keyAlias) throws Exception { KeyManager[] kms = null; // hack if(System.getProperty("javax.net.ssl.keyStore") == null) { _logger.log(Level.WARNING, " No keystores defined"); return null; } _logger.log(Level.FINE, "Algorithm ::" + algorithm); _logger.log(Level.FINE, "Key Alias ::" + keyAlias); _logger.log(Level.FINE, "KeyStore Type ::" + sslParams.getKeyStoreType()); String keystorePass = sslParams.getKeyStorePassword(); KeyStore ks = getStore(sslParams.getKeyStoreType(), sslParams.getKeyStore().getPath(), keystorePass); if (keyAlias != null && !ks.isKeyEntry(keyAlias)) { _logger.log(Level.WARNING, "No Key store found for " + keyAlias); //throw new IOException( "jsse.alias_no_key_entry for "+keyAlias); return null; } KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); kmf.init(ks, keystorePass.toCharArray()); kms = kmf.getKeyManagers(); return kms; } /** * Gets the intialized trust managers. */ protected TrustManager[] getTrustManagers(String algorithm) throws Exception { String crlf = sslParams.getCrlFile(); TrustManager[] tms = null; _logger.log(Level.FINE, "in getTrustManagers "+ " TrustManager type = "+ sslParams.getTrustStoreType() + " path = "+sslParams.getTrustStore().getPath() + " password = "+ sslParams.getTrustStorePassword().toString()); KeyStore trustStore = getStore(sslParams.getTrustStoreType(), sslParams.getTrustStore().getPath(), sslParams.getTrustStorePassword().toString()); if (trustStore != null) { if (crlf == null) { TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); tmf.init(trustStore); tms = tmf.getTrustManagers(); } else { TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); CertPathParameters params = getParameters(algorithm, crlf, trustStore); ManagerFactoryParameters mfp = new CertPathTrustManagerParameters(params); tmf.init(mfp); tms = tmf.getTrustManagers(); } } return tms; } /** * Return the initialization parameters for the TrustManager. * Currently, only the default <code>PKIX is supported. * * @param algorithm The algorithm to get parameters for. * @param crlf The path to the CRL file. * @param trustStore The configured TrustStore. * @return The parameters including the CRLs and TrustStore. */ protected CertPathParameters getParameters(String algorithm, String crlf, KeyStore trustStore) throws Exception { CertPathParameters params = null; if ("PKIX".equalsIgnoreCase(algorithm)) { PKIXBuilderParameters xparams = new PKIXBuilderParameters(trustStore, new X509CertSelector()); Collection crls = getCRLs(crlf); CertStoreParameters csp = new CollectionCertStoreParameters(crls); CertStore store = CertStore.getInstance("Collection", csp); xparams.addCertStore(store); xparams.setRevocationEnabled(true); String trustLength = sslParams.getTrustMaxCertLength(); if (trustLength != null) { try { xparams.setMaxPathLength(Integer.parseInt(trustLength)); } catch(Exception ex) { _logger.warning("Bad maxCertLength: " + trustLength); } } params = xparams; } else { throw new CRLException("CRLs not supported for type: " + algorithm); } return params; } /** * Load the collection of CRLs. */ protected Collection<? extends CRL> getCRLs(String crlf) throws IOException, CRLException, CertificateException { File crlFile = new File(crlf); if (!crlFile.isAbsolute()) { crlFile = new File(System.getProperty("catalina.base"), crlf); } Collection<? extends CRL> crls = null; InputStream is = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); is = new FileInputStream(crlFile); crls = cf.generateCRLs(is); } catch(IOException iex) { throw iex; } catch(CRLException crle) { throw crle; } catch(CertificateException ce) { throw ce; } finally { if (is != null) { try { is.close(); } catch (Exception ex) { } } } return crls; } /* * Gets the key- or truststore with the specified type, path, and password. */ private KeyStore getStore(String type, String path, String pass) throws IOException { KeyStore ks = null; InputStream istream = null; try { ks = KeyStore.getInstance(type); if (!("PKCS11".equalsIgnoreCase(type) || "".equalsIgnoreCase(path))) { File keyStoreFile = new File(path); if (!keyStoreFile.isAbsolute()) { keyStoreFile = new File(System.getProperty("catalina.base"), path); } istream = new FileInputStream(keyStoreFile); } ks.load(istream, pass.toCharArray()); } catch (FileNotFoundException fnfe) { _logger.log(Level.SEVERE, "jsse.keystore_load_failed for:"+ "type = "+ type + "path = "+ path + fnfe.getMessage(), fnfe); throw fnfe; } catch (IOException ioe) { _logger.log(Level.SEVERE, "jsse.keystore_load_failed for:"+ "type = "+ type + "path = "+ path + ioe.getMessage(), ioe); throw ioe; } catch(Exception ex) { _logger.log(Level.SEVERE, "jsse.keystore_load_failed for:"+ "type = "+ type + "path = "+ path + ex.getMessage(), ex); throw new IOException(ex.getMessage()); } finally { if (istream != null) { try { istream.close(); } catch (IOException ioe) { // Do nothing } } } return ks; } private void configureCiphersAndProtocols() { List<String> tmpSSLArtifactsList = new LinkedList Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish SSLClientConfigurator.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.