summary refs log tree commit diff
path: root/crypto/src/asn1/ess/ESSCertIDv2.cs
blob: b3d49ceb5c924b30b3b3c05da07bdf6c1862dbce (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;

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

namespace Org.BouncyCastle.Asn1.Ess
{
    public class EssCertIDv2
        : Asn1Encodable
    {
        private readonly AlgorithmIdentifier hashAlgorithm;
        private readonly byte[]              certHash;
        private readonly IssuerSerial        issuerSerial;

        private static readonly AlgorithmIdentifier DefaultAlgID = new AlgorithmIdentifier(
            NistObjectIdentifiers.IdSha256);

        public static EssCertIDv2 GetInstance(object obj)
        {
            if (obj == null)
                return null;
            EssCertIDv2 existing = obj as EssCertIDv2;
            if (existing != null)
                return existing;
            return new EssCertIDv2(Asn1Sequence.GetInstance(obj));
        }

        private EssCertIDv2(
            Asn1Sequence seq)
        {
            if (seq.Count > 3)
                throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");

            int count = 0;

            if (seq[0] is Asn1OctetString)
            {
                // Default value
                this.hashAlgorithm = DefaultAlgID;
            }
            else
            {
                this.hashAlgorithm = AlgorithmIdentifier.GetInstance(seq[count++].ToAsn1Object());
            }

            this.certHash = Asn1OctetString.GetInstance(seq[count++].ToAsn1Object()).GetOctets();

            if (seq.Count > count)
            {
                this.issuerSerial = IssuerSerial.GetInstance(
                    Asn1Sequence.GetInstance(seq[count].ToAsn1Object()));
            }
        }

        public EssCertIDv2(byte[] certHash)
            : this(null, certHash, null)
        {
        }

        public EssCertIDv2(
            AlgorithmIdentifier	algId,
            byte[]				certHash)
            : this(algId, certHash, null)
        {
        }

        public EssCertIDv2(
            byte[]              certHash,
            IssuerSerial        issuerSerial)
            : this(null, certHash, issuerSerial)
        {
        }

        public EssCertIDv2(
            AlgorithmIdentifier	algId,
            byte[]				certHash,
            IssuerSerial		issuerSerial)
        {
            if (algId == null)
            {
                // Default value
                this.hashAlgorithm = DefaultAlgID;
            }
            else
            {
                this.hashAlgorithm = algId;
            }

            this.certHash = certHash;
            this.issuerSerial = issuerSerial;
        }

        public AlgorithmIdentifier HashAlgorithm
        {
            get { return this.hashAlgorithm; }
        }

        public byte[] GetCertHash()
        {
            return Arrays.Clone(certHash);
        }

        public IssuerSerial IssuerSerial
        {
            get { return issuerSerial; }
        }

        /**
         * <pre>
         * EssCertIDv2 ::=  SEQUENCE {
         *     hashAlgorithm     AlgorithmIdentifier
         *              DEFAULT {algorithm id-sha256},
         *     certHash          Hash,
         *     issuerSerial      IssuerSerial OPTIONAL
         * }
         *
         * Hash ::= OCTET STRING
         *
         * IssuerSerial ::= SEQUENCE {
         *     issuer         GeneralNames,
         *     serialNumber   CertificateSerialNumber
         * }
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(3);

            if (!hashAlgorithm.Equals(DefaultAlgID))
            {
                v.Add(hashAlgorithm);
            }

            v.Add(new DerOctetString(certHash).ToAsn1Object());
            v.AddOptional(issuerSerial);
            return new DerSequence(v);
        }

    }
}