-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple and pluggable content encryption keys and encryption…
… algorithms (#2240) * Refactor content encryption into pluggable components * Extend possible configuration for EncryptingContentStore Allow configuration of all pluggable components and provide default values when nothing is configured * Update documentation to reference swappable components * Add encryption of DEKs with vault * Add test for AesCtrEncryptionEngine * Add tests for StoredDataEncryptionKey data conversions * Add integration tests with a DataEncryptionKeyWrapper that does not decrypt * Move implementation of addKey/removeKey methods to DataEncryptionKeyAccessor interface * Add integration test using custom DataEncryptionKeyAccessor We need to ensure that the accessor is able to read the content property from the entity before it is removed/after it is created. This is necessary to have custom key accessors work, so they can store the encryption key somewhere other than the entity itself, for example based on the content id * Move VaultTransitDataEncryptionKeyWrapper to public API This DataEncryptionKeyWrapper object needs to be instanciated by users to use vault encryption, so it should not be in the internal package * Temp: Use updated gettingstarted repo * reset gettingstarted branch - gettingstarted PR now merged
- Loading branch information
1 parent
7e93b51
commit aebb709
Showing
40 changed files
with
2,407 additions
and
648 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
129 changes: 129 additions & 0 deletions
129
...n/java/internal/org/springframework/content/encryption/engine/AesCtrEncryptionEngine.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package internal.org.springframework.content.encryption.engine; | ||
|
||
import java.io.InputStream; | ||
import java.math.BigInteger; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.CipherInputStream; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import lombok.SneakyThrows; | ||
import org.springframework.content.encryption.engine.ContentEncryptionEngine; | ||
|
||
/** | ||
* Symmetric data encryption engine using AES-CTR encryption mode | ||
*/ | ||
public class AesCtrEncryptionEngine implements ContentEncryptionEngine { | ||
private final KeyGenerator keyGenerator; | ||
private static final SecureRandom secureRandom = new SecureRandom(); | ||
|
||
private static final int AES_BLOCK_SIZE_BYTES = 16; // AES has a 128-bit block size | ||
private static final int IV_SIZE_BYTES = AES_BLOCK_SIZE_BYTES; // IV is the same size as a block | ||
|
||
@SneakyThrows({NoSuchAlgorithmException.class}) | ||
public AesCtrEncryptionEngine(int keySizeBits) { | ||
keyGenerator = KeyGenerator.getInstance("AES"); | ||
keyGenerator.init(keySizeBits, secureRandom); | ||
} | ||
|
||
@Override | ||
public EncryptionParameters createNewParameters() { | ||
var secretKey = keyGenerator.generateKey(); | ||
byte[] iv = new byte[IV_SIZE_BYTES]; | ||
secureRandom.nextBytes(iv); | ||
return new EncryptionParameters( | ||
secretKey, | ||
iv | ||
); | ||
} | ||
|
||
@SneakyThrows | ||
private Cipher initializeCipher(EncryptionParameters parameters, boolean forEncryption) { | ||
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); | ||
cipher.init( | ||
forEncryption?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, | ||
parameters.getSecretKey(), | ||
new IvParameterSpec(parameters.getInitializationVector()) | ||
); | ||
|
||
return cipher; | ||
} | ||
|
||
@Override | ||
public InputStream encrypt(InputStream plainText, EncryptionParameters encryptionParameters) { | ||
return new CipherInputStream(plainText, initializeCipher(encryptionParameters, true)); | ||
} | ||
|
||
@Override | ||
public InputStream decrypt( | ||
Function<InputStreamRequestParameters, InputStream> cipherTextStreamRequest, | ||
EncryptionParameters encryptionParameters, | ||
InputStreamRequestParameters requestParameters | ||
) { | ||
var blockStartOffset = calculateBlockOffset(requestParameters.getStartByteOffset()); | ||
|
||
var adjustedIv = adjustIvForOffset(encryptionParameters.getInitializationVector(), blockStartOffset); | ||
|
||
var adjustedParameters = new EncryptionParameters( | ||
encryptionParameters.getSecretKey(), | ||
adjustedIv | ||
); | ||
|
||
var byteStartOffset = blockStartOffset * AES_BLOCK_SIZE_BYTES; | ||
|
||
var cipherTextStream = cipherTextStreamRequest.apply(requestParameters); | ||
|
||
var cipher = initializeCipher(adjustedParameters, false); | ||
|
||
return new ZeroPrefixedInputStream( | ||
new EnsureSingleSkipInputStream( | ||
new CipherInputStream( | ||
new SkippingInputStream( | ||
cipherTextStream, | ||
byteStartOffset | ||
), | ||
cipher | ||
) | ||
), | ||
byteStartOffset | ||
); | ||
} | ||
|
||
private static long calculateBlockOffset(long offsetBytes) { | ||
return (offsetBytes - (offsetBytes % AES_BLOCK_SIZE_BYTES)) / AES_BLOCK_SIZE_BYTES; | ||
} | ||
|
||
private byte[] adjustIvForOffset(byte[] iv, long offsetBlocks) { | ||
// Optimization: no need to adjust the IV when we have no block offset | ||
if(offsetBlocks == 0) { | ||
return iv; | ||
} | ||
|
||
// AES-CTR works by having a separate IV for every block. | ||
// This block IV is built from the initial IV and the block counter. | ||
var initialIv = new BigInteger(1, iv); | ||
byte[] bigintBytes = initialIv.add(BigInteger.valueOf(offsetBlocks)) | ||
.toByteArray(); | ||
|
||
// Because we're using BigInteger for math here, | ||
// the resulting byte array may be longer (when overflowing the IV size, we should wrap around) | ||
// or shorter (when our IV starts with a bunch of 0) | ||
// It needs to be the proper length, and aligned properly | ||
if(bigintBytes.length == AES_BLOCK_SIZE_BYTES) { | ||
return bigintBytes; | ||
} else if(bigintBytes.length > AES_BLOCK_SIZE_BYTES) { | ||
// Byte array is longer, we need to cut a part of the front | ||
return Arrays.copyOfRange(bigintBytes, bigintBytes.length-IV_SIZE_BYTES, bigintBytes.length); | ||
} else { | ||
// Byte array is shorter, we need to pad the front with 0 bytes | ||
// Note that a bytes array is initialized to be all-zero by default | ||
byte[] ivBytes = new byte[IV_SIZE_BYTES]; | ||
System.arraycopy(bigintBytes, 0, ivBytes, IV_SIZE_BYTES-bigintBytes.length, bigintBytes.length); | ||
return ivBytes; | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...a/internal/org/springframework/content/encryption/engine/EnsureSingleSkipInputStream.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package internal.org.springframework.content.encryption.engine; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Ensures that a single {@link #skip(long)} call skips exactly that amount of bytes. | ||
* <p> | ||
* This fixes an issue in the {@link javax.crypto.CipherInputStream} where skips can stop short of the requested skip amount | ||
*/ | ||
class EnsureSingleSkipInputStream extends FilterInputStream { | ||
|
||
public EnsureSingleSkipInputStream(InputStream in) { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
long totalSkipped = 0; | ||
while(totalSkipped < n) { | ||
var skipAmount = super.skip(n-totalSkipped); | ||
totalSkipped+=skipAmount; | ||
if(skipAmount == 0) { // no bytes were skipped | ||
// Read one byte to check for EOF | ||
if(read() == -1) { | ||
return totalSkipped; | ||
} | ||
totalSkipped++; // We skipped the byte we read above | ||
} | ||
} | ||
return n; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...main/java/internal/org/springframework/content/encryption/engine/SkippingInputStream.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package internal.org.springframework.content.encryption.engine; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Skips a certain amount of bytes from the delegate {@link InputStream} | ||
*/ | ||
class SkippingInputStream extends InputStream { | ||
private final InputStream delegate; | ||
private final long skipBytes; | ||
private boolean hasSkipped; | ||
|
||
public SkippingInputStream(InputStream delegate, long skipBytes) { | ||
this.delegate = delegate; | ||
this.skipBytes = skipBytes; | ||
} | ||
|
||
private void ensureSkipped() throws IOException { | ||
if(!hasSkipped) { | ||
delegate.skipNBytes(skipBytes); | ||
hasSkipped = true; | ||
} | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
ensureSkipped(); | ||
return delegate.skip(n); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
ensureSkipped(); | ||
return delegate.read(); | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
ensureSkipped(); | ||
return delegate.read(b, off, len); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
} |
Oops, something went wrong.