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

namespace Org.BouncyCastle.Asn1
{
	public class BerTaggedObjectParser
		: Asn1TaggedObjectParser
	{
        private readonly int m_tagClass;
        private readonly int m_tagNo;
        private readonly bool m_constructed;
        private readonly Asn1StreamParser m_parser;

		internal BerTaggedObjectParser(int tagClass, int tagNo, bool constructed, Asn1StreamParser parser)
		{
            this.m_tagClass = tagClass;
            this.m_tagNo = tagNo;
            this.m_constructed = constructed;
            this.m_parser = parser;
		}

		public bool IsConstructed
		{
			get { return m_constructed; }
		}

        public int TagClass
        {
            get { return m_tagClass; }
        }

		public int TagNo
		{
			get { return m_tagNo; }
		}

		public IAsn1Convertible GetObjectParser(int baseTagNo, bool declaredExplicit)
		{
            if (Asn1Tags.ContextSpecific != TagClass)
                throw new Asn1Exception("this method only valid for CONTEXT_SPECIFIC tags");

            if (!declaredExplicit)
                return m_parser.ReadImplicit(m_constructed, baseTagNo);

			if (!m_constructed)
				throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)");

	        return m_parser.ReadObject();
		}

		public Asn1Object ToAsn1Object()
		{
			try
			{
				return m_parser.ReadTaggedObject(TagClass, TagNo, IsConstructed);
			}
			catch (IOException e)
			{
				throw new Asn1ParsingException(e.Message);
			}
		}
	}
}