001: /*
002: * @(#)IssuerAlternativeNameExtension.java 1.19 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:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.lang.reflect.Array;
034: import java.util.Enumeration;
035:
036: import sun.security.util.*;
037:
038: /**
039: * This represents the Issuer Alternative Name Extension.
040: *
041: * This extension, if present, allows the issuer to specify multiple
042: * alternative names.
043: *
044: * <p>Extensions are represented as a sequence of the extension identifier
045: * (Object Identifier), a boolean flag stating whether the extension is to
046: * be treated as being critical and the extension value itself (this is again
047: * a DER encoding of the extension value).
048: *
049: * @author Amit Kapoor
050: * @author Hemma Prafullchandra
051: * @version 1.12
052: * @see Extension
053: * @see CertAttrSet
054: */
055: public class IssuerAlternativeNameExtension extends Extension implements
056: CertAttrSet {
057: /**
058: * Identifier for this attribute, to be used with the
059: * get, set, delete methods of Certificate, x509 type.
060: */
061: public static final String IDENT = "x509.info.extensions.IssuerAlternativeName";
062: /**
063: * Attribute names.
064: */
065: public static final String NAME = "IssuerAlternativeName";
066: public static final String ISSUER_NAME = "issuer_name";
067:
068: // private data members
069: GeneralNames names = null;
070:
071: // Encode this extension
072: private void encodeThis() throws IOException {
073: if (names == null || names.isEmpty()) {
074: this .extensionValue = null;
075: return;
076: }
077: DerOutputStream os = new DerOutputStream();
078: names.encode(os);
079: this .extensionValue = os.toByteArray();
080: }
081:
082: /**
083: * Create a IssuerAlternativeNameExtension with the passed GeneralNames.
084: *
085: * @param names the GeneralNames for the issuer.
086: * @exception IOException on error.
087: */
088: public IssuerAlternativeNameExtension(GeneralNames names)
089: throws IOException {
090: this .names = names;
091: this .extensionId = PKIXExtensions.IssuerAlternativeName_Id;
092: this .critical = false;
093: encodeThis();
094: }
095:
096: /**
097: * Create a default IssuerAlternativeNameExtension.
098: */
099: public IssuerAlternativeNameExtension() {
100: extensionId = PKIXExtensions.IssuerAlternativeName_Id;
101: critical = false;
102: names = new GeneralNames();
103: }
104:
105: /**
106: * Create the extension from the passed DER encoded value.
107: *
108: * @param critical true if the extension is to be treated as critical.
109: * @param value Array of DER encoded bytes of the actual value.
110: * @exception IOException on error.
111: */
112: public IssuerAlternativeNameExtension(Boolean critical, Object value)
113: throws IOException {
114: this .extensionId = PKIXExtensions.IssuerAlternativeName_Id;
115: this .critical = critical.booleanValue();
116:
117: int len = Array.getLength(value);
118: byte[] extValue = new byte[len];
119: for (int i = 0; i < len; i++) {
120: extValue[i] = Array.getByte(value, i);
121: }
122: this .extensionValue = extValue;
123: DerValue val = new DerValue(extValue);
124: if (val.data == null) {
125: names = new GeneralNames();
126: return;
127: }
128:
129: names = new GeneralNames(val);
130: }
131:
132: /**
133: * Returns a printable representation of the IssuerAlternativeName.
134: */
135: public String toString() {
136: return super .toString() + "IssuerAlternativeName [\n"
137: + String.valueOf(names) + "]\n";
138: }
139:
140: /**
141: * Decode the extension from the InputStream.
142: *
143: * @param in the InputStream to unmarshal the contents from.
144: * @exception IOException on decoding or validity errors.
145: */
146: public void decode(InputStream in) throws IOException {
147: throw new IOException("Method not to be called directly.");
148: }
149:
150: /**
151: * Write the extension to the OutputStream.
152: *
153: * @param out the OutputStream to write the extension to.
154: * @exception IOException on encoding error.
155: */
156: public void encode(OutputStream out) throws IOException {
157: DerOutputStream tmp = new DerOutputStream();
158: if (extensionValue == null) {
159: extensionId = PKIXExtensions.IssuerAlternativeName_Id;
160: critical = false;
161: encodeThis();
162: }
163: super .encode(tmp);
164: out.write(tmp.toByteArray());
165: }
166:
167: /**
168: * Set the attribute value.
169: */
170: public void set(String name, Object obj) throws IOException {
171: if (name.equalsIgnoreCase(ISSUER_NAME)) {
172: if (!(obj instanceof GeneralNames)) {
173: throw new IOException("Attribute value should be of"
174: + " type GeneralNames.");
175: }
176: names = (GeneralNames) obj;
177: } else {
178: throw new IOException("Attribute name not recognized by "
179: + "CertAttrSet:IssuerAlternativeName.");
180: }
181: encodeThis();
182: }
183:
184: /**
185: * Get the attribute value.
186: */
187: public Object get(String name) throws IOException {
188: if (name.equalsIgnoreCase(ISSUER_NAME)) {
189: return (names);
190: } else {
191: throw new IOException("Attribute name not recognized by "
192: + "CertAttrSet:IssuerAlternativeName.");
193: }
194: }
195:
196: /**
197: * Delete the attribute value.
198: */
199: public void delete(String name) throws IOException {
200: if (name.equalsIgnoreCase(ISSUER_NAME)) {
201: names = null;
202: } else {
203: throw new IOException("Attribute name not recognized by "
204: + "CertAttrSet:IssuerAlternativeName.");
205: }
206: encodeThis();
207: }
208:
209: /**
210: * Return an enumeration of names of attributes existing within this
211: * attribute.
212: */
213: public Enumeration getElements() {
214: AttributeNameEnumeration elements = new AttributeNameEnumeration();
215: elements.addElement(ISSUER_NAME);
216:
217: return (elements.elements());
218: }
219:
220: /**
221: * Return the name of this attribute.
222: */
223: public String getName() {
224: return (NAME);
225: }
226: }
|