package io.netty.handler.ssl;
import io.netty.util.internal.StringUtil;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLException;
import java.nio.ByteBuffer;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.function.BiFunction;
import static io.netty.handler.ssl.SslUtils.toSSLHandshakeException;
import static io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectionListener;
import static io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector;
final class Java9SslEngine extends JdkSslEngine {
private final ProtocolSelectionListener selectionListener;
private final AlpnSelector alpnSelector;
private final class AlpnSelector implements BiFunction<SSLEngine, List<String>, String> {
private final ProtocolSelector selector;
private boolean called;
AlpnSelector(ProtocolSelector selector) {
this.selector = selector;
}
@Override
public String apply(SSLEngine sslEngine, List<String> strings) {
assert !called;
called = true;
try {
String selected = selector.select(strings);
return selected == null ? StringUtil.EMPTY_STRING : selected;
} catch (Exception cause) {
return null;
}
}
void checkUnsupported() {
if (called) {
return;
}
String protocol = getApplicationProtocol();
assert protocol != null;
if (protocol.isEmpty()) {
selector.unsupported();
}
}
}
Java9SslEngine(SSLEngine engine, JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
super(engine);
if (isServer) {
selectionListener = null;
alpnSelector = new AlpnSelector(applicationNegotiator.protocolSelectorFactory().
newSelector(this, new LinkedHashSet<String>(applicationNegotiator.protocols())));
Java9SslUtils.setHandshakeApplicationProtocolSelector(engine, alpnSelector);
} else {
selectionListener = applicationNegotiator.protocolListenerFactory()
.newListener(this, applicationNegotiator.protocols());
alpnSelector = null;
Java9SslUtils.setApplicationProtocols(engine, applicationNegotiator.protocols());
}
}
private SSLEngineResult verifyProtocolSelection(SSLEngineResult result) throws SSLException {
if (result.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.FINISHED) {
if (alpnSelector == null) {
try {
String protocol = getApplicationProtocol();
assert protocol != null;
if (protocol.isEmpty()) {
selectionListener.unsupported();
} else {
selectionListener.selected(protocol);
}
} catch (Throwable e) {
throw toSSLHandshakeException(e);
}
} else {
assert selectionListener == null;
alpnSelector.checkUnsupported();
}
}
return result;
}
@Override
public SSLEngineResult wrap(ByteBuffer src, ByteBuffer dst) throws SSLException {
return verifyProtocolSelection(super.wrap(src, dst));
}
@Override
public SSLEngineResult wrap(ByteBuffer[] srcs, ByteBuffer dst) throws SSLException {
return verifyProtocolSelection(super.wrap(srcs, dst));
}
@Override
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int len, ByteBuffer dst) throws SSLException {
return verifyProtocolSelection(super.wrap(srcs, offset, len, dst));
}
@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer dst) throws SSLException {
return verifyProtocolSelection(super.unwrap(src, dst));
}
@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts) throws SSLException {
return verifyProtocolSelection(super.unwrap(src, dsts));
}
@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dst, int offset, int len) throws SSLException {
return verifyProtocolSelection(super.unwrap(src, dst, offset, len));
}
@Override
void setNegotiatedApplicationProtocol(String applicationProtocol) {
}
@Override
public String getNegotiatedApplicationProtocol() {
String protocol = getApplicationProtocol();
if (protocol != null) {
return protocol.isEmpty() ? null : protocol;
}
return protocol;
}
public String getApplicationProtocol() {
return Java9SslUtils.getApplicationProtocol(getWrappedEngine());
}
public String getHandshakeApplicationProtocol() {
return Java9SslUtils.getHandshakeApplicationProtocol(getWrappedEngine());
}
public void setHandshakeApplicationProtocolSelector(BiFunction<SSLEngine, List<String>, String> selector) {
Java9SslUtils.setHandshakeApplicationProtocolSelector(getWrappedEngine(), selector);
}
public BiFunction<SSLEngine, List<String>, String> getHandshakeApplicationProtocolSelector() {
return Java9SslUtils.getHandshakeApplicationProtocolSelector(getWrappedEngine());
}
}