Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/x509/IssuerAlternativeNameExtension.java
41159 views
1
/*
2
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
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
/**
93
* Create a IssuerAlternativeNameExtension with the passed criticality
94
* and GeneralNames.
95
*
96
* @param critical true if the extension is to be treated as critical.
97
* @param names the GeneralNames for the issuer.
98
* @exception IOException on error.
99
*/
100
public IssuerAlternativeNameExtension(Boolean critical, GeneralNames names)
101
throws IOException {
102
this.names = names;
103
this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;
104
this.critical = critical.booleanValue();
105
encodeThis();
106
}
107
108
/**
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
StringBuilder sb = new StringBuilder();
144
sb.append(super.toString())
145
.append("IssuerAlternativeName [\n");
146
if (names == null) {
147
sb.append(" null\n");
148
} else {
149
for (GeneralName name : names.names()) {
150
sb.append(" ")
151
.append(name)
152
.append('\n');
153
}
154
}
155
sb.append("]\n");
156
return sb.toString();
157
}
158
159
/**
160
* Write the extension to the OutputStream.
161
*
162
* @param out the OutputStream to write the extension to.
163
* @exception IOException on encoding error.
164
*/
165
public void encode(OutputStream out) throws IOException {
166
DerOutputStream tmp = new DerOutputStream();
167
if (extensionValue == null) {
168
extensionId = PKIXExtensions.IssuerAlternativeName_Id;
169
critical = false;
170
encodeThis();
171
}
172
super.encode(tmp);
173
out.write(tmp.toByteArray());
174
}
175
176
/**
177
* Set the attribute value.
178
*/
179
public void set(String name, Object obj) throws IOException {
180
if (name.equalsIgnoreCase(ISSUER_NAME)) {
181
if (!(obj instanceof GeneralNames)) {
182
throw new IOException("Attribute value should be of" +
183
" type GeneralNames.");
184
}
185
names = (GeneralNames)obj;
186
} else {
187
throw new IOException("Attribute name not recognized by " +
188
"CertAttrSet:IssuerAlternativeName.");
189
}
190
encodeThis();
191
}
192
193
/**
194
* Get the attribute value.
195
*/
196
public GeneralNames get(String name) throws IOException {
197
if (name.equalsIgnoreCase(ISSUER_NAME)) {
198
return (names);
199
} else {
200
throw new IOException("Attribute name not recognized by " +
201
"CertAttrSet:IssuerAlternativeName.");
202
}
203
}
204
205
/**
206
* Delete the attribute value.
207
*/
208
public void delete(String name) throws IOException {
209
if (name.equalsIgnoreCase(ISSUER_NAME)) {
210
names = null;
211
} else {
212
throw new IOException("Attribute name not recognized by " +
213
"CertAttrSet:IssuerAlternativeName.");
214
}
215
encodeThis();
216
}
217
218
/**
219
* Return an enumeration of names of attributes existing within this
220
* attribute.
221
*/
222
public Enumeration<String> getElements() {
223
AttributeNameEnumeration elements = new AttributeNameEnumeration();
224
elements.addElement(ISSUER_NAME);
225
226
return (elements.elements());
227
}
228
229
/**
230
* Return the name of this attribute.
231
*/
232
public String getName() {
233
return (NAME);
234
}
235
}
236
237