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

using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Cmp
{
    public class CmpCertificate
        : Asn1Encodable, IAsn1Choice
    {
        public static CmpCertificate GetInstance(object obj)
        {
            // TODO[cmp] Review this whole metho

            if (obj == null)
                return null;

            if (obj is CmpCertificate cmpCertificate)
                return cmpCertificate;

            if (obj is byte[] bs)
            {
                try
                {
                    obj = Asn1Object.FromByteArray(bs);
                }
                catch (IOException)
                {
                    throw new ArgumentException("Invalid encoding in CmpCertificate");
                }
            }

            if (obj is Asn1Sequence)
                return new CmpCertificate(X509CertificateStructure.GetInstance(obj));

            if (obj is Asn1TaggedObject taggedObject)
                return new CmpCertificate(taggedObject.TagNo, taggedObject.GetObject());

            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj));
        }

        public static CmpCertificate GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            // TODO[cmp]
            if (taggedObject == null)
                return null;

            if (!declaredExplicit)
                throw new ArgumentException("tag must be explicit");

            // TODO[cmp]
            return GetInstance(taggedObject.GetObject());
        }

        private readonly X509CertificateStructure m_x509v3PKCert;

        private readonly int m_otherTagValue;
        private readonly Asn1Encodable m_otherCert;

        /**
         * Note: the addition of other certificates is a BC extension. If you use this constructor they
         * will be added with an explicit tag value of type.
         *
         * @param type      the type of the certificate (used as a tag value).
         * @param otherCert the object representing the certificate
         */
        public CmpCertificate(int type, Asn1Encodable otherCert)
        {
            m_otherTagValue = type;
            m_otherCert = otherCert;
        }

        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_otherTagValue;

        public virtual Asn1Encodable OtherCert => m_otherCert;

        /**
         * <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_otherCert != null)
            {
                // explicit following CMP conventions
                return new DerTaggedObject(true, m_otherTagValue, m_otherCert);
            }

            return m_x509v3PKCert.ToAsn1Object();
        }
    }
}