62 lines
2.7 KiB
Java
62 lines
2.7 KiB
Java
|
package org.bouncycastle.crypto.tls;
|
||
|
|
||
|
import java.math.BigInteger;
|
||
|
import org.bouncycastle.crypto.BasicAgreement;
|
||
|
import org.bouncycastle.crypto.agreement.DHBasicAgreement;
|
||
|
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
|
||
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
||
|
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
|
||
|
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||
|
import org.bouncycastle.util.BigIntegers;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class DefaultTlsAgreementCredentials extends AbstractTlsAgreementCredentials {
|
||
|
protected BasicAgreement basicAgreement;
|
||
|
protected Certificate certificate;
|
||
|
protected AsymmetricKeyParameter privateKey;
|
||
|
protected boolean truncateAgreement;
|
||
|
|
||
|
@Override // org.bouncycastle.crypto.tls.TlsCredentials
|
||
|
public Certificate getCertificate() {
|
||
|
return this.certificate;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.crypto.tls.TlsAgreementCredentials
|
||
|
public byte[] generateAgreement(AsymmetricKeyParameter asymmetricKeyParameter) {
|
||
|
this.basicAgreement.init(this.privateKey);
|
||
|
BigInteger calculateAgreement = this.basicAgreement.calculateAgreement(asymmetricKeyParameter);
|
||
|
return this.truncateAgreement ? BigIntegers.asUnsignedByteArray(calculateAgreement) : BigIntegers.asUnsignedByteArray(this.basicAgreement.getFieldSize(), calculateAgreement);
|
||
|
}
|
||
|
|
||
|
public DefaultTlsAgreementCredentials(Certificate certificate, AsymmetricKeyParameter asymmetricKeyParameter) {
|
||
|
boolean z;
|
||
|
if (certificate == null) {
|
||
|
throw new IllegalArgumentException("'certificate' cannot be null");
|
||
|
}
|
||
|
if (certificate.isEmpty()) {
|
||
|
throw new IllegalArgumentException("'certificate' cannot be empty");
|
||
|
}
|
||
|
if (asymmetricKeyParameter == null) {
|
||
|
throw new IllegalArgumentException("'privateKey' cannot be null");
|
||
|
}
|
||
|
if (!asymmetricKeyParameter.isPrivate()) {
|
||
|
throw new IllegalArgumentException("'privateKey' must be private");
|
||
|
}
|
||
|
if (asymmetricKeyParameter instanceof DHPrivateKeyParameters) {
|
||
|
this.basicAgreement = new DHBasicAgreement();
|
||
|
z = true;
|
||
|
} else {
|
||
|
if (!(asymmetricKeyParameter instanceof ECPrivateKeyParameters)) {
|
||
|
StringBuilder sb = new StringBuilder("'privateKey' type not supported: ");
|
||
|
sb.append(asymmetricKeyParameter.getClass().getName());
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
this.basicAgreement = new ECDHBasicAgreement();
|
||
|
z = false;
|
||
|
}
|
||
|
this.truncateAgreement = z;
|
||
|
this.certificate = certificate;
|
||
|
this.privateKey = asymmetricKeyParameter;
|
||
|
}
|
||
|
}
|