01: package org.bouncycastle.asn1.x509;
02:
03: import java.util.Enumeration;
04: import java.util.Vector;
05:
06: import org.bouncycastle.asn1.ASN1Encodable;
07: import org.bouncycastle.asn1.ASN1EncodableVector;
08: import org.bouncycastle.asn1.ASN1Sequence;
09: import org.bouncycastle.asn1.ASN1TaggedObject;
10: import org.bouncycastle.asn1.DERObject;
11: import org.bouncycastle.asn1.DERSequence;
12: import org.bouncycastle.asn1.DERTaggedObject;
13:
14: public class NameConstraints extends ASN1Encodable {
15: private ASN1Sequence permitted, excluded;
16:
17: public NameConstraints(ASN1Sequence seq) {
18: Enumeration e = seq.getObjects();
19: while (e.hasMoreElements()) {
20: ASN1TaggedObject o = ASN1TaggedObject.getInstance(e
21: .nextElement());
22: switch (o.getTagNo()) {
23: case 0:
24: permitted = ASN1Sequence.getInstance(o, false);
25: break;
26: case 1:
27: excluded = ASN1Sequence.getInstance(o, false);
28: break;
29: }
30: }
31: }
32:
33: /**
34: * Constructor from a given details.
35: *
36: * <p>
37: * permitted and excluded are Vectors of GeneralSubtree objects.
38: *
39: * @param permitted
40: * Permitted subtrees
41: * @param excluded
42: * Excludes subtrees
43: */
44: public NameConstraints(Vector permitted, Vector excluded) {
45: if (permitted != null) {
46: this .permitted = createSequence(permitted);
47: }
48: if (excluded != null) {
49: this .excluded = createSequence(excluded);
50: }
51: }
52:
53: private DERSequence createSequence(Vector subtree) {
54: ASN1EncodableVector vec = new ASN1EncodableVector();
55: Enumeration e = subtree.elements();
56: while (e.hasMoreElements()) {
57: vec.add((GeneralSubtree) e.nextElement());
58: }
59:
60: return new DERSequence(vec);
61: }
62:
63: public ASN1Sequence getPermittedSubtrees() {
64: return permitted;
65: }
66:
67: public ASN1Sequence getExcludedSubtrees() {
68: return excluded;
69: }
70:
71: /*
72: * NameConstraints ::= SEQUENCE { permittedSubtrees [0] GeneralSubtrees
73: * OPTIONAL, excludedSubtrees [1] GeneralSubtrees OPTIONAL }
74: */
75: public DERObject toASN1Object() {
76: ASN1EncodableVector v = new ASN1EncodableVector();
77:
78: if (permitted != null) {
79: v.add(new DERTaggedObject(false, 0, permitted));
80: }
81:
82: if (excluded != null) {
83: v.add(new DERTaggedObject(false, 1, excluded));
84: }
85:
86: return new DERSequence(v);
87: }
88: }
|