what-the-bank/sources/org/bouncycastle/crypto/tls/AbstractTlsServer.java

232 lines
9.3 KiB
Java

package org.bouncycastle.crypto.tls;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import org.bouncycastle.util.Arrays;
/* loaded from: classes6.dex */
public abstract class AbstractTlsServer extends AbstractTlsPeer implements TlsServer {
protected TlsCipherFactory cipherFactory;
protected short[] clientECPointFormats;
protected Hashtable clientExtensions;
protected ProtocolVersion clientVersion;
protected TlsServerContext context;
protected boolean eccCipherSuitesOffered;
protected boolean encryptThenMACOffered;
protected short maxFragmentLengthOffered;
protected int[] namedCurves;
protected int[] offeredCipherSuites;
protected short[] offeredCompressionMethods;
protected int selectedCipherSuite;
protected short selectedCompressionMethod;
protected short[] serverECPointFormats;
protected Hashtable serverExtensions;
protected ProtocolVersion serverVersion;
protected Vector supportedSignatureAlgorithms;
protected boolean truncatedHMacOffered;
protected boolean allowEncryptThenMAC() {
return true;
}
protected boolean allowTruncatedHMac() {
return false;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public CertificateRequest getCertificateRequest() throws IOException {
return null;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public CertificateStatus getCertificateStatus() throws IOException {
return null;
}
protected abstract int[] getCipherSuites();
@Override // org.bouncycastle.crypto.tls.TlsServer
public Vector getServerSupplementalData() throws IOException {
return null;
}
protected boolean supportsClientECCCapabilities(int[] iArr, short[] sArr) {
if (iArr == null) {
return TlsECCUtils.hasAnySupportedNamedCurves();
}
for (int i : iArr) {
if (NamedCurve.isValid(i) && (!NamedCurve.refersToASpecificNamedCurve(i) || TlsECCUtils.isSupportedNamedCurve(i))) {
return true;
}
}
return false;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void processClientSupplementalData(Vector vector) throws IOException {
if (vector != null) {
throw new TlsFatalAlert((short) 10);
}
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void processClientExtensions(Hashtable hashtable) throws IOException {
this.clientExtensions = hashtable;
if (hashtable != null) {
this.encryptThenMACOffered = TlsExtensionsUtils.hasEncryptThenMACExtension(hashtable);
short maxFragmentLengthExtension = TlsExtensionsUtils.getMaxFragmentLengthExtension(hashtable);
this.maxFragmentLengthOffered = maxFragmentLengthExtension;
if (maxFragmentLengthExtension >= 0 && !MaxFragmentLength.isValid(maxFragmentLengthExtension)) {
throw new TlsFatalAlert((short) 47);
}
this.truncatedHMacOffered = TlsExtensionsUtils.hasTruncatedHMacExtension(hashtable);
Vector signatureAlgorithmsExtension = TlsUtils.getSignatureAlgorithmsExtension(hashtable);
this.supportedSignatureAlgorithms = signatureAlgorithmsExtension;
if (signatureAlgorithmsExtension != null && !TlsUtils.isSignatureAlgorithmsExtensionAllowed(this.clientVersion)) {
throw new TlsFatalAlert((short) 47);
}
this.namedCurves = TlsECCUtils.getSupportedEllipticCurvesExtension(hashtable);
this.clientECPointFormats = TlsECCUtils.getSupportedPointFormatsExtension(hashtable);
}
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void notifyOfferedCompressionMethods(short[] sArr) throws IOException {
this.offeredCompressionMethods = sArr;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void notifyOfferedCipherSuites(int[] iArr) throws IOException {
this.offeredCipherSuites = iArr;
this.eccCipherSuitesOffered = TlsECCUtils.containsECCCipherSuites(iArr);
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void notifyFallback(boolean z) throws IOException {
if (z && getMaximumVersion().isLaterVersionOf(this.clientVersion)) {
throw new TlsFatalAlert((short) 86);
}
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void notifyClientVersion(ProtocolVersion protocolVersion) throws IOException {
this.clientVersion = protocolVersion;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void notifyClientCertificate(Certificate certificate) throws IOException {
throw new TlsFatalAlert((short) 80);
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public void init(TlsServerContext tlsServerContext) {
this.context = tlsServerContext;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public ProtocolVersion getServerVersion() throws IOException {
if (getMinimumVersion().isEqualOrEarlierVersionOf(this.clientVersion)) {
ProtocolVersion maximumVersion = getMaximumVersion();
if (this.clientVersion.isEqualOrEarlierVersionOf(maximumVersion)) {
ProtocolVersion protocolVersion = this.clientVersion;
this.serverVersion = protocolVersion;
return protocolVersion;
}
if (this.clientVersion.isLaterVersionOf(maximumVersion)) {
this.serverVersion = maximumVersion;
return maximumVersion;
}
}
throw new TlsFatalAlert((short) 70);
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public Hashtable getServerExtensions() throws IOException {
if (this.encryptThenMACOffered && allowEncryptThenMAC() && TlsUtils.isBlockCipherSuite(this.selectedCipherSuite)) {
TlsExtensionsUtils.addEncryptThenMACExtension(checkServerExtensions());
}
short s = this.maxFragmentLengthOffered;
if (s >= 0 && MaxFragmentLength.isValid(s)) {
TlsExtensionsUtils.addMaxFragmentLengthExtension(checkServerExtensions(), this.maxFragmentLengthOffered);
}
if (this.truncatedHMacOffered && allowTruncatedHMac()) {
TlsExtensionsUtils.addTruncatedHMacExtension(checkServerExtensions());
}
if (this.clientECPointFormats != null && TlsECCUtils.isECCCipherSuite(this.selectedCipherSuite)) {
this.serverECPointFormats = new short[]{0, 1, 2};
TlsECCUtils.addSupportedPointFormatsExtension(checkServerExtensions(), this.serverECPointFormats);
}
return this.serverExtensions;
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public short getSelectedCompressionMethod() throws IOException {
short[] compressionMethods = getCompressionMethods();
for (int i = 0; i < compressionMethods.length; i++) {
if (Arrays.contains(this.offeredCompressionMethods, compressionMethods[i])) {
short s = compressionMethods[i];
this.selectedCompressionMethod = s;
return s;
}
}
throw new TlsFatalAlert((short) 40);
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public int getSelectedCipherSuite() throws IOException {
boolean supportsClientECCCapabilities = supportsClientECCCapabilities(this.namedCurves, this.clientECPointFormats);
for (int i : getCipherSuites()) {
if (Arrays.contains(this.offeredCipherSuites, i) && ((supportsClientECCCapabilities || !TlsECCUtils.isECCCipherSuite(i)) && TlsUtils.isValidCipherSuiteForVersion(i, this.serverVersion))) {
this.selectedCipherSuite = i;
return i;
}
}
throw new TlsFatalAlert((short) 40);
}
@Override // org.bouncycastle.crypto.tls.TlsServer
public NewSessionTicket getNewSessionTicket() throws IOException {
return new NewSessionTicket(0L, TlsUtils.EMPTY_BYTES);
}
protected ProtocolVersion getMinimumVersion() {
return ProtocolVersion.TLSv10;
}
protected ProtocolVersion getMaximumVersion() {
return ProtocolVersion.TLSv11;
}
protected short[] getCompressionMethods() {
return new short[]{0};
}
@Override // org.bouncycastle.crypto.tls.TlsPeer
public TlsCompression getCompression() throws IOException {
if (this.selectedCompressionMethod == 0) {
return new TlsNullCompression();
}
throw new TlsFatalAlert((short) 80);
}
@Override // org.bouncycastle.crypto.tls.TlsPeer
public TlsCipher getCipher() throws IOException {
return this.cipherFactory.createCipher(this.context, TlsUtils.getEncryptionAlgorithm(this.selectedCipherSuite), TlsUtils.getMACAlgorithm(this.selectedCipherSuite));
}
protected Hashtable checkServerExtensions() {
Hashtable ensureExtensionsInitialised = TlsExtensionsUtils.ensureExtensionsInitialised(this.serverExtensions);
this.serverExtensions = ensureExtensionsInitialised;
return ensureExtensionsInitialised;
}
public AbstractTlsServer(TlsCipherFactory tlsCipherFactory) {
this.cipherFactory = tlsCipherFactory;
}
public AbstractTlsServer() {
this(new DefaultTlsCipherFactory());
}
}