Skip to content

Commit 78c26be

Browse files
My-Lan Aragonmp911de
authored andcommitted
Add hash and signature algorithm to VaultSignRequest and VaultSignatureVerificationRequest
1 parent fb324a7 commit 78c26be

File tree

4 files changed

+110
-39
lines changed

4 files changed

+110
-39
lines changed

spring-vault-core/src/main/java/org/springframework/vault/core/VaultTransitTemplate.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,12 @@ public Signature sign(String keyName, VaultSignRequest signRequest) {
384384
Map<String, Object> request = new LinkedHashMap<>();
385385
request.put("input", Base64Utils.encodeToString(signRequest.getPlaintext().getPlaintext()));
386386

387-
if (StringUtils.hasText(signRequest.getAlgorithm())) {
388-
request.put("algorithm", signRequest.getAlgorithm());
387+
if (StringUtils.hasText(signRequest.getHashAlgorithm())) {
388+
request.put("hash_algorithm", signRequest.getHashAlgorithm());
389+
}
390+
391+
if (StringUtils.hasText(signRequest.getSignatureAlgorithm())) {
392+
request.put("signature_algorithm", signRequest.getSignatureAlgorithm());
389393
}
390394

391395
String signature = (String) this.vaultOperations.write(String.format("%s/sign/%s", this.path, keyName), request)
@@ -422,8 +426,12 @@ public SignatureValidation verify(String keyName, VaultSignatureVerificationRequ
422426
request.put("signature", verificationRequest.getSignature().getSignature());
423427
}
424428

425-
if (StringUtils.hasText(verificationRequest.getAlgorithm())) {
426-
request.put("algorithm", verificationRequest.getAlgorithm());
429+
if (StringUtils.hasText(verificationRequest.getHashAlgorithm())) {
430+
request.put("hash_algorithm", verificationRequest.getHashAlgorithm());
431+
}
432+
433+
if (StringUtils.hasText(verificationRequest.getSignatureAlgorithm())) {
434+
request.put("signature_algorithm", verificationRequest.getSignatureAlgorithm());
427435
}
428436

429437
Map<String, Object> response = this.vaultOperations

spring-vault-core/src/main/java/org/springframework/vault/support/VaultSignRequest.java

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ public class VaultSignRequest {
2929

3030
private final Plaintext plaintext;
3131

32-
private final @Nullable String algorithm;
32+
private final @Nullable String hashAlgorithm;
3333

34-
private VaultSignRequest(Plaintext plaintext, @Nullable String algorithm) {
34+
private final @Nullable String signatureAlgorithm;
35+
36+
private VaultSignRequest(Plaintext plaintext, @Nullable String hashAlgorithm, @Nullable String signatureAlgorithm) {
3537

3638
this.plaintext = plaintext;
37-
this.algorithm = algorithm;
39+
this.hashAlgorithm = hashAlgorithm;
40+
this.signatureAlgorithm = signatureAlgorithm;
3841
}
3942

4043
/**
@@ -61,12 +64,21 @@ public Plaintext getPlaintext() {
6164
}
6265

6366
/**
64-
* @return algorithm used for creating the signature or {@literal null} to use the
65-
* default algorithm.
67+
* @return hashAlgorithm used for creating the signature or {@literal null} to use the
68+
* default hash algorithm.
69+
*/
70+
@Nullable
71+
public String getHashAlgorithm() {
72+
return this.hashAlgorithm;
73+
}
74+
75+
/**
76+
* @return signatureAlgorithm used for creating the signature when using a RSA key or
77+
* {@literal null} to use the default signature algorithm.
6678
*/
6779
@Nullable
68-
public String getAlgorithm() {
69-
return this.algorithm;
80+
public String getSignatureAlgorithm() {
81+
return this.signatureAlgorithm;
7082
}
7183

7284
/**
@@ -76,7 +88,9 @@ public static class VaultSignRequestBuilder {
7688

7789
private @Nullable Plaintext plaintext;
7890

79-
private @Nullable String algorithm;
91+
private @Nullable String hashAlgorithm;
92+
93+
private @Nullable String signatureAlgorithm;
8094

8195
/**
8296
* Configure the input to be used to create the signature.
@@ -92,17 +106,34 @@ public VaultSignRequestBuilder plaintext(Plaintext input) {
92106
}
93107

94108
/**
95-
* Configure the algorithm to be used for the operation.
96-
* @param algorithm Specify the algorithm to be used for the operation. Supported
97-
* algorithms are: {@literal sha2-224}, {@literal sha2-256}, {@literal sha2-384},
98-
* {@literal sha2-512}. Defaults to {@literal sha2-256} if not set.
109+
* Configure the hash algorithm to be used for the operation.
110+
* @param hashAlgorithm Specify the hash algorithm to be used for the operation.
111+
* Supported algorithms are: {@literal sha1}, {@literal sha2-224},
112+
* {@literal sha2-256}, {@literal sha2-384}, {@literal sha2-512}. Defaults to
113+
* {@literal sha2-256} if not set.
114+
* @return {@code this} {@link VaultSignRequestBuilder}.
115+
*/
116+
public VaultSignRequestBuilder hashAlgorithm(String hashAlgorithm) {
117+
118+
Assert.hasText(hashAlgorithm, "Hash algorithm must not be null or empty");
119+
120+
this.hashAlgorithm = hashAlgorithm;
121+
return this;
122+
}
123+
124+
/**
125+
* Configure the signature algorithm to be used for the operation when using a RSA
126+
* key.
127+
* @param signatureAlgorithm Specify the signature algorithm to be used for the
128+
* operation. Supported algorithms are: {@literal pss}, {@literal pkcs1v15}.
129+
* Defaults to {@literal pss} if not set.
99130
* @return {@code this} {@link VaultSignRequestBuilder}.
100131
*/
101-
public VaultSignRequestBuilder algorithm(String algorithm) {
132+
public VaultSignRequestBuilder signatureAlgorithm(String signatureAlgorithm) {
102133

103-
Assert.hasText(algorithm, "Algorithm must not be null or empty");
134+
Assert.hasText(signatureAlgorithm, "Hash algorithm must not be null or empty");
104135

105-
this.algorithm = algorithm;
136+
this.signatureAlgorithm = signatureAlgorithm;
106137
return this;
107138
}
108139

@@ -115,7 +146,7 @@ public VaultSignRequest build() {
115146

116147
Assert.notNull(this.plaintext, "Plaintext input must not be null");
117148

118-
return new VaultSignRequest(this.plaintext, this.algorithm);
149+
return new VaultSignRequest(this.plaintext, this.hashAlgorithm, this.signatureAlgorithm);
119150
}
120151

121152
}

spring-vault-core/src/main/java/org/springframework/vault/support/VaultSignatureVerificationRequest.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ public class VaultSignatureVerificationRequest {
3333

3434
private final @Nullable Hmac hmac;
3535

36-
private final @Nullable String algorithm;
36+
private final @Nullable String hashAlgorithm;
37+
38+
private final @Nullable String signatureAlgorithm;
3739

3840
private VaultSignatureVerificationRequest(Plaintext plaintext, @Nullable Signature signature, @Nullable Hmac hmac,
39-
@Nullable String algorithm) {
41+
@Nullable String hashAlgorithm, @Nullable String signatureAlgorithm) {
4042

4143
this.plaintext = plaintext;
4244
this.signature = signature;
4345
this.hmac = hmac;
44-
this.algorithm = algorithm;
46+
this.hashAlgorithm = hashAlgorithm;
47+
this.signatureAlgorithm = signatureAlgorithm;
4548
}
4649

4750
/**
@@ -101,12 +104,21 @@ public Hmac getHmac() {
101104
}
102105

103106
/**
104-
* @return algorithm used for verifying the signature or {@literal null} to use the
105-
* default algorithm.
107+
* @return hash algorithm used for verifying the signature or {@literal null} to use
108+
* the default algorithm.
109+
*/
110+
@Nullable
111+
public String getHashAlgorithm() {
112+
return this.hashAlgorithm;
113+
}
114+
115+
/**
116+
* @return signature algorithm used for verifying the signature when using a RSA key
117+
* or {@literal null} to use the default algorithm.
106118
*/
107119
@Nullable
108-
public String getAlgorithm() {
109-
return this.algorithm;
120+
public String getSignatureAlgorithm() {
121+
return this.signatureAlgorithm;
110122
}
111123

112124
/**
@@ -120,7 +132,9 @@ public static class VaultSignatureVerificationRequestBuilder {
120132

121133
private @Nullable Hmac hmac;
122134

123-
private @Nullable String algorithm;
135+
private @Nullable String hashAlgorithm;
136+
137+
private @Nullable String signatureAlgorithm;
124138

125139
/**
126140
* Configure the {@link Plaintext} input to be used to verify the signature.
@@ -168,17 +182,34 @@ public VaultSignatureVerificationRequestBuilder hmac(Hmac hmac) {
168182
}
169183

170184
/**
171-
* Configure the algorithm to be used for the operation.
172-
* @param algorithm Specify the algorithm to be used for the operation. Supported
173-
* algorithms are: {@literal sha2-224}, {@literal sha2-256}, {@literal sha2-384},
174-
* {@literal sha2-512}. Defaults to {@literal sha2-256} if not set.
185+
* Configure the hash algorithm to be used for the operation.
186+
* @param hashAlgorithm Specify the hash algorithm to be used for the operation.
187+
* Supported algorithms are: {@literal sha1}, {@literal sha2-224},
188+
* {@literal sha2-256}, {@literal sha2-384}, {@literal sha2-512}. Defaults to
189+
* {@literal sha2-256} if not set.
190+
* @return {@code this} {@link VaultSignatureVerificationRequestBuilder}.
191+
*/
192+
public VaultSignatureVerificationRequestBuilder hashAlgorithm(String hashAlgorithm) {
193+
194+
Assert.hasText(hashAlgorithm, "Hash algorithm must not be null or empty");
195+
196+
this.hashAlgorithm = hashAlgorithm;
197+
return this;
198+
}
199+
200+
/**
201+
* Configure the signature algorithm to be used for the operation when using a RSA
202+
* key.
203+
* @param signatureAlgorithm Specify the signature algorithm to be used for the
204+
* operation. Supported algorithms are: {@literal pss}, {@literal pkcs1v15}.
205+
* Defaults to {@literal pss} if not set.
175206
* @return {@code this} {@link VaultSignatureVerificationRequestBuilder}.
176207
*/
177-
public VaultSignatureVerificationRequestBuilder algorithm(String algorithm) {
208+
public VaultSignatureVerificationRequestBuilder signatureAlgorithm(String signatureAlgorithm) {
178209

179-
Assert.hasText(algorithm, "Algorithm must not be null or empty");
210+
Assert.hasText(signatureAlgorithm, "Signature algorithm must not be null or empty");
180211

181-
this.algorithm = algorithm;
212+
this.signatureAlgorithm = signatureAlgorithm;
182213
return this;
183214
}
184215

@@ -193,7 +224,8 @@ public VaultSignatureVerificationRequest build() {
193224
Assert.notNull(this.input, "Plaintext input must not be null");
194225
Assert.isTrue(this.hmac != null || this.signature != null, "Either Signature or Hmac must not be null");
195226

196-
return new VaultSignatureVerificationRequest(this.input, this.signature, this.hmac, this.algorithm);
227+
return new VaultSignatureVerificationRequest(this.input, this.signature, this.hmac, this.hashAlgorithm,
228+
this.signatureAlgorithm);
197229
}
198230

199231
}

spring-vault-core/src/test/java/org/springframework/vault/core/VaultTransitTemplateIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ void signWithCustomAlgorithmShouldCreateSignature() {
683683
String keyName = createEcdsaP256Key();
684684

685685
Plaintext plaintext = Plaintext.of("hello-world");
686-
VaultSignRequest request = VaultSignRequest.builder().plaintext(plaintext).algorithm("sha2-512").build();
686+
VaultSignRequest request = VaultSignRequest.builder().plaintext(plaintext).hashAlgorithm("sha2-512").build();
687687

688688
Signature signature = this.transitOperations.sign(keyName, request);
689689
assertThat(signature.getSignature()).isNotEmpty();
@@ -723,12 +723,12 @@ void shouldVerifyValidSignatureWithCustomAlgorithm() {
723723
String keyName = createEcdsaP256Key();
724724

725725
Plaintext plaintext = Plaintext.of("hello-world");
726-
VaultSignRequest request = VaultSignRequest.builder().plaintext(plaintext).algorithm("sha2-512").build();
726+
VaultSignRequest request = VaultSignRequest.builder().plaintext(plaintext).hashAlgorithm("sha2-512").build();
727727

728728
Signature signature = this.transitOperations.sign(keyName, request);
729729

730730
VaultSignatureVerificationRequest verificationRequest = VaultSignatureVerificationRequest.builder()
731-
.algorithm("sha2-512").plaintext(plaintext).signature(signature).build();
731+
.hashAlgorithm("sha2-512").plaintext(plaintext).signature(signature).build();
732732

733733
SignatureValidation valid = this.transitOperations.verify(keyName, verificationRequest);
734734
assertThat(valid).isEqualTo(SignatureValidation.valid());

0 commit comments

Comments
 (0)