8 use Phinx\Migration\AbstractMigration;
10 define(
"PARMENCDEC_CIPHER_MODE",
"aes-256-cbc");
12 define(
"PARMENCDEC_AUTH_HASH_ALGO",
"sha256");
14 function GetOpenSSLKeyBilbo() {
16 return "VXVtbYOrbt5avJme6ihbeS9MznYMPe9a";
19 function GetOpenSSLKeyBugs() {
20 return "w0ki5QwpYWkM2FpitNEsosG9HGL9uogS";
26 function getOpenSSLKey($key_suffix, $bit_size=
'256') {
30 if ($bit_size !=
'256')
31 throw new exception(
"Invalid key size. Supported: [256] bits.", 3);
33 $hashKeyBilbo = GetOpenSSLKeyBilbo() . $key_suffix;
34 $hashKeyBugs = GetOpenSSLKeyBugs() . $key_suffix;
37 $defaultKey = hash_hmac(
'sha256', $hashKeyBilbo, $hashKeyBugs, $raw_output);
52 function getEncryptionAuthHash($message, $key_suffix, $auth_hash_algo, $auth_hash_binary) {
53 if ($auth_hash_algo !=
'sha1' && $auth_hash_algo !=
'sha256') {
54 throw new exception(
"Invalid hash algorithm: ". $auth_hash_algo, 2);
56 $hashKeyBilbo = GetOpenSSLKeyBilbo() . $key_suffix;
57 return hash_hmac($auth_hash_algo, $message, $hashKeyBilbo, $auth_hash_binary);
64 function hcuOpenSSLEncrypt($message,
66 $method=
'aes-256-cbc',
67 $auth_hash_algo=
'sha1',
68 $auth_hash_binary=
true,
73 if ($auth_hash_algo !=
'sha1' && $auth_hash_algo !=
'sha256') {
74 throw new exception(
"Invalid hash algorithm: ". $auth_hash_algo, 2);
81 $ivsize = openssl_cipher_iv_length($method);
83 $iv = openssl_random_pseudo_bytes($ivsize);
85 $iv = mb_substr($iv, 0, $ivsize,
'8bit');
92 if($context ==
"connect_chkfree" ||
93 $context ==
"connect_ezcard" ||
94 $context ==
"connect_certegy" ||
95 $context ==
"connect_digital" ||
96 $context ==
"connect_ipay" ||
97 $context ==
"connect_vsoft" ||
98 $context ==
"connect_mvi")
102 $openssl_options = OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING;
103 $encryptionKey = $key_suffix;
108 $openssl_options = OPENSSL_RAW_DATA;
109 $encryptionKey = getOpenSSLKey($key_suffix);
113 $ciphertext = openssl_encrypt($message,
121 $defaultCipher = $iv . $ciphertext;
122 $defaultHash = getEncryptionAuthHash($defaultCipher,
126 $defaultResp = array(
"message" => $defaultCipher,
"hash" => $defaultHash);
130 case "connect_chkfree":
131 case "connect_ezcard":
132 case "connect_certegy":
133 case "connect_digital":
135 case "connect_vsoft":
137 $connectCipher = $ciphertext;
138 $returnArray = array(
"message" => $connectCipher,
"iv" => $iv);
142 $credentialCipher = $ciphertext;
143 $returnArray = array(
"message" => $credentialCipher,
"iv" => $iv);
149 $returnArray= $defaultResp;
157 function hcuOpenSSLDecrypt($message,
160 $method=
'aes-256-cbc',
161 $auth_hash_algo=
"sha1",
162 $auth_hash_binary=
true,
166 $hashKeyBilbo = GetOpenSSLKeyBilbo() . $key_suffix;
167 $ivsize = openssl_cipher_iv_length($method);
169 $iv = mb_substr($iv, 0, $ivsize,
'8bit');
176 $openssl_options = OPENSSL_RAW_DATA;
177 $encryptionKey = getOpenSSLKey($key_suffix);
178 $ciphertext = $message;
181 case "connect_chkfree":
182 case "connect_ezcard":
183 case "connect_certegy":
184 case "connect_digital":
186 case "connect_vsoft":
188 $openssl_options = OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING;
189 $encryptionKey = $key_suffix;
194 throw new exception(
"IV cannot be empty for Credentials.", 3);
201 $expectedHash = getEncryptionAuthHash($message,
205 if (md5($expectedHash) !== md5($hash))
206 throw new exception(
"Hash doesn't match!", 2);
209 $iv = mb_substr($message, 0, $ivsize,
'8bit');
210 $ciphertext = mb_substr($message, $ivsize,
null,
'8bit');
214 return openssl_decrypt($ciphertext,
223 function addpadding($string, $blocksize = 32) {
224 # implements PKCS7 padding 225 $len = strlen($string);
226 $pad = $blocksize - ($len % $blocksize);
227 $string .= str_repeat(chr($pad), $pad);
234 function strippadding($string) {
235 $slast = ord(substr($string, -1));
236 $slastc = chr($slast);
238 $match=
"/\\x" . dechex($slast) .
"{" . $slast .
"}/";
240 if (strlen($string) && preg_match($match, $string)) {
241 $string = substr($string, 0, strlen($string) - $slast);
251 function parmencrypt($str,
253 $cipher_method=PARMENCDEC_CIPHER_MODE) {
255 $enc_resp = hcuOpenSSLEncrypt($str,
257 $method=$cipher_method,
258 $auth_hash_algo=PARMENCDEC_AUTH_HASH_ALGO);
260 $ciphertext = $enc_resp[
"message"];
261 $hash_hmac = $enc_resp[
"hash"];
262 return base64_encode($hash_hmac.$ciphertext);
263 }
catch (Exception $ex) {
272 function parmencrypt_mcrypt($str, $key) {
273 $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
274 $key = substr($key, 0, $key_size);
275 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
276 $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
277 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
278 $str = addpadding($str, $blocksize);
279 return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv));
285 function parmdecrypt_openssl($str,
289 $cipher_all = base64_decode($str);
291 if(PARMENCDEC_AUTH_HASH_ALGO ==
"sha256") {
299 $encrypted_hash = substr($cipher_all, 0, $auth_hash_len);
300 $ciphertext = substr($cipher_all, $auth_hash_len);
302 return hcuOpenSSLDecrypt($ciphertext,
305 $method=$cipher_method,
306 $auth_hash_algo=PARMENCDEC_AUTH_HASH_ALGO);
307 }
catch (Exception $ex) {
316 function parmdecrypt_mcrypt($orig_str, $orig_key) {
318 $base64_str = base64_decode($orig_str);
319 $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
320 $key = substr($orig_key, 0, $key_size);
321 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
322 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
323 $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $base64_str, MCRYPT_MODE_ECB, $iv);
324 return strippadding($str);
325 }
catch (exception $ex) {
334 function parmdecrypt($orig_str,
336 $cipher_method=PARMENCDEC_CIPHER_MODE) {
338 $openssl_decrypt_result = parmdecrypt_openssl($orig_str,
340 $cipher_method=$cipher_method);
346 if($openssl_decrypt_result == False) {
347 $mcrypt_decrypt_result = parmdecrypt_mcrypt($orig_str, $orig_key);
348 if ($mcrypt_decrypt_result ==
false) {
350 throw new exception(
"Error: Parm could not be decrypted.");
354 return $mcrypt_decrypt_result;
358 return $openssl_decrypt_result;
360 }
catch(Exception $ex){
365 function mcrypt_lib_exists() {
369 if(function_exists(
'mcrypt_encrypt')) {
378 public function up() {
379 if (mcrypt_lib_exists()) {
381 $tableName =
"cutrusteddetail";
382 $exists = $this->hasTable($tableName);
385 $sql =
"select cu, trustedid, parms from ${tableName}";
386 $trustedDetailsList = $this->fetchAll($sql);
388 for ($i=0; $i< count($trustedDetailsList); $i++) {
390 $Cu = strtoupper(trim($trustedDetailsList[$i][
"cu"]));
391 $trustedid = trim($trustedDetailsList[$i][
"trustedid"]);
392 $tddkey = sha1(
"${Cu}:3pk4osso");
394 $parms_old_encrypt = trim($trustedDetailsList[$i][
"parms"]);
395 if (!is_null($parms_old_encrypt) && $parms_old_encrypt !=
'') {
397 $decrypt_content = parmdecrypt($parms_old_encrypt, $tddkey);
400 $enc_content = parmencrypt($decrypt_content, $tddkey);
401 $sql =
"update ${tableName} set parms='${enc_content}' where cu='${Cu}' and trustedid='${trustedid}'";
414 public function down() {
415 if (mcrypt_lib_exists()) {
417 $tableName =
"cutrusteddetail";
418 $exists = $this->hasTable($tableName);
421 $sql =
"select cu, trustedid, parms from ${tableName}";
422 $trustedDetailsList = $this->fetchAll($sql);
423 for ($i=0; $i< count($trustedDetailsList); $i++) {
425 $Cu = strtoupper(trim($trustedDetailsList[$i][
"cu"]));
426 $trustedid = trim($trustedDetailsList[$i][
"trustedid"]);
427 $tddkey = sha1(
"${Cu}:3pk4osso");
429 $parms_old_encrypt = trim($trustedDetailsList[$i][
"parms"]);
431 if (!is_null($parms_old_encrypt) && $parms_old_encrypt !=
'') {
433 $decrypt_content = parmdecrypt($parms_old_encrypt, $tddkey);
436 $enc_content = parmencrypt_mcrypt($decrypt_content, $tddkey);
437 $sql =
"update ${tableName} set parms='${enc_content}' where cu='${Cu}' and trustedid='${trustedid}'";