summary refs log tree commit diff
path: root/crypto/src/asn1/x500/Rdn.cs
blob: 069a592a521a71585adbe86e928c05977266859a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;

namespace Org.BouncyCastle.Asn1.X500
{
    /**
     * Holding class for a single Relative Distinguished Name (RDN).
     */
    public class Rdn
        : Asn1Encodable
    {
        public static Rdn GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is Rdn rdn)
                return rdn;
            return new Rdn(Asn1Set.GetInstance(obj));
        }

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

        private readonly Asn1Set m_values;

        private Rdn(Asn1Set values)
        {
            // TODO Require minimum size of 1?
            m_values = values;
        }

        /**
         * Create a single valued RDN.
         *
         * @param oid RDN type.
         * @param value RDN value.
         */
        public Rdn(DerObjectIdentifier oid, Asn1Encodable value)
            : this(new AttributeTypeAndValue(oid, value))
        {
        }

        public Rdn(AttributeTypeAndValue attrTAndV)
        {
            m_values = new DerSet(attrTAndV);
        }

        /**
         * Create a multi-valued RDN.
         *
         * @param aAndVs attribute type/value pairs making up the RDN
         */
        public Rdn(AttributeTypeAndValue[] aAndVs)
        {
            m_values = new DerSet(aAndVs);
        }

        public virtual bool IsMultiValued => m_values.Count > 1;

        /**
         * Return the number of AttributeTypeAndValue objects in this RDN,
         *
         * @return size of RDN, greater than 1 if multi-valued.
         */
        public virtual int Count => m_values.Count;

        public virtual AttributeTypeAndValue GetFirst() =>
            m_values.Count == 0 ? null : AttributeTypeAndValue.GetInstance(m_values[0]);

        public virtual AttributeTypeAndValue[] GetTypesAndValues() =>
            m_values.MapElements(AttributeTypeAndValue.GetInstance);

        /**
         * <pre>
         * RelativeDistinguishedName ::=
         *                     SET OF AttributeTypeAndValue

         * AttributeTypeAndValue ::= SEQUENCE {
         *        type     AttributeType,
         *        value    AttributeValue }
         * </pre>
         * @return this object as its ASN1Primitive type
         */
        public override Asn1Object ToAsn1Object() => m_values;
    }
}