diff options
Diffstat (limited to 'crypto/src/asn1/smime')
-rw-r--r-- | crypto/src/asn1/smime/SMIMECapabilities.cs | 65 | ||||
-rw-r--r-- | crypto/src/asn1/smime/SMIMECapability.cs | 84 |
2 files changed, 66 insertions, 83 deletions
diff --git a/crypto/src/asn1/smime/SMIMECapabilities.cs b/crypto/src/asn1/smime/SMIMECapabilities.cs index 47710d9cb..e69dc8db4 100644 --- a/crypto/src/asn1/smime/SMIMECapabilities.cs +++ b/crypto/src/asn1/smime/SMIMECapabilities.cs @@ -7,8 +7,6 @@ using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.Smime { /** @@ -36,40 +34,31 @@ namespace Org.BouncyCastle.Asn1.Smime public static readonly DerObjectIdentifier DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc; public static readonly DerObjectIdentifier RC2Cbc = PkcsObjectIdentifiers.RC2Cbc; - private Asn1Sequence capabilities; - - /** - * return an Attr object from the given object. - * - * @param o the object we want converted. - * @exception ArgumentException if the object cannot be converted. - */ - public static SmimeCapabilities GetInstance( - object obj) + public static SmimeCapabilities GetInstance(object obj) { - if (obj == null || obj is SmimeCapabilities) - { - return (SmimeCapabilities) obj; - } + if (obj == null) + return null; + if (obj is SmimeCapabilities smimeCapabilities) + return smimeCapabilities; - if (obj is Asn1Sequence) - { - return new SmimeCapabilities((Asn1Sequence) obj); - } + // TODO[api] Remove this handler + if (obj is AttributeX509 attributeX509) + return new SmimeCapabilities((Asn1Sequence)attributeX509.AttrValues[0]); - if (obj is AttributeX509) - { - return new SmimeCapabilities( - (Asn1Sequence)(((AttributeX509) obj).AttrValues[0])); - } - - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); + return new SmimeCapabilities(Asn1Sequence.GetInstance(obj)); } - public SmimeCapabilities( - Asn1Sequence seq) + public static SmimeCapabilities GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new SmimeCapabilities(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + + public static SmimeCapabilities GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new SmimeCapabilities(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); + + private readonly Asn1Sequence m_capabilities; + + public SmimeCapabilities(Asn1Sequence seq) { - capabilities = seq; + m_capabilities = seq ?? throw new ArgumentNullException(nameof(seq)); } /** @@ -84,15 +73,14 @@ namespace Org.BouncyCastle.Asn1.Smime return list; } - private void DoGetCapabilitiesForOid(DerObjectIdentifier capability, IList<SmimeCapability> list) + private void DoGetCapabilitiesForOid(DerObjectIdentifier capability, List<SmimeCapability> list) { - foreach (object o in capabilities) + foreach (var element in m_capabilities) { - SmimeCapability cap = SmimeCapability.GetInstance(o); - - if (capability == null || capability.Equals(cap.CapabilityID)) + SmimeCapability smimeCapability = SmimeCapability.GetInstance(element); + if (smimeCapability.CapabilityID.Equals(capability)) { - list.Add(cap); + list.Add(smimeCapability); } } } @@ -103,9 +91,6 @@ namespace Org.BouncyCastle.Asn1.Smime * SMIMECapabilities ::= Sequence OF SMIMECapability * </pre> */ - public override Asn1Object ToAsn1Object() - { - return capabilities; - } + public override Asn1Object ToAsn1Object() => m_capabilities; } } 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); } } } |