diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-05-04 13:08:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-05-04 13:08:25 +0700 |
commit | 525dad93ce6d9444bab33f68fa5a43d8d9b9f94b (patch) | |
tree | 2cec4a9619a2f2edf88dc7b3536f106d96544ad7 | |
parent | When there is reference equality, Equals() should return true. (diff) | |
download | BouncyCastle.NET-ed25519-525dad93ce6d9444bab33f68fa5a43d8d9b9f94b.tar.xz |
Refactoring in CMS cert selectors
-rw-r--r-- | crypto/src/cms/OriginatorId.cs | 51 | ||||
-rw-r--r-- | crypto/src/cms/RecipientId.cs | 56 | ||||
-rw-r--r-- | crypto/src/cms/SignerId.cs | 49 | ||||
-rw-r--r-- | crypto/src/x509/store/X509CertStoreSelector.cs | 34 |
4 files changed, 65 insertions, 125 deletions
diff --git a/crypto/src/cms/OriginatorId.cs b/crypto/src/cms/OriginatorId.cs index 6ae64c503..440112736 100644 --- a/crypto/src/cms/OriginatorId.cs +++ b/crypto/src/cms/OriginatorId.cs @@ -1,49 +1,30 @@ -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Math; +using System; + using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509.Store; namespace Org.BouncyCastle.Cms { - /** - * a basic index for an originator. - */ + // TODO[api] sealed public class OriginatorID - : X509CertStoreSelector + : X509CertStoreSelector, IEquatable<OriginatorID> { - public override int GetHashCode() + public virtual bool Equals(OriginatorID other) { - int code = Arrays.GetHashCode(this.SubjectKeyIdentifier); - - BigInteger serialNumber = this.SerialNumber; - if (serialNumber != null) - { - code ^= serialNumber.GetHashCode(); - } - - X509Name issuer = this.Issuer; - if (issuer != null) - { - code ^= issuer.GetHashCode(); - } - - return code; + return other == null ? false + : other == this ? true + : MatchesSubjectKeyIdentifier(other) + && MatchesSerialNumber(other) + && MatchesIssuer(other); } - public override bool Equals( - object obj) - { - if (obj == this) - return false; - - OriginatorID id = obj as OriginatorID; - - if (id == null) - return false; + public override bool Equals(object obj) => Equals(obj as OriginatorID); - return Arrays.AreEqual(SubjectKeyIdentifier, id.SubjectKeyIdentifier) - && Objects.Equals(SerialNumber, id.SerialNumber) - && IssuersMatch(Issuer, id.Issuer); + public override int GetHashCode() + { + return GetHashCodeOfSubjectKeyIdentifier() + ^ Objects.GetHashCode(SerialNumber) + ^ Objects.GetHashCode(Issuer); } } } diff --git a/crypto/src/cms/RecipientId.cs b/crypto/src/cms/RecipientId.cs index 815f3ff90..c4107b14e 100644 --- a/crypto/src/cms/RecipientId.cs +++ b/crypto/src/cms/RecipientId.cs @@ -1,58 +1,40 @@ using System; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509.Store; namespace Org.BouncyCastle.Cms { + // TODO[api] sealed public class RecipientID - : X509CertStoreSelector + : X509CertStoreSelector, IEquatable<RecipientID> { - private byte[] keyIdentifier; + private byte[] m_keyIdentifier; public byte[] KeyIdentifier { - get { return Arrays.Clone(keyIdentifier); } - set { keyIdentifier = Arrays.Clone(value); } + get { return Arrays.Clone(m_keyIdentifier); } + set { m_keyIdentifier = Arrays.Clone(value); } } - public override int GetHashCode() + public virtual bool Equals(RecipientID other) { - int code = Arrays.GetHashCode(keyIdentifier) - ^ Arrays.GetHashCode(this.SubjectKeyIdentifier); - - BigInteger serialNumber = this.SerialNumber; - if (serialNumber != null) - { - code ^= serialNumber.GetHashCode(); - } - - X509Name issuer = this.Issuer; - if (issuer != null) - { - code ^= issuer.GetHashCode(); - } - - return code; + return other == null ? false + : other == this ? true + : Arrays.AreEqual(m_keyIdentifier, other.m_keyIdentifier) + && MatchesSubjectKeyIdentifier(other) + && MatchesSerialNumber(other) + && MatchesIssuer(other); } - public override bool Equals( - object obj) - { - if (obj == this) - return true; + public override bool Equals(object obj) => Equals(obj as RecipientID); - RecipientID id = obj as RecipientID; - - if (id == null) - return false; - - return Arrays.AreEqual(keyIdentifier, id.keyIdentifier) - && Arrays.AreEqual(SubjectKeyIdentifier, id.SubjectKeyIdentifier) - && Objects.Equals(SerialNumber, id.SerialNumber) - && IssuersMatch(Issuer, id.Issuer); + public override int GetHashCode() + { + return Arrays.GetHashCode(m_keyIdentifier) + ^ GetHashCodeOfSubjectKeyIdentifier() + ^ Objects.GetHashCode(SerialNumber) + ^ Objects.GetHashCode(Issuer); } } } diff --git a/crypto/src/cms/SignerId.cs b/crypto/src/cms/SignerId.cs index 3d38a58dc..ca84a6cf7 100644 --- a/crypto/src/cms/SignerId.cs +++ b/crypto/src/cms/SignerId.cs @@ -1,51 +1,30 @@ using System; -using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.X509.Store; namespace Org.BouncyCastle.Cms { - /** - * a basic index for a signer. - */ + // TODO[api] sealed public class SignerID - : X509CertStoreSelector + : X509CertStoreSelector, IEquatable<SignerID> { - public override int GetHashCode() + public virtual bool Equals(SignerID other) { - int code = Arrays.GetHashCode(this.SubjectKeyIdentifier); - - BigInteger serialNumber = this.SerialNumber; - if (serialNumber != null) - { - code ^= serialNumber.GetHashCode(); - } - - X509Name issuer = this.Issuer; - if (issuer != null) - { - code ^= issuer.GetHashCode(); - } - - return code; + return other == null ? false + : other == this ? true + : MatchesSubjectKeyIdentifier(other) + && MatchesSerialNumber(other) + && MatchesIssuer(other); } - public override bool Equals( - object obj) - { - if (obj == this) - return true; + public override bool Equals(object obj) => Equals(obj as SignerID); - SignerID id = obj as SignerID; - - if (id == null) - return false; - - return Arrays.AreEqual(SubjectKeyIdentifier, id.SubjectKeyIdentifier) - && Objects.Equals(SerialNumber, id.SerialNumber) - && IssuersMatch(Issuer, id.Issuer); + public override int GetHashCode() + { + return GetHashCodeOfSubjectKeyIdentifier() + ^ Objects.GetHashCode(SerialNumber) + ^ Objects.GetHashCode(Issuer); } } } diff --git a/crypto/src/x509/store/X509CertStoreSelector.cs b/crypto/src/x509/store/X509CertStoreSelector.cs index 357b5e76d..30cd44a8e 100644 --- a/crypto/src/x509/store/X509CertStoreSelector.cs +++ b/crypto/src/x509/store/X509CertStoreSelector.cs @@ -110,8 +110,8 @@ namespace Org.BouncyCastle.X509.Store public bool[] KeyUsage { - get { return CopyBoolArray(keyUsage); } - set { keyUsage = CopyBoolArray(value); } + get { return Arrays.Clone(keyUsage); } + set { keyUsage = Arrays.Clone(value); } } /// <summary> @@ -286,17 +286,19 @@ namespace Org.BouncyCastle.X509.Store return true; } - internal static bool IssuersMatch( - X509Name a, - X509Name b) - { - return a == null ? b == null : a.Equivalent(b, true); - } + protected internal int GetHashCodeOfSubjectKeyIdentifier() => Arrays.GetHashCode(subjectKeyIdentifier); + + protected internal bool MatchesIssuer(X509CertStoreSelector other) => IssuersMatch(issuer, other.issuer); - private static bool[] CopyBoolArray( - bool[] b) + protected internal bool MatchesSerialNumber(X509CertStoreSelector other) => + Objects.Equals(serialNumber, other.SerialNumber); + + protected internal bool MatchesSubjectKeyIdentifier(X509CertStoreSelector other) => + Arrays.AreEqual(subjectKeyIdentifier, other.subjectKeyIdentifier); + + private static bool IssuersMatch(X509Name a, X509Name b) { - return b == null ? null : (bool[]) b.Clone(); + return a == null ? b == null : a.Equivalent(b, true); } private static ISet<T> CopySet<T>(ISet<T> s) @@ -304,16 +306,12 @@ namespace Org.BouncyCastle.X509.Store return s == null ? null : new HashSet<T>(s); } - private static SubjectPublicKeyInfo GetSubjectPublicKey( - X509Certificate c) + private static SubjectPublicKeyInfo GetSubjectPublicKey(X509Certificate c) { - return SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(c.GetPublicKey()); + return c.CertificateStructure.SubjectPublicKeyInfo; } - private static bool MatchExtension( - byte[] b, - X509Certificate c, - DerObjectIdentifier oid) + private static bool MatchExtension(byte[] b, X509Certificate c, DerObjectIdentifier oid) { if (b == null) return true; |