187 lines
8.2 KiB
Java
187 lines
8.2 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.ASN1EncodableVector;
|
||
|
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.V1TBSCertificateGenerator;
|
||
|
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;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class X509V1CertificateGenerator {
|
||
|
private AlgorithmIdentifier sigAlgId;
|
||
|
private ASN1ObjectIdentifier sigOID;
|
||
|
private String signatureAlgorithm;
|
||
|
private final JcaJceHelper bcHelper = new BCJcaJceHelper();
|
||
|
private V1TBSCertificateGenerator tbsGen = new V1TBSCertificateGenerator();
|
||
|
|
||
|
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");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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) {
|
||
|
try {
|
||
|
this.tbsGen.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
|
||
|
} 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 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 V1TBSCertificateGenerator();
|
||
|
}
|
||
|
|
||
|
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 generateTBSCertificate = this.tbsGen.generateTBSCertificate();
|
||
|
try {
|
||
|
return generateJcaObject(generateTBSCertificate, X509Util.calculateSignature(this.sigOID, this.signatureAlgorithm, privateKey, secureRandom, generateTBSCertificate));
|
||
|
} catch (IOException e) {
|
||
|
throw new ExtCertificateEncodingException("exception encoding TBS cert", e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public X509Certificate generate(PrivateKey privateKey, String str, SecureRandom secureRandom) throws CertificateEncodingException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
|
||
|
TBSCertificate generateTBSCertificate = this.tbsGen.generateTBSCertificate();
|
||
|
try {
|
||
|
return generateJcaObject(generateTBSCertificate, X509Util.calculateSignature(this.sigOID, this.signatureAlgorithm, str, privateKey, secureRandom, generateTBSCertificate));
|
||
|
} catch (IOException e) {
|
||
|
throw new ExtCertificateEncodingException("exception encoding TBS cert", e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
private X509Certificate generateJcaObject(TBSCertificate tBSCertificate, byte[] bArr) throws CertificateEncodingException {
|
||
|
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
|
||
|
aSN1EncodableVector.add(tBSCertificate);
|
||
|
aSN1EncodableVector.add(this.sigAlgId);
|
||
|
aSN1EncodableVector.add(new DERBitString(bArr));
|
||
|
try {
|
||
|
return new X509CertificateObject(Certificate.getInstance(new DERSequence(aSN1EncodableVector)));
|
||
|
} catch (CertificateParsingException e) {
|
||
|
throw new ExtCertificateEncodingException("exception producing certificate object", e);
|
||
|
}
|
||
|
}
|
||
|
}
|