what-the-bank/sources/org/bouncycastle/x509/X509V3CertificateGenerator....

258 lines
11 KiB
Java

package org.bouncycastle.x509;
import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Iterator;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.Certificate;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x509.TBSCertificate;
import org.bouncycastle.asn1.x509.Time;
import org.bouncycastle.asn1.x509.V3TBSCertificateGenerator;
import org.bouncycastle.asn1.x509.X509ExtensionsGenerator;
import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertificateObject;
import org.bouncycastle.x509.extension.X509ExtensionUtil;
/* loaded from: classes6.dex */
public class X509V3CertificateGenerator {
private AlgorithmIdentifier sigAlgId;
private ASN1ObjectIdentifier sigOID;
private String signatureAlgorithm;
private final JcaJceHelper bcHelper = new BCJcaJceHelper();
private V3TBSCertificateGenerator tbsGen = new V3TBSCertificateGenerator();
private X509ExtensionsGenerator extGenerator = new X509ExtensionsGenerator();
public void setSubjectUniqueID(boolean[] zArr) {
this.tbsGen.setSubjectUniqueID(booleanToBitString(zArr));
}
public void setSubjectDN(X509Name x509Name) {
this.tbsGen.setSubject(x509Name);
}
public void setSubjectDN(X500Principal x500Principal) {
try {
this.tbsGen.setSubject(new X509Principal(x500Principal.getEncoded()));
} catch (IOException e) {
throw new IllegalArgumentException("can't process principal: ".concat(String.valueOf(e)));
}
}
public void setSignatureAlgorithm(String str) {
this.signatureAlgorithm = str;
try {
ASN1ObjectIdentifier algorithmOID = X509Util.getAlgorithmOID(str);
this.sigOID = algorithmOID;
AlgorithmIdentifier sigAlgID = X509Util.getSigAlgID(algorithmOID, str);
this.sigAlgId = sigAlgID;
this.tbsGen.setSignature(sigAlgID);
} catch (Exception unused) {
throw new IllegalArgumentException("Unknown signature type requested: ".concat(String.valueOf(str)));
}
}
public void setSerialNumber(BigInteger bigInteger) {
if (bigInteger.compareTo(BigInteger.ZERO) <= 0) {
throw new IllegalArgumentException("serial number must be a positive integer");
}
this.tbsGen.setSerialNumber(new ASN1Integer(bigInteger));
}
public void setPublicKey(PublicKey publicKey) throws IllegalArgumentException {
try {
this.tbsGen.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(new ASN1InputStream(publicKey.getEncoded()).readObject()));
} catch (Exception e) {
StringBuilder sb = new StringBuilder("unable to process key - ");
sb.append(e.toString());
throw new IllegalArgumentException(sb.toString());
}
}
public void setNotBefore(Date date) {
this.tbsGen.setStartDate(new Time(date));
}
public void setNotAfter(Date date) {
this.tbsGen.setEndDate(new Time(date));
}
public void setIssuerUniqueID(boolean[] zArr) {
this.tbsGen.setIssuerUniqueID(booleanToBitString(zArr));
}
public void setIssuerDN(X509Name x509Name) {
this.tbsGen.setIssuer(x509Name);
}
public void setIssuerDN(X500Principal x500Principal) {
try {
this.tbsGen.setIssuer(new X509Principal(x500Principal.getEncoded()));
} catch (IOException e) {
throw new IllegalArgumentException("can't process principal: ".concat(String.valueOf(e)));
}
}
public void reset() {
this.tbsGen = new V3TBSCertificateGenerator();
this.extGenerator.reset();
}
public Iterator getSignatureAlgNames() {
return X509Util.getAlgNames();
}
public X509Certificate generateX509Certificate(PrivateKey privateKey, SecureRandom secureRandom) throws SecurityException, SignatureException, InvalidKeyException {
try {
return generateX509Certificate(privateKey, BouncyCastleProvider.PROVIDER_NAME, secureRandom);
} catch (NoSuchProviderException unused) {
throw new SecurityException("BC provider not installed!");
}
}
public X509Certificate generateX509Certificate(PrivateKey privateKey, String str, SecureRandom secureRandom) throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException {
try {
return generate(privateKey, str, secureRandom);
} catch (InvalidKeyException e) {
throw e;
} catch (NoSuchProviderException e2) {
throw e2;
} catch (SignatureException e3) {
throw e3;
} catch (GeneralSecurityException e4) {
throw new SecurityException("exception: ".concat(String.valueOf(e4)));
}
}
public X509Certificate generateX509Certificate(PrivateKey privateKey, String str) throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException {
return generateX509Certificate(privateKey, str, null);
}
public X509Certificate generateX509Certificate(PrivateKey privateKey) throws SecurityException, SignatureException, InvalidKeyException {
try {
return generateX509Certificate(privateKey, BouncyCastleProvider.PROVIDER_NAME, null);
} catch (NoSuchProviderException unused) {
throw new SecurityException("BC provider not installed!");
}
}
public X509Certificate generate(PrivateKey privateKey, SecureRandom secureRandom) throws CertificateEncodingException, IllegalStateException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
TBSCertificate generateTbsCert = generateTbsCert();
try {
try {
return generateJcaObject(generateTbsCert, X509Util.calculateSignature(this.sigOID, this.signatureAlgorithm, privateKey, secureRandom, generateTbsCert));
} catch (CertificateParsingException e) {
throw new ExtCertificateEncodingException("exception producing certificate object", e);
}
} catch (IOException e2) {
throw new ExtCertificateEncodingException("exception encoding TBS cert", e2);
}
}
public X509Certificate generate(PrivateKey privateKey, String str, SecureRandom secureRandom) throws CertificateEncodingException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
TBSCertificate generateTbsCert = generateTbsCert();
try {
try {
return generateJcaObject(generateTbsCert, X509Util.calculateSignature(this.sigOID, this.signatureAlgorithm, str, privateKey, secureRandom, generateTbsCert));
} catch (CertificateParsingException e) {
throw new ExtCertificateEncodingException("exception producing certificate object", e);
}
} catch (IOException e2) {
throw new ExtCertificateEncodingException("exception encoding TBS cert", e2);
}
}
public X509Certificate generate(PrivateKey privateKey, String str) throws CertificateEncodingException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return generate(privateKey, str, null);
}
public X509Certificate generate(PrivateKey privateKey) throws CertificateEncodingException, IllegalStateException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return generate(privateKey, (SecureRandom) null);
}
public void copyAndAddExtension(ASN1ObjectIdentifier aSN1ObjectIdentifier, boolean z, X509Certificate x509Certificate) throws CertificateParsingException {
copyAndAddExtension(aSN1ObjectIdentifier.getId(), z, x509Certificate);
}
public void copyAndAddExtension(String str, boolean z, X509Certificate x509Certificate) throws CertificateParsingException {
byte[] extensionValue = x509Certificate.getExtensionValue(str);
if (extensionValue != null) {
try {
addExtension(str, z, X509ExtensionUtil.fromExtensionValue(extensionValue));
} catch (IOException e) {
throw new CertificateParsingException(e.toString());
}
} else {
StringBuilder sb = new StringBuilder("extension ");
sb.append(str);
sb.append(" not present");
throw new CertificateParsingException(sb.toString());
}
}
public void addExtension(ASN1ObjectIdentifier aSN1ObjectIdentifier, boolean z, byte[] bArr) {
this.extGenerator.addExtension(new ASN1ObjectIdentifier(aSN1ObjectIdentifier.getId()), z, bArr);
}
public void addExtension(ASN1ObjectIdentifier aSN1ObjectIdentifier, boolean z, ASN1Encodable aSN1Encodable) {
this.extGenerator.addExtension(new ASN1ObjectIdentifier(aSN1ObjectIdentifier.getId()), z, aSN1Encodable);
}
public void addExtension(String str, boolean z, byte[] bArr) {
addExtension(new ASN1ObjectIdentifier(str), z, bArr);
}
public void addExtension(String str, boolean z, ASN1Encodable aSN1Encodable) {
addExtension(new ASN1ObjectIdentifier(str), z, aSN1Encodable);
}
private TBSCertificate generateTbsCert() {
if (!this.extGenerator.isEmpty()) {
this.tbsGen.setExtensions(this.extGenerator.generate());
}
return this.tbsGen.generateTBSCertificate();
}
private X509Certificate generateJcaObject(TBSCertificate tBSCertificate, byte[] bArr) throws CertificateParsingException {
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
aSN1EncodableVector.add(tBSCertificate);
aSN1EncodableVector.add(this.sigAlgId);
aSN1EncodableVector.add(new DERBitString(bArr));
return new X509CertificateObject(Certificate.getInstance(new DERSequence(aSN1EncodableVector)));
}
private DERBitString booleanToBitString(boolean[] zArr) {
byte[] bArr = new byte[(zArr.length + 7) / 8];
for (int i = 0; i != zArr.length; i++) {
int i2 = i / 8;
bArr[i2] = (byte) (bArr[i2] | (zArr[i] ? 1 << (7 - (i % 8)) : 0));
}
int length = zArr.length % 8;
return length == 0 ? new DERBitString(bArr) : new DERBitString(bArr, 8 - length);
}
}