ExportControlled.java in  » Database-JDBC-Connection-Pool » mysql-connector-java-5.1.3 » com » mysql » jdbc » Java Source Code / Java Documentation Java Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Natural Language Processing
51.Net
52.Parser
53.PDF
54.Portal
55.Profiler
56.Project Management
57.Report
58.RSS RDF
59.Rule Engine
60.Science
61.Scripting
62.Search Engine
63.Security
64.Sevlet Container
65.Source Control
66.Swing Library
67.Template Engine
68.Test Coverage
69.Testing
70.UML
71.Web Crawler
72.Web Framework
73.Web Mail
74.Web Server
75.Web Services
76.Web Services apache cxf 2.2.6
77.Web Services AXIS2
78.Wiki Engine
79.Workflow Engines
80.XML
81.XML UI
Java Source Code / Java Documentation  » Database JDBC Connection Pool » mysql connector java 5.1.3 » com.mysql.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


        /*
         Copyright (C) 2002-2004 MySQL AB

         This program is free software; you can redistribute it and/or modify
         it under the terms of version 2 of the GNU General Public License as 
         published by the Free Software Foundation.

         There are special exceptions to the terms and conditions of the GPL 
         as it is applied to this software. View the full text of the 
         exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
         software distribution.

         This program is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         GNU General Public License for more details.

         You should have received a copy of the GNU General Public License
         along with this program; if not, write to the Free Software
         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



         */
        package com.mysql.jdbc;

        import java.io.BufferedInputStream;
        import java.io.BufferedOutputStream;
        import java.io.IOException;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.security.KeyManagementException;
        import java.security.KeyStore;
        import java.security.KeyStoreException;
        import java.security.NoSuchAlgorithmException;
        import java.security.UnrecoverableKeyException;
        import java.security.cert.CertificateException;
        import java.sql.SQLException;

        import javax.net.ssl.KeyManagerFactory;
        import javax.net.ssl.SSLContext;
        import javax.net.ssl.SSLSocketFactory;
        import javax.net.ssl.TrustManagerFactory;

        /**
         * Holds functionality that falls under export-control regulations.
         * 
         * @author Mark Matthews
         * 
         * @version $Id: ExportControlled.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
         *          Exp $
         */
        public class ExportControlled {
            private static final String SQL_STATE_BAD_SSL_PARAMS = "08000";

            protected static boolean enabled() {
                // we may wish to un-static-ify this class
                // this static method call may be removed entirely by the compiler
                return true;
            }

            /**
             * Converts the socket being used in the given MysqlIO to an SSLSocket by
             * performing the SSL/TLS handshake.
             * 
             * @param mysqlIO
             *            the MysqlIO instance containing the socket to convert to an
             *            SSLSocket.
             * 
             * @throws CommunicationsException
             *             if the handshake fails, or if this distribution of
             *             Connector/J doesn't contain the SSL crytpo hooks needed to
             *             perform the handshake.
             */
            protected static void transformSocketToSSLSocket(MysqlIO mysqlIO)
                    throws SQLException {
                javax.net.ssl.SSLSocketFactory sslFact = getSSLSocketFactoryDefaultOrConfigured(mysqlIO);

                try {
                    mysqlIO.mysqlConnection = sslFact.createSocket(
                            mysqlIO.mysqlConnection, mysqlIO.host,
                            mysqlIO.port, true);

                    // need to force TLSv1, or else JSSE tries to do a SSLv2 handshake
                    // which MySQL doesn't understand
                    ((javax.net.ssl.SSLSocket) mysqlIO.mysqlConnection)
                            .setEnabledProtocols(new String[] { "TLSv1" }); //$NON-NLS-1$
                    ((javax.net.ssl.SSLSocket) mysqlIO.mysqlConnection)
                            .startHandshake();

                    if (mysqlIO.connection.getUseUnbufferedInput()) {
                        mysqlIO.mysqlInput = mysqlIO.mysqlConnection
                                .getInputStream();
                    } else {
                        mysqlIO.mysqlInput = new BufferedInputStream(
                                mysqlIO.mysqlConnection.getInputStream(), 16384);
                    }

                    mysqlIO.mysqlOutput = new BufferedOutputStream(
                            mysqlIO.mysqlConnection.getOutputStream(), 16384);

                    mysqlIO.mysqlOutput.flush();
                } catch (IOException ioEx) {
                    throw SQLError.createCommunicationsException(
                            mysqlIO.connection, mysqlIO.lastPacketSentTimeMs,
                            ioEx);
                }
            }

            private ExportControlled() { /* prevent instantiation */
            }

            private static SSLSocketFactory getSSLSocketFactoryDefaultOrConfigured(
                    MysqlIO mysqlIO) throws SQLException {
                String clientCertificateKeyStoreUrl = mysqlIO.connection
                        .getClientCertificateKeyStoreUrl();
                String trustCertificateKeyStoreUrl = mysqlIO.connection
                        .getTrustCertificateKeyStoreUrl();
                String clientCertificateKeyStoreType = mysqlIO.connection
                        .getClientCertificateKeyStoreType();
                String clientCertificateKeyStorePassword = mysqlIO.connection
                        .getClientCertificateKeyStorePassword();
                String trustCertificateKeyStoreType = mysqlIO.connection
                        .getTrustCertificateKeyStoreType();
                String trustCertificateKeyStorePassword = mysqlIO.connection
                        .getTrustCertificateKeyStorePassword();

                if (StringUtils.isNullOrEmpty(clientCertificateKeyStoreUrl)
                        && StringUtils
                                .isNullOrEmpty(trustCertificateKeyStoreUrl)) {
                    return (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory
                            .getDefault();
                }

                TrustManagerFactory tmf = null;
                KeyManagerFactory kmf = null;

                try {
                    tmf = TrustManagerFactory.getInstance(TrustManagerFactory
                            .getDefaultAlgorithm());
                    kmf = KeyManagerFactory.getInstance(KeyManagerFactory
                            .getDefaultAlgorithm());
                } catch (NoSuchAlgorithmException nsae) {
                    throw SQLError
                            .createSQLException(
                                    "Default algorithm definitions for TrustManager and/or KeyManager are invalid.  Check java security properties file.",
                                    SQL_STATE_BAD_SSL_PARAMS, 0, false);
                }

                if (StringUtils.isNullOrEmpty(clientCertificateKeyStoreUrl)) {
                    try {
                        KeyStore clientKeyStore = KeyStore
                                .getInstance(clientCertificateKeyStoreType);
                        URL ksURL = new URL(clientCertificateKeyStoreUrl);
                        char[] password = (clientCertificateKeyStorePassword == null) ? new char[0]
                                : clientCertificateKeyStorePassword
                                        .toCharArray();
                        clientKeyStore.load(ksURL.openStream(), password);
                        kmf.init(clientKeyStore, password);
                    } catch (UnrecoverableKeyException uke) {
                        throw SQLError
                                .createSQLException(
                                        "Could not recover keys from client keystore.  Check password?",
                                        SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (NoSuchAlgorithmException nsae) {
                        throw SQLError.createSQLException(
                                "Unsupported keystore algorithm ["
                                        + nsae.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (KeyStoreException kse) {
                        throw SQLError.createSQLException(
                                "Could not create KeyStore instance ["
                                        + kse.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (CertificateException nsae) {
                        throw SQLError
                                .createSQLException("Could not load client"
                                        + clientCertificateKeyStoreType
                                        + " keystore from "
                                        + clientCertificateKeyStoreUrl);
                    } catch (MalformedURLException mue) {
                        throw SQLError
                                .createSQLException(
                                        clientCertificateKeyStoreUrl
                                                + " does not appear to be a valid URL.",
                                        SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (IOException ioe) {
                        throw SQLError.createSQLException("Cannot open "
                                + clientCertificateKeyStoreUrl + " ["
                                + ioe.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    }
                }

                if (StringUtils.isNullOrEmpty(trustCertificateKeyStoreUrl)) {

                    try {
                        KeyStore trustKeyStore = KeyStore
                                .getInstance(trustCertificateKeyStoreType);
                        URL ksURL = new URL(trustCertificateKeyStoreUrl);

                        char[] password = (trustCertificateKeyStorePassword == null) ? new char[0]
                                : trustCertificateKeyStorePassword
                                        .toCharArray();
                        trustKeyStore.load(ksURL.openStream(), password);
                        tmf.init(trustKeyStore);
                    } catch (NoSuchAlgorithmException nsae) {
                        throw SQLError.createSQLException(
                                "Unsupported keystore algorithm ["
                                        + nsae.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (KeyStoreException kse) {
                        throw SQLError.createSQLException(
                                "Could not create KeyStore instance ["
                                        + kse.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (CertificateException nsae) {
                        throw SQLError.createSQLException(
                                "Could not load trust"
                                        + trustCertificateKeyStoreType
                                        + " keystore from "
                                        + trustCertificateKeyStoreUrl,
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (MalformedURLException mue) {
                        throw SQLError
                                .createSQLException(
                                        trustCertificateKeyStoreUrl
                                                + " does not appear to be a valid URL.",
                                        SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    } catch (IOException ioe) {
                        throw SQLError.createSQLException("Cannot open "
                                + trustCertificateKeyStoreUrl + " ["
                                + ioe.getMessage() + "]",
                                SQL_STATE_BAD_SSL_PARAMS, 0, false);
                    }
                }

                SSLContext sslContext = null;

                try {
                    sslContext = SSLContext.getInstance("TLS");
                    sslContext.init(kmf.getKeyManagers(), tmf
                            .getTrustManagers(), null);

                    return sslContext.getSocketFactory();
                } catch (NoSuchAlgorithmException nsae) {
                    throw SQLError.createSQLException("TLS"
                            + " is not a valid SSL protocol.",
                            SQL_STATE_BAD_SSL_PARAMS, 0, false);
                } catch (KeyManagementException kme) {
                    throw SQLError.createSQLException(
                            "KeyManagementException: " + kme.getMessage(),
                            SQL_STATE_BAD_SSL_PARAMS, 0, false);
                }
            }

        }
w__w__w_.__j_a__v___a___2s__.c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.