blob: aad153b646a5a768e2c96c21c5b99ef588f55408 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.x509;
27
28import java.io.IOException;
29import java.io.OutputStream;
30import java.util.Enumeration;
31
32import sun.security.util.*;
33
34/**
35 * This represents the Issuer Alternative Name Extension.
36 *
37 * This extension, if present, allows the issuer to specify multiple
38 * alternative names.
39 *
40 * <p>Extensions are represented as a sequence of the extension identifier
41 * (Object Identifier), a boolean flag stating whether the extension is to
42 * be treated as being critical and the extension value itself (this is again
43 * a DER encoding of the extension value).
44 *
45 * @author Amit Kapoor
46 * @author Hemma Prafullchandra
47 * @see Extension
48 * @see CertAttrSet
49 */
50public class IssuerAlternativeNameExtension
51extends Extension implements CertAttrSet<String> {
52 /**
53 * Identifier for this attribute, to be used with the
54 * get, set, delete methods of Certificate, x509 type.
55 */
56 public static final String IDENT =
57 "x509.info.extensions.IssuerAlternativeName";
58 /**
59 * Attribute names.
60 */
61 public static final String NAME = "IssuerAlternativeName";
62 public static final String ISSUER_NAME = "issuer_name";
63
64 // private data members
65 GeneralNames names = null;
66
67 // Encode this extension
68 private void encodeThis() throws IOException {
69 if (names == null || names.isEmpty()) {
70 this.extensionValue = null;
71 return;
72 }
73 DerOutputStream os = new DerOutputStream();
74 names.encode(os);
75 this.extensionValue = os.toByteArray();
76 }
77
78 /**
79 * Create a IssuerAlternativeNameExtension with the passed GeneralNames.
80 *
81 * @param names the GeneralNames for the issuer.
82 * @exception IOException on error.
83 */
84 public IssuerAlternativeNameExtension(GeneralNames names)
85 throws IOException {
86 this.names = names;
87 this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;
88 this.critical = false;
89 encodeThis();
90 }
91
92 /**
93 * Create a default IssuerAlternativeNameExtension.
94 */
95 public IssuerAlternativeNameExtension() {
96 extensionId = PKIXExtensions.IssuerAlternativeName_Id;
97 critical = false;
98 names = new GeneralNames();
99 }
100
101 /**
102 * Create the extension from the passed DER encoded value.
103 *
104 * @param critical true if the extension is to be treated as critical.
105 * @param value an array of DER encoded bytes of the actual value.
106 * @exception ClassCastException if value is not an array of bytes
107 * @exception IOException on error.
108 */
109 public IssuerAlternativeNameExtension(Boolean critical, Object value)
110 throws IOException {
111 this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;
112 this.critical = critical.booleanValue();
113 this.extensionValue = (byte[]) value;
114 DerValue val = new DerValue(this.extensionValue);
115 if (val.data == null) {
116 names = new GeneralNames();
117 return;
118 }
119
120 names = new GeneralNames(val);
121 }
122
123 /**
124 * Returns a printable representation of the IssuerAlternativeName.
125 */
126 public String toString() {
127
128 String result = super.toString() + "IssuerAlternativeName [\n";
129 if(names == null) {
130 result += " null\n";
131 } else {
132 for(GeneralName name: names.names()) {
133 result += " "+name+"\n";
134 }
135 }
136 result += "]\n";
137 return result;
138 }
139
140 /**
141 * Write the extension to the OutputStream.
142 *
143 * @param out the OutputStream to write the extension to.
144 * @exception IOException on encoding error.
145 */
146 public void encode(OutputStream out) throws IOException {
147 DerOutputStream tmp = new DerOutputStream();
148 if (extensionValue == null) {
149 extensionId = PKIXExtensions.IssuerAlternativeName_Id;
150 critical = false;
151 encodeThis();
152 }
153 super.encode(tmp);
154 out.write(tmp.toByteArray());
155 }
156
157 /**
158 * Set the attribute value.
159 */
160 public void set(String name, Object obj) throws IOException {
161 if (name.equalsIgnoreCase(ISSUER_NAME)) {
162 if (!(obj instanceof GeneralNames)) {
163 throw new IOException("Attribute value should be of" +
164 " type GeneralNames.");
165 }
166 names = (GeneralNames)obj;
167 } else {
168 throw new IOException("Attribute name not recognized by " +
169 "CertAttrSet:IssuerAlternativeName.");
170 }
171 encodeThis();
172 }
173
174 /**
175 * Get the attribute value.
176 */
177 public Object get(String name) throws IOException {
178 if (name.equalsIgnoreCase(ISSUER_NAME)) {
179 return (names);
180 } else {
181 throw new IOException("Attribute name not recognized by " +
182 "CertAttrSet:IssuerAlternativeName.");
183 }
184 }
185
186 /**
187 * Delete the attribute value.
188 */
189 public void delete(String name) throws IOException {
190 if (name.equalsIgnoreCase(ISSUER_NAME)) {
191 names = null;
192 } else {
193 throw new IOException("Attribute name not recognized by " +
194 "CertAttrSet:IssuerAlternativeName.");
195 }
196 encodeThis();
197 }
198
199 /**
200 * Return an enumeration of names of attributes existing within this
201 * attribute.
202 */
203 public Enumeration<String> getElements() {
204 AttributeNameEnumeration elements = new AttributeNameEnumeration();
205 elements.addElement(ISSUER_NAME);
206
207 return (elements.elements());
208 }
209
210 /**
211 * Return the name of this attribute.
212 */
213 public String getName() {
214 return (NAME);
215 }
216}