Skip to content

Commit 3e04779

Browse files
console proxy: use AeadBase64Encryptor instead of AES/CBC/PKCS5Padding (#7237)
1 parent 5e5d194 commit 3e04779

File tree

3 files changed

+25
-137
lines changed

3 files changed

+25
-137
lines changed

server/src/main/java/com/cloud/servlet/ConsoleProxyPasswordBasedEncryptor.java

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,15 @@
1616
// under the License.
1717
package com.cloud.servlet;
1818

19-
import java.security.InvalidAlgorithmParameterException;
20-
import java.security.InvalidKeyException;
21-
import java.security.NoSuchAlgorithmException;
22-
23-
import javax.crypto.BadPaddingException;
24-
import javax.crypto.Cipher;
25-
import javax.crypto.IllegalBlockSizeException;
26-
import javax.crypto.NoSuchPaddingException;
27-
import javax.crypto.spec.IvParameterSpec;
28-
import javax.crypto.spec.SecretKeySpec;
29-
3019
import org.apache.commons.codec.binary.Base64;
3120
import org.apache.log4j.Logger;
3221

3322
import com.google.gson.Gson;
3423
import com.google.gson.GsonBuilder;
3524

25+
import com.cloud.utils.crypt.AeadBase64Encryptor;
26+
import com.cloud.utils.crypt.Base64Encryptor;
27+
3628
// To maintain independency of console proxy project, we duplicate this class from console proxy project
3729
public class ConsoleProxyPasswordBasedEncryptor {
3830
private static final Logger s_logger = Logger.getLogger(ConsoleProxyPasswordBasedEncryptor.class);
@@ -51,65 +43,16 @@ public String encryptText(String text) {
5143
if (text == null || text.isEmpty())
5244
return text;
5345

54-
try {
55-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
56-
SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
57-
58-
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
59-
60-
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
61-
return Base64.encodeBase64URLSafeString(encryptedBytes);
62-
} catch (NoSuchAlgorithmException e) {
63-
s_logger.error("Unexpected exception ", e);
64-
return null;
65-
} catch (NoSuchPaddingException e) {
66-
s_logger.error("Unexpected exception ", e);
67-
return null;
68-
} catch (IllegalBlockSizeException e) {
69-
s_logger.error("Unexpected exception ", e);
70-
return null;
71-
} catch (BadPaddingException e) {
72-
s_logger.error("Unexpected exception ", e);
73-
return null;
74-
} catch (InvalidKeyException e) {
75-
s_logger.error("Unexpected exception ", e);
76-
return null;
77-
} catch (InvalidAlgorithmParameterException e) {
78-
s_logger.error("Unexpected exception ", e);
79-
return null;
80-
}
46+
Base64Encryptor encryptor = new AeadBase64Encryptor(keyIvPair.getKeyBytes(), keyIvPair.getIvBytes());
47+
return encryptor.encrypt(text);
8148
}
8249

8350
public String decryptText(String encryptedText) {
8451
if (encryptedText == null || encryptedText.isEmpty())
8552
return encryptedText;
8653

87-
try {
88-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
89-
SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
90-
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
91-
92-
byte[] encryptedBytes = Base64.decodeBase64(encryptedText);
93-
return new String(cipher.doFinal(encryptedBytes));
94-
} catch (NoSuchAlgorithmException e) {
95-
s_logger.error("Unexpected exception ", e);
96-
return null;
97-
} catch (NoSuchPaddingException e) {
98-
s_logger.error("Unexpected exception ", e);
99-
return null;
100-
} catch (IllegalBlockSizeException e) {
101-
s_logger.error("Unexpected exception ", e);
102-
return null;
103-
} catch (BadPaddingException e) {
104-
s_logger.error("Unexpected exception ", e);
105-
return null;
106-
} catch (InvalidKeyException e) {
107-
s_logger.error("Unexpected exception ", e);
108-
return null;
109-
} catch (InvalidAlgorithmParameterException e) {
110-
s_logger.error("Unexpected exception ", e);
111-
return null;
112-
}
54+
Base64Encryptor encryptor = new AeadBase64Encryptor(keyIvPair.getKeyBytes(), keyIvPair.getIvBytes());
55+
return encryptor.decrypt(encryptedText);
11356
}
11457

11558
public <T> String encryptObject(Class<?> clz, T obj) {

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyPasswordBasedEncryptor.java

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,15 @@
1616
// under the License.
1717
package com.cloud.consoleproxy;
1818

19-
import java.security.InvalidAlgorithmParameterException;
20-
import java.security.InvalidKeyException;
21-
import java.security.NoSuchAlgorithmException;
22-
23-
import javax.crypto.BadPaddingException;
24-
import javax.crypto.Cipher;
25-
import javax.crypto.IllegalBlockSizeException;
26-
import javax.crypto.NoSuchPaddingException;
27-
import javax.crypto.spec.IvParameterSpec;
28-
import javax.crypto.spec.SecretKeySpec;
29-
3019
import org.apache.commons.codec.binary.Base64;
3120
import org.apache.log4j.Logger;
3221

3322
import com.google.gson.Gson;
3423
import com.google.gson.GsonBuilder;
3524

36-
/**
37-
*
38-
* @author Kelven Yang
39-
* A simple password based encyrptor based on AES/CBC. It can serialize simple POJO object into URL safe string
40-
* and deserialize it back.
41-
*
42-
*/
25+
import com.cloud.utils.crypt.AeadBase64Encryptor;
26+
import com.cloud.utils.crypt.Base64Encryptor;
27+
4328
public class ConsoleProxyPasswordBasedEncryptor {
4429
private static final Logger s_logger = Logger.getLogger(ConsoleProxyPasswordBasedEncryptor.class);
4530

@@ -57,65 +42,16 @@ public String encryptText(String text) {
5742
if (text == null || text.isEmpty())
5843
return text;
5944

60-
try {
61-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
62-
SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
63-
64-
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
65-
66-
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
67-
return Base64.encodeBase64URLSafeString(encryptedBytes);
68-
} catch (NoSuchAlgorithmException e) {
69-
s_logger.error("Unexpected exception ", e);
70-
return null;
71-
} catch (NoSuchPaddingException e) {
72-
s_logger.error("Unexpected exception ", e);
73-
return null;
74-
} catch (IllegalBlockSizeException e) {
75-
s_logger.error("Unexpected exception ", e);
76-
return null;
77-
} catch (BadPaddingException e) {
78-
s_logger.error("Unexpected exception ", e);
79-
return null;
80-
} catch (InvalidKeyException e) {
81-
s_logger.error("Unexpected exception ", e);
82-
return null;
83-
} catch (InvalidAlgorithmParameterException e) {
84-
s_logger.error("Unexpected exception ", e);
85-
return null;
86-
}
45+
Base64Encryptor encryptor = new AeadBase64Encryptor(keyIvPair.getKeyBytes(), keyIvPair.getIvBytes());
46+
return encryptor.encrypt(text);
8747
}
8848

8949
public String decryptText(String encryptedText) {
9050
if (encryptedText == null || encryptedText.isEmpty())
9151
return encryptedText;
9252

93-
try {
94-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
95-
SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
96-
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
97-
98-
byte[] encryptedBytes = Base64.decodeBase64(encryptedText);
99-
return new String(cipher.doFinal(encryptedBytes));
100-
} catch (NoSuchAlgorithmException e) {
101-
s_logger.error("Unexpected exception ", e);
102-
return null;
103-
} catch (NoSuchPaddingException e) {
104-
s_logger.error("Unexpected exception ", e);
105-
return null;
106-
} catch (IllegalBlockSizeException e) {
107-
s_logger.error("Unexpected exception ", e);
108-
return null;
109-
} catch (BadPaddingException e) {
110-
s_logger.error("Unexpected exception ", e);
111-
return null;
112-
} catch (InvalidKeyException e) {
113-
s_logger.error("Unexpected exception ", e);
114-
return null;
115-
} catch (InvalidAlgorithmParameterException e) {
116-
s_logger.error("Unexpected exception ", e);
117-
return null;
118-
}
53+
Base64Encryptor encryptor = new AeadBase64Encryptor(keyIvPair.getKeyBytes(), keyIvPair.getIvBytes());
54+
return encryptor.decrypt(encryptedText);
11955
}
12056

12157
public <T> String encryptObject(Class<?> clz, T obj) {

utils/src/main/java/com/cloud/utils/crypt/AeadBase64Encryptor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
public class AeadBase64Encryptor implements Base64Encryptor {
3131
Aead aead = null;
32-
private final byte[] aad = new byte[]{};
32+
private byte[] aad = new byte[]{};
3333

34-
public AeadBase64Encryptor(byte[] key) {
34+
private void initEncryptor(byte[] key) {
3535
try {
3636
AeadConfig.register();
3737
MessageDigest digest = MessageDigest.getInstance("SHA-256");
@@ -42,6 +42,15 @@ public AeadBase64Encryptor(byte[] key) {
4242
}
4343
}
4444

45+
public AeadBase64Encryptor(byte[] key) {
46+
initEncryptor(key);
47+
}
48+
49+
public AeadBase64Encryptor(byte[] key, byte[] aad) {
50+
initEncryptor(key);
51+
this.aad = aad;
52+
}
53+
4554
@Override
4655
public String encrypt(String plain) {
4756
try {

0 commit comments

Comments
 (0)