author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.x509; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.util.Enumeration; |
|
31 |
||
32 |
import 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 |
*/ |
|
50 |
public class IssuerAlternativeNameExtension |
|
51 |
extends 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 |
/** |
|
2067
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
93 |
* Create a IssuerAlternativeNameExtension with the passed criticality |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
94 |
* and GeneralNames. |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
95 |
* |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
96 |
* @param critical true if the extension is to be treated as critical. |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
97 |
* @param names the GeneralNames for the issuer. |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
98 |
* @exception IOException on error. |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
99 |
*/ |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
100 |
public IssuerAlternativeNameExtension(Boolean critical, GeneralNames names) |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
101 |
throws IOException { |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
102 |
this.names = names; |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
103 |
this.extensionId = PKIXExtensions.IssuerAlternativeName_Id; |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
104 |
this.critical = critical.booleanValue(); |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
105 |
encodeThis(); |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
106 |
} |
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
107 |
|
6f9db5f305cd
6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents:
2
diff
changeset
|
108 |
/** |
2 | 109 |
* Create a default IssuerAlternativeNameExtension. |
110 |
*/ |
|
111 |
public IssuerAlternativeNameExtension() { |
|
112 |
extensionId = PKIXExtensions.IssuerAlternativeName_Id; |
|
113 |
critical = false; |
|
114 |
names = new GeneralNames(); |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Create the extension from the passed DER encoded value. |
|
119 |
* |
|
120 |
* @param critical true if the extension is to be treated as critical. |
|
121 |
* @param value an array of DER encoded bytes of the actual value. |
|
122 |
* @exception ClassCastException if value is not an array of bytes |
|
123 |
* @exception IOException on error. |
|
124 |
*/ |
|
125 |
public IssuerAlternativeNameExtension(Boolean critical, Object value) |
|
126 |
throws IOException { |
|
127 |
this.extensionId = PKIXExtensions.IssuerAlternativeName_Id; |
|
128 |
this.critical = critical.booleanValue(); |
|
129 |
this.extensionValue = (byte[]) value; |
|
130 |
DerValue val = new DerValue(this.extensionValue); |
|
131 |
if (val.data == null) { |
|
132 |
names = new GeneralNames(); |
|
133 |
return; |
|
134 |
} |
|
135 |
||
136 |
names = new GeneralNames(val); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Returns a printable representation of the IssuerAlternativeName. |
|
141 |
*/ |
|
142 |
public String toString() { |
|
143 |
||
144 |
String result = super.toString() + "IssuerAlternativeName [\n"; |
|
145 |
if(names == null) { |
|
146 |
result += " null\n"; |
|
147 |
} else { |
|
148 |
for(GeneralName name: names.names()) { |
|
149 |
result += " "+name+"\n"; |
|
150 |
} |
|
151 |
} |
|
152 |
result += "]\n"; |
|
153 |
return result; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Write the extension to the OutputStream. |
|
158 |
* |
|
159 |
* @param out the OutputStream to write the extension to. |
|
160 |
* @exception IOException on encoding error. |
|
161 |
*/ |
|
162 |
public void encode(OutputStream out) throws IOException { |
|
163 |
DerOutputStream tmp = new DerOutputStream(); |
|
164 |
if (extensionValue == null) { |
|
165 |
extensionId = PKIXExtensions.IssuerAlternativeName_Id; |
|
166 |
critical = false; |
|
167 |
encodeThis(); |
|
168 |
} |
|
169 |
super.encode(tmp); |
|
170 |
out.write(tmp.toByteArray()); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Set the attribute value. |
|
175 |
*/ |
|
176 |
public void set(String name, Object obj) throws IOException { |
|
177 |
if (name.equalsIgnoreCase(ISSUER_NAME)) { |
|
178 |
if (!(obj instanceof GeneralNames)) { |
|
179 |
throw new IOException("Attribute value should be of" + |
|
180 |
" type GeneralNames."); |
|
181 |
} |
|
182 |
names = (GeneralNames)obj; |
|
183 |
} else { |
|
184 |
throw new IOException("Attribute name not recognized by " + |
|
185 |
"CertAttrSet:IssuerAlternativeName."); |
|
186 |
} |
|
187 |
encodeThis(); |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Get the attribute value. |
|
192 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
193 |
public GeneralNames get(String name) throws IOException { |
2 | 194 |
if (name.equalsIgnoreCase(ISSUER_NAME)) { |
195 |
return (names); |
|
196 |
} else { |
|
197 |
throw new IOException("Attribute name not recognized by " + |
|
198 |
"CertAttrSet:IssuerAlternativeName."); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Delete the attribute value. |
|
204 |
*/ |
|
205 |
public void delete(String name) throws IOException { |
|
206 |
if (name.equalsIgnoreCase(ISSUER_NAME)) { |
|
207 |
names = null; |
|
208 |
} else { |
|
209 |
throw new IOException("Attribute name not recognized by " + |
|
210 |
"CertAttrSet:IssuerAlternativeName."); |
|
211 |
} |
|
212 |
encodeThis(); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Return an enumeration of names of attributes existing within this |
|
217 |
* attribute. |
|
218 |
*/ |
|
219 |
public Enumeration<String> getElements() { |
|
220 |
AttributeNameEnumeration elements = new AttributeNameEnumeration(); |
|
221 |
elements.addElement(ISSUER_NAME); |
|
222 |
||
223 |
return (elements.elements()); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Return the name of this attribute. |
|
228 |
*/ |
|
229 |
public String getName() { |
|
230 |
return (NAME); |
|
231 |
} |
|
232 |
} |