summary refs log tree commit diff
path: root/crypto/src/asn1/cmp/PKIHeader.cs
blob: 71dc18e7d5aa81fc90f22b3a0ab96da7ff5cfe3b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Cmp
{
    public class PkiHeader
        : Asn1Encodable
    {
        /**
         * Value for a "null" recipient or sender.
         */
        public static readonly GeneralName NULL_NAME = new GeneralName(X509Name.GetInstance(DerSequence.Empty));

        public static readonly int CMP_1999 = 1;
        public static readonly int CMP_2000 = 2;

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

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

        private readonly DerInteger m_pvno;
        private readonly GeneralName m_sender;
        private readonly GeneralName m_recipient;
        private readonly Asn1GeneralizedTime m_messageTime;
        private readonly AlgorithmIdentifier m_protectionAlg;
        private readonly Asn1OctetString m_senderKID;       // KeyIdentifier
        private readonly Asn1OctetString m_recipKID;        // KeyIdentifier
        private readonly Asn1OctetString m_transactionID;
        private readonly Asn1OctetString m_senderNonce;
        private readonly Asn1OctetString m_recipNonce;
        private readonly PkiFreeText m_freeText;
        private readonly Asn1Sequence m_generalInfo;

        private PkiHeader(Asn1Sequence seq)
        {
            int count = seq.Count, pos = 0;
            if (count < 3 || count > 12)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            m_pvno = DerInteger.GetInstance(seq[pos++]);
            m_sender = GeneralName.GetInstance(seq[pos++]);
            m_recipient = GeneralName.GetInstance(seq[pos++]);
            m_messageTime = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1GeneralizedTime.GetInstance);
            m_protectionAlg = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, AlgorithmIdentifier.GetInstance);
            m_senderKID = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, Asn1OctetString.GetInstance);
            m_recipKID = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 3, true, Asn1OctetString.GetInstance);
            m_transactionID = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 4, true, Asn1OctetString.GetInstance);
            m_senderNonce = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 5, true, Asn1OctetString.GetInstance);
            m_recipNonce = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 6, true, Asn1OctetString.GetInstance);
            m_freeText = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 7, true, PkiFreeText.GetInstance);
            m_generalInfo = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 8, true, Asn1Sequence.GetInstance);

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

        public PkiHeader(int pvno, GeneralName sender, GeneralName recipient)
            : this(new DerInteger(pvno), sender, recipient)
        {
        }

        private PkiHeader(DerInteger pvno, GeneralName sender, GeneralName recipient)
        {
            m_pvno = pvno ?? throw new ArgumentNullException(nameof(pvno));
            m_sender = sender ?? throw new ArgumentNullException(nameof(sender));
            m_recipient = recipient ?? throw new ArgumentNullException(nameof(recipient));
        }

        public virtual DerInteger Pvno => m_pvno;

        public virtual GeneralName Sender => m_sender;

        public virtual GeneralName Recipient => m_recipient;

        public virtual Asn1GeneralizedTime MessageTime => m_messageTime;

        public virtual AlgorithmIdentifier ProtectionAlg => m_protectionAlg;

        public virtual Asn1OctetString SenderKID => m_senderKID;

        public virtual Asn1OctetString RecipKID => m_recipKID;

        public virtual Asn1OctetString TransactionID => m_transactionID;

        public virtual Asn1OctetString SenderNonce => m_senderNonce;

        public virtual Asn1OctetString RecipNonce => m_recipNonce;

        public virtual PkiFreeText FreeText => m_freeText;

        public virtual InfoTypeAndValue[] GetGeneralInfo() => m_generalInfo?.MapElements(InfoTypeAndValue.GetInstance);

        /**
         * <pre>
         *  PkiHeader ::= SEQUENCE {
         *            pvno                INTEGER     { cmp1999(1), cmp2000(2) },
         *            sender              GeneralName,
         *            -- identifies the sender
         *            recipient           GeneralName,
         *            -- identifies the intended recipient
         *            messageTime     [0] GeneralizedTime         OPTIONAL,
         *            -- time of production of this message (used when sender
         *            -- believes that the transport will be "suitable"; i.e.,
         *            -- that the time will still be meaningful upon receipt)
         *            protectionAlg   [1] AlgorithmIdentifier     OPTIONAL,
         *            -- algorithm used for calculation of protection bits
         *            senderKID       [2] KeyIdentifier           OPTIONAL,
         *            recipKID        [3] KeyIdentifier           OPTIONAL,
         *            -- to identify specific keys used for protection
         *            transactionID   [4] OCTET STRING            OPTIONAL,
         *            -- identifies the transaction; i.e., this will be the same in
         *            -- corresponding request, response, certConf, and PKIConf
         *            -- messages
         *            senderNonce     [5] OCTET STRING            OPTIONAL,
         *            recipNonce      [6] OCTET STRING            OPTIONAL,
         *            -- nonces used to provide replay protection, senderNonce
         *            -- is inserted by the creator of this message; recipNonce
         *            -- is a nonce previously inserted in a related message by
         *            -- the intended recipient of this message
         *            freeText        [7] PKIFreeText             OPTIONAL,
         *            -- this may be used to indicate context-specific instructions
         *            -- (this field is intended for human consumption)
         *            generalInfo     [8] SEQUENCE SIZE (1..MAX) OF
         *                                 InfoTypeAndValue     OPTIONAL
         *            -- this may be used to convey context-specific information
         *            -- (this field not primarily intended for human consumption)
         * }
         * </pre>
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(12);
            v.Add(m_pvno, m_sender, m_recipient);
            v.AddOptionalTagged(true, 0, m_messageTime);
            v.AddOptionalTagged(true, 1, m_protectionAlg);
            v.AddOptionalTagged(true, 2, m_senderKID);
            v.AddOptionalTagged(true, 3, m_recipKID);
            v.AddOptionalTagged(true, 4, m_transactionID);
            v.AddOptionalTagged(true, 5, m_senderNonce);
            v.AddOptionalTagged(true, 6, m_recipNonce);
            v.AddOptionalTagged(true, 7, m_freeText);
            v.AddOptionalTagged(true, 8, m_generalInfo);
            return new DerSequence(v);
        }
    }
}