SSLEngine can be retrieved through Pipline / ChannelHandlerContext
ChannelHandlerContext ctx = ... SslHandler sslhandler = (SslHandler) ctx.channel().pipeline().get("ssl"); sslhandler.engine().getSession().getPeerCertificateChain()[0].getSubjectDN());
This allows you to obtain certificates in handler objects. Please note that SSL-Handshake needs to be completed when you do this. Otherwise you will receive
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
an exception. To avoid this, you can listen to userEvent (in our case, HandshakeCompletionEvent) in a handler, which might look like this:
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { logger.info("userEventTriggered: {0}, Class: {1}", evt.toString(), evt.getClass()); if (evt instanceof HandshakeCompletionEvent) { fetchCertificate(ctx); } }
source share