summary refs log tree commit diff
path: root/crypto/src/asn1/x509/NameConstraints.cs
blob: 0c5fea8b3cf2c8382fb2d1c6b611b9e119fb4c00 (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
using System;
using System.Collections;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.X509
{
	public class NameConstraints
		: Asn1Encodable
	{
		private Asn1Sequence permitted, excluded;

		public static NameConstraints GetInstance(
			object obj)
		{
			if (obj == null || obj is NameConstraints)
			{
				return (NameConstraints) obj;
			}

			if (obj is Asn1Sequence)
			{
				return new NameConstraints((Asn1Sequence) obj);
			}

            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
		}

		public NameConstraints(
			Asn1Sequence seq)
		{
			foreach (Asn1TaggedObject o in seq)
			{
				switch (o.TagNo)
				{
					case 0:
						permitted = Asn1Sequence.GetInstance(o, false);
						break;
					case 1:
						excluded = Asn1Sequence.GetInstance(o, false);
						break;
				}
			}
		}

#if !(SILVERLIGHT || PORTABLE)
        public NameConstraints(
            ArrayList permitted,
            ArrayList excluded)
            : this((IList)permitted, (IList)excluded)
        {
        }
#endif

        /**
		 * Constructor from a given details.
		 *
		 * <p>permitted and excluded are Vectors of GeneralSubtree objects.</p>
		 *
		 * @param permitted Permitted subtrees
		 * @param excluded Excluded subtrees
		 */
		public NameConstraints(
			IList   permitted,
			IList   excluded)
		{
			if (permitted != null)
			{
				this.permitted = CreateSequence(permitted);
			}

			if (excluded != null)
			{
				this.excluded = CreateSequence(excluded);
			}
		}

		private DerSequence CreateSequence(
			IList subtrees)
		{
            GeneralSubtree[] gsts = new GeneralSubtree[subtrees.Count];
            for (int i = 0; i < subtrees.Count; ++i)
            {
                gsts[i] = (GeneralSubtree)subtrees[i];
            }
            return new DerSequence(gsts);
		}

		public Asn1Sequence PermittedSubtrees
		{
			get { return permitted; }
		}

		public Asn1Sequence ExcludedSubtrees
		{
			get { return excluded; }
		}

		/*
		 * NameConstraints ::= SEQUENCE { permittedSubtrees [0] GeneralSubtrees
		 * OPTIONAL, excludedSubtrees [1] GeneralSubtrees OPTIONAL }
		 */
		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector();

			if (permitted != null)
			{
				v.Add(new DerTaggedObject(false, 0, permitted));
			}

			if (excluded != null)
			{
				v.Add(new DerTaggedObject(false, 1, excluded));
			}

			return new DerSequence(v);
		}
	}
}