~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/ocsp/RespData.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.ocsp;
 
2
 
 
3
import org.bouncycastle.asn1.ASN1Encodable;
 
4
import org.bouncycastle.asn1.ASN1Sequence;
 
5
import org.bouncycastle.asn1.DERObjectIdentifier;
 
6
import org.bouncycastle.asn1.ocsp.ResponseData;
 
7
import org.bouncycastle.asn1.ocsp.SingleResponse;
 
8
import org.bouncycastle.asn1.x509.X509Extension;
 
9
import org.bouncycastle.asn1.x509.X509Extensions;
 
10
 
 
11
import java.text.ParseException;
 
12
import java.util.Date;
 
13
import java.util.Enumeration;
 
14
import java.util.HashSet;
 
15
import java.util.Set;
 
16
 
 
17
public class RespData
 
18
    implements java.security.cert.X509Extension
 
19
{
 
20
    ResponseData    data;
 
21
 
 
22
    public RespData(
 
23
        ResponseData    data)
 
24
    {
 
25
        this.data = data;
 
26
    }
 
27
 
 
28
    public int getVersion()
 
29
    {
 
30
        return data.getVersion().getValue().intValue() + 1;
 
31
    }
 
32
 
 
33
    public RespID getResponderId()
 
34
    {
 
35
        return new RespID(data.getResponderID());
 
36
    }
 
37
 
 
38
    public Date getProducedAt()
 
39
    {
 
40
        try
 
41
        {
 
42
            return data.getProducedAt().getDate();
 
43
        }
 
44
        catch (ParseException e)
 
45
        {
 
46
            throw new IllegalStateException("ParseException:" + e.getMessage());
 
47
        }
 
48
    }
 
49
 
 
50
    public SingleResp[] getResponses()
 
51
    {
 
52
        ASN1Sequence    s = data.getResponses();
 
53
        SingleResp[]    rs = new SingleResp[s.size()];
 
54
 
 
55
        for (int i = 0; i != rs.length; i++)
 
56
        {
 
57
            rs[i] = new SingleResp(SingleResponse.getInstance(s.getObjectAt(i)));
 
58
        }
 
59
 
 
60
        return rs;
 
61
    }
 
62
 
 
63
    public X509Extensions getResponseExtensions()
 
64
    {
 
65
        return data.getResponseExtensions();
 
66
    }
 
67
    
 
68
    /**
 
69
     * RFC 2650 doesn't specify any critical extensions so we return true
 
70
     * if any are encountered.
 
71
     * 
 
72
     * @return true if any critical extensions are present.
 
73
     */
 
74
    public boolean hasUnsupportedCriticalExtension()
 
75
    {
 
76
        Set extns = getCriticalExtensionOIDs();
 
77
        if (extns != null && !extns.isEmpty())
 
78
        {
 
79
            return true;
 
80
        }
 
81
 
 
82
        return false;
 
83
    }
 
84
 
 
85
    private Set getExtensionOIDs(boolean critical)
 
86
    {
 
87
        Set             set = new HashSet();
 
88
        X509Extensions  extensions = this.getResponseExtensions();
 
89
        
 
90
        if (extensions != null)
 
91
        {
 
92
            Enumeration     e = extensions.oids();
 
93
    
 
94
            while (e.hasMoreElements())
 
95
            {
 
96
                DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
 
97
                X509Extension       ext = extensions.getExtension(oid);
 
98
    
 
99
                if (critical == ext.isCritical())
 
100
                {
 
101
                    set.add(oid.getId());
 
102
                }
 
103
            }
 
104
        }
 
105
 
 
106
        return set;
 
107
    }
 
108
 
 
109
    public Set getCriticalExtensionOIDs()
 
110
    {
 
111
        return getExtensionOIDs(true);
 
112
    }
 
113
 
 
114
    public Set getNonCriticalExtensionOIDs()
 
115
    {
 
116
        return getExtensionOIDs(false);
 
117
    }
 
118
 
 
119
    public byte[] getExtensionValue(String oid)
 
120
    {
 
121
        X509Extensions exts = this.getResponseExtensions();
 
122
 
 
123
        if (exts != null)
 
124
        {
 
125
            X509Extension   ext = exts.getExtension(new DERObjectIdentifier(oid));
 
126
 
 
127
            if (ext != null)
 
128
            {
 
129
                try
 
130
                {
 
131
                    return ext.getValue().getEncoded(ASN1Encodable.DER);
 
132
                }
 
133
                catch (Exception e)
 
134
                {
 
135
                    throw new RuntimeException("error encoding " + e.toString());
 
136
                }
 
137
            }
 
138
        }
 
139
 
 
140
        return null;
 
141
    }
 
142
}