1package org.bouncycastle.openssl;
2
3import java.util.Collections;
4import java.util.Enumeration;
5import java.util.HashSet;
6import java.util.Iterator;
7import java.util.Set;
8
9import org.bouncycastle.asn1.ASN1Encodable;
10import org.bouncycastle.asn1.ASN1EncodableVector;
11import org.bouncycastle.asn1.ASN1ObjectIdentifier;
12import org.bouncycastle.asn1.ASN1Sequence;
13import org.bouncycastle.asn1.ASN1TaggedObject;
14import org.bouncycastle.asn1.DERSequence;
15import org.bouncycastle.asn1.DERTaggedObject;
16import org.bouncycastle.asn1.DERUTF8String;
17
18public class CertificateTrustBlock
19{
20    private ASN1Sequence uses;
21    private ASN1Sequence prohibitions;
22    private String alias;
23
24    public CertificateTrustBlock(Set<ASN1ObjectIdentifier> uses)
25    {
26        this(null, uses, null);
27    }
28
29    public CertificateTrustBlock(String alias, Set<ASN1ObjectIdentifier> uses)
30    {
31        this(alias, uses, null);
32    }
33
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
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
64    public String getAlias()
65    {
66        return alias;
67    }
68
69    public Set<ASN1ObjectIdentifier> getUses()
70    {
71        return toSet(uses);
72    }
73
74    public Set<ASN1ObjectIdentifier> getProhibitions()
75    {
76        return toSet(prohibitions);
77    }
78
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
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
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