package org.bouncycastle.crypto.tls; 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.DHParameters; import org.bouncycastle.crypto.params.DHPrivateKeyParameters; import org.bouncycastle.crypto.params.DHPublicKeyParameters; import org.bouncycastle.crypto.util.PublicKeyFactory; /* loaded from: classes6.dex */ public class TlsDHKeyExchange extends AbstractTlsKeyExchange { protected TlsAgreementCredentials agreementCredentials; protected DHPrivateKeyParameters dhAgreePrivateKey; protected DHPublicKeyParameters dhAgreePublicKey; protected DHParameters dhParameters; protected AsymmetricKeyParameter serverPublicKey; protected TlsSigner tlsSigner; protected int getMinimumPrimeBits() { return 1024; } @Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange public void processClientCertificate(Certificate certificate) throws IOException { } /* JADX INFO: Access modifiers changed from: protected */ public DHParameters validateDHParameters(DHParameters dHParameters) throws IOException { if (dHParameters.getP().bitLength() >= getMinimumPrimeBits()) { return TlsDHUtils.validateDHParameters(dHParameters); } throw new TlsFatalAlert((short) 71); } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void validateCertificateRequest(CertificateRequest certificateRequest) throws IOException { for (short s : certificateRequest.getCertificateTypes()) { if (s != 1 && s != 2 && s != 3 && s != 4 && s != 64) { throw new TlsFatalAlert((short) 47); } } } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void skipServerCredentials() throws IOException { 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 == 3 || i == 5 || i == 11; } @Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange public void processServerCertificate(Certificate certificate) throws IOException { int i; 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 { DHPublicKeyParameters validateDHPublicKey = TlsDHUtils.validateDHPublicKey((DHPublicKeyParameters) createKey); this.dhAgreePublicKey = validateDHPublicKey; this.dhParameters = validateDHParameters(validateDHPublicKey.getParameters()); 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.dhAgreePublicKey != null) { return; } this.dhAgreePublicKey = TlsDHUtils.validateDHPublicKey(new DHPublicKeyParameters(TlsDHUtils.readDHParameter(inputStream), this.dhParameters)); } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void processClientCredentials(TlsCredentials tlsCredentials) throws IOException { 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 init(TlsContext tlsContext) { super.init(tlsContext); TlsSigner tlsSigner = this.tlsSigner; if (tlsSigner != null) { tlsSigner.init(tlsContext); } } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public byte[] generatePremasterSecret() throws IOException { TlsAgreementCredentials tlsAgreementCredentials = this.agreementCredentials; if (tlsAgreementCredentials != null) { return tlsAgreementCredentials.generateAgreement(this.dhAgreePublicKey); } DHPrivateKeyParameters dHPrivateKeyParameters = this.dhAgreePrivateKey; if (dHPrivateKeyParameters != null) { return TlsDHUtils.calculateDHBasicAgreement(this.dhAgreePublicKey, dHPrivateKeyParameters); } throw new TlsFatalAlert((short) 80); } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void generateClientKeyExchange(OutputStream outputStream) throws IOException { if (this.agreementCredentials == null) { this.dhAgreePrivateKey = TlsDHUtils.generateEphemeralClientKeyExchange(this.context.getSecureRandom(), this.dhParameters, outputStream); } } public TlsDHKeyExchange(int i, Vector vector, DHParameters dHParameters) { super(i, vector); TlsSigner tlsDSSSigner; if (i == 3) { tlsDSSSigner = new TlsDSSSigner(); } else if (i == 5) { tlsDSSSigner = new TlsRSASigner(); } else { if (i != 7 && i != 9) { throw new IllegalArgumentException("unsupported key exchange algorithm"); } tlsDSSSigner = null; } this.tlsSigner = tlsDSSSigner; this.dhParameters = dHParameters; } }