001: /*
002: * @(#)ExtendedKeyUsageExtension.java 1.8 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.security.x509;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032: import java.lang.reflect.Array;
033: import java.util.Enumeration;
034: import java.util.Vector;
035: import java.util.List;
036: import java.util.ArrayList;
037:
038: import sun.security.util.DerValue;
039: import sun.security.util.DerOutputStream;
040: import sun.security.util.ObjectIdentifier;
041:
042: /**
043: * This class defines the Extended Key Usage Extension, which
044: * indicates one or more purposes for which the certified public key
045: * may be used, in addition to or in place of the basic purposes
046: * indicated in the key usage extension field. This field is defined
047: * as follows:<p>
048: *
049: * id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}<p>
050: *
051: * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId<p>
052: *
053: * KeyPurposeId ::= OBJECT IDENTIFIER<p>
054: *
055: * Key purposes may be defined by any organization with a need. Object
056: * identifiers used to identify key purposes shall be assigned in
057: * accordance with IANA or ITU-T Rec. X.660 | ISO/IEC/ITU 9834-1.<p>
058: *
059: * This extension may, at the option of the certificate issuer, be
060: * either critical or non-critical.<p>
061: *
062: * If the extension is flagged critical, then the certificate MUST be
063: * used only for one of the purposes indicated.<p>
064: *
065: * If the extension is flagged non-critical, then it indicates the
066: * intended purpose or purposes of the key, and may be used in finding
067: * the correct key/certificate of an entity that has multiple
068: * keys/certificates. It is an advisory field and does not imply that
069: * usage of the key is restricted by the certification authority to
070: * the purpose indicated. Certificate using applications may
071: * nevertheless require that a particular purpose be indicated in
072: * order for the certificate to be acceptable to that application.<p>
073:
074: * If a certificate contains both a critical key usage field and a
075: * critical extended key usage field, then both fields MUST be
076: * processed independently and the certificate MUST only be used for a
077: * purpose consistent with both fields. If there is no purpose
078: * consistent with both fields, then the certificate MUST NOT be used
079: * for any purpose.<p>
080: *
081: * @version 1.8, 10/10/06
082: * @since 1.4
083: */
084: public class ExtendedKeyUsageExtension extends Extension implements
085: CertAttrSet {
086:
087: /**
088: * Identifier for this attribute, to be used with the
089: * get, set, delete methods of Certificate, x509 type.
090: */
091: public static final String IDENT = "x509.info.extensions.ExtendedKeyUsage";
092:
093: /**
094: * Attribute names.
095: */
096: public static final String NAME = "ExtendedKeyUsage";
097: public static final String USAGES = "usages";
098:
099: /**
100: * Vector of KeyUsages for this object.
101: */
102: private Vector keyUsages;
103:
104: // Encode this extension value.
105: private void encodeThis() throws IOException {
106: if (keyUsages == null || keyUsages.isEmpty()) {
107: this .extensionValue = null;
108: return;
109: }
110: DerOutputStream os = new DerOutputStream();
111: DerOutputStream tmp = new DerOutputStream();
112:
113: for (int i = 0; i < keyUsages.size(); i++) {
114: tmp.putOID((ObjectIdentifier) keyUsages.elementAt(i));
115: }
116:
117: os.write(DerValue.tag_Sequence, tmp);
118: this .extensionValue = os.toByteArray();
119: }
120:
121: /**
122: * Create a ExtendedKeyUsageExtension object from
123: * a Vector of Key Usages; the criticality is set to false.
124: *
125: * @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
126: */
127: public ExtendedKeyUsageExtension(Vector keyUsages)
128: throws IOException {
129: this (Boolean.FALSE, keyUsages);
130: }
131:
132: /**
133: * Create a ExtendedKeyUsageExtension object from
134: * a Vector of KeyUsages with specified criticality.
135: *
136: * @param critical true if the extension is to be treated as critical.
137: * @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
138: */
139: public ExtendedKeyUsageExtension(Boolean critical, Vector keyUsages)
140: throws IOException {
141: this .keyUsages = keyUsages;
142: this .extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
143: this .critical = critical.booleanValue();
144: encodeThis();
145: }
146:
147: /**
148: * Create the extension from its DER encoded value and criticality.
149: *
150: * @param critical true if the extension is to be treated as critical.
151: * @param value Array of DER encoded bytes of the actual value.
152: * @exception IOException on error.
153: */
154: public ExtendedKeyUsageExtension(Boolean critical, Object value)
155: throws IOException {
156: this .extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
157: this .critical = critical.booleanValue();
158: //Array.getLength will throw an IllegalArgumentException if
159: //value is not an array, and Array.getByte will do so if the
160: //values are not bytes, so a type check is not needed.
161: int len = 0;
162: byte[] extValue = null;
163: try {
164: len = Array.getLength(value);
165: extValue = new byte[len];
166: for (int i = 0; i < len; i++)
167: extValue[i] = Array.getByte(value, i);
168: } catch (IllegalArgumentException iae) {
169: throw new IOException(iae.toString());
170: }
171: this .extensionValue = extValue;
172: DerValue val = new DerValue(extValue);
173: if (val.tag != DerValue.tag_Sequence) {
174: throw new IOException("Invalid encoding for "
175: + "ExtendedKeyUsageExtension.");
176: }
177: keyUsages = new Vector(1, 1);
178: while (val.data.available() != 0) {
179: DerValue seq = val.data.getDerValue();
180: ObjectIdentifier usage = seq.getOID();
181: keyUsages.addElement(usage);
182: }
183: }
184:
185: /**
186: * Return the extension as user readable string.
187: */
188: public String toString() {
189: if (keyUsages == null)
190: return "";
191: return super .toString() + "ExtendedKeyUsages [\n"
192: + keyUsages.toString() + "]\n";
193: }
194:
195: /**
196: * Decode the extension from the InputStream.
197: *
198: * @param in the InputStream to unmarshal the contents from.
199: * @exception IOException on decoding or validity errors.
200: */
201: public void decode(InputStream in) throws IOException {
202: throw new IOException("Method not to be called directly.");
203: }
204:
205: /**
206: * Write the extension to the DerOutputStream.
207: *
208: * @param out the DerOutputStream to write the extension to.
209: * @exception IOException on encoding errors.
210: */
211: public void encode(OutputStream out) throws IOException {
212: DerOutputStream tmp = new DerOutputStream();
213: if (extensionValue == null) {
214: extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
215: critical = false;
216: encodeThis();
217: }
218: super .encode(tmp);
219: out.write(tmp.toByteArray());
220: }
221:
222: /**
223: * Set the attribute value.
224: */
225: public void set(String name, Object obj) throws IOException {
226: if (name.equalsIgnoreCase(USAGES)) {
227: if (!(obj instanceof Vector)) {
228: throw new IOException(
229: "Attribute value should be of type Vector.");
230: }
231: this .keyUsages = (Vector) obj;
232: } else {
233: throw new IOException("Attribute name [" + name
234: + "] not recognized by "
235: + "CertAttrSet:ExtendedKeyUsageExtension.");
236: }
237: encodeThis();
238: }
239:
240: /**
241: * Get the attribute value.
242: */
243: public Object get(String name) throws IOException {
244: if (name.equalsIgnoreCase(USAGES)) {
245: //NOTE: May want to consider cloning this
246: return keyUsages;
247: } else {
248: throw new IOException("Attribute name [" + name
249: + "] not recognized by "
250: + "CertAttrSet:ExtendedKeyUsageExtension.");
251: }
252: }
253:
254: /**
255: * Delete the attribute value.
256: */
257: public void delete(String name) throws IOException {
258: if (name.equalsIgnoreCase(USAGES)) {
259: keyUsages = null;
260: } else {
261: throw new IOException("Attribute name [" + name
262: + "] not recognized by "
263: + "CertAttrSet:ExtendedKeyUsageExtension.");
264: }
265: encodeThis();
266: }
267:
268: /**
269: * Return an enumeration of names of attributes existing within this
270: * attribute.
271: */
272: public Enumeration getElements() {
273: AttributeNameEnumeration elements = new AttributeNameEnumeration();
274: elements.addElement(USAGES);
275:
276: return (elements.elements());
277: }
278:
279: /**
280: * Return the name of this attribute.
281: */
282: public String getName() {
283: return (NAME);
284: }
285:
286: public List getExtendedKeyUsage() {
287: ArrayList al = new ArrayList(keyUsages.size());
288: for (int i = 0; i < keyUsages.size(); i++) {
289: al.add(keyUsages.elementAt(i).toString());
290: }
291: return al;
292: }
293:
294: }
|