diff options
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/src/asn1/icao/CscaMasterList.cs | 79 | ||||
-rw-r--r-- | crypto/src/asn1/icao/DataGroupHash.cs | 57 | ||||
-rw-r--r-- | crypto/src/asn1/icao/LDSSecurityObject.cs | 171 | ||||
-rw-r--r-- | crypto/src/asn1/icao/LDSVersionInfo.cs | 76 |
4 files changed, 173 insertions, 210 deletions
diff --git a/crypto/src/asn1/icao/CscaMasterList.cs b/crypto/src/asn1/icao/CscaMasterList.cs index d03b3adb4..4c6ea827c 100644 --- a/crypto/src/asn1/icao/CscaMasterList.cs +++ b/crypto/src/asn1/icao/CscaMasterList.cs @@ -4,7 +4,7 @@ using Org.BouncyCastle.Asn1.X509; namespace Org.BouncyCastle.Asn1.Icao { - /** + /** * The CscaMasterList object. This object can be wrapped in a * CMSSignedData to be published in LDAP. * @@ -16,68 +16,49 @@ namespace Org.BouncyCastle.Asn1.Icao * CscaMasterListVersion :: INTEGER {v0(0)} * </pre> */ - public class CscaMasterList + public class CscaMasterList : Asn1Encodable { - private DerInteger version = new DerInteger(0); - private X509CertificateStructure[] certList; + public static CscaMasterList GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is CscaMasterList cscaMasterList) + return cscaMasterList; + return new CscaMasterList(Asn1Sequence.GetInstance(obj)); + } - public static CscaMasterList GetInstance( - object obj) - { - if (obj is CscaMasterList) - return (CscaMasterList)obj; - - if (obj != null) - return new CscaMasterList(Asn1Sequence.GetInstance(obj)); + public static CscaMasterList GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new CscaMasterList(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } - return null; - } + private readonly DerInteger m_version; + private readonly X509CertificateStructure[] m_certList; - private CscaMasterList( - Asn1Sequence seq) + private CscaMasterList(Asn1Sequence seq) { - if (seq == null || seq.Count == 0) - throw new ArgumentException("null or empty sequence passed."); - - if (seq.Count != 2) - throw new ArgumentException("Incorrect sequence size: " + seq.Count); + int count = seq.Count; + if (count != 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - this.version = DerInteger.GetInstance(seq[0]); - - Asn1Set certSet = Asn1Set.GetInstance(seq[1]); - - this.certList = new X509CertificateStructure[certSet.Count]; - for (int i = 0; i < certList.Length; i++) - { - certList[i] = X509CertificateStructure.GetInstance(certSet[i]); - } + m_version = DerInteger.GetInstance(seq[0]); + m_certList = Asn1Set.GetInstance(seq[1]).MapElements(X509CertificateStructure.GetInstance); } - public CscaMasterList( - X509CertificateStructure[] certStructs) + public CscaMasterList(X509CertificateStructure[] certStructs) { - certList = CopyCertList(certStructs); + m_version = new DerInteger(0); + m_certList = CopyCertList(certStructs); } - public virtual int Version - { - get { return version.IntValueExact; } - } + public virtual int Version => m_version.IntValueExact; - public X509CertificateStructure[] GetCertStructs() - { - return CopyCertList(certList); - } + public X509CertificateStructure[] GetCertStructs() => CopyCertList(m_certList); - private static X509CertificateStructure[] CopyCertList(X509CertificateStructure[] orig) - { - return (X509CertificateStructure[])orig.Clone(); - } + private static X509CertificateStructure[] CopyCertList(X509CertificateStructure[] orig) => + (X509CertificateStructure[])orig.Clone(); - public override Asn1Object ToAsn1Object() - { - return new DerSequence(version, new DerSet(certList)); - } + public override Asn1Object ToAsn1Object() => new DerSequence(m_version, new DerSet(m_certList)); } } diff --git a/crypto/src/asn1/icao/DataGroupHash.cs b/crypto/src/asn1/icao/DataGroupHash.cs index c4a674152..c07fc8953 100644 --- a/crypto/src/asn1/icao/DataGroupHash.cs +++ b/crypto/src/asn1/icao/DataGroupHash.cs @@ -32,50 +32,43 @@ namespace Org.BouncyCastle.Asn1.Icao public class DataGroupHash : Asn1Encodable { - private readonly DerInteger dataGroupNumber; - private readonly Asn1OctetString dataGroupHashValue; - - public static DataGroupHash GetInstance(object obj) + public static DataGroupHash GetInstance(object obj) { - if (obj is DataGroupHash) - return (DataGroupHash)obj; + if (obj == null) + return null; + if (obj is DataGroupHash dataGroupHash) + return dataGroupHash; + return new DataGroupHash(Asn1Sequence.GetInstance(obj)); + } - if (obj != null) - return new DataGroupHash(Asn1Sequence.GetInstance(obj)); + public static DataGroupHash GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new DataGroupHash(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } - return null; - } + private readonly DerInteger m_dataGroupNumber; + private readonly Asn1OctetString m_dataGroupHashValue; - private DataGroupHash(Asn1Sequence seq) + private DataGroupHash(Asn1Sequence seq) { - if (seq.Count != 2) - throw new ArgumentException("Wrong number of elements in sequence", "seq"); + int count = seq.Count; + if (count != 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - this.dataGroupNumber = DerInteger.GetInstance(seq[0]); - this.dataGroupHashValue = Asn1OctetString.GetInstance(seq[1]); + m_dataGroupNumber = DerInteger.GetInstance(seq[0]); + m_dataGroupHashValue = Asn1OctetString.GetInstance(seq[1]); } - public DataGroupHash( - int dataGroupNumber, - Asn1OctetString dataGroupHashValue) + public DataGroupHash(int dataGroupNumber, Asn1OctetString dataGroupHashValue) { - this.dataGroupNumber = new DerInteger(dataGroupNumber); - this.dataGroupHashValue = dataGroupHashValue; + m_dataGroupNumber = new DerInteger(dataGroupNumber); + m_dataGroupHashValue = dataGroupHashValue ?? throw new ArgumentNullException(nameof(dataGroupHashValue)); } - public int DataGroupNumber - { - get { return dataGroupNumber.IntValueExact; } - } + public int DataGroupNumber => m_dataGroupNumber.IntValueExact; - public Asn1OctetString DataGroupHashValue - { - get { return dataGroupHashValue; } - } + public Asn1OctetString DataGroupHashValue => m_dataGroupHashValue; - public override Asn1Object ToAsn1Object() - { - return new DerSequence(dataGroupNumber, dataGroupHashValue); - } + public override Asn1Object ToAsn1Object() => new DerSequence(m_dataGroupNumber, m_dataGroupHashValue); } } diff --git a/crypto/src/asn1/icao/LDSSecurityObject.cs b/crypto/src/asn1/icao/LDSSecurityObject.cs index c379d76bb..9fcb4e5b9 100644 --- a/crypto/src/asn1/icao/LDSSecurityObject.cs +++ b/crypto/src/asn1/icao/LDSSecurityObject.cs @@ -5,7 +5,7 @@ using Org.BouncyCastle.Math; namespace Org.BouncyCastle.Asn1.Icao { - /** + /** * The LDSSecurityObject object (V1.8). * <pre> * LDSSecurityObject ::= SEQUENCE { @@ -20,117 +20,96 @@ namespace Org.BouncyCastle.Asn1.Icao * LDSSecurityObjectVersion :: INTEGER {V0(0)} * </pre> */ - public class LdsSecurityObject + public class LdsSecurityObject : Asn1Encodable { public const int UBDataGroups = 16; - private DerInteger version = new DerInteger(0); - private AlgorithmIdentifier digestAlgorithmIdentifier; - private DataGroupHash[] datagroupHash; - private LdsVersionInfo versionInfo; - - public static LdsSecurityObject GetInstance(object obj) + public static LdsSecurityObject GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is LdsSecurityObject ldsSecurityObject) + return ldsSecurityObject; + return new LdsSecurityObject(Asn1Sequence.GetInstance(obj)); + } + + public static LdsSecurityObject GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new LdsSecurityObject(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private readonly DerInteger m_version; + private readonly AlgorithmIdentifier m_hashAlgorithm; + private readonly DataGroupHash[] m_datagroupHashValues; + private readonly LdsVersionInfo m_ldsVersionInfo; + + private LdsSecurityObject(Asn1Sequence seq) { - if (obj is LdsSecurityObject) - return (LdsSecurityObject)obj; - - if (obj != null) - return new LdsSecurityObject(Asn1Sequence.GetInstance(obj)); - - return null; + int count = seq.Count, pos = 0; + if (count < 3 || count > 4) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_version = DerInteger.GetInstance(seq[pos++]); + m_hashAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]); + m_datagroupHashValues = ConvertDataGroupHash(Asn1Sequence.GetInstance(seq[pos++])); + m_ldsVersionInfo = Asn1Utilities.ReadOptional(seq, ref pos, LdsVersionInfo.GetOptional); + + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); + + if (m_ldsVersionInfo != null && !m_version.HasValue(1)) + throw new ArgumentException("'ldsVersionInfo' is present, but 'version' is NOT 'v1'"); + } + + public LdsSecurityObject(AlgorithmIdentifier digestAlgorithmIdentifier, DataGroupHash[] datagroupHash) + { + m_version = new DerInteger(0); + m_hashAlgorithm = digestAlgorithmIdentifier ?? throw new ArgumentNullException(nameof(digestAlgorithmIdentifier)); ; + m_datagroupHashValues = datagroupHash ?? throw new ArgumentNullException(nameof(datagroupHash)); + m_ldsVersionInfo = null; + + CheckDatagroupHashCount(m_datagroupHashValues.Length); } - private LdsSecurityObject(Asn1Sequence seq) - { - if (seq == null || seq.Count == 0) - throw new ArgumentException("null or empty sequence passed."); - - var e = seq.GetEnumerator(); - - // version - e.MoveNext(); - version = DerInteger.GetInstance(e.Current); - // digestAlgorithmIdentifier - e.MoveNext(); - digestAlgorithmIdentifier = AlgorithmIdentifier.GetInstance(e.Current); - - e.MoveNext(); - Asn1Sequence datagroupHashSeq = Asn1Sequence.GetInstance(e.Current); - - if (version.HasValue(1)) - { - e.MoveNext(); - versionInfo = LdsVersionInfo.GetInstance(e.Current); - } - - CheckDatagroupHashSeqSize(datagroupHashSeq.Count); - - datagroupHash = new DataGroupHash[datagroupHashSeq.Count]; - for (int i= 0; i< datagroupHashSeq.Count; i++) - { - datagroupHash[i] = DataGroupHash.GetInstance(datagroupHashSeq[i]); - } - } + public LdsSecurityObject(AlgorithmIdentifier digestAlgorithmIdentifier, DataGroupHash[] datagroupHash, + LdsVersionInfo versionInfo) + { + m_version = new DerInteger(1); + m_hashAlgorithm = digestAlgorithmIdentifier ?? throw new ArgumentNullException(nameof(digestAlgorithmIdentifier)); ; + m_datagroupHashValues = datagroupHash ?? throw new ArgumentNullException(nameof(datagroupHash)); + m_ldsVersionInfo = versionInfo; - public LdsSecurityObject( - AlgorithmIdentifier digestAlgorithmIdentifier, - DataGroupHash[] datagroupHash) - { - this.version = new DerInteger(0); - this.digestAlgorithmIdentifier = digestAlgorithmIdentifier; - this.datagroupHash = datagroupHash; - - CheckDatagroupHashSeqSize(datagroupHash.Length); - } - - - public LdsSecurityObject( - AlgorithmIdentifier digestAlgorithmIdentifier, - DataGroupHash[] datagroupHash, - LdsVersionInfo versionInfo) - { - this.version = new DerInteger(1); - this.digestAlgorithmIdentifier = digestAlgorithmIdentifier; - this.datagroupHash = datagroupHash; - this.versionInfo = versionInfo; + CheckDatagroupHashCount(m_datagroupHashValues.Length); + } - CheckDatagroupHashSeqSize(datagroupHash.Length); - } + public BigInteger Version => m_version.Value; - private void CheckDatagroupHashSeqSize(int size) - { - if (size < 2 || size > UBDataGroups) - throw new ArgumentException("wrong size in DataGroupHashValues : not in (2.."+ UBDataGroups +")"); - } + public AlgorithmIdentifier DigestAlgorithmIdentifier => m_hashAlgorithm; - public BigInteger Version - { - get { return version.Value; } - } + public DataGroupHash[] GetDatagroupHash() => m_datagroupHashValues; - public AlgorithmIdentifier DigestAlgorithmIdentifier - { - get { return digestAlgorithmIdentifier; } - } + public LdsVersionInfo VersionInfo => m_ldsVersionInfo; - public DataGroupHash[] GetDatagroupHash() - { - return datagroupHash; - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(4); + v.Add(m_version, m_hashAlgorithm, DerSequence.FromElements(m_datagroupHashValues)); + v.AddOptional(m_ldsVersionInfo); + return new DerSequence(v); + } - public LdsVersionInfo VersionInfo - { - get { return versionInfo; } - } + private static void CheckDatagroupHashCount(int count) + { + if (count < 2 || count > UBDataGroups) + throw new ArgumentException("wrong size in DataGroupHashValues : not in (2.." + UBDataGroups + ")"); + } - public override Asn1Object ToAsn1Object() + private static DataGroupHash[] ConvertDataGroupHash(Asn1Sequence seq) { - DerSequence hashSeq = new DerSequence(datagroupHash); + CheckDatagroupHashCount(seq.Count); - Asn1EncodableVector v = new Asn1EncodableVector(version, digestAlgorithmIdentifier, hashSeq); - v.AddOptional(versionInfo); - return new DerSequence(v); + return seq.MapElements(DataGroupHash.GetInstance); } - } + } } diff --git a/crypto/src/asn1/icao/LDSVersionInfo.cs b/crypto/src/asn1/icao/LDSVersionInfo.cs index 2cdcad2db..e0a1af5c3 100644 --- a/crypto/src/asn1/icao/LDSVersionInfo.cs +++ b/crypto/src/asn1/icao/LDSVersionInfo.cs @@ -2,48 +2,61 @@ using System; namespace Org.BouncyCastle.Asn1.Icao { - public class LdsVersionInfo + public class LdsVersionInfo : Asn1Encodable { - private DerPrintableString ldsVersion; - private DerPrintableString unicodeVersion; + public static LdsVersionInfo GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is LdsVersionInfo ldsVersionInfo) + return ldsVersionInfo; + return new LdsVersionInfo(Asn1Sequence.GetInstance(obj)); + } - public LdsVersionInfo(string ldsVersion, string unicodeVersion) - { - this.ldsVersion = new DerPrintableString(ldsVersion); - this.unicodeVersion = new DerPrintableString(unicodeVersion); - } + public static LdsVersionInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new LdsVersionInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } - private LdsVersionInfo(Asn1Sequence seq) - { - if (seq.Count != 2) - throw new ArgumentException("sequence wrong size for LDSVersionInfo", "seq"); + public static LdsVersionInfo GetOptional(Asn1Encodable element) + { + if (element == null) + throw new ArgumentNullException(nameof(element)); - this.ldsVersion = DerPrintableString.GetInstance(seq[0]); - this.unicodeVersion = DerPrintableString.GetInstance(seq[1]); - } + if (element is LdsVersionInfo ldsVersionInfo) + return ldsVersionInfo; - public static LdsVersionInfo GetInstance(object obj) - { - if (obj is LdsVersionInfo) - return (LdsVersionInfo)obj; + Asn1Sequence asn1Sequence = Asn1Sequence.GetOptional(element); + if (asn1Sequence != null) + return new LdsVersionInfo(asn1Sequence); - if (obj != null) - return new LdsVersionInfo(Asn1Sequence.GetInstance(obj)); + return null; + } - return null; - } + private DerPrintableString m_ldsVersion; + private DerPrintableString m_unicodeVersion; - public virtual string GetLdsVersion() + private LdsVersionInfo(Asn1Sequence seq) { - return ldsVersion.GetString(); - } + int count = seq.Count; + if (count != 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - public virtual string GetUnicodeVersion() - { - return unicodeVersion.GetString(); + m_ldsVersion = DerPrintableString.GetInstance(seq[0]); + m_unicodeVersion = DerPrintableString.GetInstance(seq[1]); } + public LdsVersionInfo(string ldsVersion, string unicodeVersion) + { + m_ldsVersion = new DerPrintableString(ldsVersion); + m_unicodeVersion = new DerPrintableString(unicodeVersion); + } + + public virtual string GetLdsVersion() => m_ldsVersion.GetString(); + + public virtual string GetUnicodeVersion() => m_unicodeVersion.GetString(); + /** * <pre> * LDSVersionInfo ::= SEQUENCE { @@ -53,9 +66,6 @@ namespace Org.BouncyCastle.Asn1.Icao * </pre> * @return */ - public override Asn1Object ToAsn1Object() - { - return new DerSequence(ldsVersion, unicodeVersion); - } + public override Asn1Object ToAsn1Object() => new DerSequence(m_ldsVersion, m_unicodeVersion); } } |