-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Make licensing FIPS-140 compliant #30251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jkakavas
merged 3 commits into
elastic:master
from
jkakavas:make-licensing-fips-compliant
May 2, 2018
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,95 +15,71 @@ | |
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.InvalidKeyException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyFactory; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.SecureRandom; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.InvalidKeyException; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import java.util.Base64; | ||
|
||
public class CryptUtils { | ||
private static final int minimumPadding = 20; | ||
private static final byte[] salt = { | ||
(byte) 0xA9, (byte) 0xA2, (byte) 0xB5, (byte) 0xDE, | ||
(byte) 0x2A, (byte) 0x8A, (byte) 0x9A, (byte) 0xE6 | ||
// SALT must be at least 128bits | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add |
||
private static final byte[] SALT = { | ||
(byte) 0x74, (byte) 0x68, (byte) 0x69, (byte) 0x73, | ||
(byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x68, | ||
(byte) 0x65, (byte) 0x73, (byte) 0x61, (byte) 0x6C, | ||
(byte) 0x74, (byte) 0x77, (byte) 0x65, (byte) 0x75 | ||
}; | ||
private static final int iterationCount = 1024; | ||
private static final int aesKeyLength = 128; | ||
private static final String keyAlgorithm = "RSA"; | ||
private static final String passHashAlgorithm = "SHA-512"; | ||
private static final String DEFAULT_PASS_PHRASE = "elasticsearch-license"; | ||
|
||
private static final SecureRandom random = new SecureRandom(); | ||
private static final String KEY_ALGORITHM = "RSA"; | ||
private static final char[] DEFAULT_PASS_PHRASE = "elasticsearch-license".toCharArray(); | ||
private static final String KDF_ALGORITHM = "PBKDF2WithHmacSHA512"; | ||
private static final int KDF_ITERATION_COUNT = 10000; | ||
private static final String CIPHER_ALGORITHM = "AES"; | ||
// This can be changed to 256 once Java 9 is the minimum version | ||
// http://www.oracle.com/technetwork/java/javase/terms/readme/jdk9-readme-3852447.html#jce | ||
private static final int ENCRYPTION_KEY_LENGTH = 128; | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
|
||
/** | ||
* Read encrypted private key file content with default pass phrase | ||
*/ | ||
public static PrivateKey readEncryptedPrivateKey(byte[] fileContents) { | ||
try { | ||
return readEncryptedPrivateKey(fileContents, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Read encrypted public key file content with default pass phrase | ||
*/ | ||
public static PublicKey readEncryptedPublicKey(byte[] fileContents) { | ||
try { | ||
return readEncryptedPublicKey(fileContents, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Returns encrypted public key file content with default pass phrase | ||
*/ | ||
public static byte[] writeEncryptedPublicKey(PublicKey publicKey) { | ||
try { | ||
return writeEncryptedPublicKey(publicKey, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
return readEncryptedPrivateKey(fileContents, DEFAULT_PASS_PHRASE, false); | ||
} | ||
|
||
/** | ||
* Returns encrypted private key file content with default pass phrase | ||
*/ | ||
public static byte[] writeEncryptedPrivateKey(PrivateKey privateKey) { | ||
try { | ||
return writeEncryptedPrivateKey(privateKey, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
return writeEncryptedPrivateKey(privateKey, DEFAULT_PASS_PHRASE); | ||
} | ||
|
||
/** | ||
* Read encrypted private key file content with provided <code>passPhrase</code> | ||
*/ | ||
public static PrivateKey readEncryptedPrivateKey(byte[] fileContents, char[] passPhrase) { | ||
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decrypt(fileContents, passPhrase)); | ||
public static PrivateKey readEncryptedPrivateKey(byte[] fileContents, char[] passPhrase, boolean preV4) { | ||
byte[] keyBytes = preV4 ? decryptV3Format(fileContents) : decrypt(fileContents, passPhrase); | ||
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes); | ||
try { | ||
return KeyFactory.getInstance(keyAlgorithm).generatePrivate(privateKeySpec); | ||
return KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(privateKeySpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Read encrypted public key file content with provided <code>passPhrase</code> | ||
* Read public key file content | ||
*/ | ||
public static PublicKey readEncryptedPublicKey(byte[] fileContents, char[] passPhrase) { | ||
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decrypt(fileContents, passPhrase)); | ||
public static PublicKey readPublicKey(byte[] fileContents) { | ||
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(fileContents); | ||
try { | ||
return KeyFactory.getInstance(CryptUtils.keyAlgorithm).generatePublic(publicKeySpec); | ||
return KeyFactory.getInstance(CryptUtils.KEY_ALGORITHM).generatePublic(publicKeySpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
@@ -112,9 +88,9 @@ public static PublicKey readEncryptedPublicKey(byte[] fileContents, char[] passP | |
/** | ||
* Returns encrypted public key file content with provided <code>passPhrase</code> | ||
*/ | ||
public static byte[] writeEncryptedPublicKey(PublicKey publicKey, char[] passPhrase) { | ||
public static byte[] writeEncryptedPublicKey(PublicKey publicKey) { | ||
X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); | ||
return encrypt(encodedKeySpec.getEncoded(), passPhrase); | ||
return encrypt(encodedKeySpec.getEncoded(), DEFAULT_PASS_PHRASE); | ||
} | ||
|
||
/** | ||
|
@@ -128,33 +104,25 @@ public static byte[] writeEncryptedPrivateKey(PrivateKey privateKey, char[] pass | |
/** | ||
* Encrypts provided <code>data</code> with <code>DEFAULT_PASS_PHRASE</code> | ||
*/ | ||
public static byte[] encrypt(byte[] data) { | ||
try { | ||
return encrypt(data, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
static byte[] encrypt(byte[] data) { | ||
return encrypt(data, DEFAULT_PASS_PHRASE); | ||
} | ||
|
||
/** | ||
* Decrypts provided <code>encryptedData</code> with <code>DEFAULT_PASS_PHRASE</code> | ||
*/ | ||
public static byte[] decrypt(byte[] encryptedData) { | ||
try { | ||
return decrypt(encryptedData, hashPassPhrase(DEFAULT_PASS_PHRASE)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
static byte[] decrypt(byte[] encryptedData) { | ||
return decrypt(encryptedData, DEFAULT_PASS_PHRASE); | ||
} | ||
|
||
/** | ||
* Encrypts provided <code>data</code> with <code>passPhrase</code> | ||
*/ | ||
public static byte[] encrypt(byte[] data, char[] passPhrase) { | ||
private static byte[] encrypt(byte[] data, char[] passPhrase) { | ||
try { | ||
final Cipher encryptionCipher = getEncryptionCipher(getSecretKey(passPhrase)); | ||
return encryptionCipher.doFinal(pad(data, minimumPadding)); | ||
} catch (InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException e) { | ||
final Cipher encryptionCipher = getEncryptionCipher(deriveSecretKey(passPhrase)); | ||
return encryptionCipher.doFinal(data); | ||
} catch (IllegalBlockSizeException | BadPaddingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
@@ -164,29 +132,60 @@ public static byte[] encrypt(byte[] data, char[] passPhrase) { | |
*/ | ||
private static byte[] decrypt(byte[] encryptedData, char[] passPhrase) { | ||
try { | ||
final Cipher cipher = getDecryptionCipher(getSecretKey(passPhrase)); | ||
return unPad(cipher.doFinal(encryptedData)); | ||
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeySpecException e) { | ||
final Cipher cipher = getDecryptionCipher(deriveSecretKey(passPhrase)); | ||
return cipher.doFinal(encryptedData); | ||
} catch (IllegalBlockSizeException | BadPaddingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
static byte[] encryptV3Format(byte[] data) { | ||
try { | ||
SecretKey encryptionKey = getv3Key(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style wise, I think |
||
final Cipher encryptionCipher = getEncryptionCipher(encryptionKey); | ||
return encryptionCipher.doFinal(pad(data, 20)); | ||
} catch (GeneralSecurityException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private static SecretKey getSecretKey(char[] passPhrase) throws InvalidKeySpecException { | ||
static byte[] decryptV3Format(byte[] data) { | ||
try { | ||
PBEKeySpec keySpec = new PBEKeySpec(passPhrase, salt, iterationCount, aesKeyLength); | ||
SecretKey decryptionKey = getv3Key(); | ||
final Cipher decryptionCipher = getDecryptionCipher(decryptionKey); | ||
return unPad(decryptionCipher.doFinal(data)); | ||
} catch (GeneralSecurityException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
byte[] shortKey = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede"). | ||
generateSecret(keySpec).getEncoded(); | ||
private static SecretKey getv3Key() throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
final byte[] salt = { | ||
(byte) 0xA9, (byte) 0xA2, (byte) 0xB5, (byte) 0xDE, | ||
(byte) 0x2A, (byte) 0x8A, (byte) 0x9A, (byte) 0xE6 | ||
}; | ||
final byte[] passBytes = "elasticsearch-license".getBytes(StandardCharsets.UTF_8); | ||
final byte[] digest = MessageDigest.getInstance("SHA-512").digest(passBytes); | ||
final char[] hashedPassphrase = Base64.getEncoder().encodeToString(digest).toCharArray(); | ||
PBEKeySpec keySpec = new PBEKeySpec(hashedPassphrase, salt, 1024, 128); | ||
byte[] shortKey = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede"). | ||
generateSecret(keySpec).getEncoded(); | ||
byte[] intermediaryKey = new byte[16]; | ||
for (int i = 0, j = 0; i < 16; i++) { | ||
intermediaryKey[i] = shortKey[j]; | ||
if (++j == shortKey.length) | ||
j = 0; | ||
} | ||
return new SecretKeySpec(intermediaryKey, "AES"); | ||
} | ||
|
||
byte[] intermediaryKey = new byte[aesKeyLength / 8]; | ||
for (int i = 0, j = 0; i < aesKeyLength / 8; i++) { | ||
intermediaryKey[i] = shortKey[j]; | ||
if (++j == shortKey.length) | ||
j = 0; | ||
} | ||
private static SecretKey deriveSecretKey(char[] passPhrase) { | ||
try { | ||
PBEKeySpec keySpec = new PBEKeySpec(passPhrase, SALT, KDF_ITERATION_COUNT, ENCRYPTION_KEY_LENGTH); | ||
|
||
return new SecretKeySpec(intermediaryKey, "AES"); | ||
SecretKey secretKey = SecretKeyFactory.getInstance(KDF_ALGORITHM). | ||
generateSecret(keySpec); | ||
return new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGORITHM); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
@@ -202,8 +201,8 @@ private static Cipher getDecryptionCipher(SecretKey secretKey) { | |
|
||
private static Cipher getCipher(int mode, SecretKey secretKey) { | ||
try { | ||
Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); | ||
cipher.init(mode, secretKey, random); | ||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); | ||
cipher.init(mode, secretKey, RANDOM); | ||
return cipher; | ||
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) { | ||
throw new IllegalStateException(e); | ||
|
@@ -228,7 +227,7 @@ private static byte[] pad(byte[] bytes, int length) { | |
|
||
// fill the rest with random bytes | ||
byte[] fill = new byte[padded - 1]; | ||
random.nextBytes(fill); | ||
RANDOM.nextBytes(fill); | ||
System.arraycopy(fill, 0, out, i, padded - 1); | ||
|
||
out[length] = (byte) (padded + 1); | ||
|
@@ -246,10 +245,4 @@ private static byte[] unPad(byte[] bytes) { | |
|
||
return out; | ||
} | ||
|
||
private static char[] hashPassPhrase(String passPhrase) throws NoSuchAlgorithmException { | ||
final byte[] passBytes = passPhrase.getBytes(StandardCharsets.UTF_8); | ||
final byte[] digest = MessageDigest.getInstance(passHashAlgorithm).digest(passBytes); | ||
return Base64.getEncoder().encodeToString(digest).toCharArray(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use
preV4
instead oflegacy
here?