📄 sslsocket.java
字号:
/* SSLSocket.java -- the SSL socket class. Copyright (C) 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.net.ssl.provider;import java.io.BufferedOutputStream;import java.io.InputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;import java.math.BigInteger;import java.net.InetAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.SocketException;import java.nio.channels.SocketChannel;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.KeyPair;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.Principal;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Security;import java.security.SecureRandom;import java.security.cert.X509Certificate;import java.security.interfaces.DSAPrivateKey;import java.security.interfaces.DSAPublicKey;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.util.Arrays;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.SortedSet;import java.util.TreeSet;import java.util.logging.Logger;import javax.crypto.Cipher;import javax.crypto.Mac;import javax.crypto.NoSuchPaddingException;import javax.crypto.interfaces.DHPublicKey;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import javax.net.ssl.HandshakeCompletedEvent;import javax.net.ssl.HandshakeCompletedListener;import javax.net.ssl.SSLException;import javax.net.ssl.SSLHandshakeException;import javax.net.ssl.SSLPeerUnverifiedException;import javax.net.ssl.SSLProtocolException;import javax.net.ssl.SSLSession;import javax.net.ssl.X509KeyManager;import javax.net.ssl.X509TrustManager;import javax.security.auth.callback.Callback;import javax.security.auth.callback.CallbackHandler;import javax.security.auth.callback.ConfirmationCallback;import javax.security.auth.callback.PasswordCallback;import javax.security.auth.callback.TextInputCallback;import gnu.classpath.debug.Component;import gnu.classpath.debug.SystemLogger;import gnu.java.security.Registry;import gnu.javax.security.auth.callback.DefaultCallbackHandler;import gnu.java.security.hash.HashFactory;import gnu.java.security.hash.IMessageDigest;import gnu.javax.crypto.key.IKeyAgreementParty;import gnu.javax.crypto.key.KeyAgreementFactory;import gnu.javax.crypto.key.KeyAgreementException;import gnu.javax.crypto.key.OutgoingMessage;import gnu.javax.crypto.key.IncomingMessage;import gnu.javax.crypto.key.dh.DiffieHellmanKeyAgreement;import gnu.javax.crypto.key.dh.ElGamalKeyAgreement;import gnu.javax.crypto.key.dh.GnuDHPrivateKey;import gnu.javax.crypto.key.dh.GnuDHPublicKey;import gnu.javax.crypto.key.srp6.SRPPrivateKey;import gnu.javax.crypto.key.srp6.SRPPublicKey;import gnu.javax.crypto.key.srp6.SRP6KeyAgreement;import gnu.javax.crypto.mac.IMac;import gnu.javax.crypto.mode.IMode;import gnu.javax.crypto.prng.ARCFour;import gnu.java.security.prng.IRandom;import gnu.java.security.prng.LimitReachedException;import gnu.javax.crypto.sasl.srp.SRPAuthInfoProvider;import gnu.javax.crypto.sasl.srp.SRPRegistry;import gnu.java.security.sig.ISignature;import gnu.java.security.sig.SignatureFactory;import gnu.java.security.sig.dss.DSSSignature;import gnu.java.security.sig.rsa.EME_PKCS1_V1_5;import gnu.java.security.sig.rsa.RSA;import gnu.javax.net.ssl.SRPTrustManager;/** * This is the core of the Jessie SSL implementation; it implements the {@link * javax.net.ssl.SSLSocket} for normal and "wrapped" sockets, and handles all * protocols implemented by this library. */final class SSLSocket extends javax.net.ssl.SSLSocket{ // This class is almost unbearably large and complex, but is laid out // as follows: // // 1. Fields. // 2. Constructors. // 3. SSLSocket methods. These are the public methods defined in // javax.net.ssl.SSLSocket. // 4. Socket methods. These override the public methods of java.net.Socket, // and delegate the method call to either the underlying socket if this is // a wrapped socket, or to the superclass. // 5. Package-private methods that various pieces of Jessie use. // 6. Private methods. These compose the SSL handshake. // // Each part is preceeded by a form feed.// Constants and fields. // ------------------------------------------------------------------------- // Debuggery. private static final boolean DEBUG_HANDSHAKE_LAYER = true; private static final boolean DEBUG_KEY_EXCHANGE = false; private static final Logger logger = SystemLogger.SYSTEM; // Fields for using this class as a wrapped socket. private Socket underlyingSocket; private int underlyingPort; private boolean autoClose; // Cryptography fields. SessionContext sessionContext; Session session; LinkedList handshakeListeners; private boolean clientMode, wantClientAuth, needClientAuth, createSessions; private boolean handshakeDone; // I/O fields. private String remoteHost; private InputStream socketIn; private OutputStream socketOut; private InputStream applicationIn; private OutputStream applicationOut; private InputStream handshakeIn; private OutputStream handshakeOut;// private ThreadGroup recordLayer; RecordInput recordInput;// RecordOutput recordOutput; private long handshakeTime; private SocketChannel channel; static SortedSet supportedProtocols = new TreeSet(); static List supportedSuites = new ArrayList(30);// Static initializer. // ------------------------------------------------------------------------- static { //supportedProtocols.add(ProtocolVersion.TLS_1_1); supportedProtocols.add(ProtocolVersion.TLS_1); supportedProtocols.add(ProtocolVersion.SSL_3); // These are in preference order. It's my preference order, but I'm not // a total idiot. supportedSuites.add(CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_RSA_WITH_AES_256_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_RC4_128_MD5); supportedSuites.add(CipherSuite.TLS_RSA_WITH_RC4_128_SHA); supportedSuites.add(CipherSuite.TLS_DHE_DSS_WITH_DES_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_RSA_WITH_DES_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_DSS_WITH_DES_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_RSA_WITH_DES_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_DES_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_EXPORT_WITH_DES40_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_EXPORT_WITH_RC4_40_MD5); supportedSuites.add(CipherSuite.TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA); supportedSuites.add(CipherSuite.TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA); supportedSuites.add(CipherSuite.TLS_RSA_WITH_NULL_MD5); supportedSuites.add(CipherSuite.TLS_RSA_WITH_NULL_SHA); }// Constructors. // ------------------------------------------------------------------------- SSLSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { underlyingSocket = socket; remoteHost = host; underlyingPort = port; this.autoClose = autoClose; initialize(); } SSLSocket (Socket socket, SocketChannel channel) throws IOException { underlyingSocket = socket; this.channel = channel; initialize (); } SSLSocket() throws IOException { super(); initialize(); } SSLSocket(InetAddress addr, int port) throws IOException { super(addr, port); initialize(); remoteHost = addr.getHostName(); if (remoteHost == null) { remoteHost = addr.getHostAddress(); } } SSLSocket(InetAddress addr, int port, InetAddress laddr, int lport) throws IOException { super(addr, port, laddr, lport); initialize(); remoteHost = addr.getHostName(); if (remoteHost == null) remoteHost = addr.getHostAddress(); } SSLSocket(String host, int port) throws IOException { super(host, port); initialize(); remoteHost = host; } SSLSocket(String host, int port, InetAddress laddr, int lport) throws IOException { super(host, port, laddr, lport); initialize(); remoteHost = host; } private void initialize() { session = new Session(); session.enabledSuites = new ArrayList(supportedSuites); session.enabledProtocols = new TreeSet(supportedProtocols); session.protocol = ProtocolVersion.TLS_1; session.params.setVersion (ProtocolVersion.TLS_1); handshakeListeners = new LinkedList(); handshakeDone = false; }// SSL methods. // ------------------------------------------------------------------------- public void addHandshakeCompletedListener(HandshakeCompletedListener l) { synchronized (handshakeListeners) { if (l == null) throw new NullPointerException(); if (!handshakeListeners.contains(l)) handshakeListeners.add(l); } } public void removeHandshakeCompletedListener(HandshakeCompletedListener l) { synchronized (handshakeListeners) { handshakeListeners.remove(l); } } public String[] getEnabledProtocols() { synchronized (session.enabledProtocols) { try { return (String[]) Util.transform(session.enabledProtocols.toArray(), String.class, "toString", null); } catch (Exception x) { RuntimeException re = new RuntimeException (x.getMessage()); re.initCause (x); throw re; } } } public void setEnabledProtocols(String[] protocols) { if (protocols == null || protocols.length == 0) throw new IllegalArgumentException(); for (int i = 0; i < protocols.length; i++) { if (!(protocols[i].equalsIgnoreCase("SSLv3") || protocols[i].equalsIgnoreCase("TLSv1") || protocols[i].equalsIgnoreCase("TLSv1.1"))) { throw new IllegalArgumentException("unsupported protocol: " + protocols[i]); } } synchronized (session.enabledProtocols) { session.enabledProtocols.clear(); for (int i = 0; i < protocols.length; i++) { if (protocols[i].equalsIgnoreCase("SSLv3")) { session.enabledProtocols.add(ProtocolVersion.SSL_3); } else if (protocols[i].equalsIgnoreCase("TLSv1")) { session.enabledProtocols.add(ProtocolVersion.TLS_1); } else { session.enabledProtocols.add(ProtocolVersion.TLS_1_1); } } } } public String[] getSupportedProtocols() { return new String[] { /* "TLSv1.1", */ "TLSv1", "SSLv3" }; } public String[] getEnabledCipherSuites() { synchronized (session.enabledSuites) { try { return (String[]) Util.transform(session.enabledSuites.toArray(), String.class, "toString", null); } catch (Exception x) { RuntimeException re = new RuntimeException (x.getMessage()); re.initCause (x);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -