Skip to content

Commit 8e43e04

Browse files
committed
Align sig alg checks in X509Certificate, X509Crl
1 parent 21169fe commit 8e43e04

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

crypto/src/x509/X509Certificate.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ protected virtual bool CheckSignatureValid(IVerifierFactory verifier)
717717
{
718718
var tbsCertificate = c.TbsCertificate;
719719

720-
if (!IsAlgIDEqual(c.SignatureAlgorithm, tbsCertificate.Signature))
720+
if (!X509SignatureUtilities.AreEquivalentAlgorithms(c.SignatureAlgorithm, tbsCertificate.Signature))
721721
throw new CertificateException("signature algorithm in TBS cert not same as outer cert");
722722

723723
return X509Utilities.VerifySignature(verifier, tbsCertificate, c.Signature);
@@ -748,22 +748,5 @@ private static AsymmetricKeyParameter CreatePublicKey(X509CertificateStructure c
748748
{
749749
return PublicKeyFactory.CreateKey(c.SubjectPublicKeyInfo);
750750
}
751-
752-
private static bool IsAlgIDEqual(AlgorithmIdentifier id1, AlgorithmIdentifier id2)
753-
{
754-
if (!id1.Algorithm.Equals(id2.Algorithm))
755-
return false;
756-
757-
Asn1Encodable p1 = id1.Parameters;
758-
Asn1Encodable p2 = id2.Parameters;
759-
760-
if ((p1 == null) == (p2 == null))
761-
return Objects.Equals(p1, p2);
762-
763-
// Exactly one of p1, p2 is null at this point
764-
return p1 == null
765-
? p2.ToAsn1Object() is Asn1Null
766-
: p1.ToAsn1Object() is Asn1Null;
767-
}
768751
}
769-
}
752+
}

crypto/src/x509/X509Crl.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ protected virtual bool CheckSignatureValid(IVerifierFactory verifier)
181181
{
182182
var tbsCertList = c.TbsCertList;
183183

184-
// TODO Compare IsAlgIDEqual in X509Certificate.CheckSignature
185-
if (!c.SignatureAlgorithm.Equals(tbsCertList.Signature))
184+
if (!X509SignatureUtilities.AreEquivalentAlgorithms(c.SignatureAlgorithm, tbsCertList.Signature))
186185
throw new CrlException("Signature algorithm on CertificateList does not match TbsCertList.");
187186

188187
return X509Utilities.VerifySignature(verifier, tbsCertList, c.Signature);

crypto/src/x509/X509SignatureUtil.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,25 @@ namespace Org.BouncyCastle.X509
1212
{
1313
internal class X509SignatureUtilities
1414
{
15-
internal static string GetSignatureName(AlgorithmIdentifier sigAlgID)
15+
internal static bool AreEquivalentAlgorithms(AlgorithmIdentifier id1, AlgorithmIdentifier id2)
16+
{
17+
if (!id1.Algorithm.Equals(id2.Algorithm))
18+
return false;
19+
20+
Asn1Encodable p1 = id1.Parameters;
21+
Asn1Encodable p2 = id2.Parameters;
22+
23+
if (p1 == p2)
24+
return true;
25+
if (p1 == null)
26+
return p2.ToAsn1Object() is Asn1Null;
27+
if (p2 == null)
28+
return p1.ToAsn1Object() is Asn1Null;
29+
30+
return p1.Equals(p2);
31+
}
32+
33+
internal static string GetSignatureName(AlgorithmIdentifier sigAlgID)
1634
{
1735
DerObjectIdentifier sigAlgOid = sigAlgID.Algorithm;
1836
Asn1Encodable parameters = sigAlgID.Parameters;
@@ -87,5 +105,5 @@ private static string GetDigestAlgName(DerObjectIdentifier digestAlgOID)
87105
return digestAlgOID.GetID();
88106
}
89107
}
90-
}
108+
}
91109
}

0 commit comments

Comments
 (0)