@@ -94,22 +94,23 @@ public static void getPasswordParam(String paddedEncryptedPwd) {
9494 if (encryptedPwd != null && encryptedPwd .length >= 7 ) {
9595 int index = 0 ;
9696
97- mkCipher = encryptedPwd [index ];
98- mkKeySize = Integer .parseInt (encryptedPwd [++index ]);
99- saltSize = Integer .parseInt (encryptedPwd [++index ]);
100- pbeAlgo = encryptedPwd [++index ];
101- mdAlgo = encryptedPwd [++index ];
102- iterationCount = Integer .parseInt (encryptedPwd [++index ]);
103- salt = encryptedPwd [++index ];
104- password = encryptedPwd [++index ];
97+ mkCipher = encryptedPwd [index ];
98+ mkKeySize = Integer .parseInt (encryptedPwd [++index ]);
99+ int tempSaltSize = Integer .parseInt (encryptedPwd [++index ]);
100+ pbeAlgo = encryptedPwd [++index ];
101+ saltSize = calculateCompliantSaltSize (tempSaltSize , SupportedPBECryptoAlgo .valueOf (pbeAlgo ));
102+ mdAlgo = encryptedPwd [++index ];
103+ iterationCount = Integer .parseInt (encryptedPwd [++index ]);
104+ salt = encryptedPwd [++index ];
105+ password = encryptedPwd [++index ];
105106 } else {
106- mkCipher = DEFAULT_MK_CIPHER ;
107- mkKeySize = DEFAULT_MK_KeySize ;
108- saltSize = DEFAULT_SALT_SIZE ;
109- pbeAlgo = isFipsEnabled ? SupportedPBECryptoAlgo . PBEWithMD5AndTripleDES . getAlgoName () : defaultCryptAlgo . getAlgoName ( );
110- mdAlgo = defaultMdAlgo ;
111- password = paddedEncryptedPwd ;
112- salt = password ;
107+ mkCipher = DEFAULT_MK_CIPHER ;
108+ mkKeySize = DEFAULT_MK_KeySize ;
109+ pbeAlgo = isFipsEnabled ? SupportedPBECryptoAlgo . PBEWithMD5AndTripleDES . getAlgoName () : defaultCryptAlgo . getAlgoName () ;
110+ saltSize = calculateCompliantSaltSize ( DEFAULT_SALT_SIZE , SupportedPBECryptoAlgo . valueOf ( pbeAlgo ) );
111+ mdAlgo = defaultMdAlgo ;
112+ password = paddedEncryptedPwd ;
113+ salt = password ;
113114
114115 if (password != null ) {
115116 iterationCount = password .toCharArray ().length + 1 ;
@@ -181,17 +182,16 @@ public void init() {
181182 defaultCryptAlgo = isFipsEnabled ? SupportedPBECryptoAlgo .PBKDF2WithHmacSHA256 : defaultCryptAlgo ;
182183 mkCipher = getConfig ("ranger.kms.service.masterkey.password.cipher" , DEFAULT_MK_CIPHER );
183184 mkKeySize = getIntConfig ("ranger.kms.service.masterkey.password.size" , DEFAULT_MK_KeySize );
184- saltSize = getIntConfig ("ranger.kms.service.masterkey.password.salt.size" , DEFAULT_SALT_SIZE );
185- salt = getConfig ("ranger.kms.service.masterkey.password.salt" , DEFAULT_SALT );
186185 pbeAlgo = getConfig ("ranger.kms.service.masterkey.password.encryption.algorithm" , defaultCryptAlgo .getAlgoName ());
187186 encrCryptoAlgo = SupportedPBECryptoAlgo .valueOf (pbeAlgo );
187+ saltSize = calculateCompliantSaltSize (getIntConfig ("ranger.kms.service.masterkey.password.salt.size" , DEFAULT_SALT_SIZE ), encrCryptoAlgo );
188+ salt = getConfig ("ranger.kms.service.masterkey.password.salt" , DEFAULT_SALT );
188189 mdAlgo = getConfig ("ranger.kms.service.masterkey.password.md.algorithm" , defaultMdAlgo );
189190 iterationCount = getIntConfig ("ranger.kms.service.masterkey.password.iteration.count" , DEFAULT_ITERATION_COUNT );
190191 paddingString = Joiner .on ("," ).skipNulls ().join (mkCipher , mkKeySize , saltSize , pbeAlgo , mdAlgo , iterationCount , salt );
191192
192193 logger .info ("Selected DEFAULT_CRYPT_ALGO={}" , defaultCryptAlgo );
193- logger .info ("Selected MD_ALGO={}" , mdAlgo );
194- logger .info ("Selected ENCR_CRYPTO_ALGO={}" , encrCryptoAlgo );
194+ logger .info ("MK metadata={}" , paddingString );
195195 logger .debug ("<== RangerMasterKey.init()" );
196196 }
197197
@@ -541,15 +541,12 @@ private PBEKeySpec getPBEParameterSpec(String password, SupportedPBECryptoAlgo e
541541 logger .debug ("==> RangerMasterKey.getPBEParameterSpec()" );
542542
543543 PBEKeySpec pbeKeySpec ;
544+ char [] compliantPwd = getCompliantPassword (password , encrAlgo ).toCharArray ();
545+
544546 if (SupportedPBECryptoAlgo .isFIPSCompliantAlgorithm (encrAlgo )) {
545- // For FIPS, salt size must be at least 128 bits, that is, at least 16 in length.
546- int saltSize = RangerMasterKey .saltSize ;
547- while (saltSize < 16 ) {
548- saltSize = saltSize * 2 ;
549- }
550- pbeKeySpec = new PBEKeySpec (getFIPSCompliantPassword (password ).toCharArray (), generateSalt (saltSize ), iterationCount , encrAlgo .getKeyLength ());
547+ pbeKeySpec = new PBEKeySpec (compliantPwd , generateSalt (saltSize ), iterationCount , encrAlgo .getKeyLength ());
551548 } else {
552- pbeKeySpec = new PBEKeySpec (password . toCharArray () , generateSalt (RangerMasterKey . saltSize ), iterationCount );
549+ pbeKeySpec = new PBEKeySpec (compliantPwd , generateSalt (saltSize ), iterationCount );
553550 }
554551 return pbeKeySpec ;
555552 }
@@ -570,14 +567,32 @@ private byte[] generateSalt(int saltSize) throws Throwable {
570567 If provided password is less than 14, this method appends the same password till it reaches the minimum length of 14.
571568 And it is for FIPS only.
572569 */
573- private String getFIPSCompliantPassword (String password ) {
570+ private String getCompliantPassword (String password , SupportedPBECryptoAlgo encrAlgo ) {
574571 String newPwd = password ;
575- while (newPwd .length () < 14 ) {
576- newPwd = newPwd .concat (password );
572+
573+ if (encrAlgo .getMinPwdLength ().isPresent ()) {
574+ int requiredPwdLength = encrAlgo .getMinPwdLength ().get ();
575+ while (newPwd .length () < requiredPwdLength ) {
576+ newPwd = newPwd .concat (password );
577+ }
577578 }
579+
578580 return newPwd ;
579581 }
580582
583+ // For FIPS, salt size must be at least 128 bits, that is, at least 16 in length.
584+ private static int calculateCompliantSaltSize (int saltSize , SupportedPBECryptoAlgo encrAlgo ) {
585+ int compliantSaltSize = saltSize ;
586+ if (encrAlgo .getMinSaltSize ().isPresent ()) {
587+ int minSaltSize = encrAlgo .getMinSaltSize ().get ();
588+ while (compliantSaltSize < minSaltSize ) {
589+ compliantSaltSize = compliantSaltSize * 2 ;
590+ }
591+ }
592+
593+ return compliantSaltSize ;
594+ }
595+
581596 private byte [] encryptKey (byte [] data , PBEKeySpec keyspec ) throws Throwable {
582597 logger .debug ("==> RangerMasterKey.encryptKey()" );
583598
0 commit comments