Extension.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.x509;
2
3import java.io.IOException;
4
5import org.bouncycastle.asn1.ASN1Boolean;
6import org.bouncycastle.asn1.ASN1Encodable;
7import org.bouncycastle.asn1.ASN1ObjectIdentifier;
8import org.bouncycastle.asn1.ASN1OctetString;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.DEROctetString;
11
12/**
13 * an object for the elements in the X.509 V3 extension block.
14 */
15public class Extension
16{
17    /**
18     * Subject Directory Attributes
19     */
20    public static final ASN1ObjectIdentifier subjectDirectoryAttributes = new ASN1ObjectIdentifier("2.5.29.9");
21
22    /**
23     * Subject Key Identifier
24     */
25    public static final ASN1ObjectIdentifier subjectKeyIdentifier = new ASN1ObjectIdentifier("2.5.29.14");
26
27    /**
28     * Key Usage
29     */
30    public static final ASN1ObjectIdentifier keyUsage = new ASN1ObjectIdentifier("2.5.29.15");
31
32    /**
33     * Private Key Usage Period
34     */
35    public static final ASN1ObjectIdentifier privateKeyUsagePeriod = new ASN1ObjectIdentifier("2.5.29.16");
36
37    /**
38     * Subject Alternative Name
39     */
40    public static final ASN1ObjectIdentifier subjectAlternativeName = new ASN1ObjectIdentifier("2.5.29.17");
41
42    /**
43     * Issuer Alternative Name
44     */
45    public static final ASN1ObjectIdentifier issuerAlternativeName = new ASN1ObjectIdentifier("2.5.29.18");
46
47    /**
48     * Basic Constraints
49     */
50    public static final ASN1ObjectIdentifier basicConstraints = new ASN1ObjectIdentifier("2.5.29.19");
51
52    /**
53     * CRL Number
54     */
55    public static final ASN1ObjectIdentifier cRLNumber = new ASN1ObjectIdentifier("2.5.29.20");
56
57    /**
58     * Reason code
59     */
60    public static final ASN1ObjectIdentifier reasonCode = new ASN1ObjectIdentifier("2.5.29.21");
61
62    /**
63     * Hold Instruction Code
64     */
65    public static final ASN1ObjectIdentifier instructionCode = new ASN1ObjectIdentifier("2.5.29.23");
66
67    /**
68     * Invalidity Date
69     */
70    public static final ASN1ObjectIdentifier invalidityDate = new ASN1ObjectIdentifier("2.5.29.24");
71
72    /**
73     * Delta CRL indicator
74     */
75    public static final ASN1ObjectIdentifier deltaCRLIndicator = new ASN1ObjectIdentifier("2.5.29.27");
76
77    /**
78     * Issuing Distribution Point
79     */
80    public static final ASN1ObjectIdentifier issuingDistributionPoint = new ASN1ObjectIdentifier("2.5.29.28");
81
82    /**
83     * Certificate Issuer
84     */
85    public static final ASN1ObjectIdentifier certificateIssuer = new ASN1ObjectIdentifier("2.5.29.29");
86
87    /**
88     * Name Constraints
89     */
90    public static final ASN1ObjectIdentifier nameConstraints = new ASN1ObjectIdentifier("2.5.29.30");
91
92    /**
93     * CRL Distribution Points
94     */
95    public static final ASN1ObjectIdentifier cRLDistributionPoints = new ASN1ObjectIdentifier("2.5.29.31");
96
97    /**
98     * Certificate Policies
99     */
100    public static final ASN1ObjectIdentifier certificatePolicies = new ASN1ObjectIdentifier("2.5.29.32");
101
102    /**
103     * Policy Mappings
104     */
105    public static final ASN1ObjectIdentifier policyMappings = new ASN1ObjectIdentifier("2.5.29.33");
106
107    /**
108     * Authority Key Identifier
109     */
110    public static final ASN1ObjectIdentifier authorityKeyIdentifier = new ASN1ObjectIdentifier("2.5.29.35");
111
112    /**
113     * Policy Constraints
114     */
115    public static final ASN1ObjectIdentifier policyConstraints = new ASN1ObjectIdentifier("2.5.29.36");
116
117    /**
118     * Extended Key Usage
119     */
120    public static final ASN1ObjectIdentifier extendedKeyUsage = new ASN1ObjectIdentifier("2.5.29.37");
121
122    /**
123     * Freshest CRL
124     */
125    public static final ASN1ObjectIdentifier freshestCRL = new ASN1ObjectIdentifier("2.5.29.46");
126
127    /**
128     * Inhibit Any Policy
129     */
130    public static final ASN1ObjectIdentifier inhibitAnyPolicy = new ASN1ObjectIdentifier("2.5.29.54");
131
132    /**
133     * Authority Info Access
134     */
135    public static final ASN1ObjectIdentifier authorityInfoAccess = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.1");
136
137    /**
138     * Subject Info Access
139     */
140    public static final ASN1ObjectIdentifier subjectInfoAccess = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.11");
141
142    /**
143     * Logo Type
144     */
145    public static final ASN1ObjectIdentifier logoType = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.12");
146
147    /**
148     * BiometricInfo
149     */
150    public static final ASN1ObjectIdentifier biometricInfo = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.2");
151
152    /**
153     * QCStatements
154     */
155    public static final ASN1ObjectIdentifier qCStatements = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.3");
156
157    /**
158     * Audit identity extension in attribute certificates.
159     */
160    public static final ASN1ObjectIdentifier auditIdentity = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.1.4");
161
162    /**
163     * NoRevAvail extension in attribute certificates.
164     */
165    public static final ASN1ObjectIdentifier noRevAvail = new ASN1ObjectIdentifier("2.5.29.56");
166
167    /**
168     * TargetInformation extension in attribute certificates.
169     */
170    public static final ASN1ObjectIdentifier targetInformation = new ASN1ObjectIdentifier("2.5.29.55");
171
172    private ASN1ObjectIdentifier extnId;
173
174    boolean             critical;
175    ASN1OctetString      value;
176
177    public Extension(
178        ASN1ObjectIdentifier extnId,
179        ASN1Boolean critical,
180        ASN1OctetString value)
181    {
182        this(extnId, critical.isTrue(), value);
183    }
184
185    public Extension(
186        ASN1ObjectIdentifier extnId,
187        boolean critical,
188        byte[] value)
189    {
190        this(extnId, critical, new DEROctetString(value));
191    }
192
193    public Extension(
194        ASN1ObjectIdentifier extnId,
195        boolean critical,
196        ASN1OctetString value)
197    {
198        this.extnId = extnId;
199        this.critical = critical;
200        this.value = value;
201    }
202
203    public ASN1ObjectIdentifier getExtnId()
204    {
205        return extnId;
206    }
207
208    public boolean isCritical()
209    {
210        return critical;
211    }
212
213    public ASN1OctetString getExtnValue()
214    {
215        return value;
216    }
217
218    public ASN1Encodable getParsedValue()
219    {
220        return convertValueToObject(this);
221    }
222
223    public int hashCode()
224    {
225        if (this.isCritical())
226        {
227            return this.getExtnValue().hashCode();
228        }
229
230        return ~this.getExtnValue().hashCode();
231    }
232
233    public boolean equals(
234        Object  o)
235    {
236        if (!(o instanceof Extension))
237        {
238            return false;
239        }
240
241        Extension other = (Extension)o;
242
243        return other.getExtnValue().equals(this.getExtnValue())
244            && (other.isCritical() == this.isCritical());
245    }
246
247    /**
248     * Convert the value of the passed in extension to an object
249     * @param ext the extension to parse
250     * @return the object the value string contains
251     * @exception IllegalArgumentException if conversion is not possible
252     */
253    private static ASN1Primitive convertValueToObject(
254        Extension ext)
255        throws IllegalArgumentException
256    {
257        try
258        {
259            return ASN1Primitive.fromByteArray(ext.getExtnValue().getOctets());
260        }
261        catch (IOException e)
262        {
263            throw new IllegalArgumentException("can't convert extension: " +  e);
264        }
265    }
266}
267