1 /* Extension.java -- an X.509 certificate or CRL extension.
2 Copyright (C) 2004, 2006, 2010 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.x509.ext;
41 import gnu.java.security.Configuration;
42 import gnu.java.security.OID;
43 import gnu.java.security.der.DER;
44 import gnu.java.security.der.DERReader;
45 import gnu.java.security.der.DERValue;
46 import gnu.java.security.x509.Util;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.List;
52 import java.util.logging.Logger;
54 public class Extension
56 private static final Logger log = Configuration.DEBUG ?
57 Logger.getLogger(Extension.class.getName()) : null;
59 * This extension's object identifier.
61 protected final OID oid;
64 * The criticality flag.
66 protected final boolean critical;
69 * Whether or not this extension is locally supported.
71 protected boolean isSupported;
74 * The extension value.
76 protected final Value value;
79 * The DER encoded form.
81 protected byte[] encoded;
84 // -------------------------------------------------------------------------
86 public Extension(byte[] encoded) throws IOException
88 this.encoded = (byte[]) encoded.clone();
89 DERReader der = new DERReader(encoded);
91 // Extension ::= SEQUENCE {
92 DERValue val = der.read();
93 if (Configuration.DEBUG)
94 log.fine("read val tag == " + val.getTag() + " len == " + val.getLength());
95 if (!val.isConstructed())
96 throw new IOException("malformed Extension");
98 // extnID OBJECT IDENTIFIER,
100 if (val.getTag() != DER.OBJECT_IDENTIFIER)
101 throw new IOException("expecting OBJECT IDENTIFIER");
102 oid = (OID) val.getValue();
103 if (Configuration.DEBUG)
104 log.fine("read oid == " + oid);
106 // critical BOOLEAN DEFAULT FALSE,
108 if (val.getTag() == DER.BOOLEAN)
110 critical = ((Boolean) val.getValue()).booleanValue();
115 if (Configuration.DEBUG)
116 log.fine("is critical == " + critical);
118 // extnValue OCTET STRING }
119 if (val.getTag() != DER.OCTET_STRING)
120 throw new IOException("expecting OCTET STRING");
121 byte[] encval = (byte[]) val.getValue();
123 if (oid.equals(AuthorityKeyIdentifier.ID))
125 value = new AuthorityKeyIdentifier(encval);
127 else if (oid.equals(SubjectKeyIdentifier.ID))
129 value = new SubjectKeyIdentifier(encval);
131 else if (oid.equals(KeyUsage.ID))
133 value = new KeyUsage(encval);
135 else if (oid.equals(PrivateKeyUsagePeriod.ID))
137 value = new PrivateKeyUsagePeriod(encval);
139 else if (oid.equals(CertificatePolicies.ID))
141 value = new CertificatePolicies(encval);
143 else if (oid.equals (PolicyConstraint.ID))
145 value = new PolicyConstraint (encval);
147 else if (oid.equals(PolicyMappings.ID))
149 value = new PolicyMappings(encval);
151 else if (oid.equals(SubjectAlternativeNames.ID))
153 value = new SubjectAlternativeNames(encval);
155 else if (oid.equals(IssuerAlternativeNames.ID))
157 value = new IssuerAlternativeNames(encval);
159 else if (oid.equals(BasicConstraints.ID))
161 value = new BasicConstraints(encval);
163 else if (oid.equals(ExtendedKeyUsage.ID))
165 value = new ExtendedKeyUsage(encval);
167 else if (oid.equals(CRLNumber.ID))
169 value = new CRLNumber(encval);
171 else if (oid.equals(ReasonCode.ID))
173 value = new ReasonCode(encval);
175 else if (oid.equals(NameConstraints.ID))
177 value = new NameConstraints(encval);
181 value = new Value(encval);
184 if (Configuration.DEBUG)
185 log.fine("read value == " + value);
188 public Extension (final OID oid, final Value value, final boolean critical)
192 this.critical = critical;
197 // -------------------------------------------------------------------------
204 public boolean isCritical()
209 public boolean isSupported()
214 public Value getValue()
219 public byte[] getEncoded()
223 return (byte[]) encoded.clone();
226 public String toString()
228 return Extension.class.getName() + " [ id=" + oid + " critical=" +
229 critical + " value=" + value + " ]";
232 public DERValue getDerValue()
234 List<DERValue> ext = new ArrayList<DERValue>(3);
235 ext.add(new DERValue(DER.OBJECT_IDENTIFIER, oid));
236 ext.add(new DERValue(DER.BOOLEAN, Boolean.valueOf(critical)));
237 ext.add(new DERValue(DER.OCTET_STRING, value.getEncoded()));
238 return new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, ext);
242 // -------------------------------------------------------------------------
244 private void encode()
246 encoded = getDerValue().getEncoded();
250 // -------------------------------------------------------------------------
252 public static class Value
256 // -----------------------------------------------------------------------
258 protected byte[] encoded;
261 // -----------------------------------------------------------------------
263 public Value(byte[] encoded)
265 this.encoded = (byte[]) encoded.clone();
268 protected Value() { }
271 // -----------------------------------------------------------------------
273 public byte[] getEncoded()
275 return (byte[]) encoded;
278 public int hashCode()
281 for (int i = 0; i < encoded.length; ++i)
282 result = result * 31 + encoded[i];
286 public boolean equals(Object o)
288 if (!(o instanceof Value))
290 return Arrays.equals(encoded, ((Value) o).encoded);
293 public String toString()
295 return Util.toHexString(encoded, ':');