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

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Cmp
{
    public class CmpCertificate
        : Asn1Encodable, IAsn1Choice
    {
        public static CmpCertificate GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is CmpCertificate cmpCertificate)
                return cmpCertificate;
            if (obj is X509CertificateStructure certificate)
                return new CmpCertificate(certificate);
            if (obj is Asn1TaggedObject taggedObject)
                return new CmpCertificate(taggedObject);

            Asn1Object asn1Object = null;
            if (obj is IAsn1Convertible asn1Convertible)
            {
                asn1Object = asn1Convertible.ToAsn1Object();
            }
            else if (obj is byte[] bytes)
            {
                asn1Object = Asn1Object.FromByteArray(bytes);
            }

            if (asn1Object is Asn1TaggedObject asn1TaggedObject)
                return new CmpCertificate(asn1TaggedObject);

            return new CmpCertificate(X509CertificateStructure.GetInstance(asn1Object ?? obj));
        }

        public static CmpCertificate GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            Asn1Utilities.GetInstanceChoice(taggedObject, declaredExplicit, GetInstance);

        public static CmpCertificate GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance);

        private readonly X509CertificateStructure m_x509v3PKCert;

        private readonly int m_otherTag;
        private readonly Asn1Encodable m_otherObject;

        [Obsolete("Use 'GetInstance' from tagged object instead")]
        public CmpCertificate(int type, Asn1Encodable otherCert)
        {
            m_otherTag = type;
            m_otherObject = otherCert;
        }

        internal CmpCertificate(Asn1TaggedObject taggedObject)
        {
            Asn1Encodable otherCert;
            if (taggedObject.HasContextTag(1))
            {
                otherCert = AttributeCertificate.GetInstance(taggedObject, true);
            }
            else
            {
                throw new ArgumentException("Invalid CHOICE element", nameof(taggedObject));
            }

            m_otherTag = taggedObject.TagNo;
            m_otherObject = taggedObject.GetExplicitBaseObject();
        }

        internal CmpCertificate(CmpCertificate other)
        {
            m_x509v3PKCert = other.m_x509v3PKCert;
            m_otherTag = other.m_otherTag;
            m_otherObject = other.m_otherObject;
        }

        public CmpCertificate(X509CertificateStructure x509v3PKCert)
        {
            if (x509v3PKCert.Version != 3)
                throw new ArgumentException("only version 3 certificates allowed", nameof(x509v3PKCert));

            m_x509v3PKCert = x509v3PKCert;
        }

        public virtual bool IsX509v3PKCert => m_x509v3PKCert != null;

        public virtual X509CertificateStructure X509v3PKCert => m_x509v3PKCert;

        public virtual int OtherCertTag => m_otherTag;

        public virtual Asn1Encodable OtherCert => m_otherObject;

        /**
         * <pre>
         * CMPCertificate ::= CHOICE {
         *            x509v3PKCert        Certificate
         *            x509v2AttrCert      [1] AttributeCertificate
         *  }
         * </pre>
         * Note: the addition of attribute certificates is a BC extension.
         *
         * @return a basic ASN.1 object representation.
         */
        public override Asn1Object ToAsn1Object()
        {
            if (m_otherObject != null)
                return new DerTaggedObject(true, m_otherTag, m_otherObject);
            if (m_x509v3PKCert != null)
                return m_x509v3PKCert.ToAsn1Object();
            throw new InvalidOperationException();
        }
    }
}