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

186 lines
7.9 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.tls;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.util.PublicKeyFactory;
/* loaded from: classes6.dex */
public class TlsECDHKeyExchange extends AbstractTlsKeyExchange {
protected TlsAgreementCredentials agreementCredentials;
protected short[] clientECPointFormats;
protected ECPrivateKeyParameters ecAgreePrivateKey;
protected ECPublicKeyParameters ecAgreePublicKey;
protected int[] namedCurves;
protected short[] serverECPointFormats;
protected AsymmetricKeyParameter serverPublicKey;
protected TlsSigner tlsSigner;
@Override // org.bouncycastle.crypto.tls.TlsKeyExchange
public void validateCertificateRequest(CertificateRequest certificateRequest) throws IOException {
for (short s : certificateRequest.getCertificateTypes()) {
if (s != 1 && s != 2) {
switch (s) {
case 64:
case 65:
case 66:
break;
default:
throw new TlsFatalAlert((short) 47);
}
}
}
}
@Override // org.bouncycastle.crypto.tls.TlsKeyExchange
public void skipServerCredentials() throws IOException {
if (this.keyExchange != 20) {
throw new TlsFatalAlert((short) 10);
}
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public boolean requiresServerKeyExchange() {
int i = this.keyExchange;
return i == 17 || i == 19 || i == 20;
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public void processServerKeyExchange(InputStream inputStream) throws IOException {
if (!requiresServerKeyExchange()) {
throw new TlsFatalAlert((short) 10);
}
this.ecAgreePublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey(this.clientECPointFormats, TlsECCUtils.readECParameters(this.namedCurves, this.clientECPointFormats, inputStream), TlsUtils.readOpaque8(inputStream)));
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public void processServerCertificate(Certificate certificate) throws IOException {
int i;
if (this.keyExchange == 20) {
throw new TlsFatalAlert((short) 10);
}
if (certificate.isEmpty()) {
throw new TlsFatalAlert((short) 42);
}
org.bouncycastle.asn1.x509.Certificate certificateAt = certificate.getCertificateAt(0);
try {
AsymmetricKeyParameter createKey = PublicKeyFactory.createKey(certificateAt.getSubjectPublicKeyInfo());
this.serverPublicKey = createKey;
TlsSigner tlsSigner = this.tlsSigner;
if (tlsSigner == null) {
try {
this.ecAgreePublicKey = TlsECCUtils.validateECPublicKey((ECPublicKeyParameters) createKey);
i = 8;
} catch (ClassCastException e) {
throw new TlsFatalAlert((short) 46, e);
}
} else {
if (!tlsSigner.isValidPublicKey(createKey)) {
throw new TlsFatalAlert((short) 46);
}
i = 128;
}
TlsUtils.validateKeyUsage(certificateAt, i);
super.processServerCertificate(certificate);
} catch (RuntimeException e2) {
throw new TlsFatalAlert((short) 43, e2);
}
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public void processClientKeyExchange(InputStream inputStream) throws IOException {
if (this.ecAgreePublicKey != null) {
return;
}
byte[] readOpaque8 = TlsUtils.readOpaque8(inputStream);
this.ecAgreePublicKey = TlsECCUtils.validateECPublicKey(TlsECCUtils.deserializeECPublicKey(this.serverECPointFormats, this.ecAgreePrivateKey.getParameters(), readOpaque8));
}
@Override // org.bouncycastle.crypto.tls.TlsKeyExchange
public void processClientCredentials(TlsCredentials tlsCredentials) throws IOException {
if (this.keyExchange == 20) {
throw new TlsFatalAlert((short) 80);
}
if (tlsCredentials instanceof TlsAgreementCredentials) {
this.agreementCredentials = (TlsAgreementCredentials) tlsCredentials;
} else if (!(tlsCredentials instanceof TlsSignerCredentials)) {
throw new TlsFatalAlert((short) 80);
}
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public void processClientCertificate(Certificate certificate) throws IOException {
if (this.keyExchange == 20) {
throw new TlsFatalAlert((short) 10);
}
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public void init(TlsContext tlsContext) {
super.init(tlsContext);
TlsSigner tlsSigner = this.tlsSigner;
if (tlsSigner != null) {
tlsSigner.init(tlsContext);
}
}
@Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange
public byte[] generateServerKeyExchange() throws IOException {
if (!requiresServerKeyExchange()) {
return null;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
this.ecAgreePrivateKey = TlsECCUtils.generateEphemeralServerKeyExchange(this.context.getSecureRandom(), this.namedCurves, this.clientECPointFormats, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
@Override // org.bouncycastle.crypto.tls.TlsKeyExchange
public byte[] generatePremasterSecret() throws IOException {
TlsAgreementCredentials tlsAgreementCredentials = this.agreementCredentials;
if (tlsAgreementCredentials != null) {
return tlsAgreementCredentials.generateAgreement(this.ecAgreePublicKey);
}
ECPrivateKeyParameters eCPrivateKeyParameters = this.ecAgreePrivateKey;
if (eCPrivateKeyParameters != null) {
return TlsECCUtils.calculateECDHBasicAgreement(this.ecAgreePublicKey, eCPrivateKeyParameters);
}
throw new TlsFatalAlert((short) 80);
}
@Override // org.bouncycastle.crypto.tls.TlsKeyExchange
public void generateClientKeyExchange(OutputStream outputStream) throws IOException {
if (this.agreementCredentials == null) {
this.ecAgreePrivateKey = TlsECCUtils.generateEphemeralClientKeyExchange(this.context.getSecureRandom(), this.serverECPointFormats, this.ecAgreePublicKey.getParameters(), outputStream);
}
}
public TlsECDHKeyExchange(int i, Vector vector, int[] iArr, short[] sArr, short[] sArr2) {
super(i, vector);
TlsSigner tlsSigner;
switch (i) {
case 16:
case 18:
case 20:
tlsSigner = null;
break;
case 17:
tlsSigner = new TlsECDSASigner();
break;
case 19:
tlsSigner = new TlsRSASigner();
break;
default:
throw new IllegalArgumentException("unsupported key exchange algorithm");
}
this.tlsSigner = tlsSigner;
this.namedCurves = iArr;
this.clientECPointFormats = sArr;
this.serverECPointFormats = sArr2;
}
}