Search in sources :

Example 26 with SSLProtocolException

use of javax.net.ssl.SSLProtocolException in project mongo-java-driver by mongodb.

the class TlsExplorer method exploreSNIExt.

/*
   * struct { NameType name_type; select (name_type) { case host_name:
   * HostName; } name; } ServerName;
   *
   * enum { host_name(0), (255) } NameType;
   *
   * opaque HostName<1..2^16-1>;
   *
   * struct { ServerName server_name_list<1..2^16-1> } ServerNameList;
   */
private static Map<Integer, SNIServerName> exploreSNIExt(ByteBuffer input, int extLen) throws SSLProtocolException {
    Map<Integer, SNIServerName> sniMap = new HashMap<>();
    int remains = extLen;
    if (extLen >= 2) {
        // "server_name" extension in ClientHello
        // length of server_name_list
        int listLen = getInt16(input);
        if (listLen == 0 || listLen + 2 != extLen)
            throw new SSLProtocolException("Invalid server name indication extension");
        // 2: the length field of server_name_list
        remains -= 2;
        while (remains > 0) {
            // name_type
            int code = getInt8(input);
            // length field of server name
            int snLen = getInt16(input);
            if (snLen > remains)
                throw new SSLProtocolException("Not enough data to fill declared vector size");
            byte[] encoded = new byte[snLen];
            input.get(encoded);
            SNIServerName serverName;
            switch(code) {
                case StandardConstants.SNI_HOST_NAME:
                    if (encoded.length == 0)
                        throw new SSLProtocolException("Empty HostName in server name indication");
                    serverName = new SNIHostName(encoded);
                    break;
                default:
                    serverName = new UnknownServerName(code, encoded);
            }
            // check for duplicated server name type
            if (sniMap.put(serverName.getType(), serverName) != null)
                throw new SSLProtocolException("Duplicated server name of type " + serverName.getType());
            // NameType: 1 byte; HostName;
            remains -= encoded.length + 3;
        // length: 2 bytesProduced
        }
    } else if (extLen == 0) {
        // "server_name" extension in ServerHello
        throw new SSLProtocolException("Not server name indication extension in client");
    }
    if (remains != 0)
        throw new SSLProtocolException("Invalid server name indication extension");
    return sniMap;
}
Also used : SNIServerName(javax.net.ssl.SNIServerName) SSLProtocolException(javax.net.ssl.SSLProtocolException) HashMap(java.util.HashMap) SNIHostName(javax.net.ssl.SNIHostName)

Example 27 with SSLProtocolException

use of javax.net.ssl.SSLProtocolException in project mongo-java-driver by mongodb.

the class TlsExplorer method explore.

public static Map<Integer, SNIServerName> explore(ByteBuffer source) throws SSLProtocolException {
    if (source.remaining() < RECORD_HEADER_SIZE)
        throw new BufferUnderflowException();
    ((Buffer) source).mark();
    try {
        byte firstByte = source.get();
        // ignore second byte
        ignore(source, 1);
        byte thirdByte = source.get();
        if ((firstByte & 0x80) != 0 && thirdByte == 0x01) {
            // looks like a V2ClientHello
            return new HashMap<>();
        } else if (firstByte == 22) {
            // 22: handshake record
            return exploreTLSRecord(source, firstByte);
        } else {
            throw new SSLProtocolException("Not handshake record");
        }
    } finally {
        ((Buffer) source).reset();
    }
}
Also used : Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer) SSLProtocolException(javax.net.ssl.SSLProtocolException) HashMap(java.util.HashMap) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 28 with SSLProtocolException

use of javax.net.ssl.SSLProtocolException in project mongo-java-driver by mongodb.

the class TlsExplorer method exploreHandshake.

/*
   * enum { hello_request(0), client_hello(1), server_hello(2),
   * certificate(11), server_key_exchange (12), certificate_request(13),
   * server_hello_done(14), certificate_verify(15), client_key_exchange(16),
   * finished(20) (255) } HandshakeType;
   *
   * struct { HandshakeType msg_type; uint24 length; select (HandshakeType) {
   * case hello_request: HelloRequest; case client_hello: ClientHello; case
   * server_hello: ServerHello; case certificate: Certificate; case
   * server_key_exchange: ServerKeyExchange; case certificate_request:
   * CertificateRequest; case server_hello_done: ServerHelloDone; case
   * certificate_verify: CertificateVerify; case client_key_exchange:
   * ClientKeyExchange; case finished: Finished; } body; } Handshake;
   */
private static Map<Integer, SNIServerName> exploreHandshake(ByteBuffer input, int recordLength) throws SSLProtocolException {
    // What is the handshake type?
    byte handshakeType = input.get();
    if (// 0x01: client_hello message
    handshakeType != 0x01)
        throw new SSLProtocolException("Not initial handshaking");
    // What is the handshake body length?
    int handshakeLength = getInt24(input);
    // records, but in practice this does not occur.
    if (// 4: handshake header size
    handshakeLength > recordLength - 4)
        throw new SSLProtocolException("Handshake message spans multiple records");
    ((Buffer) input).limit(handshakeLength + input.position());
    return exploreClientHello(input);
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 29 with SSLProtocolException

use of javax.net.ssl.SSLProtocolException in project j2objc by google.

the class SSLProtocolExceptionTest method test_Constructor01.

/**
 * Test for <code>SSLProtocolException(String)</code> constructor Assertion:
 * constructs SSLProtocolException with detail message msg. Parameter
 * <code>msg</code> is not null.
 */
public void test_Constructor01() {
    SSLProtocolException sslE;
    for (int i = 0; i < msgs.length; i++) {
        sslE = new SSLProtocolException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), sslE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", sslE.getCause());
    }
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException)

Example 30 with SSLProtocolException

use of javax.net.ssl.SSLProtocolException in project j2objc by google.

the class SSLProtocolExceptionTest method test_Constructor02.

/**
 * Test for <code>SSLProtocolException(String)</code> constructor Assertion:
 * constructs SSLProtocolException with detail message msg. Parameter
 * <code>msg</code> is null.
 */
public void test_Constructor02() {
    String msg = null;
    SSLProtocolException sslE = new SSLProtocolException(msg);
    assertNull("getMessage() must return null.", sslE.getMessage());
    assertNull("getCause() must return null", sslE.getCause());
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException)

Aggregations

SSLProtocolException (javax.net.ssl.SSLProtocolException)32 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)9 IOException (java.io.IOException)6 SSLException (javax.net.ssl.SSLException)4 SSLKeyException (javax.net.ssl.SSLKeyException)4 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)4 BufferUnderflowException (java.nio.BufferUnderflowException)3 CertificateException (java.security.cert.CertificateException)3 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)3 Test (org.junit.Test)3 HandshakeState (sun.security.ssl.HandshakeStateManager.HandshakeState)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 HttpRetryException (java.net.HttpRetryException)2 InetSocketAddress (java.net.InetSocketAddress)2 NoRouteToHostException (java.net.NoRouteToHostException)2 ProtocolException (java.net.ProtocolException)2 Socket (java.net.Socket)2 SocketTimeoutException (java.net.SocketTimeoutException)2