summary refs log tree commit diff
path: root/crypto/src/asn1/cms/Attributes.cs
blob: 00f3b464f5be9d086c5414bd04428233a8c52a25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;

namespace Org.BouncyCastle.Asn1.Cms
{
    public class Attributes
        : Asn1Encodable
    {
        public static Attributes GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is Attributes attributes)
                return attributes;
            return new Attributes(Asn1Set.GetInstance(obj));
        }

        public static Attributes GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new Attributes(Asn1Set.GetInstance(taggedObject, declaredExplicit));

        public static Attributes GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new Attributes(Asn1Set.GetTagged(taggedObject, declaredExplicit));

        public static Attributes GetOptional(Asn1Encodable element)
        {
            if (element == null)
                throw new ArgumentNullException(nameof(element));

            if (element is Attributes attributes)
                return attributes;

            Asn1Set asn1Set = Asn1Set.GetOptional(element);
            if (asn1Set != null)
                return new Attributes(asn1Set);

            return null;
        }

        private readonly Asn1Set m_attributes;

        private Attributes(Asn1Set attributes)
        {
            m_attributes = attributes;
        }

        public Attributes(Asn1EncodableVector v)
        {
            m_attributes = BerSet.FromVector(v);
        }

        public virtual Attribute[] GetAttributes() => m_attributes.MapElements(Attribute.GetInstance);

        /**
         * <pre>
         * Attributes ::=
         *   SET SIZE(1..MAX) OF Attribute -- according to RFC 5652
         * </pre>
         * @return
         */
        public override Asn1Object ToAsn1Object() => m_attributes;
    }
}