Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/EncryptedData.java
41159 views
1
/*
2
* Copyright (c) 2000, 2018, 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
/*
27
*
28
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
29
* Copyright 1997 The Open Group Research Institute. All rights reserved.
30
*/
31
32
package sun.security.krb5;
33
34
import sun.security.util.*;
35
import sun.security.krb5.internal.crypto.*;
36
import sun.security.krb5.internal.*;
37
import java.io.IOException;
38
import java.math.BigInteger;
39
40
/**
41
* This class encapsulates Kerberos encrypted data. It allows
42
* callers access to both the ASN.1 encoded form of the EncryptedData
43
* type as well as the raw cipher text.
44
*/
45
46
public class EncryptedData implements Cloneable {
47
int eType;
48
Integer kvno; // optional
49
byte[] cipher;
50
byte[] plain; // not part of ASN.1 encoding
51
52
// ----------------+-----------+----------+----------------+---------------
53
// Encryption type |etype value|block size|minimum pad size|confounder size
54
// ----------------+-----------+----------+----------------+---------------
55
public static final int
56
ETYPE_NULL = 0; // 1 0 0
57
public static final int
58
ETYPE_DES_CBC_CRC = 1; // 8 4 8
59
public static final int
60
ETYPE_DES_CBC_MD4 = 2; // 8 0 8
61
public static final int
62
ETYPE_DES_CBC_MD5 = 3; // 8 0 8
63
64
// draft-brezak-win2k-krb-rc4-hmac-04.txt
65
public static final int
66
ETYPE_ARCFOUR_HMAC = 23; // 1
67
// NOTE: the exportable RC4-HMAC is not supported;
68
// it is no longer a usable encryption type
69
public static final int
70
ETYPE_ARCFOUR_HMAC_EXP = 24; // 1
71
72
// draft-ietf-krb-wg-crypto-07.txt
73
public static final int
74
ETYPE_DES3_CBC_HMAC_SHA1_KD = 16; // 8 0 8
75
76
// draft-raeburn-krb-rijndael-krb-07.txt
77
public static final int
78
ETYPE_AES128_CTS_HMAC_SHA1_96 = 17; // 16 0 16
79
public static final int
80
ETYPE_AES256_CTS_HMAC_SHA1_96 = 18; // 16 0 16
81
82
// rfc8009
83
public static final int
84
ETYPE_AES128_CTS_HMAC_SHA256_128 = 19; // 16 0 16
85
public static final int
86
ETYPE_AES256_CTS_HMAC_SHA384_192 = 20; // 16 0 16
87
88
/* used by self */
89
private EncryptedData() {
90
}
91
92
public Object clone() {
93
EncryptedData new_encryptedData = new EncryptedData();
94
new_encryptedData.eType = eType;
95
if (kvno != null) {
96
new_encryptedData.kvno = kvno.intValue();
97
}
98
if (cipher != null) {
99
new_encryptedData.cipher = new byte[cipher.length];
100
System.arraycopy(cipher, 0, new_encryptedData.cipher,
101
0, cipher.length);
102
}
103
return new_encryptedData;
104
}
105
106
// Used by test
107
public EncryptedData(
108
int new_eType,
109
Integer new_kvno,
110
byte[] new_cipher) {
111
eType = new_eType;
112
kvno = new_kvno;
113
cipher = new_cipher;
114
}
115
116
/*
117
// Not used.
118
public EncryptedData(
119
EncryptionKey key,
120
byte[] plaintext)
121
throws KdcErrException, KrbCryptoException {
122
EType etypeEngine = EType.getInstance(key.getEType());
123
cipher = etypeEngine.encrypt(plaintext, key.getBytes());
124
eType = key.getEType();
125
kvno = key.getKeyVersionNumber();
126
}
127
*/
128
129
// used in KrbApRep, KrbApReq, KrbAsReq, KrbCred, KrbPriv
130
public EncryptedData(
131
EncryptionKey key,
132
byte[] plaintext,
133
int usage)
134
throws KdcErrException, KrbCryptoException {
135
EType etypeEngine = EType.getInstance(key.getEType());
136
cipher = etypeEngine.encrypt(plaintext, key.getBytes(), usage);
137
eType = key.getEType();
138
kvno = key.getKeyVersionNumber();
139
}
140
141
/*
142
// Not used.
143
public EncryptedData(
144
EncryptionKey key,
145
byte[] ivec,
146
byte[] plaintext)
147
throws KdcErrException, KrbCryptoException {
148
EType etypeEngine = EType.getInstance(key.getEType());
149
cipher = etypeEngine.encrypt(plaintext, key.getBytes(), ivec);
150
eType = key.getEType();
151
kvno = key.getKeyVersionNumber();
152
}
153
*/
154
155
/*
156
// Not used.
157
EncryptedData(
158
StringBuffer password,
159
byte[] plaintext)
160
throws KdcErrException, KrbCryptoException {
161
EncryptionKey key = new EncryptionKey(password);
162
EType etypeEngine = EType.getInstance(key.getEType());
163
cipher = etypeEngine.encrypt(plaintext, key.getBytes());
164
eType = key.getEType();
165
kvno = key.getKeyVersionNumber();
166
}
167
*/
168
public byte[] decrypt(
169
EncryptionKey key, int usage)
170
throws KdcErrException, KrbApErrException, KrbCryptoException {
171
if (eType != key.getEType()) {
172
throw new KrbCryptoException(
173
"EncryptedData is encrypted using keytype " +
174
EType.toString(eType) +
175
" but decryption key is of type " +
176
EType.toString(key.getEType()));
177
}
178
179
EType etypeEngine = EType.getInstance(eType);
180
plain = etypeEngine.decrypt(cipher, key.getBytes(), usage);
181
// The service ticket will be used in S4U2proxy request. Therefore
182
// the raw ticket is still needed.
183
//cipher = null;
184
return etypeEngine.decryptedData(plain);
185
}
186
187
/*
188
// currently destructive on cipher
189
// Not used.
190
public byte[] decrypt(
191
EncryptionKey key,
192
byte[] ivec, int usage)
193
throws KdcErrException, KrbApErrException, KrbCryptoException {
194
// XXX check for matching eType and kvno here
195
EType etypeEngine = EType.getInstance(eType);
196
plain = etypeEngine.decrypt(cipher, key.getBytes(), ivec, usage);
197
cipher = null;
198
return etypeEngine.decryptedData(plain);
199
}
200
201
// currently destructive on cipher
202
// Not used.
203
byte[] decrypt(StringBuffer password)
204
throws KdcErrException, KrbApErrException, KrbCryptoException {
205
EncryptionKey key = new EncryptionKey(password);
206
// XXX check for matching eType here
207
EType etypeEngine = EType.getInstance(eType);
208
plain = etypeEngine.decrypt(cipher, key.getBytes());
209
cipher = null;
210
return etypeEngine.decryptedData(plain);
211
}
212
*/
213
214
private byte[] decryptedData() throws KdcErrException {
215
if (plain != null) {
216
EType etypeEngine = EType.getInstance(eType);
217
return etypeEngine.decryptedData(plain);
218
}
219
return null;
220
}
221
222
/**
223
* Constructs an instance of EncryptedData type.
224
* @param encoding a single DER-encoded value.
225
* @exception Asn1Exception if an error occurs while decoding an
226
* ASN1 encoded data.
227
* @exception IOException if an I/O error occurs while reading encoded
228
* data.
229
*
230
*/
231
/* Used by self */
232
private EncryptedData(DerValue encoding)
233
throws Asn1Exception, IOException {
234
235
DerValue der = null;
236
if (encoding.getTag() != DerValue.tag_Sequence) {
237
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
238
}
239
der = encoding.getData().getDerValue();
240
if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
241
eType = (der.getData().getBigInteger()).intValue();
242
} else {
243
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
244
}
245
246
if ((encoding.getData().peekByte() & 0x1F) == 1) {
247
der = encoding.getData().getDerValue();
248
int i = (der.getData().getBigInteger()).intValue();
249
kvno = i;
250
} else {
251
kvno = null;
252
}
253
der = encoding.getData().getDerValue();
254
if ((der.getTag() & (byte)0x1F) == (byte)0x02) {
255
cipher = der.getData().getOctetString();
256
} else {
257
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
258
}
259
if (encoding.getData().available() > 0) {
260
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
261
}
262
}
263
264
/**
265
* Returns an ASN.1 encoded EncryptedData type.
266
*
267
* <pre>{@code
268
* EncryptedData ::= SEQUENCE {
269
* etype [0] Int32 -- EncryptionType --,
270
* kvno [1] UInt32 OPTIONAL,
271
* cipher [2] OCTET STRING -- ciphertext
272
* }
273
* }</pre>
274
*
275
* <p>
276
* This definition reflects the Network Working Group RFC 4120
277
* specification available at
278
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
279
* http://www.ietf.org/rfc/rfc4120.txt</a>.
280
*
281
* @return byte array of encoded EncryptedData object.
282
* @exception Asn1Exception if an error occurs while decoding an
283
* ASN1 encoded data.
284
* @exception IOException if an I/O error occurs while reading
285
* encoded data.
286
*
287
*/
288
public byte[] asn1Encode() throws Asn1Exception, IOException {
289
DerOutputStream bytes = new DerOutputStream();
290
DerOutputStream temp = new DerOutputStream();
291
temp.putInteger(BigInteger.valueOf(this.eType));
292
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
293
true, (byte)0x00), temp);
294
temp = new DerOutputStream();
295
if (kvno != null) {
296
// encode as an unsigned integer (UInt32)
297
temp.putInteger(BigInteger.valueOf(this.kvno.longValue()));
298
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
299
true, (byte)0x01), temp);
300
temp = new DerOutputStream();
301
}
302
temp.putOctetString(this.cipher);
303
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
304
(byte)0x02), temp);
305
temp = new DerOutputStream();
306
temp.write(DerValue.tag_Sequence, bytes);
307
return temp.toByteArray();
308
}
309
310
311
/**
312
* Parse (unmarshal) an EncryptedData from a DER input stream. This form
313
* parsing might be used when expanding a value which is part of
314
* a constructed sequence and uses explicitly tagged type.
315
*
316
* @param data the Der input stream value, which contains one or more
317
* marshaled value.
318
* @param explicitTag tag number.
319
* @param optional indicate if this data field is optional
320
* @exception Asn1Exception if an error occurs while decoding an
321
* ASN1 encoded data.
322
* @exception IOException if an I/O error occurs while reading
323
* encoded data.
324
* @return an instance of EncryptedData.
325
*
326
*/
327
public static EncryptedData parse(DerInputStream data,
328
byte explicitTag,
329
boolean optional)
330
throws Asn1Exception, IOException {
331
if ((optional) &&
332
(((byte)data.peekByte() & (byte)0x1F) != explicitTag))
333
return null;
334
DerValue der = data.getDerValue();
335
if (explicitTag != (der.getTag() & (byte)0x1F)) {
336
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
337
} else {
338
DerValue subDer = der.getData().getDerValue();
339
return new EncryptedData(subDer);
340
}
341
}
342
343
/**
344
* Reset asn.1 data stream after decryption, remove redundant bytes.
345
* @param data the decrypted data from decrypt().
346
* @return the reset byte array which holds exactly one asn1 datum
347
* including its tag and length.
348
*
349
*/
350
public byte[] reset(byte[] data) {
351
byte[] bytes = null;
352
// for asn.1 encoded data, we use length field to
353
// determine the data length and remove redundant paddings.
354
if ((data[1] & 0xFF) < 128) {
355
bytes = new byte[data[1] + 2];
356
System.arraycopy(data, 0, bytes, 0, data[1] + 2);
357
} else {
358
if ((data[1] & 0xFF) > 128) {
359
int len = data[1] & (byte)0x7F;
360
int result = 0;
361
for (int i = 0; i < len; i++) {
362
result |= (data[i + 2] & 0xFF) << (8 * (len - i - 1));
363
}
364
bytes = new byte[result + len + 2];
365
System.arraycopy(data, 0, bytes, 0, result + len + 2);
366
}
367
}
368
return bytes;
369
}
370
371
public int getEType() {
372
return eType;
373
}
374
375
public Integer getKeyVersionNumber() {
376
return kvno;
377
}
378
379
/**
380
* Returns the raw cipher text bytes, not in ASN.1 encoding.
381
*/
382
public byte[] getBytes() {
383
return cipher;
384
}
385
}
386
387