Example usage for javax.net.ssl SSLSocket getUseClientMode

List of usage examples for javax.net.ssl SSLSocket getUseClientMode

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket getUseClientMode.

Prototype

public abstract boolean getUseClientMode();

Source Link

Document

Returns true if the socket is set to use client mode when handshaking.

Usage

From source file:org.apache.nifi.registry.security.util.CertificateUtils.java

/**
 * Returns the DN extracted from the peer certificate (the server DN if run on the client; the client DN (if available) if run on the server).
 *
 * If the client auth setting is WANT or NONE and a client certificate is not present, this method will return {@code null}.
 * If the client auth is NEED, it will throw a {@link CertificateException}.
 *
 * @param socket the SSL Socket//  w  ww . java2 s . c o  m
 * @return the extracted DN
 * @throws CertificateException if there is a problem parsing the certificate
 */
public static String extractPeerDNFromSSLSocket(Socket socket) throws CertificateException {
    String dn = null;
    if (socket instanceof SSLSocket) {
        final SSLSocket sslSocket = (SSLSocket) socket;

        boolean clientMode = sslSocket.getUseClientMode();
        logger.debug("SSL Socket in {} mode", clientMode ? "client" : "server");
        ClientAuth clientAuth = getClientAuthStatus(sslSocket);
        logger.debug("SSL Socket client auth status: {}", clientAuth);

        if (clientMode) {
            logger.debug(
                    "This socket is in client mode, so attempting to extract certificate from remote 'server' socket");
            dn = extractPeerDNFromServerSSLSocket(sslSocket);
        } else {
            logger.debug(
                    "This socket is in server mode, so attempting to extract certificate from remote 'client' socket");
            dn = extractPeerDNFromClientSSLSocket(sslSocket);
        }
    }

    return dn;
}