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

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1
{
    internal abstract class Asn1UniversalType
        : Asn1Type
    {
        internal readonly Asn1Tag m_tag;

        internal Asn1UniversalType(Type platformType, int tagNo)
            : base(platformType)
        {
            m_tag = Asn1Tag.Create(Asn1Tags.Universal, tagNo);
        }

        internal Asn1Object CheckedCast(Asn1Object asn1Object)
        {
            if (PlatformType.IsInstanceOfType(asn1Object))
                return asn1Object;

            throw new InvalidOperationException("unexpected object: " + Platform.GetTypeName(asn1Object));
        }

        internal virtual Asn1Object FromImplicitPrimitive(DerOctetString octetString)
        {
            throw new InvalidOperationException("unexpected implicit primitive encoding");
        }

        internal virtual Asn1Object FromImplicitConstructed(Asn1Sequence sequence)
        {
            throw new InvalidOperationException("unexpected implicit constructed encoding");
        }

        /// <exception cref="IOException"/>
        internal Asn1Object FromByteArray(byte[] bytes)
        {
            return CheckedCast(Asn1Object.FromByteArray(bytes));
        }

        internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            CheckedCast(Asn1Utilities.CheckContextTagClass(taggedObject).GetBaseUniversal(declaredExplicit, this));

        internal Asn1Tag Tag
        {
            get { return m_tag; }
        }
    }
}