|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.security.transport; |
| 7 | + |
| 8 | +import io.netty.channel.Channel; |
| 9 | +import io.netty.handler.ssl.SslHandler; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 12 | +import org.apache.logging.log4j.util.Supplier; |
| 13 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 14 | +import org.elasticsearch.http.HttpChannel; |
| 15 | +import org.elasticsearch.http.netty4.Netty4HttpChannel; |
| 16 | +import org.elasticsearch.http.nio.NioHttpChannel; |
| 17 | +import org.elasticsearch.nio.SocketChannelContext; |
| 18 | +import org.elasticsearch.transport.TcpChannel; |
| 19 | +import org.elasticsearch.transport.netty4.Netty4TcpChannel; |
| 20 | +import org.elasticsearch.transport.nio.NioTcpChannel; |
| 21 | +import org.elasticsearch.xpack.security.authc.pki.PkiRealm; |
| 22 | +import org.elasticsearch.xpack.security.transport.nio.SSLChannelContext; |
| 23 | + |
| 24 | +import javax.net.ssl.SSLEngine; |
| 25 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 26 | +import java.security.cert.Certificate; |
| 27 | +import java.security.cert.X509Certificate; |
| 28 | + |
| 29 | +public class SSLEngineUtils { |
| 30 | + |
| 31 | + private SSLEngineUtils() {} |
| 32 | + |
| 33 | + public static void extractClientCertificates(Logger logger, ThreadContext threadContext, HttpChannel httpChannel) { |
| 34 | + SSLEngine sslEngine = getSSLEngine(httpChannel); |
| 35 | + extract(logger, threadContext, sslEngine, httpChannel); |
| 36 | + } |
| 37 | + |
| 38 | + public static void extractClientCertificates(Logger logger, ThreadContext threadContext, TcpChannel tcpChannel) { |
| 39 | + SSLEngine sslEngine = getSSLEngine(tcpChannel); |
| 40 | + extract(logger, threadContext, sslEngine, tcpChannel); |
| 41 | + } |
| 42 | + |
| 43 | + public static SSLEngine getSSLEngine(HttpChannel httpChannel) { |
| 44 | + if (httpChannel instanceof Netty4HttpChannel) { |
| 45 | + Channel nettyChannel = ((Netty4HttpChannel) httpChannel).getNettyChannel(); |
| 46 | + SslHandler handler = nettyChannel.pipeline().get(SslHandler.class); |
| 47 | + assert handler != null : "Must have SslHandler"; |
| 48 | + return handler.engine(); |
| 49 | + } else if (httpChannel instanceof NioHttpChannel) { |
| 50 | + SocketChannelContext context = ((NioHttpChannel) httpChannel).getContext(); |
| 51 | + assert context instanceof SSLChannelContext : "Must be SSLChannelContext.class, found: " + context.getClass(); |
| 52 | + return ((SSLChannelContext) context).getSSLEngine(); |
| 53 | + } else { |
| 54 | + throw new AssertionError("Unknown channel class type: " + httpChannel.getClass()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static SSLEngine getSSLEngine(TcpChannel tcpChannel) { |
| 59 | + if (tcpChannel instanceof Netty4TcpChannel) { |
| 60 | + Channel nettyChannel = ((Netty4TcpChannel) tcpChannel).getNettyChannel(); |
| 61 | + SslHandler handler = nettyChannel.pipeline().get(SslHandler.class); |
| 62 | + assert handler != null : "Must have SslHandler"; |
| 63 | + return handler.engine(); |
| 64 | + } else if (tcpChannel instanceof NioTcpChannel) { |
| 65 | + SocketChannelContext context = ((NioTcpChannel) tcpChannel).getContext(); |
| 66 | + assert context instanceof SSLChannelContext : "Must be SSLChannelContext.class, found: " + context.getClass(); |
| 67 | + return ((SSLChannelContext) context).getSSLEngine(); |
| 68 | + } else { |
| 69 | + throw new AssertionError("Unknown channel class type: " + tcpChannel.getClass()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void extract(Logger logger, ThreadContext threadContext, SSLEngine sslEngine, Object channel) { |
| 74 | + try { |
| 75 | + Certificate[] certs = sslEngine.getSession().getPeerCertificates(); |
| 76 | + if (certs instanceof X509Certificate[]) { |
| 77 | + threadContext.putTransient(PkiRealm.PKI_CERT_HEADER_NAME, certs); |
| 78 | + } |
| 79 | + } catch (SSLPeerUnverifiedException e) { |
| 80 | + // this happens when client authentication is optional and the client does not provide credentials. If client |
| 81 | + // authentication was required then this connection should be closed before ever getting into this class |
| 82 | + assert sslEngine.getNeedClientAuth() == false; |
| 83 | + assert sslEngine.getWantClientAuth(); |
| 84 | + if (logger.isTraceEnabled()) { |
| 85 | + logger.trace( |
| 86 | + (Supplier<?>) () -> new ParameterizedMessage( |
| 87 | + "SSL Peer did not present a certificate on channel [{}]", channel), e); |
| 88 | + } else if (logger.isDebugEnabled()) { |
| 89 | + logger.debug("SSL Peer did not present a certificate on channel [{}]", channel); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments