4 require_once dirname(__FILE__).
'/hcuCommon.i';
13 define(
"PARMENCDEC_CIPHER_MODE",
"aes-256-cbc");
15 define(
"PARMENCDEC_AUTH_HASH_ALGO",
"sha256");
18 define(
"CREDENTIALS_ENCDEC_CIPHER_MODE",
"aes-128-cbc");
19 define(
"CREDENTIALS_AUTH_HASH_ALGO",
"sha256");
24 function check_if_mcrypt_exists() {
25 if (!function_exists(
"mdecrypt_generic") && !function_exists(
"mcrypt_generic"))
26 throw new exception(
"MCRYPT library does not exist.");
37 function cutd_write($dbh, $parms) {
39 if (!isset($parms[
'Cu']) || !isset($parms[
'trustedid']) ||
40 !isset($parms[
'fields'])) {
41 $response[
'Response'] =
'false';
42 $response[
'Message'] =
'Missing Parameters';
46 $Cu = trim($parms[
'Cu']);
47 $MasterKey = sha1(
"${Cu}:3pk4osso");
48 $trustedid = trim($parms[
'trustedid']);
49 $fields=$parms[
'fields'];
50 $storeparms = json_encode($fields);
52 $storeparms = parmencrypt($storeparms, $MasterKey);
53 $inssql =
"insert into cutrusteddetail (cu, trustedid, parms) values ('$Cu','$trustedid','$storeparms')";
54 $updsql =
"update cutrusteddetail set parms='$storeparms' 55 where cu='$Cu' and trustedid='$trustedid'";
57 # try update - if rows_affected=1 $response='updated' 58 $sth = db_query($updsql, $dbh);
59 if (db_affected_rows($sth) == 1) {
60 $response[
'Response'] =
'true';
61 $response[
'Message'] =
'Updated';
65 $sth = db_query($inssql, $dbh);
66 if (db_affected_rows($sth) == 1) {
67 $response[
'Response'] =
'true';
68 $response[
'Message'] =
'Inserted';
70 $response[
'Response'] =
'false';
71 $response[
'Message'] =
'Failed';
75 $result[
'status'] = $response;
76 if ($response[
'Response'] ==
'false') {
77 $result[
'data'] = array();
79 $result[
'data'][
"$Cu|$trustedid"] = $fields;
81 $result[
'status'][
'Errors'] = array();
93 function cutd_read($dbh, $parms) {
94 if (!isset($parms[
'Cu']) || !isset($parms[
'trustedid'])) {
95 $response[
'Response'] =
'false';
96 $response[
'Message'] =
'Missing Parameters';
99 $Cu = trim($parms[
'Cu']);
100 $MasterKey = sha1(
"${Cu}:3pk4osso");
101 $trustedid = trim($parms[
'trustedid']);
103 $sql =
"select parms from cutrusteddetail 104 where cu='$Cu' and trustedid='$trustedid'";
105 $sth = db_query($sql, $dbh);
107 if (db_num_rows($sth) != 1) {
108 $response[
'Response'] =
'false';
109 $response[
'Message'] =
'Failed';
110 $response[
'sql']=$sql;
113 $drow = db_fetch_array($sth, 0);
115 $storefields = $drow[
'parms'];
116 $fields = parmdecrypt($storefields, $MasterKey);
118 $fields = json_decode($fields, TRUE);
120 if ($fields === NULL) {
121 $response[
'Response'] =
'false';
122 $response[
'Message'] =
'Failed';
124 $response[
'Response'] =
'true';
125 $response[
'Message'] =
'OK';
129 $result[
'status'] = $response;
130 if ($response[
'Response'] ==
'false') {
131 $result[
'data'] = array();
133 $result[
'data'][
"$Cu|$trustedid"] = $fields;
135 $result[
'status'][
'Errors'] = array();
148 function cutrusted_read($dbh, $parms) {
149 $result[
'status'][
'Response'] =
'true';
150 $result[
'status'][
'Message'] =
'OK';
151 $result[
'status'][
'Code'] =
'000';
152 $result[
'status'][
'Errors'] = array();
156 if (!isset($parms[
'Cu']) || !isset($parms[
'trustedid'])) {
157 throw new Exception(
'Missing Parameters', 100);
159 $Cu = trim($parms[
'Cu']);
160 $MasterKey = sha1(
"${Cu}:3pk4osso");
161 $trustedid = trim($parms[
'trustedid']);
163 $sql =
"select parms from cutrusteddetail 164 where cu='$Cu' and trustedid='$trustedid'";
165 $sth = db_query($sql, $dbh);
167 if (db_num_rows($sth) == 1) {
168 $result[
'status'][
'Source']=
'detail';
169 $drow = db_fetch_array($sth, 0);
171 $storefields = $drow[
'parms'];
172 $fields = parmdecrypt($storefields, $MasterKey);
174 $fields = json_decode($fields, TRUE);
176 if ($fields === NULL) {
177 throw new Exception(
"Field Decryption Failed", 100);
179 $result[
'data'] = $fields;
181 # no cu-specific record, try to read the master 182 $result = cutm_readdflt($dbh, $parms);
183 if ($result[
'status'][
'Response'] ==
'false') {
184 $result[
'status'][
'Source']=
'';
186 $result[
'status'][
'Source']=
'master';
190 }
catch (Exception $e) {
191 $result[
'status'][
'Response'] =
'false';
192 $result[
'status'][
'Code'] = $e->getCode();
193 $result[
'status'][
'Message'] =
"Failed";
194 $result[
'status'][
'Errors'] = array(
"(" . $e->getLine() .
") " . htmlspecialchars($e->getMessage(), ENT_QUOTES,
'UTF-8', FALSE));
195 $result[
'status'][
'Source'] =
'';
196 $result[
'data'] = array();
210 function cutd_list($dbh, $parms) {
212 $Cu = trim($parms[
'Cu']);
213 $trustedid = (isset($parms[
'trustedid']) ? trim($parms[
'trustedid']) :
'');
219 $culimit =
"$delim cu='$Cu'";
222 if (!empty($trustedid)) {
223 $trustlimit =
"$delim trustedid = '$trustedid'";
225 else if (isset($parms[
"trustedids"]) && is_array($parms[
"trustedids"]) && count($parms[
"trustedids"]) > 0)
226 $trustlimit =
"$delim trustedid in ('" . implode(
"','", $parms[
"trustedids"]) .
"')";
228 $sql =
"select cu, trustedid, parms from cutrusteddetail $culimit $trustlimit order by trustedid, cu";
229 $sth = db_query($sql, $dbh);
231 $numrows = db_num_rows($sth);
233 $result[
'status'][
'Response'] =
'false';
234 $result[
'status'][
'Message'] =
'Failed';
235 $result[
'status'][
'Errors'] = array(
"No Records Found");
237 $result[
'status'][
'Response'] =
'true';
238 $result[
'status'][
'Message'] =
'OK';
239 $result[
'status'][
'Errors'] = array();
241 for ($i = 0; $i < $numrows; $i++) {
242 $drow = db_fetch_array($sth, $i);
244 $Cu = trim($drow[
'cu']);
245 $MasterKey = sha1(
"${Cu}:3pk4osso");
246 $trustedid = trim($drow[
'trustedid']);
247 $storefields = trim($drow[
'parms']);
248 $fields = parmdecrypt($storefields, $MasterKey);
249 $fields = json_decode($fields, TRUE);
250 $result[
'data'][
"$Cu|$trustedid"][
'cu'] = $Cu;
251 $result[
'data'][
"$Cu|$trustedid"][
'trustedid'] = $trustedid;
252 $result[
'data'][
"$Cu|$trustedid"][
'fields'] = $fields;
267 function cutm_write($dbh, $parms) {
269 if (!isset($parms[
'trustedid']) ||
270 !isset($parms[
'fields']) ||
271 !isset($parms[
'trustedvendor']) ||
272 !isset($parms[
'trustedtype']) ||
273 !isset($parms[
'hcuinterface'])) {
274 $response[
'Response'] =
'false';
275 $response[
'Message'] =
'Missing Parameters';
277 $trustedid = trim($parms[
'trustedid']);
278 $trustedvendor = trim($parms[
'trustedvendor']);
279 $trustedtype = trim($parms[
'trustedtype']);
280 $hcuinterface = trim($parms[
'hcuinterface']);
281 $fields = $parms[
'fields'];
282 $storefields = json_encode($fields);
284 $inssql =
"insert into cutrustedmaster (trustedid, trustedvendor,trustedtype,hcuinterface,trustedfields) 285 values ('$trustedid','$trustedvendor','$trustedtype','$hcuinterface','$storefields')";
286 $updsql =
"update cutrustedmaster set trustedvendor='$trustedvendor', 287 trustedtype='$trustedtype', hcuinterface='$hcuinterface', 288 trustedfields='$storefields' 289 where trustedid='$trustedid'";
291 # try update - if rows_affected=1 $response='updated' 292 $sth = db_query($updsql, $dbh);
293 if (db_affected_rows($sth) == 1) {
294 $response[
'Response'] =
'true';
295 $response[
'Message'] =
'Updated';
299 $sth = db_query($inssql, $dbh);
300 if (db_affected_rows($sth) == 1) {
301 $response[
'Response'] =
'true';
302 $response[
'Message'] =
'Inserted';
304 $response[
'Response'] =
'false';
305 $response[
'Message'] =
'Failed';
309 $result[
'status'] = $response;
310 if ($response[
'Response'] ==
'false') {
311 $result[
'data'] = array();
313 $result[
'data'][
"$trustedid"] = $fields;
315 $result[
'status'][
'Errors'] = array();
325 function cutm_read($dbh, $parms) {
327 if (!isset($parms[
'trustedid'])) {
328 $response[
'Response'] =
'false';
329 $response[
'Message'] =
'Missing Parameters';
331 $trustedid = trim($parms[
'trustedid']);
333 $sql =
"select trustedvendor, trustedtype, hcuinterface, trustedfields from cutrustedmaster 334 where trustedid='$trustedid'";
335 $sth = db_query($sql, $dbh);
337 if (db_num_rows($sth) != 1) {
338 $response[
'Response'] =
'false';
339 $response[
'Message'] =
'Failed';
341 $drow = db_fetch_array($sth, 0);
342 $trustedvendor = trim($drow[
'trustedvendor']);
343 $trustedtype = trim($drow[
'trustedtype']);
344 $hcuinterface = trim($drow[
'hcuinterface']);
345 $storedfields = trim($drow[
'trustedfields']);
346 $trustedfields = json_decode($storedfields, TRUE);
348 if ($trustedfields === NULL) {
349 $response[
'Response'] =
'false';
350 $response[
'Message'] =
'Failed';
351 $response[storedfields] = $storedfields;
353 $response[
'Response'] =
'true';
354 $response[
'Message'] =
'OK';
358 $result[
'status'] = $response;
359 if ($response[
'Response'] ==
'false') {
360 $result[
'data'] = array();
362 $result[
'data'][
'trustedid'] = $trustedid;
363 $result[
'data'][
'trustedvendor'] = $trustedvendor;
364 $result[
'data'][
'trustedtype'] = $trustedtype;
365 $result[
'data'][
'hcuinterface'] = $hcuinterface;
366 $result[
'data'][
'trustedfields'] = $trustedfields;
368 $result[
'status'][
'Errors'] = array();
378 function cutm_readdflt($dbh, $parms) {
380 if (!isset($parms[
'trustedid'])) {
381 $response[
'Response'] =
'false';
382 $response[
'Message'] =
'Missing Parameters';
384 $trustedid = trim($parms[
'trustedid']);
386 $sql =
"select trustedvendor, trustedtype, hcuinterface, trustedfields from cutrustedmaster 387 where trustedid='$trustedid'";
388 $sth = db_query($sql, $dbh);
390 if (db_num_rows($sth) != 1) {
391 $response[
'Response'] =
'false';
392 $response[
'Message'] =
'Failed';
394 $drow = db_fetch_array($sth, 0);
395 $trustedvendor = trim($drow[
'trustedvendor']);
396 $trustedtype = trim($drow[
'trustedtype']);
397 $hcuinterface = trim($drow[
'hcuinterface']);
398 $storedfields = trim($drow[
'trustedfields']);
399 $trustedfields = json_decode($storedfields, TRUE);
401 if ($trustedfields === NULL) {
402 $response[
'Response'] =
'false';
403 $response[
'Message'] =
'Failed';
404 $response[
'storedfields'] = $storedfields;
406 $response[
'Response'] =
'true';
407 $response[
'Message'] =
'OK';
411 $result[
'status'] = $response;
412 if ($response[
'Response'] ==
'false') {
413 $result[
'data'] = array();
415 $result[
'master'][
'trustedid'] = $trustedid;
416 $result[
'master'][
'trustedvendor'] = $trustedvendor;
417 $result[
'master'][
'trustedtype'] = $trustedtype;
418 $result[
'master'][
'hcuinterface'] = $hcuinterface;
419 $result[
'master'][
'trustedfields'] = $trustedfields;
420 foreach ($trustedfields as $fldkey => $fldval) {
421 if (isset($fldval[
'Default'])) {
422 $result[
'data'][$fldkey] = $fldval[
'Default'];
426 $result[
'status'][
'Errors'] = array();
437 function cutm_list($dbh, $parms) {
441 if (HCU_array_key_exists(
"trustedid", $parms)) {
442 $trustedid = trim($parms[
'trustedid']);
443 $trustlimit =
"$delim trustedid = '$trustedid'";
447 $sql =
"select trustedid, trustedvendor, trustedtype, hcuinterface, trustedfields from cutrustedmaster $trustlimit order by trustedid";
448 $sth = db_query($sql, $dbh);
450 $numrows = db_num_rows($sth);
452 $result[
'status'][
'Response'] =
'false';
453 $result[
'status'][
'Message'] =
'Failed';
454 $result[
'status'][
'Errors'] = array(
"No Records Found");
456 $result[
'status'][
'Response'] =
'true';
457 $result[
'status'][
'Message'] =
'OK';
458 $result[
'status'][
'Errors'] = array();
460 for ($i = 0; $i < $numrows; $i++) {
461 $drow = db_fetch_array($sth, $i);
463 $trustedid = trim($drow[
'trustedid']);
464 $trustedvendor = trim($drow[
'trustedvendor']);
465 $trustedtype = trim($drow[
'trustedtype']);
466 $hcuinterface = trim($drow[
'hcuinterface']);
467 $storefields = trim($drow[
'trustedfields']);
468 $fields = json_decode($storefields, TRUE);
469 $result[
'data'][
"$trustedid"][
'trustedid'] = $trustedid;
470 $result[
'data'][
"$trustedid"][
'trustedvendor'] = $trustedvendor;
471 $result[
'data'][
"$trustedid"][
'trustedtype'] = $trustedtype;
472 $result[
'data'][
"$trustedid"][
'hcuinterface'] = $hcuinterface;
473 $result[
'data'][
"$trustedid"][
'fields'] = $fields;
487 function cutrusted_list($dbh, $parms) {
489 $trustedid = trim($parms[
'trustedid']);
492 if (!empty($trustedid)) {
493 $trustlimit =
"$delim trustedid = '$trustedid'";
497 $sql =
"select trustedid, trustedvendor, trustedtype, hcuinterface, trustedfields from cutrustedmaster $trustlimit order by trustedid";
498 $sth = db_query($sql, $dbh);
500 $numrows = db_num_rows($sth);
502 $result[
'status'][
'Response'] =
'false';
503 $result[
'status'][
'Message'] =
'Failed';
504 $result[
'status'][
'Errors'] = array(
"No Records Found");
506 $result[
'status'][
'Response'] =
'true';
507 $result[
'status'][
'Message'] =
'OK';
508 $result[
'status'][
'Errors'] = array();
510 for ($i = 0; $i < $numrows; $i++) {
511 $drow = db_fetch_array($sth, $i);
513 $trustedid = trim($drow[
'trustedid']);
514 $trustedvendor = trim($drow[
'trustedvendor']);
515 $trustedtype = trim($drow[
'trustedtype']);
516 $hcuinterface = trim($drow[
'hcuinterface']);
517 $storefields = trim($drow[
'trustedfields']);
518 $fields = json_decode($storefields, TRUE);
519 $result[
'data'][
"$trustedid"][
'trustedid'] = $trustedid;
520 $result[
'data'][
"$trustedid"][
'trustedvendor'] = $trustedvendor;
521 $result[
'data'][
"$trustedid"][
'trustedtype'] = $trustedtype;
522 $result[
'data'][
"$trustedid"][
'hcuinterface'] = $hcuinterface;
523 $result[
'data'][
"$trustedid"][
'fields'] = $fields;
524 $cutdlist = cutd_list($dbh, array(
'trustedid' => $trustedid));
525 if ($cutdlist[
'status'][
'Response'] ==
'true') {
526 $result[
'data'][
"$trustedid"][
'clients'] = $cutdlist[
'data'];
528 $result[
'data'][
"$trustedid"][
'clients'] = array();
539 function parmencrypt($str,
541 $cipher_method=PARMENCDEC_CIPHER_MODE) {
543 if (!is_null($str) && trim($str) !=
''){
544 $enc_resp = hcuOpenSSLEncrypt($str,
546 $method=$cipher_method,
547 $auth_hash_algo=PARMENCDEC_AUTH_HASH_ALGO);
549 $ciphertext = $enc_resp[
"message"];
550 $hash_hmac = $enc_resp[
"hash"];
551 return base64_encode($hash_hmac.$ciphertext);
555 }
catch (Exception $ex) {
566 function parmencrypt_mcrypt($str, $key) {
567 check_if_mcrypt_exists();
568 $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
569 $key = substr($key, 0, $key_size);
570 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
571 $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
572 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
573 $str = addpadding($str, $blocksize);
574 return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv));
584 function parmdecrypt_openssl($str,
588 $cipher_all = base64_decode($str);
590 if(PARMENCDEC_AUTH_HASH_ALGO ==
"sha256") {
598 $encrypted_hash = substr($cipher_all, 0, $auth_hash_len);
599 $ciphertext = substr($cipher_all, $auth_hash_len);
601 return hcuOpenSSLDecrypt($ciphertext,
604 $method=$cipher_method,
605 $auth_hash_algo=PARMENCDEC_AUTH_HASH_ALGO);
606 }
catch (Exception $ex) {
620 function parmdecrypt_mcrypt($orig_str, $orig_key) {
622 check_if_mcrypt_exists();
623 $base64_str = base64_decode($orig_str);
624 $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
625 $key = substr($orig_key, 0, $key_size);
626 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
627 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
628 $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $base64_str, MCRYPT_MODE_ECB, $iv);
629 return strippadding($str);
630 }
catch (exception $ex) {
647 function parmdecrypt($orig_str,
649 $cipher_method=PARMENCDEC_CIPHER_MODE) {
651 if(!is_null($orig_str) && trim($orig_str) !=
'') {
652 $openssl_decrypt_result = parmdecrypt_openssl($orig_str,
654 $cipher_method=$cipher_method);
660 if($openssl_decrypt_result == False) {
661 $mcrypt_decrypt_result = parmdecrypt_mcrypt($orig_str, $orig_key);
662 if ($mcrypt_decrypt_result ==
false) {
664 throw new exception(
"Error: Parm could not be decrypted.");
668 return $mcrypt_decrypt_result;
672 return $openssl_decrypt_result;
677 }
catch(Exception $ex){
685 function EncryptCredentialsMcrypt($str,
689 check_if_mcrypt_exists();
690 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
691 $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
692 $str = addpadding($str, $blocksize);
694 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,
'', MCRYPT_MODE_CBC,
'');
695 if ($iv ==
"") $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
696 mcrypt_generic_init($td, $key, $iv);
697 $token = mcrypt_generic($td, $str);
698 mcrypt_generic_deinit($td);
699 mcrypt_module_close($td);
701 return array(base64_encode($token), base64_encode($iv));
703 }
catch (exception $ex) {
720 function EncryptCredentials($str,
723 $cipher_method=CREDENTIALS_ENCDEC_CIPHER_MODE) {
725 $encResp = hcuOpenSSLEncrypt($str,
727 $method=$cipher_method,
728 $auth_hash_algo=CREDENTIALS_AUTH_HASH_ALGO,
729 $auth_hash_binary=
true,
731 $context=
"credentials");
733 $ciphertext = $encResp[
"message"];
734 $iv_enc = $encResp[
"iv"];
736 return array(base64_encode($ciphertext), base64_encode($iv_enc));
738 }
catch (Exception $ex) {
757 function DecryptCredentialsMcrypt($str, $key, $iv) {
759 check_if_mcrypt_exists();
760 $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
765 return strippadding($str);
766 }
catch (exception $ex) {
779 function DecryptCredentialsOpenssl($str,
784 $ciphertext = base64_decode($str);
786 $iv = base64_decode($iv);
788 $encrypted_hash =
"";
789 return hcuOpenSSLDecrypt($ciphertext,
792 $method=$cipher_method,
793 $auth_hash_algo=CREDENTIALS_AUTH_HASH_ALGO,
794 $auth_hash_binary=
true,
796 $context=
"credentials");
798 }
catch (exception $ex) {
814 function DecryptCredentials($str,
817 $cipher_method=CREDENTIALS_ENCDEC_CIPHER_MODE) {
819 $openssl_result = DecryptCredentialsOpenssl($str,
823 if ($openssl_result == False) {
824 $mcrypt_result = DecryptCredentialsMcrypt($str, $key, $iv);
826 if($mcrypt_result ==
false) {
827 throw new exception(
"Credential could not be decrypted.");
829 return $mcrypt_result;
832 return $openssl_result;
835 }
catch (exception $ex) {
840 function addpadding($string, $blocksize = 32) {
841 # implements PKCS7 padding 842 $len = strlen($string);
843 $pad = $blocksize - ($len % $blocksize);
844 $string .= str_repeat(chr($pad), $pad);
848 function strippadding($string) {
849 $slast = ord(substr($string, -1));
850 $slastc = chr($slast);
852 $match=
"/\\x" . dechex($slast) .
"{" . $slast .
"}/";
854 if (strlen($string) && preg_match($match, $string)) {
855 $string = substr($string, 0, strlen($string) - $slast);