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.RSAKeyParameters; import org.bouncycastle.crypto.util.PublicKeyFactory; import org.bouncycastle.util.io.Streams; /* loaded from: classes6.dex */ public class TlsRSAKeyExchange extends AbstractTlsKeyExchange { protected byte[] premasterSecret; protected RSAKeyParameters rsaServerPublicKey; protected TlsEncryptionCredentials serverCredentials; protected AsymmetricKeyParameter serverPublicKey; protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters rSAKeyParameters) throws IOException { if (rSAKeyParameters.getExponent().isProbablePrime(2)) { return rSAKeyParameters; } throw new TlsFatalAlert((short) 47); } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void validateCertificateRequest(CertificateRequest certificateRequest) throws IOException { for (short s : certificateRequest.getCertificateTypes()) { if (s != 1 && s != 2 && 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 void processServerCredentials(TlsCredentials tlsCredentials) throws IOException { if (!(tlsCredentials instanceof TlsEncryptionCredentials)) { throw new TlsFatalAlert((short) 80); } processServerCertificate(tlsCredentials.getCertificate()); this.serverCredentials = (TlsEncryptionCredentials) tlsCredentials; } @Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange public void processServerCertificate(Certificate certificate) throws IOException { 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; if (createKey.isPrivate()) { throw new TlsFatalAlert((short) 80); } this.rsaServerPublicKey = validateRSAPublicKey((RSAKeyParameters) this.serverPublicKey); TlsUtils.validateKeyUsage(certificateAt, 32); super.processServerCertificate(certificate); } catch (RuntimeException e) { throw new TlsFatalAlert((short) 43, e); } } @Override // org.bouncycastle.crypto.tls.AbstractTlsKeyExchange, org.bouncycastle.crypto.tls.TlsKeyExchange public void processClientKeyExchange(InputStream inputStream) throws IOException { this.premasterSecret = this.serverCredentials.decryptPreMasterSecret(TlsUtils.isSSL(this.context) ? Streams.readAll(inputStream) : TlsUtils.readOpaque16(inputStream)); } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void processClientCredentials(TlsCredentials tlsCredentials) throws IOException { if (!(tlsCredentials instanceof TlsSignerCredentials)) { throw new TlsFatalAlert((short) 80); } } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public byte[] generatePremasterSecret() throws IOException { byte[] bArr = this.premasterSecret; if (bArr == null) { throw new TlsFatalAlert((short) 80); } this.premasterSecret = null; return bArr; } @Override // org.bouncycastle.crypto.tls.TlsKeyExchange public void generateClientKeyExchange(OutputStream outputStream) throws IOException { this.premasterSecret = TlsRSAUtils.generateEncryptedPreMasterSecret(this.context, this.rsaServerPublicKey, outputStream); } public TlsRSAKeyExchange(Vector vector) { super(1, vector); this.serverPublicKey = null; this.rsaServerPublicKey = null; this.serverCredentials = null; } }