diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-07-01 19:23:46 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-07-01 19:23:46 +0700 |
commit | 4e97fa1cbee0072938c5ea33ca51f4e3964f2998 (patch) | |
tree | 0aad9c9bc277429ee733c8383672cb7c6aed329a /crypto/src/asn1/tsp/TSTInfo.cs | |
parent | Refactoring in Asn1.Smime (diff) | |
download | BouncyCastle.NET-ed25519-4e97fa1cbee0072938c5ea33ca51f4e3964f2998.tar.xz |
Refactoring in Asn1.Tsp
Diffstat (limited to 'crypto/src/asn1/tsp/TSTInfo.cs')
-rw-r--r-- | crypto/src/asn1/tsp/TSTInfo.cs | 110 |
1 files changed, 35 insertions, 75 deletions
diff --git a/crypto/src/asn1/tsp/TSTInfo.cs b/crypto/src/asn1/tsp/TSTInfo.cs index 80302975f..14fac55c7 100644 --- a/crypto/src/asn1/tsp/TSTInfo.cs +++ b/crypto/src/asn1/tsp/TSTInfo.cs @@ -4,7 +4,7 @@ using Org.BouncyCastle.Asn1.X509; namespace Org.BouncyCastle.Asn1.Tsp { - public class TstInfo + public class TstInfo : Asn1Encodable { public static TstInfo GetInstance(object obj) @@ -16,13 +16,14 @@ namespace Org.BouncyCastle.Asn1.Tsp return new TstInfo(Asn1Sequence.GetInstance(obj)); } - public static TstInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) - { - return new TstInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); - } + public static TstInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new TstInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + + public static TstInfo GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new TstInfo(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); private readonly DerInteger m_version; - private readonly DerObjectIdentifier m_tsaPolicyID; + private readonly DerObjectIdentifier m_policy; private readonly MessageImprint m_messageImprint; private readonly DerInteger m_serialNumber; private readonly Asn1GeneralizedTime m_genTime; @@ -34,78 +35,37 @@ namespace Org.BouncyCastle.Asn1.Tsp private TstInfo(Asn1Sequence seq) { - var e = seq.GetEnumerator(); - - // version - e.MoveNext(); - m_version = DerInteger.GetInstance(e.Current); - - // tsaPolicy - e.MoveNext(); - m_tsaPolicyID = DerObjectIdentifier.GetInstance(e.Current); - - // messageImprint - e.MoveNext(); - m_messageImprint = MessageImprint.GetInstance(e.Current); - - // serialNumber - e.MoveNext(); - m_serialNumber = DerInteger.GetInstance(e.Current); - - // genTime - e.MoveNext(); - m_genTime = Asn1GeneralizedTime.GetInstance(e.Current); - - // default for ordering - m_ordering = DerBoolean.False; - - while (e.MoveNext()) - { - Asn1Object o = (Asn1Object) e.Current; - - if (o is Asn1TaggedObject tagged) - { - switch (tagged.TagNo) - { - case 0: - m_tsa = GeneralName.GetInstance(tagged, true); - break; - case 1: - m_extensions = X509Extensions.GetInstance(tagged, false); - break; - default: - throw new ArgumentException("Unknown tag value " + tagged.TagNo); - } - } - - if (o is Asn1Sequence) - { - m_accuracy = Accuracy.GetInstance(o); - } - - if (o is DerBoolean) - { - m_ordering = DerBoolean.GetInstance(o); - } - - if (o is DerInteger) - { - m_nonce = DerInteger.GetInstance(o); - } - } - } + int count = seq.Count, pos = 0; + if (count < 5 || count > 10) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_version = DerInteger.GetInstance(seq[pos++]); + m_policy = DerObjectIdentifier.GetInstance(seq[pos++]); + m_messageImprint = MessageImprint.GetInstance(seq[pos++]); + m_serialNumber = DerInteger.GetInstance(seq[pos++]); + m_genTime = Asn1GeneralizedTime.GetInstance(seq[pos++]); + m_accuracy = Asn1Utilities.ReadOptional(seq, ref pos, Accuracy.GetOptional); + m_ordering = Asn1Utilities.ReadOptional(seq, ref pos, DerBoolean.GetOptional) ?? DerBoolean.False; + m_nonce = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional); + m_tsa = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, GeneralName.GetTagged); // CHOICE + m_extensions = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, X509Extensions.GetTagged); + + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); + } + // TODO[api] 'tsaPolicyId' => 'policy' public TstInfo(DerObjectIdentifier tsaPolicyId, MessageImprint messageImprint, DerInteger serialNumber, Asn1GeneralizedTime genTime, Accuracy accuracy, DerBoolean ordering, DerInteger nonce, GeneralName tsa, X509Extensions extensions) { m_version = DerInteger.One; - m_tsaPolicyID = tsaPolicyId; - m_messageImprint = messageImprint; - m_serialNumber = serialNumber; - m_genTime = genTime; + m_policy = tsaPolicyId ?? throw new ArgumentNullException(nameof(tsaPolicyId)); + m_messageImprint = messageImprint ?? throw new ArgumentNullException(nameof(messageImprint)); + m_serialNumber = serialNumber ?? throw new ArgumentNullException(nameof(serialNumber)); + m_genTime = genTime ?? throw new ArgumentNullException(nameof(genTime)); m_accuracy = accuracy; - m_ordering = ordering; + m_ordering = ordering ?? DerBoolean.False; m_nonce = nonce; m_tsa = tsa; m_extensions = extensions; @@ -115,7 +75,7 @@ namespace Org.BouncyCastle.Asn1.Tsp public MessageImprint MessageImprint => m_messageImprint; - public DerObjectIdentifier Policy => m_tsaPolicyID; + public DerObjectIdentifier Policy => m_policy; public DerInteger SerialNumber => m_serialNumber; @@ -157,16 +117,16 @@ namespace Org.BouncyCastle.Asn1.Tsp public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(10); - v.Add(m_version, m_tsaPolicyID, m_messageImprint, m_serialNumber, m_genTime); + v.Add(m_version, m_policy, m_messageImprint, m_serialNumber, m_genTime); v.AddOptional(m_accuracy); - if (m_ordering != null && m_ordering.IsTrue) + if (m_ordering.IsTrue) { v.Add(m_ordering); } v.AddOptional(m_nonce); - v.AddOptionalTagged(true, 0, m_tsa); + v.AddOptionalTagged(true, 0, m_tsa); // CHOICE v.AddOptionalTagged(false, 1, m_extensions); return new DerSequence(v); } |