Allow empty plaintext for transit encryption.

We now allow construction and encryption of empty Plaintext objects. Empty plaintext can make sense from a security perspective.

Closes gh-223.
This commit is contained in:
Mark Paluch
2018-03-20 13:03:26 +01:00
parent 61c1657743
commit a6807c09c3
2 changed files with 22 additions and 6 deletions

View File

@@ -18,7 +18,6 @@ package org.springframework.vault.support;
import lombok.EqualsAndHashCode;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Value object representing plaintext with an optional {@link VaultTransitContext}.
@@ -44,13 +43,12 @@ public class Plaintext {
/**
* Factory method to create {@link Plaintext} from a byte sequence.
*
* @param plaintext the plaintext to encrypt, must not be {@literal null} or empty.
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
* @return the {@link Plaintext} for {@code plaintext}.
*/
public static Plaintext of(byte[] plaintext) {
Assert.isTrue(!ObjectUtils.isEmpty(plaintext),
"Plaintext must not be null or empty");
Assert.notNull(plaintext, "Plaintext must not be null");
return new Plaintext(plaintext, VaultTransitContext.empty());
}
@@ -60,12 +58,12 @@ public class Plaintext {
* {@link String} is encoded to {@code byte} using the default
* {@link java.nio.charset.Charset}.
*
* @param plaintext the plaintext to encrypt, must not be {@literal null} or empty.
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
* @return the {@link Plaintext} for {@code plaintext}.
*/
public static Plaintext of(String plaintext) {
Assert.hasText(plaintext, "Plaintext must not be null or empty");
Assert.notNull(plaintext, "Plaintext must not be null");
return of(plaintext.getBytes());
}

View File

@@ -293,6 +293,24 @@ public class VaultTransitTemplateIntegrationTests extends IntegrationTestSupport
assertThat(ciphertext).startsWith("vault:v1:");
}
@Test
public void encryptShouldEncryptEmptyValues() {
transitOperations.createKey("mykey", VaultTransitKeyCreationRequest.builder()
.convergentEncryption(true).derived(true).build());
VaultTransitContext context = VaultTransitContext.builder()
.context("blubb".getBytes()) //
.nonce("123456789012".getBytes()) //
.build();
Ciphertext ciphertext = transitOperations.encrypt("mykey",
Plaintext.of("").with(context));
assertThat(ciphertext.getCiphertext()).startsWith("vault:v1:");
assertThat(ciphertext.getContext()).isEqualTo(context);
}
@Test
public void encryptShouldCreateWrappedCiphertextWithNonceAndContext() {