summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/CertReqMsg.cs
blob: 03ce32d99cae9b5b4ebae3dae0c5cb1ceaab4e37 (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
using System;

namespace Org.BouncyCastle.Asn1.Crmf
{
    public class CertReqMsg
        : Asn1Encodable
    {
        private readonly CertRequest certReq;
        private readonly ProofOfPossession popo;
        private readonly Asn1Sequence regInfo;

        private CertReqMsg(Asn1Sequence seq)
        {
            certReq = CertRequest.GetInstance(seq[0]);

            for (int pos = 1; pos < seq.Count; ++pos)
            {
                object o = seq[pos];

                if (o is Asn1TaggedObject || o is ProofOfPossession)
                {
                    popo = ProofOfPossession.GetInstance(o);
                }
                else
                {
                    regInfo = Asn1Sequence.GetInstance(o);
                }
            }
        }

        public static CertReqMsg GetInstance(object obj)
        {
            if (obj is CertReqMsg)
                return (CertReqMsg)obj;

            if (obj != null)
                return new CertReqMsg(Asn1Sequence.GetInstance(obj));

            return null;
        }

        public static CertReqMsg GetInstance(
            Asn1TaggedObject obj,
            bool isExplicit)
        {
            return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
        }

        /**
         * Creates a new CertReqMsg.
         * @param certReq CertRequest
         * @param popo may be null
         * @param regInfo may be null
         */
        public CertReqMsg(
            CertRequest				certReq,
            ProofOfPossession		popo,
            AttributeTypeAndValue[]	regInfo)
        {
            if (certReq == null)
                throw new ArgumentNullException("certReq");

            this.certReq = certReq;
            this.popo = popo;

            if (regInfo != null)
            {
                this.regInfo = new DerSequence(regInfo);
            }
        }

        public virtual CertRequest CertReq
        {
            get { return certReq; }
        }

        public virtual ProofOfPossession Popo
        {
            get { return popo; }
        }

        public virtual AttributeTypeAndValue[] GetRegInfo()
        {
            if (regInfo == null)
                return null;

            AttributeTypeAndValue[] results = new AttributeTypeAndValue[regInfo.Count];
            for (int i = 0; i != results.Length; ++i)
            {
                results[i] = AttributeTypeAndValue.GetInstance(regInfo[i]);
            }
            return results;
        }

        /**
         * <pre>
         * CertReqMsg ::= SEQUENCE {
         *                    certReq   CertRequest,
         *                    pop       ProofOfPossession  OPTIONAL,
         *                    -- content depends upon key type
         *                    regInfo   SEQUENCE SIZE(1..MAX) OF AttributeTypeAndValue OPTIONAL }
         * </pre>
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(certReq);
            v.AddOptional(popo, regInfo);
            return new DerSequence(v);
        }
    }
}