what-the-bank/sources/org/bouncycastle/jce/provider/X509CRLObject.java

426 lines
18 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jce.provider;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Principal;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CRLException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509CRL;
import java.security.cert.X509CRLEntry;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.util.ASN1Dump;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.CRLDistPoint;
import org.bouncycastle.asn1.x509.CRLNumber;
import org.bouncycastle.asn1.x509.CertificateList;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.Extensions;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.IssuingDistributionPoint;
import org.bouncycastle.asn1.x509.TBSCertList;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;
/* loaded from: classes6.dex */
public class X509CRLObject extends X509CRL {
private CertificateList c;
private int hashCodeValue;
private boolean isHashCodeSet = false;
private boolean isIndirect;
private String sigAlgName;
private byte[] sigAlgParams;
@Override // java.security.cert.X509CRL
public void verify(PublicKey publicKey, Provider provider) throws CRLException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
doVerify(publicKey, provider != null ? Signature.getInstance(getSigAlgName(), provider) : Signature.getInstance(getSigAlgName()));
}
@Override // java.security.cert.X509CRL
public void verify(PublicKey publicKey, String str) throws CRLException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
doVerify(publicKey, str != null ? Signature.getInstance(getSigAlgName(), str) : Signature.getInstance(getSigAlgName()));
}
@Override // java.security.cert.X509CRL
public void verify(PublicKey publicKey) throws CRLException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
Signature signature;
try {
signature = Signature.getInstance(getSigAlgName(), BouncyCastleProvider.PROVIDER_NAME);
} catch (Exception unused) {
signature = Signature.getInstance(getSigAlgName());
}
doVerify(publicKey, signature);
}
@Override // java.security.cert.CRL
public String toString() {
StringBuffer append;
Object cRLDistPoint;
StringBuffer append2;
String str;
StringBuffer stringBuffer = new StringBuffer(" Version: ");
String lineSeparator = Strings.lineSeparator();
stringBuffer.append(getVersion()).append(lineSeparator);
stringBuffer.append(" IssuerDN: ").append(getIssuerDN()).append(lineSeparator);
stringBuffer.append(" This update: ").append(getThisUpdate()).append(lineSeparator);
stringBuffer.append(" Next update: ").append(getNextUpdate()).append(lineSeparator);
stringBuffer.append(" Signature Algorithm: ").append(getSigAlgName()).append(lineSeparator);
byte[] signature = getSignature();
stringBuffer.append(" Signature: ").append(new String(Hex.encode(signature, 0, 20))).append(lineSeparator);
for (int i = 20; i < signature.length; i += 20) {
if (i < signature.length - 20) {
append2 = stringBuffer.append(" ");
str = new String(Hex.encode(signature, i, 20));
} else {
append2 = stringBuffer.append(" ");
str = new String(Hex.encode(signature, i, signature.length - i));
}
append2.append(str).append(lineSeparator);
}
Extensions extensions = this.c.getTBSCertList().getExtensions();
if (extensions != null) {
Enumeration oids = extensions.oids();
if (oids.hasMoreElements()) {
stringBuffer.append(" Extensions: ").append(lineSeparator);
}
while (oids.hasMoreElements()) {
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier) oids.nextElement();
Extension extension = extensions.getExtension(aSN1ObjectIdentifier);
if (extension.getExtnValue() != null) {
ASN1InputStream aSN1InputStream = new ASN1InputStream(extension.getExtnValue().getOctets());
stringBuffer.append(" critical(").append(extension.isCritical()).append(") ");
try {
if (aSN1ObjectIdentifier.equals(Extension.cRLNumber)) {
cRLDistPoint = new CRLNumber(ASN1Integer.getInstance(aSN1InputStream.readObject()).getPositiveValue());
} else {
if (aSN1ObjectIdentifier.equals(Extension.deltaCRLIndicator)) {
StringBuilder sb = new StringBuilder();
sb.append("Base CRL: ");
sb.append(new CRLNumber(ASN1Integer.getInstance(aSN1InputStream.readObject()).getPositiveValue()));
append = stringBuffer.append(sb.toString());
} else if (aSN1ObjectIdentifier.equals(Extension.issuingDistributionPoint)) {
cRLDistPoint = IssuingDistributionPoint.getInstance(aSN1InputStream.readObject());
} else {
if (!aSN1ObjectIdentifier.equals(Extension.cRLDistributionPoints) && !aSN1ObjectIdentifier.equals(Extension.freshestCRL)) {
stringBuffer.append(aSN1ObjectIdentifier.getId());
append = stringBuffer.append(" value = ").append(ASN1Dump.dumpAsString(aSN1InputStream.readObject()));
}
cRLDistPoint = CRLDistPoint.getInstance(aSN1InputStream.readObject());
}
append.append(lineSeparator);
}
append = stringBuffer.append(cRLDistPoint);
append.append(lineSeparator);
} catch (Exception unused) {
stringBuffer.append(aSN1ObjectIdentifier.getId());
stringBuffer.append(" value = *****").append(lineSeparator);
}
} else {
stringBuffer.append(lineSeparator);
}
}
}
Set<? extends X509CRLEntry> revokedCertificates = getRevokedCertificates();
if (revokedCertificates != null) {
Iterator<? extends X509CRLEntry> it = revokedCertificates.iterator();
while (it.hasNext()) {
stringBuffer.append(it.next());
stringBuffer.append(lineSeparator);
}
}
return stringBuffer.toString();
}
@Override // java.security.cert.CRL
public boolean isRevoked(Certificate certificate) {
X500Name issuer;
Extension extension;
if (!certificate.getType().equals("X.509")) {
throw new RuntimeException("X.509 CRL used with non X.509 Cert");
}
Enumeration revokedCertificateEnumeration = this.c.getRevokedCertificateEnumeration();
X500Name issuer2 = this.c.getIssuer();
if (revokedCertificateEnumeration != null) {
X509Certificate x509Certificate = (X509Certificate) certificate;
BigInteger serialNumber = x509Certificate.getSerialNumber();
while (revokedCertificateEnumeration.hasMoreElements()) {
TBSCertList.CRLEntry cRLEntry = TBSCertList.CRLEntry.getInstance(revokedCertificateEnumeration.nextElement());
if (this.isIndirect && cRLEntry.hasExtensions() && (extension = cRLEntry.getExtensions().getExtension(Extension.certificateIssuer)) != null) {
issuer2 = X500Name.getInstance(GeneralNames.getInstance(extension.getParsedValue()).getNames()[0].getName());
}
if (cRLEntry.getUserCertificate().getValue().equals(serialNumber)) {
if (certificate instanceof X509Certificate) {
issuer = X500Name.getInstance(x509Certificate.getIssuerX500Principal().getEncoded());
} else {
try {
issuer = org.bouncycastle.asn1.x509.Certificate.getInstance(certificate.getEncoded()).getIssuer();
} catch (CertificateEncodingException unused) {
throw new RuntimeException("Cannot process certificate");
}
}
return issuer2.equals(issuer);
}
}
}
return false;
}
@Override // java.security.cert.X509CRL
public int hashCode() {
if (!this.isHashCodeSet) {
this.isHashCodeSet = true;
this.hashCodeValue = super.hashCode();
}
return this.hashCodeValue;
}
@Override // java.security.cert.X509Extension
public boolean hasUnsupportedCriticalExtension() {
Set criticalExtensionOIDs = getCriticalExtensionOIDs();
if (criticalExtensionOIDs == null) {
return false;
}
criticalExtensionOIDs.remove(RFC3280CertPathUtilities.ISSUING_DISTRIBUTION_POINT);
criticalExtensionOIDs.remove(RFC3280CertPathUtilities.DELTA_CRL_INDICATOR);
return !criticalExtensionOIDs.isEmpty();
}
@Override // java.security.cert.X509CRL
public int getVersion() {
return this.c.getVersionNumber();
}
@Override // java.security.cert.X509CRL
public Date getThisUpdate() {
return this.c.getThisUpdate().getDate();
}
@Override // java.security.cert.X509CRL
public byte[] getTBSCertList() throws CRLException {
try {
return this.c.getTBSCertList().getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
throw new CRLException(e.toString());
}
}
@Override // java.security.cert.X509CRL
public byte[] getSignature() {
return this.c.getSignature().getOctets();
}
@Override // java.security.cert.X509CRL
public byte[] getSigAlgParams() {
byte[] bArr = this.sigAlgParams;
if (bArr == null) {
return null;
}
int length = bArr.length;
byte[] bArr2 = new byte[length];
System.arraycopy(bArr, 0, bArr2, 0, length);
return bArr2;
}
@Override // java.security.cert.X509CRL
public String getSigAlgOID() {
return this.c.getSignatureAlgorithm().getAlgorithm().getId();
}
@Override // java.security.cert.X509CRL
public String getSigAlgName() {
return this.sigAlgName;
}
@Override // java.security.cert.X509CRL
public Set getRevokedCertificates() {
Set loadCRLEntries = loadCRLEntries();
if (loadCRLEntries.isEmpty()) {
return null;
}
return Collections.unmodifiableSet(loadCRLEntries);
}
@Override // java.security.cert.X509CRL
public X509CRLEntry getRevokedCertificate(BigInteger bigInteger) {
Extension extension;
Enumeration revokedCertificateEnumeration = this.c.getRevokedCertificateEnumeration();
X500Name x500Name = null;
while (revokedCertificateEnumeration.hasMoreElements()) {
TBSCertList.CRLEntry cRLEntry = (TBSCertList.CRLEntry) revokedCertificateEnumeration.nextElement();
if (bigInteger.equals(cRLEntry.getUserCertificate().getValue())) {
return new X509CRLEntryObject(cRLEntry, this.isIndirect, x500Name);
}
if (this.isIndirect && cRLEntry.hasExtensions() && (extension = cRLEntry.getExtensions().getExtension(Extension.certificateIssuer)) != null) {
x500Name = X500Name.getInstance(GeneralNames.getInstance(extension.getParsedValue()).getNames()[0].getName());
}
}
return null;
}
@Override // java.security.cert.X509Extension
public Set getNonCriticalExtensionOIDs() {
return getExtensionOIDs(false);
}
@Override // java.security.cert.X509CRL
public Date getNextUpdate() {
if (this.c.getNextUpdate() != null) {
return this.c.getNextUpdate().getDate();
}
return null;
}
@Override // java.security.cert.X509CRL
public X500Principal getIssuerX500Principal() {
try {
return new X500Principal(this.c.getIssuer().getEncoded());
} catch (IOException unused) {
throw new IllegalStateException("can't encode issuer DN");
}
}
@Override // java.security.cert.X509CRL
public Principal getIssuerDN() {
return new X509Principal(X500Name.getInstance(this.c.getIssuer().toASN1Primitive()));
}
@Override // java.security.cert.X509Extension
public byte[] getExtensionValue(String str) {
Extension extension;
Extensions extensions = this.c.getTBSCertList().getExtensions();
if (extensions == null || (extension = extensions.getExtension(new ASN1ObjectIdentifier(str))) == null) {
return null;
}
try {
return extension.getExtnValue().getEncoded();
} catch (Exception e) {
StringBuilder sb = new StringBuilder("error parsing ");
sb.append(e.toString());
throw new IllegalStateException(sb.toString());
}
}
@Override // java.security.cert.X509CRL
public byte[] getEncoded() throws CRLException {
try {
return this.c.getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
throw new CRLException(e.toString());
}
}
@Override // java.security.cert.X509Extension
public Set getCriticalExtensionOIDs() {
return getExtensionOIDs(true);
}
@Override // java.security.cert.X509CRL
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof X509CRL)) {
return false;
}
if (!(obj instanceof X509CRLObject)) {
return super.equals(obj);
}
X509CRLObject x509CRLObject = (X509CRLObject) obj;
if (this.isHashCodeSet && x509CRLObject.isHashCodeSet && x509CRLObject.hashCodeValue != this.hashCodeValue) {
return false;
}
return this.c.equals(x509CRLObject.c);
}
private Set loadCRLEntries() {
Extension extension;
HashSet hashSet = new HashSet();
Enumeration revokedCertificateEnumeration = this.c.getRevokedCertificateEnumeration();
X500Name x500Name = null;
while (revokedCertificateEnumeration.hasMoreElements()) {
TBSCertList.CRLEntry cRLEntry = (TBSCertList.CRLEntry) revokedCertificateEnumeration.nextElement();
hashSet.add(new X509CRLEntryObject(cRLEntry, this.isIndirect, x500Name));
if (this.isIndirect && cRLEntry.hasExtensions() && (extension = cRLEntry.getExtensions().getExtension(Extension.certificateIssuer)) != null) {
x500Name = X500Name.getInstance(GeneralNames.getInstance(extension.getParsedValue()).getNames()[0].getName());
}
}
return hashSet;
}
public static boolean isIndirectCRL(X509CRL x509crl) throws CRLException {
try {
byte[] extensionValue = x509crl.getExtensionValue(Extension.issuingDistributionPoint.getId());
if (extensionValue != null) {
if (IssuingDistributionPoint.getInstance(ASN1OctetString.getInstance(extensionValue).getOctets()).isIndirectCRL()) {
return true;
}
}
return false;
} catch (Exception e) {
throw new ExtCRLException("Exception reading IssuingDistributionPoint", e);
}
}
private Set getExtensionOIDs(boolean z) {
Extensions extensions;
if (getVersion() != 2 || (extensions = this.c.getTBSCertList().getExtensions()) == null) {
return null;
}
HashSet hashSet = new HashSet();
Enumeration oids = extensions.oids();
while (oids.hasMoreElements()) {
ASN1ObjectIdentifier aSN1ObjectIdentifier = (ASN1ObjectIdentifier) oids.nextElement();
if (z == extensions.getExtension(aSN1ObjectIdentifier).isCritical()) {
hashSet.add(aSN1ObjectIdentifier.getId());
}
}
return hashSet;
}
private void doVerify(PublicKey publicKey, Signature signature) throws CRLException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
if (!this.c.getSignatureAlgorithm().equals(this.c.getTBSCertList().getSignature())) {
throw new CRLException("Signature algorithm on CertificateList does not match TBSCertList.");
}
signature.initVerify(publicKey);
signature.update(getTBSCertList());
if (!signature.verify(getSignature())) {
throw new SignatureException("CRL does not verify with supplied public key.");
}
}
public X509CRLObject(CertificateList certificateList) throws CRLException {
this.c = certificateList;
try {
this.sigAlgName = X509SignatureUtil.getSignatureName(certificateList.getSignatureAlgorithm());
if (certificateList.getSignatureAlgorithm().getParameters() != null) {
this.sigAlgParams = certificateList.getSignatureAlgorithm().getParameters().toASN1Primitive().getEncoded(ASN1Encoding.DER);
} else {
this.sigAlgParams = null;
}
this.isIndirect = isIndirectCRL(this);
} catch (Exception e) {
throw new CRLException("CRL contents invalid: ".concat(String.valueOf(e)));
}
}
}