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

using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;

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

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

        private readonly Asn1OctetString m_certHash;
		private readonly DerInteger m_certReqID;
		private readonly PkiStatusInfo m_statusInfo;
        private readonly AlgorithmIdentifier m_hashAlg;

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

            m_certHash = Asn1OctetString.GetInstance(seq[pos++]);
            m_certReqID = DerInteger.GetInstance(seq[pos++]);
            m_statusInfo = Asn1Utilities.ReadOptional(seq, ref pos, PkiStatusInfo.GetOptional);
            m_hashAlg = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, AlgorithmIdentifier.GetTagged);

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

        public CertStatus(byte[] certHash, BigInteger certReqID)
            : this(certHash, certReqID, null, null)
        {
        }

        public CertStatus(byte[] certHash, DerInteger certReqID)
		{
			m_certHash = new DerOctetString(certHash);
			m_certReqID = certReqID;
            m_statusInfo = null;
            m_hashAlg = null;
        }

        public CertStatus(byte[] certHash, BigInteger certReqID, PkiStatusInfo statusInfo)
		{
            m_certHash = new DerOctetString(certHash);
            m_certReqID = new DerInteger(certReqID);
            m_statusInfo = statusInfo;
            m_hashAlg = null;
        }

        public CertStatus(byte[] certHash, BigInteger certReqID, PkiStatusInfo statusInfo, AlgorithmIdentifier hashAlg)
        {
            m_certHash = new DerOctetString(certHash);
            m_certReqID = new DerInteger(certReqID);
            m_statusInfo = statusInfo;
            m_hashAlg = hashAlg;
        }

        public virtual Asn1OctetString CertHash => m_certHash;

		public virtual DerInteger CertReqID => m_certReqID;

		public virtual PkiStatusInfo StatusInfo => m_statusInfo;

		public virtual AlgorithmIdentifier HashAlg => m_hashAlg;

        /**
         * <pre>
         *
         *  CertStatus ::= SEQUENCE {
         *     certHash    OCTET STRING,
         *     certReqId   INTEGER,
         *     statusInfo  PKIStatusInfo OPTIONAL,
         *     hashAlg [0] AlgorithmIdentifier{DIGEST-ALGORITHM, {...}} OPTIONAL
         *   }
         *
         * </pre>
         *
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(4);
			v.Add(m_certHash, m_certReqID);
			v.AddOptional(m_statusInfo);
			v.AddOptionalTagged(true, 0, m_hashAlg);
			return new DerSequence(v);
		}
	}
}