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/sun/security/provider/KeyProtector.java
38830 views
1
/*
2
* Copyright (c) 1997, 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
package sun.security.provider;
27
28
import java.io.IOException;
29
import java.io.UnsupportedEncodingException;
30
import java.security.Key;
31
import java.security.KeyStoreException;
32
import java.security.MessageDigest;
33
import java.security.NoSuchAlgorithmException;
34
import java.security.SecureRandom;
35
import java.security.UnrecoverableKeyException;
36
import java.util.*;
37
38
import sun.security.pkcs.PKCS8Key;
39
import sun.security.pkcs.EncryptedPrivateKeyInfo;
40
import sun.security.x509.AlgorithmId;
41
import sun.security.util.ObjectIdentifier;
42
import sun.security.util.DerValue;
43
44
/**
45
* This is an implementation of a Sun proprietary, exportable algorithm
46
* intended for use when protecting (or recovering the cleartext version of)
47
* sensitive keys.
48
* This algorithm is not intended as a general purpose cipher.
49
*
50
* This is how the algorithm works for key protection:
51
*
52
* p - user password
53
* s - random salt
54
* X - xor key
55
* P - to-be-protected key
56
* Y - protected key
57
* R - what gets stored in the keystore
58
*
59
* Step 1:
60
* Take the user's password, append a random salt (of fixed size) to it,
61
* and hash it: d1 = digest(p, s)
62
* Store d1 in X.
63
*
64
* Step 2:
65
* Take the user's password, append the digest result from the previous step,
66
* and hash it: dn = digest(p, dn-1).
67
* Store dn in X (append it to the previously stored digests).
68
* Repeat this step until the length of X matches the length of the private key
69
* P.
70
*
71
* Step 3:
72
* XOR X and P, and store the result in Y: Y = X XOR P.
73
*
74
* Step 4:
75
* Store s, Y, and digest(p, P) in the result buffer R:
76
* R = s + Y + digest(p, P), where "+" denotes concatenation.
77
* (NOTE: digest(p, P) is stored in the result buffer, so that when the key is
78
* recovered, we can check if the recovered key indeed matches the original
79
* key.) R is stored in the keystore.
80
*
81
* The protected key is recovered as follows:
82
*
83
* Step1 and Step2 are the same as above, except that the salt is not randomly
84
* generated, but taken from the result R of step 4 (the first length(s)
85
* bytes).
86
*
87
* Step 3 (XOR operation) yields the plaintext key.
88
*
89
* Then concatenate the password with the recovered key, and compare with the
90
* last length(digest(p, P)) bytes of R. If they match, the recovered key is
91
* indeed the same key as the original key.
92
*
93
* @author Jan Luehe
94
*
95
*
96
* @see java.security.KeyStore
97
* @see JavaKeyStore
98
* @see KeyTool
99
*
100
* @since 1.2
101
*/
102
103
final class KeyProtector {
104
105
private static final int SALT_LEN = 20; // the salt length
106
private static final String DIGEST_ALG = "SHA";
107
private static final int DIGEST_LEN = 20;
108
109
// defined by JavaSoft
110
private static final String KEY_PROTECTOR_OID = "1.3.6.1.4.1.42.2.17.1.1";
111
112
// The password used for protecting/recovering keys passed through this
113
// key protector. We store it as a byte array, so that we can digest it.
114
private byte[] passwdBytes;
115
116
private MessageDigest md;
117
118
119
/**
120
* Creates an instance of this class, and initializes it with the given
121
* password.
122
*/
123
public KeyProtector(byte[] passwordBytes)
124
throws NoSuchAlgorithmException
125
{
126
if (passwordBytes == null) {
127
throw new IllegalArgumentException("password can't be null");
128
}
129
md = MessageDigest.getInstance(DIGEST_ALG);
130
this.passwdBytes = passwordBytes;
131
}
132
133
/**
134
* Ensures that the password bytes of this key protector are
135
* set to zero when there are no more references to it.
136
*/
137
protected void finalize() {
138
if (passwdBytes != null) {
139
Arrays.fill(passwdBytes, (byte)0x00);
140
passwdBytes = null;
141
}
142
}
143
144
/*
145
* Protects the given plaintext key, using the password provided at
146
* construction time.
147
*/
148
public byte[] protect(Key key) throws KeyStoreException
149
{
150
int i;
151
int numRounds;
152
byte[] digest;
153
int xorOffset; // offset in xorKey where next digest will be stored
154
int encrKeyOffset = 0;
155
156
if (key == null) {
157
throw new IllegalArgumentException("plaintext key can't be null");
158
}
159
160
if (!"PKCS#8".equalsIgnoreCase(key.getFormat())) {
161
throw new KeyStoreException(
162
"Cannot get key bytes, not PKCS#8 encoded");
163
}
164
165
byte[] plainKey = key.getEncoded();
166
if (plainKey == null) {
167
throw new KeyStoreException(
168
"Cannot get key bytes, encoding not supported");
169
}
170
171
// Determine the number of digest rounds
172
numRounds = plainKey.length / DIGEST_LEN;
173
if ((plainKey.length % DIGEST_LEN) != 0)
174
numRounds++;
175
176
// Create a random salt
177
byte[] salt = new byte[SALT_LEN];
178
SecureRandom random = new SecureRandom();
179
random.nextBytes(salt);
180
181
// Set up the byte array which will be XORed with "plainKey"
182
byte[] xorKey = new byte[plainKey.length];
183
184
// Compute the digests, and store them in "xorKey"
185
for (i = 0, xorOffset = 0, digest = salt;
186
i < numRounds;
187
i++, xorOffset += DIGEST_LEN) {
188
md.update(passwdBytes);
189
md.update(digest);
190
digest = md.digest();
191
md.reset();
192
// Copy the digest into "xorKey"
193
if (i < numRounds - 1) {
194
System.arraycopy(digest, 0, xorKey, xorOffset,
195
digest.length);
196
} else {
197
System.arraycopy(digest, 0, xorKey, xorOffset,
198
xorKey.length - xorOffset);
199
}
200
}
201
202
// XOR "plainKey" with "xorKey", and store the result in "tmpKey"
203
byte[] tmpKey = new byte[plainKey.length];
204
for (i = 0; i < tmpKey.length; i++) {
205
tmpKey[i] = (byte)(plainKey[i] ^ xorKey[i]);
206
}
207
208
// Store salt and "tmpKey" in "encrKey"
209
byte[] encrKey = new byte[salt.length + tmpKey.length + DIGEST_LEN];
210
System.arraycopy(salt, 0, encrKey, encrKeyOffset, salt.length);
211
encrKeyOffset += salt.length;
212
System.arraycopy(tmpKey, 0, encrKey, encrKeyOffset, tmpKey.length);
213
encrKeyOffset += tmpKey.length;
214
215
// Append digest(password, plainKey) as an integrity check to "encrKey"
216
md.update(passwdBytes);
217
Arrays.fill(passwdBytes, (byte)0x00);
218
passwdBytes = null;
219
md.update(plainKey);
220
digest = md.digest();
221
md.reset();
222
System.arraycopy(digest, 0, encrKey, encrKeyOffset, digest.length);
223
224
// wrap the protected private key in a PKCS#8-style
225
// EncryptedPrivateKeyInfo, and returns its encoding
226
AlgorithmId encrAlg;
227
try {
228
encrAlg = new AlgorithmId(new ObjectIdentifier(KEY_PROTECTOR_OID));
229
return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
230
} catch (IOException ioe) {
231
throw new KeyStoreException(ioe.getMessage());
232
}
233
}
234
235
/*
236
* Recovers the plaintext version of the given key (in protected format),
237
* using the password provided at construction time.
238
*/
239
public Key recover(EncryptedPrivateKeyInfo encrInfo)
240
throws UnrecoverableKeyException
241
{
242
int i;
243
byte[] digest;
244
int numRounds;
245
int xorOffset; // offset in xorKey where next digest will be stored
246
int encrKeyLen; // the length of the encrpyted key
247
248
// do we support the algorithm?
249
AlgorithmId encrAlg = encrInfo.getAlgorithm();
250
if (!(encrAlg.getOID().toString().equals(KEY_PROTECTOR_OID))) {
251
throw new UnrecoverableKeyException("Unsupported key protection "
252
+ "algorithm");
253
}
254
255
byte[] protectedKey = encrInfo.getEncryptedData();
256
257
/*
258
* Get the salt associated with this key (the first SALT_LEN bytes of
259
* <code>protectedKey</code>)
260
*/
261
byte[] salt = new byte[SALT_LEN];
262
System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
263
264
// Determine the number of digest rounds
265
encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
266
numRounds = encrKeyLen / DIGEST_LEN;
267
if ((encrKeyLen % DIGEST_LEN) != 0) numRounds++;
268
269
// Get the encrypted key portion and store it in "encrKey"
270
byte[] encrKey = new byte[encrKeyLen];
271
System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
272
273
// Set up the byte array which will be XORed with "encrKey"
274
byte[] xorKey = new byte[encrKey.length];
275
276
// Compute the digests, and store them in "xorKey"
277
for (i = 0, xorOffset = 0, digest = salt;
278
i < numRounds;
279
i++, xorOffset += DIGEST_LEN) {
280
md.update(passwdBytes);
281
md.update(digest);
282
digest = md.digest();
283
md.reset();
284
// Copy the digest into "xorKey"
285
if (i < numRounds - 1) {
286
System.arraycopy(digest, 0, xorKey, xorOffset,
287
digest.length);
288
} else {
289
System.arraycopy(digest, 0, xorKey, xorOffset,
290
xorKey.length - xorOffset);
291
}
292
}
293
294
// XOR "encrKey" with "xorKey", and store the result in "plainKey"
295
byte[] plainKey = new byte[encrKey.length];
296
for (i = 0; i < plainKey.length; i++) {
297
plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
298
}
299
300
/*
301
* Check the integrity of the recovered key by concatenating it with
302
* the password, digesting the concatenation, and comparing the
303
* result of the digest operation with the digest provided at the end
304
* of <code>protectedKey</code>. If the two digest values are
305
* different, throw an exception.
306
*/
307
md.update(passwdBytes);
308
Arrays.fill(passwdBytes, (byte)0x00);
309
passwdBytes = null;
310
md.update(plainKey);
311
digest = md.digest();
312
md.reset();
313
for (i = 0; i < digest.length; i++) {
314
if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
315
throw new UnrecoverableKeyException("Cannot recover key");
316
}
317
}
318
319
// The parseKey() method of PKCS8Key parses the key
320
// algorithm and instantiates the appropriate key factory,
321
// which in turn parses the key material.
322
try {
323
return PKCS8Key.parseKey(new DerValue(plainKey));
324
} catch (IOException ioe) {
325
throw new UnrecoverableKeyException(ioe.getMessage());
326
}
327
}
328
}
329
330