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

namespace Org.BouncyCastle.Asn1.Crmf
{
    public class CertRequest
        : Asn1Encodable
    {
        public static CertRequest GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is CertRequest certRequest)
                return certRequest;
            return new CertRequest(Asn1Sequence.GetInstance(obj));
        }

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

        private readonly DerInteger m_certReqId;
        private readonly CertTemplate m_certTemplate;
        private readonly Controls m_controls;

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

            int pos = 0;

            m_certReqId = DerInteger.GetInstance(seq[pos++]);
            m_certTemplate = CertTemplate.GetInstance(seq[pos++]);
            m_controls = Asn1Utilities.ReadOptional(seq, ref pos, Controls.GetOptional);

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

        public CertRequest(int certReqId, CertTemplate certTemplate, Controls controls)
            : this(new DerInteger(certReqId), certTemplate, controls)
        {
        }

        public CertRequest(DerInteger certReqId, CertTemplate certTemplate, Controls controls)
        {
            m_certReqId = certReqId ?? throw new ArgumentNullException(nameof(certReqId));
            m_certTemplate = certTemplate ?? throw new ArgumentNullException(nameof(certTemplate));
            m_controls = controls;
        }

        public virtual DerInteger CertReqID => m_certReqId;

        public virtual CertTemplate CertTemplate => m_certTemplate;

        public virtual Controls Controls => m_controls;

        /**
         * <pre>
         * CertRequest ::= SEQUENCE {
         *                      certReqId     INTEGER,          -- ID for matching request and reply
         *                      certTemplate  CertTemplate,  -- Selected fields of cert to be issued
         *                      controls      Controls OPTIONAL }   -- Attributes affecting issuance
         * </pre>
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(3);
            v.Add(m_certReqId, m_certTemplate);
            v.AddOptional(m_controls);
            return new DerSequence(v);
        }
    }
}