@@ -114,18 +114,19 @@ public void writeTo(StreamOutput out) throws IOException {
114
114
115
115
/** The oldest metadata format version that can be read. */
116
116
private static final int MIN_FORMAT_VERSION = 3 ;
117
+ /** Legacy versions of the metadata written before the keystore data. */
118
+ public static final int V2_VERSION = 2 ;
117
119
public static final int V3_VERSION = 3 ;
118
120
public static final int V4_VERSION = 4 ;
119
121
/** The version where lucene directory API changed from BE to LE. */
120
122
public static final int LE_VERSION = 5 ;
121
- public static final int HIGHER_KDF_ITERATION_COUNT_VERSION = 6 ;
122
- public static final int CURRENT_VERSION = HIGHER_KDF_ITERATION_COUNT_VERSION ;
123
+ public static final int CURRENT_VERSION = LE_VERSION ;
123
124
124
125
/** The algorithm used to derive the cipher key from a password. */
125
126
private static final String KDF_ALGO = "PBKDF2WithHmacSHA512" ;
126
127
127
128
/** The number of iterations to derive the cipher key. */
128
- private static final int KDF_ITERS = 210000 ;
129
+ private static final int KDF_ITERS = 10000 ;
129
130
130
131
/**
131
132
* The number of bits for the cipher key.
@@ -154,7 +155,6 @@ public void writeTo(StreamOutput out) throws IOException {
154
155
// 3: FIPS compliant algos, ES 6.3
155
156
// 4: remove distinction between string/files, ES 6.8/7.1
156
157
// 5: Lucene directory API changed to LE, ES 8.0
157
- // 6: increase KDF iteration count, ES 8.14
158
158
159
159
/** The metadata format version used to read the current keystore wrapper. */
160
160
private final int formatVersion ;
@@ -317,8 +317,8 @@ public boolean hasPassword() {
317
317
return hasPassword ;
318
318
}
319
319
320
- private static Cipher createCipher (int opmode , char [] password , byte [] salt , byte [] iv , int kdfIters ) throws GeneralSecurityException {
321
- PBEKeySpec keySpec = new PBEKeySpec (password , salt , kdfIters , CIPHER_KEY_BITS );
320
+ private static Cipher createCipher (int opmode , char [] password , byte [] salt , byte [] iv ) throws GeneralSecurityException {
321
+ PBEKeySpec keySpec = new PBEKeySpec (password , salt , KDF_ITERS , CIPHER_KEY_BITS );
322
322
SecretKeyFactory keyFactory = SecretKeyFactory .getInstance (KDF_ALGO );
323
323
SecretKey secretKey ;
324
324
try {
@@ -337,11 +337,6 @@ private static Cipher createCipher(int opmode, char[] password, byte[] salt, byt
337
337
return cipher ;
338
338
}
339
339
340
- private static int getKdfIterationCountForVersion (int formatVersion ) {
341
- // iteration count was increased in version 6; it was 10,000 in previous versions
342
- return formatVersion < HIGHER_KDF_ITERATION_COUNT_VERSION ? 10000 : KDF_ITERS ;
343
- }
344
-
345
340
/**
346
341
* Decrypts the underlying keystore data.
347
342
*
@@ -370,7 +365,7 @@ public void decrypt(char[] password) throws GeneralSecurityException, IOExceptio
370
365
throw new SecurityException ("Keystore has been corrupted or tampered with" , e );
371
366
}
372
367
373
- Cipher cipher = createCipher (Cipher .DECRYPT_MODE , password , salt , iv , getKdfIterationCountForVersion ( formatVersion ) );
368
+ Cipher cipher = createCipher (Cipher .DECRYPT_MODE , password , salt , iv );
374
369
try (
375
370
ByteArrayInputStream bytesStream = new ByteArrayInputStream (encryptedBytes );
376
371
CipherInputStream cipherStream = new CipherInputStream (bytesStream , cipher );
@@ -408,11 +403,11 @@ private static byte[] readByteArray(DataInput input) throws IOException {
408
403
}
409
404
410
405
/** Encrypt the keystore entries and return the encrypted data. */
411
- private byte [] encrypt (char [] password , byte [] salt , byte [] iv , int kdfIterationCount ) throws GeneralSecurityException , IOException {
406
+ private byte [] encrypt (char [] password , byte [] salt , byte [] iv ) throws GeneralSecurityException , IOException {
412
407
assert isLoaded ();
413
408
414
409
ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
415
- Cipher cipher = createCipher (Cipher .ENCRYPT_MODE , password , salt , iv , kdfIterationCount );
410
+ Cipher cipher = createCipher (Cipher .ENCRYPT_MODE , password , salt , iv );
416
411
try (
417
412
CipherOutputStream cipherStream = new CipherOutputStream (bytes , cipher );
418
413
DataOutputStream output = new DataOutputStream (cipherStream )
@@ -455,7 +450,7 @@ public synchronized void save(Path configDir, char[] password, boolean preserveP
455
450
byte [] iv = new byte [12 ];
456
451
random .nextBytes (iv );
457
452
// encrypted data
458
- byte [] encryptedBytes = encrypt (password , salt , iv , getKdfIterationCountForVersion ( CURRENT_VERSION ) );
453
+ byte [] encryptedBytes = encrypt (password , salt , iv );
459
454
460
455
// size of data block
461
456
output .writeInt (4 + salt .length + 4 + iv .length + 4 + encryptedBytes .length );
0 commit comments