Odyssey
sharedHcuCuTrustedDetailTest.php
1 <?php
2 
3 use PHPUnit\Framework\TestCase;
4 
5 require_once 'cutrusted.i';
6 
7 
8 /**
9  * Test EncryptCredentials and DecryptCredentials encryption
10  * functionalities and their migration from MCRYPT to OPENSSL.
11  */
12 class HcuCuTrustedCredentialsEncryptionTest extends TestCase {
13  function setUp() {
14  // to be compatible with mcrypt_generic_init(): key size is 16 bytes max
15  $this->test_credential = "s0m3p@ssw0rd!";
16  $this->key_suffix = "venfBWHOrtest839";
17  $this->test_iv = "oY0i74hthwNoF90l";
18  }
19 
20  function test_default_credentials_encryptions() {
21  // test openssl based credential encryption
22  // encrypt with openssl
23  list($ciphertext_openssl,
24  $iv_enc_openssl) = EncryptCredentials($this->test_credential,
25  $this->key_suffix,
26  $this->test_iv);
27 
28  // decrypt with openssl
29  if(function_exists("DecryptCredentialsOpenssl")) {
30  // temporary openssl decryption function
31  // check CREDENTIALS_ENCDEC_CIPHER_MODE value in cutrusted.i
32  // for correct cipher_method
33  $obtained_decrypted_text_openssl = DecryptCredentialsOpenssl($ciphertext_openssl,
34  $this->key_suffix,
35  $iv_enc_openssl,
36  "aes-128-cbc");
37  } else { // general decryption function
38  $obtained_decrypted_text_openssl = DecryptCredentials($ciphertext_openssl,
39  $this->key_suffix,
40  $iv_enc_openssl);
41  }
42 
43  $this->assertEquals($obtained_decrypted_text_openssl, $this->test_credential);
44 
45  // test mcrypt based encryption, if present
46  if(function_exists("EncryptCredentialsMcrypt") && function_exists("DecryptCredentialsMcrypt")) {
47 
48  // encrypt with mcrypt
49  list($ciphertext, $iv_enc) = EncryptCredentialsMcrypt($this->test_credential,
50  $this->key_suffix,
51  $this->test_iv);
52 
53  // Since both mcrypt and openssl are using the same IV and
54  // encryption key, the generated ciphertexts should be same
55  $this->assertEquals($ciphertext_openssl, $ciphertext);
56 
57  // decrypt with mcrypt
58  $obtained_decrypted_text = DecryptCredentialsMcrypt($ciphertext,
59  $this->key_suffix,
60  $iv_enc);
61  $this->assertEquals($obtained_decrypted_text, $this->test_credential);
62  }
63 
64  // decrypt with mcrypt
65  // get decrypted content using mcrypt; if function available
66  // this makes sure that the ciphertext generated by openssl can be
67  // decrypted using mcrypt with known IV and key
68  if(function_exists("DecryptCredentialsMcrypt")) {
69  $obtained_decrypted_text_mcrypt = DecryptCredentialsMcrypt($ciphertext_openssl,
70  $this->key_suffix,
71  $iv_enc_openssl);
72  $this->assertEquals($obtained_decrypted_text_mcrypt, $this->test_credential);
73  }
74 
75 
76  }
77 
78  /**
79  * Test Credentials encryption migration from mcrypt to openssl
80  */
82  // encrypt with mcrypt based encryption function
83  if(function_exists("EncryptCredentialsMcrypt")) {
84  // encrypt with mcrypt if applicable
85  list($ciphertext,
86  $iv_enc) = EncryptCredentialsMcrypt($this->test_credential,
87  $this->key_suffix);
88  } else {
89  // encrypt with openssl
90  list($ciphertext,
91  $iv_enc) = EncryptCredentials($this->test_credential,
92  $this->key_suffix);
93  }
94  // decryption function should be able to decrypt the data
95  // regardless of the library used for encryption
96  // decrypt with openssl
97  $obtained_decrypted_text = DecryptCredentials($ciphertext,
98  $this->key_suffix,
99  $iv_enc);
100 
101  $this->assertEquals($obtained_decrypted_text, $this->test_credential);
102  }
103 
104  function tearDown() {
105 
106  }
107 }
108 
109 /**
110  * Test parmencrypt and parmdecrypt encryption functionalities
111  * and their migration from MCRYPT to OPENSSL.
112  */
113 class HcuCuTrustedParmEncryptionTest extends TestCase {
114  function setUp() {
115  $this->Cu = "SCRUBCU";
116  $this->key_suffix = sha1($this->Cu.":testkeysuffix");
117 
118  $this->hcu_standard_encryption_mode_for_parm = "aes-256-cbc";
119  $this->hcu_standard_auth_hash_algo = "sha256";
120 
121  // taken from the database, but changed substantially
122  $this->trusted_data = HCU_JsonEncode(
123  array("pilot" => "1",
124  "pilotUser" => "11221122-33bc-5f5b-aggc-c91eef53e9e1",
125  "pilotPass" => "Am1HcuHcuHc6LFuhaU3Dg",
126  "piloturl" => "https:\/\/demo.demo.com\/services\/resources.ashx\/fis\/HomeCuXyCu\/consumers",
127  "pilotlender" => "HomeCuXyCU\u201dN7q\u2206\u00ba\u20acm\u02da\u02da\u00d38\u203a\u203a=\u00c1\u00f7\u00f9}\u02db[\u25caw\u03c0\u00d5\u00d5\u2022\u00a2w\u00a8\u203a\u204a\u2039\u212b\u00f4\u2023",
128  "pilotsso" => "https:\/\/demo.demo.com\/partners\/HomeCuXyCu\/loginconsumer.aspx",
129  "prodUser" => "",
130  "prodPass" => "",
131  "produrl" => "",
132  "prodlender" => "",
133  "prodsso" => "",
134  "hcuLogging" => "-1"));
135 
136  // [openssl] with ECB mode, output ciphertext is always the same for a given
137  // data when run with the same secret key
138  $this->expected_trusted_data_openssl_ecb_cipher_base64 = "G1qPAyD/VutfubF97UN2cD7VDl0x9w86k4nvU8lLa77ILiZTqYXH9sBNEyyMMU0m4z3TRUQ/Q3GLK3CSrZBCvuN5x74Yw8Ryp5sx+WcnC2Q00AttHkiM+3vnvF0sCPi9pHGrdbIp8E7SN+N4FZzkdG7beIaoJcwvZp3ovIItMhxcUVmThrVusM3vl377w6hsN7DTtmIS2RBDTvDGxx7A7EsGEP1LulOYDblk9hX9KXdGPaaJ651hYAHuYDDe6B4QuYVJMu50vh1J9FKM6HjaQytyXVWbZqeku5S8su1RQa9Wc9BLf+UzNySw+6MNyRnEmCIc9olUsHVJwcbAeW+m5CcM/YiEGI1WW/rfeBbVy2B2n2WVvb+rF+JtcsXO5XOb8Axl3fKfyCePvdIP2tfMD+uCa0C7xJUeaJsGao6I9McBY7GgE6ze7QH4BjYcHhs1sKUKPM82sCBKXMaBAhj4xaD4d+TkujQ1uOvASTOPN8CGbvIdiNkITaWLbjjVgRoWb/HeAdAZbLP/zu5DWDiLCg2xAnzDNxyixHceK9+dDt4ZKA+3/S7i4EEPcMKPpF/hEUOPMVmfGSB8OX07v+Wklduhl1+5zxOremvhcvEos2j44kYKmMRGSR+SviAt2gBejPXSSntY2cDvbEnLWtZLgtkID4s+jcA3anMCV9AQh3EfPG0y1a7tThk4tpEmZfOt0y0LZYEHcJ4fFgiheLQJGuGPHiiykOb6vsSN8ZIM4FeqtilXx+fIF7tfIXBcXWUHln4VfvyQoEh/Iq189hCokFli5/P1fXq6X1JehKZCOv/Bm4ZXntSldD3A2BVV87Fs/MLEOFewG4oCUqnmRGQ+4Q==";
139 
140  // [mcrypt-MCRYPT_RIJNDAEL_128 + MCRYPT_MODE_ECB] with ECB mode, output ciphertext
141  // is always the same for a given data when run with the same secret key
142  $this->expected_trusted_data_mcrypt_ecb_cipher_base64 = "WsKpzTsT7rsUMfhrEWlHjFsgCINnlGPPdzpzZ6YR8CTdIi+XTgscznsaUi6OtqwbT9UXLfS9HvcmzbX5EZHrxxo21XfY6pFAXcIArigtZVYCiqoBXxgYZki9+SiFIgzuaM8Py8ktxdJFep4DaukNOPoGXzUBF1umqjtB/rZqLASjQdTR+yxurzpG4YzXMuhcfg3MZBfR+Wc4+GY1+XdYs5NrOGKN3ntt3Tw0Ifug8rzHdknoLY1HxRIlIPRhVrgHGCNo3TDU8sgCPPg3fRHdChHBsfCbHPOGaDxx03fllxTrvM9D4YemIEftspPXGq2U7AZqswxwrUN/EAobG9z8ffpSdy5SSO7ZvxhAMJWz6n0TzFJn/LsEMrP4NYbijJ55sAPiocC4PzWCCn9ADIQ2nqpg5JNbGhIYn0fb1fwLOjXxZitd+grZn2PlCJ5zm1q4Nt3PYmzI9q8EAxhsihCNB8/gzrgxrPXYnesz4qnBvq+xEo5et9IH2l6rjy4QFM/2qAuVDS4J52xEhIjG8e9lqfooHEwnEJ4cQzeaiyMilDT4kOQ2jZjSOIip4QvlYnC4okY1SxYUrhvnv8GigU1iSs9yZRhdzD9k2hXrczk1cU5QoJy4sHd05DIe9x/7KWxC9va6NjCvbdwHDS9moEt2D5255LM3AI1OScG280cyizUQAnpOFt/jeIL8dBNNZDzjgHesrLSLtEiMdniL4xylCqBbdFMjh6LQ+4SbbDe+eU8kQFKBPUewcDt5CwEHLbYp+RVyyX8cEkfIL5pJpfCxv6OCoPR5ZTwTGGSDTTLYzaM=";
143  }
144 
145  /**
146  * We still use ECB mode of operations mainly due to vendor/interfaces
147  * requirements and specifications. Wherever possible, these must
148  * be upgraded based on the latest crypto standards and guidelines.
149  */
151  // if the previous mcrypt based function also exists, test it
152  if(function_exists("parmencrypt_mcrypt")) {
153  // With [mcrypt-MCRYPT_RIJNDAEL_128 + MCRYPT_MODE_ECB] ECB mode,
154  // output ciphertext is always the same for a given data when run
155  // with the same secret key
156  // ENCRYPTION
157  $obtained_ciphertext_mcrypt = parmencrypt_mcrypt($this->trusted_data,
158  $this->key_suffix);
159 
160  $this->assertEquals($obtained_ciphertext_mcrypt,
161  $this->expected_trusted_data_mcrypt_ecb_cipher_base64);
162 
163  //DECRYPTION
164  $obtained_decrypted_data_mcrypt = parmdecrypt($obtained_ciphertext_mcrypt,
165  $this->key_suffix,
166  $cipher_method="aes-256-ecb");
167 
168  $this->assertEquals($obtained_decrypted_data_mcrypt,
169  $this->trusted_data);
170  }
171 
172  // test openssl aes-256-ecb based encrypted base64 output
173  // [openssl] with ECB mode, output ciphertext is always the same for a given
174  // data when run with the same secret key
175  // ENCRYPTION
176  $obtained_ciphertext_openssl = parmencrypt($this->trusted_data,
177  $this->key_suffix,
178  $cipher_method="aes-256-ecb");
179  $this->assertEquals($obtained_ciphertext_openssl,
180  $this->expected_trusted_data_openssl_ecb_cipher_base64);
181 
182  // DECRYPTION
183  $obtained_decrypted_data_openssl = parmdecrypt($obtained_ciphertext_openssl,
184  $this->key_suffix,
185  $cipher_method="aes-256-ecb");
186  $this->assertEquals($obtained_decrypted_data_openssl, $this->trusted_data);
187 
188  // also verify that the decrypted data using the both methods were same
189  if(function_exists("paramencrypt_mcrypt")) {
190  $this->assertEquals($obtained_decrypted_data_openssl, $obtained_decrypted_data_mcrypt);
191  }
192  }
193 
194  /**
195  * Verify that the current standard is maintained eg. we are upgrading from
196  * mcrypt ecb to aes-256-cbc on March, 2019. Current standard for parm encryption
197  * is therefore aes-256-cbc.
198  */
200  $this->assertEquals($this->hcu_standard_encryption_mode_for_parm, PARMENCDEC_CIPHER_MODE);
201  $this->assertEquals($this->hcu_standard_auth_hash_algo, PARMENCDEC_AUTH_HASH_ALGO);
202  }
203 
204  /**
205  * Test parmencrypt and parmdecrypt with default arguments
206  */
208  // Note that the default encryption mode being used by the following functions
209  // is determined by PARMENCDEC_CIPHER_MODE constant in cutrusted.i script.
210  $openssl_ciphertext = parmencrypt($this->trusted_data,
211  $this->key_suffix);
212 
213  $obtained_decrypted_text = parmdecrypt($openssl_ciphertext,
214  $this->key_suffix);
215 
216  $this->assertEquals($obtained_decrypted_text, $this->trusted_data);
217  }
218 
219  /**
220  * Test parm encryption with several popular aes 256bits key size
221  * based aes encryption modes with openssl. Our openssl encryption
222  * implementation is expected to work properly for the listed
223  * encryption modes at the least.
224  *
225  */
227  // supported encryption modes
228  $openssl_aes_256_modes = array("aes-256-ctr",
229  "aes-256-ofb",
230  "aes-256-cfb",
231  "aes-256-cfb1",
232  "aes-256-cfb8",
233  "aes-256-ofb",
234  "aes-256-xts",
235  "aes-256-ecb");
236  $previous_decrypted_data = "";
237  foreach($openssl_aes_256_modes as $aes_cipher_mode) {
238  $openssl_ciphertext = "";
239  $obtained_decrypted_text = "";
240 
241  // encrypt with openssl with $aes_cipher_mode mode
242  $openssl_ciphertext = parmencrypt($this->trusted_data,
243  $this->key_suffix,
244  $cipher_method=$aes_cipher_mode);
245 
246  // creates different ciphertext and different base64_encoded output
247  // for each of the mode of operation, but they should decrypt the original
248  // text just fine.
249  $obtained_decrypted_text = parmdecrypt($openssl_ciphertext,
250  $this->key_suffix,
251  $cipher_method=$aes_cipher_mode);
252 
253  $this->assertEquals($obtained_decrypted_text, $this->trusted_data);
254  // also assert the decrypted value from the previous mode
255  if ($previous_decrypted_data != "") {
256  $this->assertEquals($previous_decrypted_data, $obtained_decrypted_text);
257  }
258  // update previous with the current decrypt
259  $previous_decrypted_dat = $obtained_decrypted_text;
260 
261  }
262 
263  }
264 
265  function tearDown(){
266 
267  }
268 }
269 
270 ?>