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

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Ocsp
{
    public class SingleResponse
        : Asn1Encodable
    {
        public static SingleResponse GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is SingleResponse singleResponse)
                return singleResponse;
#pragma warning disable CS0618 // Type or member is obsolete
            return new SingleResponse(Asn1Sequence.GetInstance(obj));
#pragma warning restore CS0618 // Type or member is obsolete
        }

        public static SingleResponse GetInstance(Asn1TaggedObject obj, bool explicitly)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            return new SingleResponse(Asn1Sequence.GetInstance(obj, explicitly));
#pragma warning restore CS0618 // Type or member is obsolete
        }

        private readonly CertID m_certID;
        private readonly CertStatus m_certStatus;
        private readonly Asn1GeneralizedTime m_thisUpdate;
        private readonly Asn1GeneralizedTime m_nextUpdate;
        private readonly X509Extensions m_singleExtensions;

        public SingleResponse(CertID certID, CertStatus certStatus, Asn1GeneralizedTime thisUpdate,
            Asn1GeneralizedTime nextUpdate, X509Extensions singleExtensions)
        {
            m_certID = certID ?? throw new ArgumentNullException(nameof(certID));
            m_certStatus = certStatus ?? throw new ArgumentNullException(nameof(certStatus));
            m_thisUpdate = thisUpdate ?? throw new ArgumentNullException(nameof(thisUpdate));
            m_nextUpdate = nextUpdate;
            m_singleExtensions = singleExtensions;
        }

        [Obsolete("Use 'GetInstance' instead")]
        public SingleResponse(Asn1Sequence seq)
        {
            int count = seq.Count;
            if (count < 3 || count > 5)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            int pos = 0;

            m_certID = CertID.GetInstance(seq[pos++]);
            m_certStatus = CertStatus.GetInstance(seq[pos++]);
            m_thisUpdate = Asn1GeneralizedTime.GetInstance(seq[pos++]);
            m_nextUpdate = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1GeneralizedTime.GetInstance);
            m_singleExtensions = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, X509Extensions.GetInstance);

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

        public CertID CertId => m_certID;

        public CertStatus CertStatus => m_certStatus;

        public Asn1GeneralizedTime ThisUpdate => m_thisUpdate;

        public Asn1GeneralizedTime NextUpdate => m_nextUpdate;

        public X509Extensions SingleExtensions => m_singleExtensions;

		/**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         *  SingleResponse ::= Sequence {
         *          certID                       CertID,
         *          certStatus                   CertStatus,
         *          thisUpdate                   GeneralizedTime,
         *          nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
         *          singleExtensions   [1]       EXPLICIT Extensions OPTIONAL }
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(5);
            v.Add(m_certID, m_certStatus, m_thisUpdate);
            v.AddOptionalTagged(true, 0, m_nextUpdate);
            v.AddOptionalTagged(true, 1, m_singleExtensions);
            return new DerSequence(v);
        }
    }
}