1 package org.bouncycastle.openssl;
2 
3 import java.util.Collections;
4 import java.util.Enumeration;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.Set;
8 
9 import org.bouncycastle.asn1.ASN1Encodable;
10 import org.bouncycastle.asn1.ASN1EncodableVector;
11 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
12 import org.bouncycastle.asn1.ASN1Sequence;
13 import org.bouncycastle.asn1.ASN1TaggedObject;
14 import org.bouncycastle.asn1.DERSequence;
15 import org.bouncycastle.asn1.DERTaggedObject;
16 import org.bouncycastle.asn1.DERUTF8String;
17 
18 public class CertificateTrustBlock
19 {
20     private ASN1Sequence uses;
21     private ASN1Sequence prohibitions;
22     private String alias;
23 
CertificateTrustBlock(Set<ASN1ObjectIdentifier> uses)24     public CertificateTrustBlock(Set<ASN1ObjectIdentifier> uses)
25     {
26         this(null, uses, null);
27     }
28 
CertificateTrustBlock(String alias, Set<ASN1ObjectIdentifier> uses)29     public CertificateTrustBlock(String alias, Set<ASN1ObjectIdentifier> uses)
30     {
31         this(alias, uses, null);
32     }
33 
CertificateTrustBlock(String alias, Set<ASN1ObjectIdentifier> uses, Set<ASN1ObjectIdentifier> prohibitions)34     public CertificateTrustBlock(String alias, Set<ASN1ObjectIdentifier> uses, Set<ASN1ObjectIdentifier> prohibitions)
35     {
36         this.alias = alias;
37         this.uses = toSequence(uses);
38         this.prohibitions = toSequence(prohibitions);
39     }
40 
CertificateTrustBlock(byte[] encoded)41     CertificateTrustBlock(byte[] encoded)
42     {
43         ASN1Sequence seq = ASN1Sequence.getInstance(encoded);
44 
45         for (Enumeration en = seq.getObjects(); en.hasMoreElements();)
46         {
47             ASN1Encodable obj = (ASN1Encodable)en.nextElement();
48 
49             if (obj instanceof ASN1Sequence)
50             {
51                 this.uses = ASN1Sequence.getInstance(obj);
52             }
53             else if (obj instanceof ASN1TaggedObject)
54             {
55                 this.prohibitions = ASN1Sequence.getInstance((ASN1TaggedObject)obj, false);
56             }
57             else if (obj instanceof DERUTF8String)
58             {
59                 this.alias = DERUTF8String.getInstance(obj).getString();
60             }
61         }
62     }
63 
getAlias()64     public String getAlias()
65     {
66         return alias;
67     }
68 
getUses()69     public Set<ASN1ObjectIdentifier> getUses()
70     {
71         return toSet(uses);
72     }
73 
getProhibitions()74     public Set<ASN1ObjectIdentifier> getProhibitions()
75     {
76         return toSet(prohibitions);
77     }
78 
toSet(ASN1Sequence seq)79     private Set<ASN1ObjectIdentifier> toSet(ASN1Sequence seq)
80     {
81         if (seq != null)
82         {
83             Set<ASN1ObjectIdentifier> oids = new HashSet<ASN1ObjectIdentifier>(seq.size());
84 
85             for (Enumeration en = seq.getObjects(); en.hasMoreElements(); )
86             {
87                 oids.add(ASN1ObjectIdentifier.getInstance(en.nextElement()));
88             }
89 
90             return oids;
91         }
92 
93         return Collections.EMPTY_SET;
94     }
95 
toSequence(Set<ASN1ObjectIdentifier> oids)96     private ASN1Sequence toSequence(Set<ASN1ObjectIdentifier> oids)
97     {
98         if (oids == null || oids.isEmpty())
99         {
100             return null;
101         }
102 
103         ASN1EncodableVector v = new ASN1EncodableVector();
104 
105         for (Iterator it = oids.iterator(); it.hasNext();)
106         {
107            v.add((ASN1Encodable)it.next());
108         }
109 
110         return new DERSequence(v);
111     }
112 
toASN1Sequence()113     ASN1Sequence toASN1Sequence()
114     {
115         ASN1EncodableVector v = new ASN1EncodableVector();
116 
117         if (uses != null)
118         {
119            v.add(uses);
120         }
121         if (prohibitions != null)
122         {
123             v.add(new DERTaggedObject(false, 0, prohibitions));
124         }
125         if (alias != null)
126         {
127             v.add(new DERUTF8String(alias));
128         }
129 
130         return new DERSequence(v);
131     }
132 }
133