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

using Org.BouncyCastle.Asn1.Pkcs;

namespace Org.BouncyCastle.Asn1.X509
{
    /**
     * an X509Certificate structure.
     * <pre>
     *  Certificate ::= Sequence {
     *      tbsCertificate          TbsCertificate,
     *      signatureAlgorithm      AlgorithmIdentifier,
     *      signature               BIT STRING
     *  }
     * </pre>
     */
    public class X509CertificateStructure
        : Asn1Encodable
    {
        private readonly TbsCertificateStructure	tbsCert;
        private readonly AlgorithmIdentifier		sigAlgID;
        private readonly DerBitString				sig;

        public static X509CertificateStructure GetInstance(
            Asn1TaggedObject	obj,
            bool				explicitly)
        {
            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
        }

        public static X509CertificateStructure GetInstance(
            object obj)
        {
            if (obj is X509CertificateStructure)
                return (X509CertificateStructure)obj;
            if (obj == null)
                return null;
            return new X509CertificateStructure(Asn1Sequence.GetInstance(obj));
        }

        public X509CertificateStructure(
            TbsCertificateStructure	tbsCert,
            AlgorithmIdentifier		sigAlgID,
            DerBitString			sig)
        {
            if (tbsCert == null)
                throw new ArgumentNullException("tbsCert");
            if (sigAlgID == null)
                throw new ArgumentNullException("sigAlgID");
            if (sig == null)
                throw new ArgumentNullException("sig");

            this.tbsCert = tbsCert;
            this.sigAlgID = sigAlgID;
            this.sig = sig;
        }

        private X509CertificateStructure(
            Asn1Sequence seq)
        {
            if (seq.Count != 3)
                throw new ArgumentException("sequence wrong size for a certificate", "seq");

            //
            // correct x509 certficate
            //
            tbsCert = TbsCertificateStructure.GetInstance(seq[0]);
            sigAlgID = AlgorithmIdentifier.GetInstance(seq[1]);
            sig = DerBitString.GetInstance(seq[2]);
        }

        public TbsCertificateStructure TbsCertificate
        {
            get { return tbsCert; }
        }

        public int Version
        {
            get { return tbsCert.Version; }
        }

        public DerInteger SerialNumber
        {
            get { return tbsCert.SerialNumber; }
        }

        public X509Name Issuer
        {
            get { return tbsCert.Issuer; }
        }

        public Time StartDate
        {
            get { return tbsCert.StartDate; }
        }

        public Time EndDate
        {
            get { return tbsCert.EndDate; }
        }

        public X509Name Subject
        {
            get { return tbsCert.Subject; }
        }

        public SubjectPublicKeyInfo SubjectPublicKeyInfo
        {
            get { return tbsCert.SubjectPublicKeyInfo; }
        }

        public AlgorithmIdentifier SignatureAlgorithm
        {
            get { return sigAlgID; }
        }

        public DerBitString Signature
        {
            get { return sig; }
        }

        public override Asn1Object ToAsn1Object()
        {
            return new DerSequence(tbsCert, sigAlgID, sig);
        }
    }
}