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