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/ExtendedKeyUsageExtension.java
41159 views
1
/*
2
* Copyright (c) 2000, 2020, 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.ArrayList;
31
import java.util.Enumeration;
32
import java.util.HashMap;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Vector;
36
37
import sun.security.util.*;
38
39
/**
40
* This class defines the Extended Key Usage Extension, which
41
* indicates one or more purposes for which the certified public key
42
* may be used, in addition to or in place of the basic purposes
43
* indicated in the key usage extension field. This field is defined
44
* as follows:<p>
45
*
46
* id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}<p>
47
*
48
* ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId<p>
49
*
50
* KeyPurposeId ::= OBJECT IDENTIFIER<p>
51
*
52
* Key purposes may be defined by any organization with a need. Object
53
* identifiers used to identify key purposes shall be assigned in
54
* accordance with IANA or ITU-T Rec. X.660 | ISO/IEC/ITU 9834-1.<p>
55
*
56
* This extension may, at the option of the certificate issuer, be
57
* either critical or non-critical.<p>
58
*
59
* If the extension is flagged critical, then the certificate MUST be
60
* used only for one of the purposes indicated.<p>
61
*
62
* If the extension is flagged non-critical, then it indicates the
63
* intended purpose or purposes of the key, and may be used in finding
64
* the correct key/certificate of an entity that has multiple
65
* keys/certificates. It is an advisory field and does not imply that
66
* usage of the key is restricted by the certification authority to
67
* the purpose indicated. Certificate using applications may
68
* nevertheless require that a particular purpose be indicated in
69
* order for the certificate to be acceptable to that application.<p>
70
*
71
* If a certificate contains both a critical key usage field and a
72
* critical extended key usage field, then both fields MUST be
73
* processed independently and the certificate MUST only be used for a
74
* purpose consistent with both fields. If there is no purpose
75
* consistent with both fields, then the certificate MUST NOT be used
76
* for any purpose.
77
*
78
* @since 1.4
79
*/
80
public class ExtendedKeyUsageExtension extends Extension
81
implements CertAttrSet<String> {
82
83
/**
84
* Identifier for this attribute, to be used with the
85
* get, set, delete methods of Certificate, x509 type.
86
*/
87
public static final String IDENT = "x509.info.extensions.ExtendedKeyUsage";
88
89
/**
90
* Attribute names.
91
*/
92
public static final String NAME = "ExtendedKeyUsage";
93
public static final String USAGES = "usages";
94
95
/**
96
* Vector of KeyUsages for this object.
97
*/
98
private Vector<ObjectIdentifier> keyUsages;
99
100
// Encode this extension value.
101
private void encodeThis() throws IOException {
102
if (keyUsages == null || keyUsages.isEmpty()) {
103
this.extensionValue = null;
104
return;
105
}
106
DerOutputStream os = new DerOutputStream();
107
DerOutputStream tmp = new DerOutputStream();
108
109
for (int i = 0; i < keyUsages.size(); i++) {
110
tmp.putOID(keyUsages.elementAt(i));
111
}
112
113
os.write(DerValue.tag_Sequence, tmp);
114
this.extensionValue = os.toByteArray();
115
}
116
117
/**
118
* Create a ExtendedKeyUsageExtension object from
119
* a Vector of Key Usages; the criticality is set to false.
120
*
121
* @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
122
*/
123
public ExtendedKeyUsageExtension(Vector<ObjectIdentifier> keyUsages)
124
throws IOException {
125
this(Boolean.FALSE, keyUsages);
126
}
127
128
/**
129
* Create a ExtendedKeyUsageExtension object from
130
* a Vector of KeyUsages with specified criticality.
131
*
132
* @param critical true if the extension is to be treated as critical.
133
* @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
134
*/
135
public ExtendedKeyUsageExtension(Boolean critical, Vector<ObjectIdentifier> keyUsages)
136
throws IOException {
137
this.keyUsages = keyUsages;
138
this.extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
139
this.critical = critical.booleanValue();
140
encodeThis();
141
}
142
143
/**
144
* Create the extension from its DER encoded value and criticality.
145
*
146
* @param critical true if the extension is to be treated as critical.
147
* @param value an array of DER encoded bytes of the actual value.
148
* @exception ClassCastException if value is not an array of bytes
149
* @exception IOException on error.
150
*/
151
public ExtendedKeyUsageExtension(Boolean critical, Object value)
152
throws IOException {
153
this.extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
154
this.critical = critical.booleanValue();
155
this.extensionValue = (byte[]) value;
156
DerValue val = new DerValue(this.extensionValue);
157
if (val.tag != DerValue.tag_Sequence) {
158
throw new IOException("Invalid encoding for " +
159
"ExtendedKeyUsageExtension.");
160
}
161
keyUsages = new Vector<ObjectIdentifier>();
162
while (val.data.available() != 0) {
163
DerValue seq = val.data.getDerValue();
164
ObjectIdentifier usage = seq.getOID();
165
keyUsages.addElement(usage);
166
}
167
}
168
169
/**
170
* Return the extension as user readable string.
171
*/
172
public String toString() {
173
if (keyUsages == null) return "";
174
String usage = " ";
175
boolean first = true;
176
for (ObjectIdentifier oid: keyUsages) {
177
if(!first) {
178
usage += "\n ";
179
}
180
181
String res = oid.toString();
182
KnownOIDs os = KnownOIDs.findMatch(res);
183
if (os != null) {
184
usage += os.stdName();
185
} else {
186
usage += res;
187
}
188
first = false;
189
}
190
return super.toString() + "ExtendedKeyUsages [\n"
191
+ usage + "\n]\n";
192
}
193
194
/**
195
* Write the extension to the DerOutputStream.
196
*
197
* @param out the DerOutputStream to write the extension to.
198
* @exception IOException on encoding errors.
199
*/
200
public void encode(OutputStream out) throws IOException {
201
DerOutputStream tmp = new DerOutputStream();
202
if (extensionValue == null) {
203
extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
204
critical = false;
205
encodeThis();
206
}
207
super.encode(tmp);
208
out.write(tmp.toByteArray());
209
}
210
211
/**
212
* Set the attribute value.
213
*/
214
@SuppressWarnings("unchecked") // Checked with instanceof
215
public void set(String name, Object obj) throws IOException {
216
if (name.equalsIgnoreCase(USAGES)) {
217
if (!(obj instanceof Vector)) {
218
throw new IOException("Attribute value should be of type Vector.");
219
}
220
this.keyUsages = (Vector<ObjectIdentifier>)obj;
221
} else {
222
throw new IOException("Attribute name [" + name +
223
"] not recognized by " +
224
"CertAttrSet:ExtendedKeyUsageExtension.");
225
}
226
encodeThis();
227
}
228
229
/**
230
* Get the attribute value.
231
*/
232
public Vector<ObjectIdentifier> get(String name) throws IOException {
233
if (name.equalsIgnoreCase(USAGES)) {
234
//XXXX May want to consider cloning this
235
return keyUsages;
236
} else {
237
throw new IOException("Attribute name [" + name +
238
"] not recognized by " +
239
"CertAttrSet:ExtendedKeyUsageExtension.");
240
}
241
}
242
243
/**
244
* Delete the attribute value.
245
*/
246
public void delete(String name) throws IOException {
247
if (name.equalsIgnoreCase(USAGES)) {
248
keyUsages = null;
249
} else {
250
throw new IOException("Attribute name [" + name +
251
"] not recognized by " +
252
"CertAttrSet:ExtendedKeyUsageExtension.");
253
}
254
encodeThis();
255
}
256
257
/**
258
* Return an enumeration of names of attributes existing within this
259
* attribute.
260
*/
261
public Enumeration<String> getElements() {
262
AttributeNameEnumeration elements = new AttributeNameEnumeration();
263
elements.addElement(USAGES);
264
265
return (elements.elements());
266
}
267
268
/**
269
* Return the name of this attribute.
270
*/
271
public String getName() {
272
return (NAME);
273
}
274
275
public List<String> getExtendedKeyUsage() {
276
List<String> al = new ArrayList<String>(keyUsages.size());
277
for (ObjectIdentifier oid : keyUsages) {
278
al.add(oid.toString());
279
}
280
return al;
281
}
282
283
}
284
285