summary refs log tree commit diff
path: root/crypto/src/asn1/cmp/PKIBody.cs
blob: 2a92053b572d0f2380d4dcdaa3de48428d5329be (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;

using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Asn1.Pkcs;

namespace Org.BouncyCastle.Asn1.Cmp
{
    /**
     * PKIBody ::= CHOICE {       -- message-specific body elements
     *          ir       [0]  CertReqMessages,        --Initialization Request
     *          ip       [1]  CertRepMessage,         --Initialization Response
     *          cr       [2]  CertReqMessages,        --Certification Request
     *          cp       [3]  CertRepMessage,         --Certification Response
     *          p10cr    [4]  CertificationRequest,   --imported from [PKCS10]
     *          popdecc  [5]  POPODecKeyChallContent, --pop Challenge
     *          popdecr  [6]  POPODecKeyRespContent,  --pop Response
     *          kur      [7]  CertReqMessages,        --Key Update Request
     *          kup      [8]  CertRepMessage,         --Key Update Response
     *          krr      [9]  CertReqMessages,        --Key Recovery Request
     *          krp      [10] KeyRecRepContent,       --Key Recovery Response
     *          rr       [11] RevReqContent,          --Revocation Request
     *          rp       [12] RevRepContent,          --Revocation Response
     *          ccr      [13] CertReqMessages,        --Cross-Cert. Request
     *          ccp      [14] CertRepMessage,         --Cross-Cert. Response
     *          ckuann   [15] CAKeyUpdAnnContent,     --CA Key Update Ann.
     *          cann     [16] CertAnnContent,         --Certificate Ann.
     *          rann     [17] RevAnnContent,          --Revocation Ann.
     *          crlann   [18] CRLAnnContent,          --CRL Announcement
     *          pkiconf  [19] PKIConfirmContent,      --Confirmation
     *          nested   [20] NestedMessageContent,   --Nested Message
     *          genm     [21] GenMsgContent,          --General Message
     *          genp     [22] GenRepContent,          --General Response
     *          error    [23] ErrorMsgContent,        --Error Message
     *          certConf [24] CertConfirmContent,     --Certificate confirm
     *          pollReq  [25] PollReqContent,         --Polling request
     *          pollRep  [26] PollRepContent          --Polling response
     *      }
     */
    public class PkiBody
        : Asn1Encodable, IAsn1Choice
    {
        public const int TYPE_INIT_REQ = 0;
        public const int TYPE_INIT_REP = 1;
        public const int TYPE_CERT_REQ = 2;
        public const int TYPE_CERT_REP = 3;
        public const int TYPE_P10_CERT_REQ = 4;
        public const int TYPE_POPO_CHALL = 5;
        public const int TYPE_POPO_REP = 6;
        public const int TYPE_KEY_UPDATE_REQ = 7;
        public const int TYPE_KEY_UPDATE_REP = 8;
        public const int TYPE_KEY_RECOVERY_REQ = 9;
        public const int TYPE_KEY_RECOVERY_REP = 10;
        public const int TYPE_REVOCATION_REQ = 11;
        public const int TYPE_REVOCATION_REP = 12;
        public const int TYPE_CROSS_CERT_REQ = 13;
        public const int TYPE_CROSS_CERT_REP = 14;
        public const int TYPE_CA_KEY_UPDATE_ANN = 15;
        public const int TYPE_CERT_ANN = 16;
        public const int TYPE_REVOCATION_ANN = 17;
        public const int TYPE_CRL_ANN = 18;
        public const int TYPE_CONFIRM = 19;
        public const int TYPE_NESTED = 20;
        public const int TYPE_GEN_MSG = 21;
        public const int TYPE_GEN_REP = 22;
        public const int TYPE_ERROR = 23;
        public const int TYPE_CERT_CONFIRM = 24;
        public const int TYPE_POLL_REQ = 25;
        public const int TYPE_POLL_REP = 26;

        public static PkiBody GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is PkiBody crlSource)
                return crlSource;
            return new PkiBody(Asn1TaggedObject.GetInstance(obj));
        }

        public static PkiBody GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return Asn1Utilities.GetInstanceFromChoice(taggedObject, declaredExplicit, GetInstance);
        }

        private readonly int m_tagNo;
        private readonly Asn1Encodable m_body;

        private PkiBody(Asn1TaggedObject taggedObject)
        {
            m_tagNo = taggedObject.TagNo;
            m_body = GetBodyForType(m_tagNo, taggedObject.GetObject());
        }

        /**
         * Creates a new PkiBody.
         * @param type one of the TYPE_* constants
         * @param content message content
         */
        public PkiBody(int type, Asn1Encodable content)
        {
            m_tagNo = type;
            m_body = GetBodyForType(type, content);
        }

        private static Asn1Encodable GetBodyForType(int type, Asn1Encodable o)
        {
            switch (type)
            {
            case TYPE_INIT_REQ:
                return CertReqMessages.GetInstance(o);
            case TYPE_INIT_REP:
                return CertRepMessage.GetInstance(o);
            case TYPE_CERT_REQ:
                return CertReqMessages.GetInstance(o);
            case TYPE_CERT_REP:
                return CertRepMessage.GetInstance(o);
            case TYPE_P10_CERT_REQ:
                return CertificationRequest.GetInstance(o);
            case TYPE_POPO_CHALL:
                return PopoDecKeyChallContent.GetInstance(o);
            case TYPE_POPO_REP:
                return PopoDecKeyRespContent.GetInstance(o);
            case TYPE_KEY_UPDATE_REQ:
                return CertReqMessages.GetInstance(o);
            case TYPE_KEY_UPDATE_REP:
                return CertRepMessage.GetInstance(o);
            case TYPE_KEY_RECOVERY_REQ:
                return CertReqMessages.GetInstance(o);
            case TYPE_KEY_RECOVERY_REP:
                return KeyRecRepContent.GetInstance(o);
            case TYPE_REVOCATION_REQ:
                return RevReqContent.GetInstance(o);
            case TYPE_REVOCATION_REP:
                return RevRepContent.GetInstance(o);
            case TYPE_CROSS_CERT_REQ:
                return CertReqMessages.GetInstance(o);
            case TYPE_CROSS_CERT_REP:
                return CertRepMessage.GetInstance(o);
            case TYPE_CA_KEY_UPDATE_ANN:
                return CAKeyUpdAnnContent.GetInstance(o);
            case TYPE_CERT_ANN:
                return CmpCertificate.GetInstance(o);
            case TYPE_REVOCATION_ANN:
                return RevAnnContent.GetInstance(o);
            case TYPE_CRL_ANN:
                return CrlAnnContent.GetInstance(o);
            case TYPE_CONFIRM:
                return PkiConfirmContent.GetInstance(o);
            case TYPE_NESTED:
                return PkiMessages.GetInstance(o);
            case TYPE_GEN_MSG:
                return GenMsgContent.GetInstance(o);
            case TYPE_GEN_REP:
                return GenRepContent.GetInstance(o);
            case TYPE_ERROR:
                return ErrorMsgContent.GetInstance(o);
            case TYPE_CERT_CONFIRM:
                return CertConfirmContent.GetInstance(o);
            case TYPE_POLL_REQ:
                return PollReqContent.GetInstance(o);
            case TYPE_POLL_REP:
                return PollRepContent.GetInstance(o);
            default:
	            throw new ArgumentException("unknown tag number: " + type, nameof(type));
            }
        }

        public virtual Asn1Encodable Content => m_body;

        public virtual int Type => m_tagNo;

        /**
         * <pre>
         * PkiBody ::= CHOICE {       -- message-specific body elements
         *        ir       [0]  CertReqMessages,        --Initialization Request
         *        ip       [1]  CertRepMessage,         --Initialization Response
         *        cr       [2]  CertReqMessages,        --Certification Request
         *        cp       [3]  CertRepMessage,         --Certification Response
         *        p10cr    [4]  CertificationRequest,   --imported from [PKCS10]
         *        popdecc  [5]  POPODecKeyChallContent, --pop Challenge
         *        popdecr  [6]  POPODecKeyRespContent,  --pop Response
         *        kur      [7]  CertReqMessages,        --Key Update Request
         *        kup      [8]  CertRepMessage,         --Key Update Response
         *        krr      [9]  CertReqMessages,        --Key Recovery Request
         *        krp      [10] KeyRecRepContent,       --Key Recovery Response
         *        rr       [11] RevReqContent,          --Revocation Request
         *        rp       [12] RevRepContent,          --Revocation Response
         *        ccr      [13] CertReqMessages,        --Cross-Cert. Request
         *        ccp      [14] CertRepMessage,         --Cross-Cert. Response
         *        ckuann   [15] CAKeyUpdAnnContent,     --CA Key Update Ann.
         *        cann     [16] CertAnnContent,         --Certificate Ann.
         *        rann     [17] RevAnnContent,          --Revocation Ann.
         *        crlann   [18] CRLAnnContent,          --CRL Announcement
         *        pkiconf  [19] PKIConfirmContent,      --Confirmation
         *        nested   [20] NestedMessageContent,   --Nested Message
         *        genm     [21] GenMsgContent,          --General Message
         *        genp     [22] GenRepContent,          --General Response
         *        error    [23] ErrorMsgContent,        --Error Message
         *        certConf [24] CertConfirmContent,     --Certificate confirm
         *        pollReq  [25] PollReqContent,         --Polling request
         *        pollRep  [26] PollRepContent          --Polling response
         * }
         * </pre>
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            return new DerTaggedObject(true, m_tagNo, m_body);
        }
    }
}