diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-07-01 17:31:59 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-07-01 17:31:59 +0700 |
commit | 75eebf49267de3e7404000ed7c359579cb68b9cb (patch) | |
tree | afc8eb1b766d3874e4615ac4c8d8c8d7ff96baea /crypto/src/asn1/smime/SMIMECapability.cs | |
parent | Refactoring in Asn1.Sec (diff) | |
download | BouncyCastle.NET-ed25519-75eebf49267de3e7404000ed7c359579cb68b9cb.tar.xz |
Refactoring in Asn1.Smime
Diffstat (limited to 'crypto/src/asn1/smime/SMIMECapability.cs')
-rw-r--r-- | crypto/src/asn1/smime/SMIMECapability.cs | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/crypto/src/asn1/smime/SMIMECapability.cs b/crypto/src/asn1/smime/SMIMECapability.cs index 7b9255a46..33d739de9 100644 --- a/crypto/src/asn1/smime/SMIMECapability.cs +++ b/crypto/src/asn1/smime/SMIMECapability.cs @@ -1,6 +1,5 @@ using System; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; @@ -23,60 +22,59 @@ namespace Org.BouncyCastle.Asn1.Smime public static readonly DerObjectIdentifier DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc; public static readonly DerObjectIdentifier RC2Cbc = PkcsObjectIdentifiers.RC2Cbc; - private DerObjectIdentifier capabilityID; - private Asn1Object parameters; - - public SmimeCapability( - Asn1Sequence seq) + public static SmimeCapability GetInstance(object obj) { - capabilityID = (DerObjectIdentifier) seq[0].ToAsn1Object(); - - if (seq.Count > 1) - { - parameters = seq[1].ToAsn1Object(); - } + if (obj == null) + return null; + if (obj is SmimeCapability smimeCapability) + return smimeCapability; +#pragma warning disable CS0618 // Type or member is obsolete + return new SmimeCapability(Asn1Sequence.GetInstance(obj)); +#pragma warning restore CS0618 // Type or member is obsolete } - public SmimeCapability( - DerObjectIdentifier capabilityID, - Asn1Encodable parameters) + public static SmimeCapability GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - if (capabilityID == null) - throw new ArgumentNullException("capabilityID"); - - this.capabilityID = capabilityID; +#pragma warning disable CS0618 // Type or member is obsolete + return new SmimeCapability(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); +#pragma warning restore CS0618 // Type or member is obsolete + } - if (parameters != null) - { - this.parameters = parameters.ToAsn1Object(); - } + public static SmimeCapability GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) + { +#pragma warning disable CS0618 // Type or member is obsolete + return new SmimeCapability(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); +#pragma warning restore CS0618 // Type or member is obsolete } - public static SmimeCapability GetInstance( - object obj) + private readonly DerObjectIdentifier m_capabilityID; + private readonly Asn1Encodable m_parameters; + + [Obsolete("Use 'GetInstance' instead")] + public SmimeCapability(Asn1Sequence seq) { - if (obj == null || obj is SmimeCapability) - { - return (SmimeCapability) obj; - } + int count = seq.Count; + if (count < 1 || count > 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - if (obj is Asn1Sequence) + m_capabilityID = DerObjectIdentifier.GetInstance(seq[0]); + + if (seq.Count > 1) { - return new SmimeCapability((Asn1Sequence) obj); + m_parameters = seq[1]; } + } - throw new ArgumentException("Invalid SmimeCapability"); + public SmimeCapability(DerObjectIdentifier capabilityID, Asn1Encodable parameters) + { + m_capabilityID = capabilityID ?? throw new ArgumentNullException(nameof(capabilityID)); + m_parameters = parameters; } - public DerObjectIdentifier CapabilityID - { - get { return capabilityID; } - } + public DerObjectIdentifier CapabilityID => m_capabilityID; - public Asn1Object Parameters - { - get { return parameters; } - } + // TODO[api] return Asn1Encodable + public Asn1Object Parameters => m_parameters?.ToAsn1Object(); /** * Produce an object suitable for an Asn1OutputStream. @@ -89,9 +87,9 @@ namespace Org.BouncyCastle.Asn1.Smime */ public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector(capabilityID); - v.AddOptional(parameters); - return new DerSequence(v); + return m_parameters == null + ? new DerSequence(m_capabilityID) + : new DerSequence(m_capabilityID, m_parameters); } } } |