Skip to content

Commit

Permalink
added equals/hashCode for OCSP CertID,changed algID check in revocati…
Browse files Browse the repository at this point in the history
…on checker - relates to github #1789
  • Loading branch information
dghgit committed Aug 31, 2024
1 parent 4e20236 commit 1f0cb48
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 3 deletions.
70 changes: 69 additions & 1 deletion core/src/main/java/org/bouncycastle/asn1/ocsp/CertID.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.bouncycastle.asn1.ocsp;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

Expand All @@ -16,7 +18,7 @@ public class CertID
AlgorithmIdentifier hashAlgorithm;
ASN1OctetString issuerNameHash;
ASN1OctetString issuerKeyHash;
ASN1Integer serialNumber;
ASN1Integer serialNumber;

public CertID(
AlgorithmIdentifier hashAlgorithm,
Expand Down Expand Up @@ -81,6 +83,72 @@ public ASN1Integer getSerialNumber()
return serialNumber;
}

public boolean equals(Object o)
{
if (this == o)
{
return true;
}

if (o instanceof ASN1Encodable)
{
try
{
CertID other = CertID.getInstance(o);

if (!this.hashAlgorithm.getAlgorithm().equals(other.hashAlgorithm.getAlgorithm()))
{
return false;
}
if (!isEqual(this.hashAlgorithm.getParameters(), other.hashAlgorithm.getParameters()))
{
return false;
}

return issuerNameHash.equals(other.issuerNameHash)
&& issuerKeyHash.equals(other.issuerKeyHash)
&& serialNumber.equals(other.serialNumber);
}
catch (Exception e)
{
return false;
}
}

return false;
}

public int hashCode()
{
ASN1Encodable params = hashAlgorithm.getParameters();
int hashCode = (params == null || DERNull.INSTANCE.equals(params)) ? 0 : params.hashCode();

return hashCode + 7 * (hashAlgorithm.getAlgorithm().hashCode()
+ 7 * (issuerNameHash.hashCode() + 7 * (issuerKeyHash.hashCode() + 7 * serialNumber.hashCode())));
}

private boolean isEqual(ASN1Encodable a, ASN1Encodable b)
{
if (a == b)
{
return true;
}

if (a == null)
{
return DERNull.INSTANCE.equals(b);
}
else
{
if (DERNull.INSTANCE.equals(a) && b == null)
{
return true;
}

return a.equals(b);
}
}

/**
* Produce an object suitable for an ASN1OutputStream.
* <pre>
Expand Down
54 changes: 54 additions & 0 deletions core/src/test/java/org/bouncycastle/asn1/test/CertIDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.bouncycastle.asn1.test;

import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.ocsp.CertID;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.internal.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.test.SimpleTest;

public class CertIDTest
extends SimpleTest
{
public String getName()
{
return "CertID";
}

public void performTest()
throws Exception
{
DEROctetString issuerAHash = new DEROctetString(Strings.toByteArray("IssuerAHash"));
DEROctetString issuerBHash = new DEROctetString(Strings.toByteArray("IssuerBHash"));
DEROctetString issuerAKeyHash = new DEROctetString(Strings.toByteArray("IssuerAKeyHash"));
DEROctetString issuerBKeyHash = new DEROctetString(Strings.toByteArray("IssuerBKeyHash"));

CertID a = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1), issuerAHash, issuerAKeyHash, new ASN1Integer(BigIntegers.ONE));
CertID b = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE), issuerAHash, issuerAKeyHash, new ASN1Integer(BigIntegers.ONE));
CertID c = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, new DEROctetString(new byte[1])), issuerAHash, issuerAKeyHash, new ASN1Integer(BigIntegers.ONE));

isTrue(a.equals(a));
isTrue(a.equals(b));
isTrue(a.hashCode() == b.hashCode());
isTrue(!a.equals(c));
isTrue(a.hashCode() != c.hashCode());

b = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1), issuerAHash, issuerAKeyHash, new ASN1Integer(BigIntegers.TWO));
isTrue(!a.equals(b));
b = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.elGamalAlgorithm), issuerAHash, issuerAKeyHash, new ASN1Integer(BigIntegers.ONE));
isTrue(!a.equals(b));
b = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1), issuerBHash, issuerAKeyHash, new ASN1Integer(BigIntegers.ONE));
isTrue(!a.equals(b));
b = new CertID(new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1), issuerAHash, issuerBKeyHash, new ASN1Integer(BigIntegers.ONE));
isTrue(!a.equals(b));
}

public static void main(
String[] args)
{
runTest(new CertIDTest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class RegressionTest
new DLExternalTest(),
new KMACParamsTest(),
new DERPrivateTest(),
new X509AltTest()
new X509AltTest(),
new CertIDTest()
};

public static void main(String[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void check(Certificate certificate)
{
throw new ExtCertPathValidatorException("OCSP response expired");
}
if (certID == null || !certID.getHashAlgorithm().equals(resp.getCertID().getHashAlgorithm()))
if (certID == null || !isEqualAlgId(certID.getHashAlgorithm(), resp.getCertID().getHashAlgorithm()))
{
org.bouncycastle.asn1.x509.Certificate issuer = extractCert();

Expand Down Expand Up @@ -340,6 +340,41 @@ public void check(Certificate certificate)
}
}

private static boolean isEqualAlgId(AlgorithmIdentifier a, AlgorithmIdentifier b)
{
if (a == b || a.equals(b))
{
return true;
}

if (a.getAlgorithm().equals(b.getAlgorithm()))
{
ASN1Encodable aParam = a.getParameters();
ASN1Encodable bParam = b.getParameters();

if (aParam == bParam)
{
return true;
}

if (aParam == null)
{
return DERNull.INSTANCE.equals(bParam);
}
else
{
if (DERNull.INSTANCE.equals(aParam) && bParam == null)
{
return true;
}

return aParam.equals(bParam);
}
}

return false;
}

static URI getOcspResponderURI(X509Certificate cert)
{
byte[] extValue = cert.getExtensionValue(org.bouncycastle.asn1.x509.Extension.authorityInfoAccess.getId());
Expand Down

0 comments on commit 1f0cb48

Please sign in to comment.