Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/kerberos/KeyImpl.java
38918 views
1
/*
2
* Copyright (c) 2000, 2013, 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 javax.security.auth.kerberos;
27
28
import java.io.*;
29
import java.util.Arrays;
30
import javax.crypto.SecretKey;
31
import javax.security.auth.Destroyable;
32
import javax.security.auth.DestroyFailedException;
33
import sun.misc.HexDumpEncoder;
34
import sun.security.krb5.Asn1Exception;
35
import sun.security.krb5.PrincipalName;
36
import sun.security.krb5.EncryptionKey;
37
import sun.security.krb5.EncryptedData;
38
import sun.security.krb5.KrbException;
39
import sun.security.krb5.KrbCryptoException;
40
import sun.security.util.DerValue;
41
42
/**
43
* This class encapsulates a Kerberos encryption key. It is not associated
44
* with a principal and may represent an ephemeral session key.
45
*
46
* @author Mayank Upadhyay
47
* @since 1.4
48
*
49
* @serial include
50
*/
51
class KeyImpl implements SecretKey, Destroyable, Serializable {
52
53
private static final long serialVersionUID = -7889313790214321193L;
54
55
private transient byte[] keyBytes;
56
private transient int keyType;
57
private transient volatile boolean destroyed = false;
58
59
60
/**
61
* Constructs a KeyImpl from the given bytes.
62
*
63
* @param keyBytes the raw bytes for the secret key
64
* @param keyType the key type for the secret key as defined by the
65
* Kerberos protocol specification.
66
*/
67
public KeyImpl(byte[] keyBytes,
68
int keyType) {
69
this.keyBytes = keyBytes.clone();
70
this.keyType = keyType;
71
}
72
73
/**
74
* Constructs a KeyImpl from a password.
75
*
76
* @param principal the principal from which to derive the salt
77
* @param password the password that should be used to compute the
78
* key.
79
* @param algorithm the name for the algorithm that this key wil be
80
* used for. This parameter may be null in which case "DES" will be
81
* assumed.
82
*/
83
public KeyImpl(KerberosPrincipal principal,
84
char[] password,
85
String algorithm) {
86
87
try {
88
PrincipalName princ = new PrincipalName(principal.getName());
89
EncryptionKey key =
90
new EncryptionKey(password, princ.getSalt(), algorithm);
91
this.keyBytes = key.getBytes();
92
this.keyType = key.getEType();
93
} catch (KrbException e) {
94
throw new IllegalArgumentException(e.getMessage());
95
}
96
}
97
98
/**
99
* Returns the keyType for this key as defined in the Kerberos Spec.
100
*/
101
public final int getKeyType() {
102
if (destroyed)
103
throw new IllegalStateException("This key is no longer valid");
104
return keyType;
105
}
106
107
/*
108
* Methods from java.security.Key
109
*/
110
111
public final String getAlgorithm() {
112
return getAlgorithmName(keyType);
113
}
114
115
private String getAlgorithmName(int eType) {
116
if (destroyed)
117
throw new IllegalStateException("This key is no longer valid");
118
119
switch (eType) {
120
case EncryptedData.ETYPE_DES_CBC_CRC:
121
case EncryptedData.ETYPE_DES_CBC_MD5:
122
return "DES";
123
124
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
125
return "DESede";
126
127
case EncryptedData.ETYPE_ARCFOUR_HMAC:
128
return "ArcFourHmac";
129
130
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
131
return "AES128";
132
133
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
134
return "AES256";
135
136
case EncryptedData.ETYPE_NULL:
137
return "NULL";
138
139
default:
140
throw new IllegalArgumentException(
141
"Unsupported encryption type: " + eType);
142
}
143
}
144
145
public final String getFormat() {
146
if (destroyed)
147
throw new IllegalStateException("This key is no longer valid");
148
return "RAW";
149
}
150
151
public final byte[] getEncoded() {
152
if (destroyed)
153
throw new IllegalStateException("This key is no longer valid");
154
return keyBytes.clone();
155
}
156
157
public void destroy() throws DestroyFailedException {
158
if (!destroyed) {
159
destroyed = true;
160
Arrays.fill(keyBytes, (byte) 0);
161
}
162
}
163
164
public boolean isDestroyed() {
165
return destroyed;
166
}
167
168
/**
169
* @serialData this {@code KeyImpl} is serialized by
170
* writing out the ASN1 Encoded bytes of the encryption key.
171
* The ASN1 encoding is defined in RFC4120 and as follows:
172
* EncryptionKey ::= SEQUENCE {
173
* keytype [0] Int32 -- actually encryption type --,
174
* keyvalue [1] OCTET STRING
175
* }
176
*/
177
private void writeObject(ObjectOutputStream ois)
178
throws IOException {
179
if (destroyed) {
180
throw new IOException("This key is no longer valid");
181
}
182
183
try {
184
ois.writeObject((new EncryptionKey(keyType, keyBytes)).asn1Encode());
185
} catch (Asn1Exception ae) {
186
throw new IOException(ae.getMessage());
187
}
188
}
189
190
private void readObject(ObjectInputStream ois)
191
throws IOException, ClassNotFoundException {
192
try {
193
EncryptionKey encKey = new EncryptionKey(new
194
DerValue((byte[])ois.readObject()));
195
keyType = encKey.getEType();
196
keyBytes = encKey.getBytes();
197
} catch (Asn1Exception ae) {
198
throw new IOException(ae.getMessage());
199
}
200
}
201
202
public String toString() {
203
HexDumpEncoder hd = new HexDumpEncoder();
204
return "EncryptionKey: keyType=" + keyType
205
+ " keyBytes (hex dump)="
206
+ (keyBytes == null || keyBytes.length == 0 ?
207
" Empty Key" :
208
'\n' + hd.encodeBuffer(keyBytes)
209
+ '\n');
210
211
212
}
213
214
public int hashCode() {
215
int result = 17;
216
if(isDestroyed()) {
217
return result;
218
}
219
result = 37 * result + Arrays.hashCode(keyBytes);
220
return 37 * result + keyType;
221
}
222
223
public boolean equals(Object other) {
224
225
if (other == this)
226
return true;
227
228
if (! (other instanceof KeyImpl)) {
229
return false;
230
}
231
232
KeyImpl otherKey = ((KeyImpl) other);
233
if (isDestroyed() || otherKey.isDestroyed()) {
234
return false;
235
}
236
237
if(keyType != otherKey.getKeyType() ||
238
!Arrays.equals(keyBytes, otherKey.getEncoded())) {
239
return false;
240
}
241
242
return true;
243
}
244
}
245
246