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

namespace Org.BouncyCastle.Asn1.Cmp
{
    /**
     * GenMsg:    {id-it 20}, RootCaCertValue | < absent >
     * GenRep:    {id-it 18}, RootCaKeyUpdateContent | < absent >
     * <p>
     * RootCaCertValue ::= CMPCertificate
     * </p><p>
     * RootCaKeyUpdateValue ::= RootCaKeyUpdateContent
     * </p><p>
     * RootCaKeyUpdateContent ::= SEQUENCE {
     * newWithNew       CMPCertificate,
     * newWithOld   [0] CMPCertificate OPTIONAL,
     * oldWithNew   [1] CMPCertificate OPTIONAL
     * }
     * </p>
     */
    public class RootCaKeyUpdateContent
        : Asn1Encodable
    {
        public static RootCaKeyUpdateContent GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is RootCaKeyUpdateContent rootCaKeyUpdateContent)
                return rootCaKeyUpdateContent;
            return new RootCaKeyUpdateContent(Asn1Sequence.GetInstance(obj));
        }

        public static RootCaKeyUpdateContent GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new RootCaKeyUpdateContent(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        private readonly CmpCertificate m_newWithNew;
        private readonly CmpCertificate m_newWithOld;
        private readonly CmpCertificate m_oldWithNew;

        public RootCaKeyUpdateContent(CmpCertificate newWithNew, CmpCertificate newWithOld, CmpCertificate oldWithNew)
        {
            m_newWithNew = newWithNew ?? throw new ArgumentNullException(nameof(newWithNew));
            m_newWithOld = newWithOld;
            m_oldWithNew = oldWithNew;
        }

        private RootCaKeyUpdateContent(Asn1Sequence seq)
        {
            if (seq.Count < 1 || seq.Count > 3)
                throw new ArgumentException("expected sequence of 1 to 3 elements only");

            CmpCertificate newWithNew = CmpCertificate.GetInstance(seq[0]);
            CmpCertificate newWithOld = null;
            CmpCertificate oldWithNew = null;

            for (int pos = 1; pos < seq.Count; ++pos)
            {
                Asn1TaggedObject ato = Asn1TaggedObject.GetInstance(seq[pos]);

                if (ato.HasContextTag(0))
                {
                    newWithOld = CmpCertificate.GetInstance(ato, true);
                }
                else if (ato.HasContextTag(1))
                {
                    oldWithNew = CmpCertificate.GetInstance(ato, true);
                }
            }

            m_newWithNew = newWithNew;
            m_newWithOld = newWithOld;
            m_oldWithNew = oldWithNew;
        }

        public virtual CmpCertificate NewWithNew => m_newWithNew;

        public virtual CmpCertificate NewWithOld => m_newWithOld;

        public virtual CmpCertificate OldWithNew => m_oldWithNew;

        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(3);
            v.Add(m_newWithNew);
            v.AddOptionalTagged(true, 0, m_newWithOld);
            v.AddOptionalTagged(true, 1, m_oldWithNew);
            return new DerSequence(v);
        }
    }
}