/hphp/test/slow/ext_openssl/ext_openssl.php

https://github.com/tstarling/hiphop-php · PHP · 364 lines · 304 code · 58 blank · 2 comment · 44 complexity · 49137fe7a583ec4f0c1ff7015ab9eec5 MD5 · raw file

  1. <?php
  2. function VS($x, $y) {
  3. var_dump($x === $y);
  4. if ($x !== $y) { echo "Failed: $y\n"; echo "Got: $x\n";
  5. var_dump(debug_backtrace()); }
  6. }
  7. function VERIFY($x) { VS($x != false, true); }
  8. //////////////////////////////////////////////////////////////////////
  9. function test_openssl_csr_export_to_file() {
  10. $csr = openssl_csr_new(null, $ignore);
  11. VERIFY($csr != null);
  12. $tmp = tempnam('/tmp', 'vmopenssltest');
  13. unlink($tmp);
  14. VS(file_get_contents($tmp), false);
  15. openssl_csr_export_to_file($csr, $tmp);
  16. VERIFY(strlen(file_get_contents($tmp)) > 400);
  17. unlink($tmp);
  18. }
  19. function test_openssl_csr_get_public_key() {
  20. $csr = openssl_csr_new(null, $ignore);
  21. VERIFY($csr != null);
  22. $publickey = openssl_csr_get_public_key($csr);
  23. VERIFY($publickey != false);
  24. VERIFY($publickey != null);
  25. }
  26. function test_openssl_csr_get_subject() {
  27. $csr = openssl_csr_new(null, $ignore);
  28. VERIFY($csr != null);
  29. VERIFY(openssl_csr_get_subject($csr)['O'] == "Internet Widgits Pty Ltd");
  30. }
  31. function test_openssl_csr_sign() {
  32. $dn = array(
  33. "countryName",
  34. "stateOrProvinceName",
  35. "localityName",
  36. "organizationName",
  37. "organizationalUnitName",
  38. "commonName",
  39. "emailAddress"
  40. );
  41. $privkeypass = "1234";
  42. $numberofdays = 365;
  43. $privkey = openssl_pkey_new();
  44. VERIFY($privkey != null);
  45. $csr = openssl_csr_new($dn, $privkey);
  46. VERIFY($csr != null);
  47. $scert = openssl_csr_sign($csr, null, $privkey, $numberofdays);
  48. openssl_x509_export($scert, $publickey);
  49. openssl_pkey_export($privkey, $privatekey, $privkeypass);
  50. openssl_csr_export($csr, $csrStr);
  51. VERIFY(strlen($privatekey) > 500);
  52. VERIFY(strlen($publickey) > 800);
  53. VERIFY(strlen($csrStr) > 500);
  54. }
  55. function test_openssl_error_string() {
  56. $ret = openssl_error_string();
  57. }
  58. function test_openssl_free_key() {
  59. $csr = openssl_csr_new(null, $ignore);
  60. VERIFY($csr != null);
  61. $publickey = openssl_csr_get_public_key($csr);
  62. VERIFY($publickey != false);
  63. VERIFY($publickey != null);
  64. openssl_free_key($publickey);
  65. }
  66. function test_openssl_pkcs12_export_to_file() {
  67. $privkey = openssl_pkey_new();
  68. VERIFY($privkey != null);
  69. $csr = openssl_csr_new(null, $privkey);
  70. VERIFY($csr != null);
  71. $scert = openssl_csr_sign($csr, null, $privkey, 365);
  72. $tmp = tempnam('/tmp', 'vmopenssltest');
  73. unlink($tmp);
  74. VS(file_get_contents($tmp), false);
  75. openssl_pkcs12_export_to_file($scert, $tmp, $privkey, "1234");
  76. VERIFY(strlen(file_get_contents($tmp)) > 400);
  77. unlink($tmp);
  78. }
  79. function test_openssl_pkcs12_read() {
  80. $privkey = openssl_pkey_new();
  81. VERIFY($privkey != null);
  82. $csr = openssl_csr_new(null, $privkey);
  83. VERIFY($csr != null);
  84. $scert = openssl_csr_sign($csr, null, $privkey, 365);
  85. openssl_pkcs12_export($scert, $pkcs12, $privkey, "1234");
  86. VERIFY(openssl_pkcs12_read($pkcs12, $certs, "1234"));
  87. VERIFY(strlen($certs['cert']) > 500);
  88. VERIFY(strlen($certs['pkey']) > 500);
  89. }
  90. function test_openssl_pkcs7_sign() {
  91. $privkey = openssl_pkey_new();
  92. VERIFY($privkey != null);
  93. $csr = openssl_csr_new(null, $privkey);
  94. VERIFY($csr != null);
  95. $scert = openssl_csr_sign($csr, null, $privkey, 365);
  96. $pubkey = openssl_csr_get_public_key($csr);
  97. VERIFY($pubkey != null);
  98. $data = "some secret data";
  99. $infile = tempnam('/tmp', 'invmtestopenssl');
  100. $outfile = tempnam('/tmp', 'outvmtestopenssl');
  101. unlink($infile);
  102. unlink($outfile);
  103. file_put_contents($infile, $data);
  104. VERIFY(openssl_pkcs7_sign
  105. ($infile, $outfile, $scert, $privkey,
  106. array("To" => "t@facebook.com", "From" => "hzhao@facebook.com")));
  107. $tmp = tempnam('/tmp', 'x509vmtestopenssl');
  108. unlink($tmp);
  109. VS(file_get_contents($tmp), false);
  110. VERIFY(openssl_x509_export_to_file($scert, $tmp));
  111. VS(openssl_pkcs7_verify($outfile, 0, $infile, (array)$tmp), true);
  112. unlink($infile);
  113. unlink($outfile);
  114. unlink($tmp);
  115. }
  116. function test_openssl_pkey_export_to_file() {
  117. $tmp = tempnam('/tmp', 'vmopenssltest');
  118. unlink($tmp);
  119. VS(file_get_contents($tmp), false);
  120. $privkey = openssl_pkey_new();
  121. VERIFY($privkey != null);
  122. openssl_pkey_export_to_file($privkey, $tmp, "1234");
  123. VERIFY(strlen(file_get_contents($tmp)) > 400);
  124. unlink($tmp);
  125. }
  126. function test_openssl_pkey_export() {
  127. $privkey = openssl_pkey_new();
  128. VERIFY($privkey != null);
  129. openssl_pkey_export($privkey, $out, "1234");
  130. VERIFY(strlen($out) > 500);
  131. }
  132. function test_openssl_pkey_free() {
  133. $fkey = file_get_contents(__DIR__."/test_public.pem");
  134. $k = openssl_pkey_get_public($fkey);
  135. VERIFY($k != false);
  136. VERIFY($k != null);
  137. openssl_pkey_free($k);
  138. }
  139. function test_openssl_pkey_get_details() {
  140. {
  141. $fkey = file_get_contents(__DIR__."/test_public.pem");
  142. $k = openssl_pkey_get_public($fkey);
  143. VERIFY($k !== false);
  144. VERIFY($k != null);
  145. VS(openssl_pkey_get_details($k)['bits'], 1024);
  146. }
  147. {
  148. $fkey = file_get_contents(__DIR__."/test_private.pem");
  149. $k = openssl_pkey_get_private($fkey);
  150. VERIFY($k !== false);
  151. VERIFY($k != null);
  152. VS(openssl_pkey_get_details($k)['bits'], 512);
  153. }
  154. }
  155. function test_openssl_private_encrypt() {
  156. $privkey = openssl_pkey_new();
  157. VERIFY($privkey != null);
  158. $csr = openssl_csr_new(null, $privkey);
  159. VERIFY($csr != null);
  160. $pubkey = openssl_csr_get_public_key($csr);
  161. VERIFY($pubkey != null);
  162. $data = "some secret data";
  163. VERIFY(openssl_private_encrypt($data, $out, $privkey));
  164. VERIFY(openssl_public_decrypt($out, $out2, $pubkey));
  165. VS($out2, $data);
  166. }
  167. function test_openssl_public_encrypt() {
  168. $privkey = openssl_pkey_new();
  169. VERIFY($privkey != null);
  170. $csr = openssl_csr_new(null, $privkey);
  171. VERIFY($csr != null);
  172. $pubkey = openssl_csr_get_public_key($csr);
  173. VERIFY($pubkey != null);
  174. $data = "some secret data";
  175. VERIFY(openssl_public_encrypt($data, $out, $pubkey));
  176. VERIFY(openssl_private_decrypt($out, $out2, $privkey));
  177. VS($out2, $data);
  178. }
  179. function test_openssl_seal() {
  180. $privkey = openssl_pkey_new();
  181. VERIFY($privkey != null);
  182. $csr = openssl_csr_new(null, $privkey);
  183. VERIFY($csr != null);
  184. $pubkey = openssl_csr_get_public_key($csr);
  185. VERIFY($pubkey != null);
  186. $data = "some secret messages";
  187. VERIFY(openssl_seal($data, $sealed, $ekeys, array($pubkey)));
  188. VERIFY(strlen($sealed) > 0);
  189. VS(count($ekeys), 1);
  190. VERIFY(strlen($ekeys[0]) > 0);
  191. VERIFY(openssl_open($sealed, $open_data, $ekeys[0], $privkey));
  192. VS($open_data, $data);
  193. VERIFY(openssl_open($sealed, $open_data, $ekeys[0], $privkey, 'RC4'));
  194. VS($open_data, $data);
  195. VERIFY(openssl_seal($data, $sealed, $ekeys, array($pubkey), 'AES-256-ECB'));
  196. VERIFY(strlen($sealed) > 0);
  197. VS(count($ekeys), 1);
  198. VERIFY(strlen($ekeys[0]) > 0);
  199. VERIFY(openssl_open($sealed, $open_data, $ekeys[0], $privkey, 'AES-256-ECB'));
  200. VS($open_data, $data);
  201. }
  202. function test_openssl_sign() {
  203. $privkey = openssl_pkey_new();
  204. VERIFY($privkey != null);
  205. $csr = openssl_csr_new(null, $privkey);
  206. VERIFY($csr != null);
  207. $pubkey = openssl_csr_get_public_key($csr);
  208. VERIFY($pubkey != null);
  209. $data = "some secret messages";
  210. VERIFY(openssl_sign($data, $signature, $privkey));
  211. VS(openssl_verify($data, $signature, $pubkey), 1);
  212. }
  213. function test_openssl_x509_check_private_key() {
  214. $privkey = openssl_pkey_new();
  215. VERIFY($privkey != null);
  216. $csr = openssl_csr_new(null, $privkey);
  217. VERIFY($csr != null);
  218. $scert = openssl_csr_sign($csr, null, $privkey, 365);
  219. VERIFY(openssl_x509_check_private_key($scert, $privkey));
  220. }
  221. function test_openssl_x509_checkpurpose() {
  222. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  223. $cert = openssl_x509_read($fcert);
  224. VS(openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_CLIENT), 0);
  225. VS(openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER), 0);
  226. }
  227. function test_openssl_x509_export_to_file() {
  228. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  229. $cert = openssl_x509_read($fcert);
  230. $tmp = tempnam('/tmp', 'x509vmopenssltest');
  231. unlink($tmp);
  232. VS(file_get_contents($tmp), false);
  233. VERIFY(openssl_x509_export_to_file($cert, $tmp));
  234. $fcert2 = file_get_contents($tmp);
  235. $cert2 = openssl_x509_read($fcert2);
  236. $info = openssl_x509_parse($cert2);
  237. VS($info['subject']['O'], "RSA Data Security, Inc.");
  238. unlink($tmp);
  239. }
  240. function test_openssl_x509_export() {
  241. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  242. $cert = openssl_x509_read($fcert);
  243. VERIFY(openssl_x509_export($cert, $out));
  244. $cert2 = openssl_x509_read($out);
  245. $info = openssl_x509_parse($cert2);
  246. VS($info['subject']['O'], "RSA Data Security, Inc.");
  247. }
  248. function test_openssl_x509_free() {
  249. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  250. $cert = openssl_x509_read($fcert);
  251. VERIFY($cert != null);
  252. openssl_x509_free($cert);
  253. }
  254. function test_openssl_x509_parse() {
  255. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  256. $cert = openssl_x509_read($fcert);
  257. $info = openssl_x509_parse($cert);
  258. VS($info['subject']['O'], "RSA Data Security, Inc.");
  259. }
  260. function test_openssl_x509_read() {
  261. $fcert = file_get_contents(__DIR__."/test_x509.crt");
  262. $cert = openssl_x509_read($fcert);
  263. VERIFY($cert != null);
  264. }
  265. function test_openssl_encrypt() {
  266. $test = "OpenSSL is good for encrypting things";
  267. $secret = "supersecretthing";
  268. $cipher = "AES-256-CBC";
  269. $iv_len = openssl_cipher_iv_length($cipher);
  270. $iv = openssl_random_pseudo_bytes($iv_len);
  271. $data = openssl_encrypt($test, $cipher, $secret, 0, $iv);
  272. VS($test, openssl_decrypt($data, $cipher, $secret, 0, $iv));
  273. $data = openssl_encrypt($test, $cipher, $secret, OPENSSL_RAW_DATA, $iv);
  274. VS($test, openssl_decrypt($data, $cipher, $secret, OPENSSL_RAW_DATA, $iv));
  275. }
  276. function test_openssl_digest() {
  277. $test = "OpenSSL is also good for hashing things";
  278. VS(md5($test), openssl_digest($test, "md5"));
  279. }
  280. //////////////////////////////////////////////////////////////////////
  281. test_openssl_csr_export_to_file();
  282. test_openssl_csr_get_public_key();
  283. test_openssl_csr_get_subject();
  284. test_openssl_csr_sign();
  285. test_openssl_error_string();
  286. test_openssl_free_key();
  287. test_openssl_pkcs12_export_to_file();
  288. test_openssl_pkcs12_read();
  289. test_openssl_pkcs7_sign();
  290. test_openssl_pkey_export_to_file();
  291. test_openssl_pkey_export();
  292. test_openssl_pkey_free();
  293. test_openssl_pkey_get_details();
  294. test_openssl_private_encrypt();
  295. test_openssl_public_encrypt();
  296. test_openssl_seal();
  297. test_openssl_sign();
  298. test_openssl_x509_check_private_key();
  299. test_openssl_x509_checkpurpose();
  300. test_openssl_x509_export_to_file();
  301. test_openssl_x509_export();
  302. test_openssl_x509_free();
  303. test_openssl_x509_parse();
  304. test_openssl_x509_read();
  305. test_openssl_encrypt();
  306. test_openssl_digest();