Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/security/Identity.java
41152 views
1
/*
2
* Copyright (c) 1996, 2019, 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 java.security;
27
28
import java.io.Serializable;
29
import java.util.*;
30
31
/**
32
* <p>This class represents identities: real-world objects such as people,
33
* companies or organizations whose identities can be authenticated using
34
* their public keys. Identities may also be more abstract (or concrete)
35
* constructs, such as daemon threads or smart cards.
36
*
37
* <p>All Identity objects have a name and a public key. Names are
38
* immutable. Identities may also be scoped. That is, if an Identity is
39
* specified to have a particular scope, then the name and public
40
* key of the Identity are unique within that scope.
41
*
42
* <p>An Identity also has a set of certificates (all certifying its own
43
* public key). The Principal names specified in these certificates need
44
* not be the same, only the key.
45
*
46
* <p>An Identity can be subclassed, to include postal and email addresses,
47
* telephone numbers, images of faces and logos, and so on.
48
*
49
* @see IdentityScope
50
* @see Signer
51
* @see Principal
52
*
53
* @author Benjamin Renaud
54
* @since 1.1
55
* @deprecated This class is deprecated and subject to removal in a future
56
* version of Java SE. It has been replaced by
57
* {@code java.security.KeyStore}, the {@code java.security.cert} package,
58
* and {@code java.security.Principal}.
59
*/
60
@Deprecated(since="1.2", forRemoval=true)
61
@SuppressWarnings("removal")
62
public abstract class Identity implements Principal, Serializable {
63
64
/** use serialVersionUID from JDK 1.1.x for interoperability */
65
@java.io.Serial
66
private static final long serialVersionUID = 3609922007826600659L;
67
68
/**
69
* The name for this identity.
70
*
71
* @serial
72
*/
73
private String name;
74
75
/**
76
* The public key for this identity.
77
*
78
* @serial
79
*/
80
private PublicKey publicKey;
81
82
/**
83
* Generic, descriptive information about the identity.
84
*
85
* @serial
86
*/
87
String info = "No further information available.";
88
89
/**
90
* The scope of the identity.
91
*
92
* @serial
93
*/
94
IdentityScope scope;
95
96
/**
97
* The certificates for this identity.
98
*
99
* @serial
100
*/
101
Vector<Certificate> certificates;
102
103
/**
104
* Constructor for serialization only.
105
*/
106
protected Identity() {
107
this("restoring...");
108
}
109
110
/**
111
* Constructs an identity with the specified name and scope.
112
*
113
* @param name the identity name.
114
* @param scope the scope of the identity.
115
*
116
* @throws KeyManagementException if there is already an identity
117
* with the same name in the scope.
118
*/
119
public Identity(String name, IdentityScope scope) throws
120
KeyManagementException {
121
this(name);
122
if (scope != null) {
123
scope.addIdentity(this);
124
}
125
this.scope = scope;
126
}
127
128
/**
129
* Constructs an identity with the specified name and no scope.
130
*
131
* @param name the identity name.
132
*/
133
public Identity(String name) {
134
this.name = name;
135
}
136
137
/**
138
* Returns this identity's name.
139
*
140
* @return the name of this identity.
141
*/
142
public final String getName() {
143
return name;
144
}
145
146
/**
147
* Returns this identity's scope.
148
*
149
* @return the scope of this identity.
150
*/
151
public final IdentityScope getScope() {
152
return scope;
153
}
154
155
/**
156
* Returns this identity's public key.
157
*
158
* @return the public key for this identity.
159
*
160
* @see #setPublicKey
161
*/
162
public PublicKey getPublicKey() {
163
return publicKey;
164
}
165
166
/**
167
* Sets this identity's public key. The old key and all of this
168
* identity's certificates are removed by this operation.
169
*
170
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
171
* method is called with {@code "setIdentityPublicKey"}
172
* as its argument to see if it's ok to set the public key.
173
*
174
* @param key the public key for this identity.
175
*
176
* @throws KeyManagementException if another identity in the
177
* identity's scope has the same public key, or if another exception occurs.
178
*
179
* @throws SecurityException if a security manager exists and its
180
* {@code checkSecurityAccess} method doesn't allow
181
* setting the public key.
182
*
183
* @see #getPublicKey
184
* @see SecurityManager#checkSecurityAccess
185
*/
186
/* Should we throw an exception if this is already set? */
187
public void setPublicKey(PublicKey key) throws KeyManagementException {
188
189
check("setIdentityPublicKey");
190
this.publicKey = key;
191
certificates = new Vector<>();
192
}
193
194
/**
195
* Specifies a general information string for this identity.
196
*
197
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
198
* method is called with {@code "setIdentityInfo"}
199
* as its argument to see if it's ok to specify the information string.
200
*
201
* @param info the information string.
202
*
203
* @throws SecurityException if a security manager exists and its
204
* {@code checkSecurityAccess} method doesn't allow
205
* setting the information string.
206
*
207
* @see #getInfo
208
* @see SecurityManager#checkSecurityAccess
209
*/
210
public void setInfo(String info) {
211
check("setIdentityInfo");
212
this.info = info;
213
}
214
215
/**
216
* Returns general information previously specified for this identity.
217
*
218
* @return general information about this identity.
219
*
220
* @see #setInfo
221
*/
222
public String getInfo() {
223
return info;
224
}
225
226
/**
227
* Adds a certificate for this identity. If the identity has a public
228
* key, the public key in the certificate must be the same, and if
229
* the identity does not have a public key, the identity's
230
* public key is set to be that specified in the certificate.
231
*
232
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
233
* method is called with {@code "addIdentityCertificate"}
234
* as its argument to see if it's ok to add a certificate.
235
*
236
* @param certificate the certificate to be added.
237
*
238
* @throws KeyManagementException if the certificate is not valid,
239
* if the public key in the certificate being added conflicts with
240
* this identity's public key, or if another exception occurs.
241
*
242
* @throws SecurityException if a security manager exists and its
243
* {@code checkSecurityAccess} method doesn't allow
244
* adding a certificate.
245
*
246
* @see SecurityManager#checkSecurityAccess
247
*/
248
public void addCertificate(Certificate certificate)
249
throws KeyManagementException {
250
251
check("addIdentityCertificate");
252
253
if (certificates == null) {
254
certificates = new Vector<>();
255
}
256
if (publicKey != null) {
257
if (!keyEquals(publicKey, certificate.getPublicKey())) {
258
throw new KeyManagementException(
259
"public key different from cert public key");
260
}
261
} else {
262
publicKey = certificate.getPublicKey();
263
}
264
certificates.addElement(certificate);
265
}
266
267
private boolean keyEquals(PublicKey aKey, PublicKey anotherKey) {
268
String aKeyFormat = aKey.getFormat();
269
String anotherKeyFormat = anotherKey.getFormat();
270
if ((aKeyFormat == null) ^ (anotherKeyFormat == null))
271
return false;
272
if (aKeyFormat != null && anotherKeyFormat != null)
273
if (!aKeyFormat.equalsIgnoreCase(anotherKeyFormat))
274
return false;
275
return java.util.Arrays.equals(aKey.getEncoded(),
276
anotherKey.getEncoded());
277
}
278
279
280
/**
281
* Removes a certificate from this identity.
282
*
283
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
284
* method is called with {@code "removeIdentityCertificate"}
285
* as its argument to see if it's ok to remove a certificate.
286
*
287
* @param certificate the certificate to be removed.
288
*
289
* @throws KeyManagementException if the certificate is
290
* missing, or if another exception occurs.
291
*
292
* @throws SecurityException if a security manager exists and its
293
* {@code checkSecurityAccess} method doesn't allow
294
* removing a certificate.
295
*
296
* @see SecurityManager#checkSecurityAccess
297
*/
298
public void removeCertificate(Certificate certificate)
299
throws KeyManagementException {
300
check("removeIdentityCertificate");
301
if (certificates != null) {
302
certificates.removeElement(certificate);
303
}
304
}
305
306
/**
307
* Returns a copy of all the certificates for this identity.
308
*
309
* @return a copy of all the certificates for this identity.
310
*/
311
public Certificate[] certificates() {
312
if (certificates == null) {
313
return new Certificate[0];
314
}
315
int len = certificates.size();
316
Certificate[] certs = new Certificate[len];
317
certificates.copyInto(certs);
318
return certs;
319
}
320
321
/**
322
* Tests for equality between the specified object and this identity.
323
* This first tests to see if the entities actually refer to the same
324
* object, in which case it returns true. Next, it checks to see if
325
* the entities have the same name and the same scope. If they do,
326
* the method returns true. Otherwise, it calls
327
* {@link #identityEquals(Identity) identityEquals}, which subclasses should
328
* override.
329
*
330
* @param identity the object to test for equality with this identity.
331
*
332
* @return true if the objects are considered equal, false otherwise.
333
*
334
* @see #identityEquals
335
*/
336
public final boolean equals(Object identity) {
337
if (identity == this) {
338
return true;
339
}
340
341
return identity instanceof Identity other
342
&& (this.fullName().equals(other.fullName()) || identityEquals(other));
343
}
344
345
/**
346
* Tests for equality between the specified identity and this identity.
347
* This method should be overridden by subclasses to test for equality.
348
* The default behavior is to return true if the names and public keys
349
* are equal.
350
*
351
* @param identity the identity to test for equality with this identity.
352
*
353
* @return true if the identities are considered equal, false
354
* otherwise.
355
*
356
* @see #equals
357
*/
358
protected boolean identityEquals(Identity identity) {
359
if (!name.equalsIgnoreCase(identity.name))
360
return false;
361
362
if ((publicKey == null) ^ (identity.publicKey == null))
363
return false;
364
365
if (publicKey != null && identity.publicKey != null)
366
if (!publicKey.equals(identity.publicKey))
367
return false;
368
369
return true;
370
371
}
372
373
/**
374
* Returns a parsable name for identity: identityName.scopeName
375
*/
376
String fullName() {
377
String parsable = name;
378
if (scope != null) {
379
parsable += "." + scope.getName();
380
}
381
return parsable;
382
}
383
384
/**
385
* Returns a short string describing this identity, telling its
386
* name and its scope (if any).
387
*
388
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
389
* method is called with {@code "printIdentity"}
390
* as its argument to see if it's ok to return the string.
391
*
392
* @return information about this identity, such as its name and the
393
* name of its scope (if any).
394
*
395
* @throws SecurityException if a security manager exists and its
396
* {@code checkSecurityAccess} method doesn't allow
397
* returning a string describing this identity.
398
*
399
* @see SecurityManager#checkSecurityAccess
400
*/
401
public String toString() {
402
check("printIdentity");
403
String printable = name;
404
if (scope != null) {
405
printable += "[" + scope.getName() + "]";
406
}
407
return printable;
408
}
409
410
/**
411
* Returns a string representation of this identity, with
412
* optionally more details than that provided by the
413
* {@code toString} method without any arguments.
414
*
415
* <p>First, if there is a security manager, its {@code checkSecurityAccess}
416
* method is called with {@code "printIdentity"}
417
* as its argument to see if it's ok to return the string.
418
*
419
* @param detailed whether or not to provide detailed information.
420
*
421
* @return information about this identity. If {@code detailed}
422
* is true, then this method returns more information than that
423
* provided by the {@code toString} method without any arguments.
424
*
425
* @throws SecurityException if a security manager exists and its
426
* {@code checkSecurityAccess} method doesn't allow
427
* returning a string describing this identity.
428
*
429
* @see #toString
430
* @see SecurityManager#checkSecurityAccess
431
*/
432
public String toString(boolean detailed) {
433
String out = toString();
434
if (detailed) {
435
out += "\n";
436
out += printKeys();
437
out += "\n" + printCertificates();
438
if (info != null) {
439
out += "\n\t" + info;
440
} else {
441
out += "\n\tno additional information available.";
442
}
443
}
444
return out;
445
}
446
447
String printKeys() {
448
String key = "";
449
if (publicKey != null) {
450
key = "\tpublic key initialized";
451
} else {
452
key = "\tno public key";
453
}
454
return key;
455
}
456
457
String printCertificates() {
458
String out = "";
459
if (certificates == null) {
460
return "\tno certificates";
461
} else {
462
out += "\tcertificates: \n";
463
464
int i = 1;
465
for (Certificate cert : certificates) {
466
out += "\tcertificate " + i++ +
467
"\tfor : " + cert.getPrincipal() + "\n";
468
out += "\t\t\tfrom : " +
469
cert.getGuarantor() + "\n";
470
}
471
}
472
return out;
473
}
474
475
/**
476
* Returns a hashcode for this identity.
477
*
478
* @return a hashcode for this identity.
479
*/
480
public int hashCode() {
481
return name.hashCode();
482
}
483
484
private static void check(String directive) {
485
SecurityManager security = System.getSecurityManager();
486
if (security != null) {
487
security.checkSecurityAccess(directive);
488
}
489
}
490
}
491
492