NameConstraints.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.x509;
2
3import java.util.Enumeration;
4import java.util.Vector;
5
6import org.bouncycastle.asn1.ASN1EncodableVector;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1Primitive;
9import org.bouncycastle.asn1.ASN1Sequence;
10import org.bouncycastle.asn1.ASN1TaggedObject;
11import org.bouncycastle.asn1.DERSequence;
12import org.bouncycastle.asn1.DERTaggedObject;
13
14public class NameConstraints
15    extends ASN1Object
16{
17    private ASN1Sequence permitted, excluded;
18
19    public static NameConstraints getInstance(Object obj)
20    {
21        if (obj instanceof NameConstraints)
22        {
23            return (NameConstraints)obj;
24        }
25        if (obj != null)
26        {
27            return new NameConstraints(ASN1Sequence.getInstance(obj));
28        }
29
30        return null;
31    }
32
33    private NameConstraints(ASN1Sequence seq)
34    {
35        Enumeration e = seq.getObjects();
36        while (e.hasMoreElements())
37        {
38            ASN1TaggedObject o = ASN1TaggedObject.getInstance(e.nextElement());
39            switch (o.getTagNo())
40            {
41            case 0:
42                permitted = ASN1Sequence.getInstance(o, false);
43                break;
44            case 1:
45                excluded = ASN1Sequence.getInstance(o, false);
46                break;
47            }
48        }
49    }
50
51    /**
52     * Constructor from a given details.
53     *
54     * <p>
55     * permitted and excluded are Vectors of GeneralSubtree objects.
56     *
57     * @param permitted
58     *            Permitted subtrees
59     * @param excluded
60     *            Excludes subtrees
61     */
62    public NameConstraints(
63        Vector permitted,
64        Vector excluded)
65    {
66        if (permitted != null)
67        {
68            this.permitted = createSequence(permitted);
69        }
70        if (excluded != null)
71        {
72            this.excluded = createSequence(excluded);
73        }
74    }
75
76    private DERSequence createSequence(Vector subtree)
77    {
78        ASN1EncodableVector vec = new ASN1EncodableVector();
79        Enumeration e = subtree.elements();
80        while (e.hasMoreElements())
81        {
82            vec.add((GeneralSubtree)e.nextElement());
83        }
84
85        return new DERSequence(vec);
86    }
87
88    public ASN1Sequence getPermittedSubtrees()
89    {
90        return permitted;
91    }
92
93    public ASN1Sequence getExcludedSubtrees()
94    {
95        return excluded;
96    }
97
98    /*
99     * NameConstraints ::= SEQUENCE { permittedSubtrees [0] GeneralSubtrees
100     * OPTIONAL, excludedSubtrees [1] GeneralSubtrees OPTIONAL }
101     */
102    public ASN1Primitive toASN1Primitive()
103    {
104        ASN1EncodableVector v = new ASN1EncodableVector();
105
106        if (permitted != null)
107        {
108            v.add(new DERTaggedObject(false, 0, permitted));
109        }
110
111        if (excluded != null)
112        {
113            v.add(new DERTaggedObject(false, 1, excluded));
114        }
115
116        return new DERSequence(v);
117    }
118}
119