summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/PKIPublicationInfo.cs
blob: 5cc1df0134ff4bfc42853080bae5886e083c203d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;

using Org.BouncyCastle.Math;

namespace Org.BouncyCastle.Asn1.Crmf
{
    /**
     * <pre>
     * PKIPublicationInfo ::= SEQUENCE {
     *                  action     INTEGER {
     *                                 dontPublish (0),
     *                                 pleasePublish (1) },
     *                  pubInfos  SEQUENCE SIZE (1..MAX) OF SinglePubInfo OPTIONAL }
     * -- pubInfos MUST NOT be present if action is "dontPublish"
     * -- (if action is "pleasePublish" and pubInfos is omitted,
     * -- "dontCare" is assumed)
     * </pre>
     */
    public class PkiPublicationInfo
        : Asn1Encodable
    {
        public static readonly DerInteger DontPublish = new DerInteger(0);
        public static readonly DerInteger PleasePublish = new DerInteger(1);

        public static PkiPublicationInfo GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is PkiPublicationInfo pkiPublicationInfo)
                return pkiPublicationInfo;
            return new PkiPublicationInfo(Asn1Sequence.GetInstance(obj));
        }

        public static PkiPublicationInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new PkiPublicationInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        private readonly DerInteger m_action;
        private readonly Asn1Sequence m_pubInfos;

        private PkiPublicationInfo(Asn1Sequence seq)
        {
            int count = seq.Count;
            if (count < 1 || count > 2)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            int pos = 0;

            m_action = DerInteger.GetInstance(seq[pos++]);
            m_pubInfos = Asn1Utilities.ReadOptional(seq, ref pos, Asn1Sequence.GetOptional);

            if (pos != count)
                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
        }

        public PkiPublicationInfo(BigInteger action)
            : this(new DerInteger(action))
        {
        }

        public PkiPublicationInfo(DerInteger action)
        {
            m_action = action ?? throw new ArgumentNullException(nameof(action));
        }

        /**
         * Constructor with a single pubInfo, assumes pleasePublish as the action.
         *
         * @param pubInfo the pubInfo to be published (can be null if don't care is required).
         */
        public PkiPublicationInfo(SinglePubInfo pubInfo)
            : this(pubInfo != null ? new SinglePubInfo[1]{ pubInfo } : null)
        {
        }

        /**
         * Constructor with multiple pubInfo, assumes pleasePublish as the action.
         *
         * @param pubInfos the pubInfos to be published (can be null if don't care is required).
         */
        public PkiPublicationInfo(SinglePubInfo[] pubInfos)
        {
            m_action = PleasePublish;

            if (pubInfos != null)
            {
                m_pubInfos = new DerSequence(pubInfos);
            }
        }

        public virtual DerInteger Action => m_action;

        public virtual SinglePubInfo[] GetPubInfos() => m_pubInfos?.MapElements(SinglePubInfo.GetInstance);

        /**
         * <pre>
         * PkiPublicationInfo ::= SEQUENCE {
         *                  action     INTEGER {
         *                                 dontPublish (0),
         *                                 pleasePublish (1) },
         *                  pubInfos  SEQUENCE SIZE (1..MAX) OF SinglePubInfo OPTIONAL }
         * -- pubInfos MUST NOT be present if action is "dontPublish"
         * -- (if action is "pleasePublish" and pubInfos is omitted,
         * -- "dontCare" is assumed)
         * </pre>
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            return m_pubInfos == null
                ?  new DerSequence(m_action)
                :  new DerSequence(m_action, m_pubInfos);
        }
    }
}