2020package org .apache .airavata .credential .store .utils ;
2121
2222import java .io .*;
23- import java .security .KeyStore ;
2423import java .security .SecureRandom ;
2524import java .util .Base64 ;
26- import javax .crypto .Cipher ;
2725import javax .crypto .KeyGenerator ;
2826import javax .crypto .SecretKey ;
2927import javax .crypto .spec .SecretKeySpec ;
3028import org .apache .airavata .common .exception .ApplicationSettingsException ;
3129import org .apache .airavata .common .utils .ApplicationSettings ;
3230import org .apache .airavata .common .utils .DefaultKeyStorePasswordCallback ;
31+ import org .apache .airavata .common .utils .KeyStorePasswordCallback ;
32+ import org .apache .airavata .common .utils .SecurityUtil ;
3333import org .apache .airavata .credential .store .credential .Credential ;
3434import org .apache .airavata .credential .store .store .CredentialStoreException ;
3535import org .slf4j .Logger ;
@@ -102,30 +102,9 @@ public static Credential deserializeCredential(byte[] serializedCredential) thro
102102 */
103103 public static byte [] serializeCredentialWithEncryption (Credential credential ) throws CredentialStoreException {
104104 try {
105- // First serialize the credential
106105 byte [] serializedCredential = serializeCredential (credential );
107-
108- // Get the secret key from keystore
109106 SecretKey secretKey = getSecretKeyFromKeyStore ();
110-
111- // Use AES/CBC/PKCS5Padding to ensure IV is used and generated
112- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
113- cipher .init (Cipher .ENCRYPT_MODE , secretKey );
114- byte [] encryptedData = cipher .doFinal (serializedCredential );
115-
116- // Get the IV (should not be null)
117- byte [] iv = cipher .getIV ();
118- if (iv == null ) {
119- throw new CredentialStoreException ("IV is null after cipher initialization" );
120- }
121-
122- // Combine IV and encrypted data
123- byte [] combined = new byte [iv .length + encryptedData .length ];
124- System .arraycopy (iv , 0 , combined , 0 , iv .length );
125- System .arraycopy (encryptedData , 0 , combined , iv .length , encryptedData .length );
126-
127- return combined ;
128-
107+ return SecurityUtil .encrypt (serializedCredential , secretKey );
129108 } catch (Exception e ) {
130109 logger .error ("Error encrypting credential" , e );
131110 throw new CredentialStoreException ("Error encrypting credential" , e );
@@ -141,21 +120,8 @@ public static byte[] serializeCredentialWithEncryption(Credential credential) th
141120 public static Credential deserializeCredentialWithDecryption (byte [] encryptedCredential )
142121 throws CredentialStoreException {
143122 try {
144- // Get the secret key from keystore
145123 SecretKey secretKey = getSecretKeyFromKeyStore ();
146-
147- // Extract IV and encrypted data
148- byte [] iv = new byte [16 ]; // AES IV size
149- byte [] encryptedData = new byte [encryptedCredential .length - 16 ];
150- System .arraycopy (encryptedCredential , 0 , iv , 0 , 16 );
151- System .arraycopy (encryptedCredential , 16 , encryptedData , 0 , encryptedData .length );
152-
153- // Decrypt the data
154- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
155- cipher .init (Cipher .DECRYPT_MODE , secretKey , new javax .crypto .spec .IvParameterSpec (iv ));
156- byte [] decryptedData = cipher .doFinal (encryptedData );
157-
158- // Deserialize the decrypted data
124+ byte [] decryptedData = SecurityUtil .decrypt (encryptedCredential , secretKey );
159125 return deserializeCredential (decryptedData );
160126
161127 } catch (Exception e ) {
@@ -177,25 +143,9 @@ public static byte[] serializeCredentialWithEncryption(
177143 Credential credential , String keystorePath , String keyAlias , KeyStorePasswordCallback passwordCallback )
178144 throws CredentialStoreException {
179145 try {
180- // First serialize the credential
181146 byte [] serializedCredential = serializeCredential (credential );
182-
183- // Get the secret key from custom keystore
184147 SecretKey secretKey = getSecretKeyFromCustomKeyStore (keystorePath , keyAlias , passwordCallback );
185-
186- // Encrypt the serialized data
187- Cipher cipher = Cipher .getInstance ("AES" );
188- cipher .init (Cipher .ENCRYPT_MODE , secretKey );
189- byte [] encryptedData = cipher .doFinal (serializedCredential );
190-
191- // Combine IV and encrypted data
192- byte [] iv = cipher .getIV ();
193- byte [] combined = new byte [iv .length + encryptedData .length ];
194- System .arraycopy (iv , 0 , combined , 0 , iv .length );
195- System .arraycopy (encryptedData , 0 , combined , iv .length , encryptedData .length );
196-
197- return combined ;
198-
148+ return SecurityUtil .encrypt (serializedCredential , secretKey );
199149 } catch (Exception e ) {
200150 logger .error ("Error encrypting credential with custom keystore" , e );
201151 throw new CredentialStoreException ("Error encrypting credential with custom keystore" , e );
@@ -215,23 +165,9 @@ public static Credential deserializeCredentialWithDecryption(
215165 byte [] encryptedCredential , String keystorePath , String keyAlias , KeyStorePasswordCallback passwordCallback )
216166 throws CredentialStoreException {
217167 try {
218- // Get the secret key from custom keystore
219168 SecretKey secretKey = getSecretKeyFromCustomKeyStore (keystorePath , keyAlias , passwordCallback );
220-
221- // Extract IV and encrypted data
222- byte [] iv = new byte [16 ]; // AES IV size
223- byte [] encryptedData = new byte [encryptedCredential .length - 16 ];
224- System .arraycopy (encryptedCredential , 0 , iv , 0 , 16 );
225- System .arraycopy (encryptedCredential , 16 , encryptedData , 0 , encryptedData .length );
226-
227- // Decrypt the data
228- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
229- cipher .init (Cipher .DECRYPT_MODE , secretKey , new javax .crypto .spec .IvParameterSpec (iv ));
230- byte [] decryptedData = cipher .doFinal (encryptedData );
231-
232- // Deserialize the decrypted data
169+ byte [] decryptedData = SecurityUtil .decrypt (encryptedCredential , secretKey );
233170 return deserializeCredential (decryptedData );
234-
235171 } catch (Exception e ) {
236172 logger .error ("Error decrypting credential with custom keystore" , e );
237173 throw new CredentialStoreException ("Error decrypting credential with custom keystore" , e );
@@ -244,12 +180,7 @@ public static Credential deserializeCredentialWithDecryption(
244180 * @throws Exception if key retrieval fails
245181 */
246182 private static SecretKey getSecretKeyFromKeyStore () throws Exception {
247- KeyStore keyStore = KeyStore .getInstance ("JKS" );
248- try (FileInputStream fis = new FileInputStream (KEYSTORE_PATH )) {
249- keyStore .load (fis , KEYSTORE_PASSWORD_CALLBACK .getStorePassword ());
250- }
251-
252- return (SecretKey ) keyStore .getKey (KEY_ALIAS , KEYSTORE_PASSWORD_CALLBACK .getSecretKeyPassPhrase (KEY_ALIAS ));
183+ return (SecretKey ) SecurityUtil .getSymmetricKey (KEYSTORE_PATH , KEY_ALIAS , KEYSTORE_PASSWORD_CALLBACK );
253184 }
254185
255186 /**
@@ -262,12 +193,7 @@ private static SecretKey getSecretKeyFromKeyStore() throws Exception {
262193 */
263194 private static SecretKey getSecretKeyFromCustomKeyStore (
264195 String keystorePath , String keyAlias , KeyStorePasswordCallback passwordCallback ) throws Exception {
265- KeyStore keyStore = KeyStore .getInstance ("JKS" );
266- try (FileInputStream fis = new FileInputStream (keystorePath )) {
267- keyStore .load (fis , passwordCallback .getStorePassword ());
268- }
269-
270- return (SecretKey ) keyStore .getKey (keyAlias , passwordCallback .getSecretKeyPassPhrase (keyAlias ));
196+ return (SecretKey ) SecurityUtil .getSymmetricKey (keystorePath , keyAlias , passwordCallback );
271197 }
272198
273199 /**
@@ -299,13 +225,4 @@ public static SecretKey stringToSecretKey(String keyString) {
299225 byte [] keyBytes = Base64 .getDecoder ().decode (keyString );
300226 return new SecretKeySpec (keyBytes , "AES" );
301227 }
302-
303- /**
304- * Interface for keystore password callbacks.
305- */
306- public interface KeyStorePasswordCallback {
307- char [] getStorePassword ();
308-
309- char [] getSecretKeyPassPhrase (String keyAlias );
310- }
311228}
0 commit comments