Skip to content

Commit 55d56f8

Browse files
committed
GUACAMOLE-1286: Add support for providing a custom IV.
1 parent f7a9d87 commit 55d56f8

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

extensions/guacamole-auth-json/src/main/java/org/apache/guacamole/auth/json/CryptoService.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class CryptoService {
7373
* HMAC signature itself as the IV. For our purposes, where the encrypted
7474
* value becomes an authentication token, this is OK.
7575
*/
76-
private static final IvParameterSpec NULL_IV = new IvParameterSpec(new byte[] {
76+
public static final IvParameterSpec NULL_IV = new IvParameterSpec(new byte[] {
7777
0, 0, 0, 0, 0, 0, 0, 0,
7878
0, 0, 0, 0, 0, 0, 0, 0
7979
});
@@ -125,6 +125,10 @@ public SecretKey createSignatureKey(byte[] keyBytes) {
125125
*
126126
* @param cipherText
127127
* The ciphertext to decrypt.
128+
*
129+
* @param ivStr
130+
* The string container the crypto IV, or null if one is not
131+
* provided and the NULL_IV should be used.
128132
*
129133
* @return
130134
* The plaintext which results from decrypting the ciphertext with the
@@ -133,13 +137,20 @@ public SecretKey createSignatureKey(byte[] keyBytes) {
133137
* @throws GuacamoleException
134138
* If any error at all occurs during decryption.
135139
*/
136-
public byte[] decrypt(Key key, byte[] cipherText) throws GuacamoleException {
140+
public byte[] decrypt(Key key, byte[] cipherText, String ivStr)
141+
throws GuacamoleException {
137142

138143
try {
139144

145+
// Use NULL_IV by default, but, if an IV has been provided as
146+
// part of the request, use that, instead.
147+
IvParameterSpec iv = NULL_IV;
148+
if (ivStr == null || ivStr.isEmpty())
149+
iv = new IvParameterSpec(ivStr.getBytes());
150+
140151
// Init cipher for descryption using secret key
141152
Cipher cipher = Cipher.getInstance(DECRYPTION_CIPHER_NAME);
142-
cipher.init(Cipher.DECRYPT_MODE, key, NULL_IV);
153+
cipher.init(Cipher.DECRYPT_MODE, key, iv);
143154

144155
// Perform decryption
145156
return cipher.doFinal(cipherText);

extensions/guacamole-auth-json/src/main/java/org/apache/guacamole/auth/json/user/UserDataService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ public class UserDataService {
100100
* HMAC/SHA-256.
101101
*/
102102
public static final String ENCRYPTED_DATA_PARAMETER = "data";
103+
104+
/**
105+
* The name of the HTTP parameter from which the cryptographic IV should be
106+
* read, if provided with the query. If this parameter is not provided, the
107+
* {@link CryptoService#NULL_IV} will be used.
108+
*/
109+
public static final String ENCRYPTED_IV_PARAMETER = "iv";
103110

104111
/**
105112
* Derives a new UserData object from the data contained within the given
@@ -129,14 +136,18 @@ public UserData fromCredentials(Credentials credentials) {
129136
String base64 = credentials.getParameter(ENCRYPTED_DATA_PARAMETER);
130137
if (base64 == null)
131138
return null;
139+
140+
// Get the IV string from the credentials
141+
String ivStr = credentials.getParameter(ENCRYPTED_IV_PARAMETER);
132142

133143
// Decrypt base64-encoded parameter
134144
try {
135145

136146
// Decrypt using defined encryption key
137147
byte[] decrypted = cryptoService.decrypt(
138148
cryptoService.createEncryptionKey(confService.getSecretKey()),
139-
BaseEncoding.base64().decode(base64)
149+
BaseEncoding.base64().decode(base64),
150+
ivStr
140151
);
141152

142153
// Abort if decrypted value cannot possibly have a signature AND data

0 commit comments

Comments
 (0)