bug81713.phpt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. --TEST--
  2. Bug #81713 (OpenSSL functions null byte injection)
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded("openssl")) die("skip openssl not loaded");
  6. ?>
  7. --FILE--
  8. <?php
  9. $priv_key_file = "file://" . __DIR__ . "/private_rsa_1024.key";
  10. $priv_key_file_null = "$priv_key_file\x00foo";
  11. $crt = __DIR__ . '/cert.crt';
  12. $crt_null = "$crt\x00foo";
  13. $csr = __DIR__ . '/cert.csr';
  14. $crt_file = "file://$crt";
  15. $crt_file_null = "$crt_file\x00foo";
  16. $csr_file = "file://$csr";
  17. $csr_file_null = "$csr_file\x00foo";
  18. $infile = __DIR__ . '/plain.txt';
  19. $infile_null = "$infile\x00acd";;
  20. $outfile = __DIR__ . '/bug81713.out';
  21. $outfile_null = "$outfile\x00acd";
  22. $eml = __DIR__ . "/signed.eml";
  23. $eml_null = "$eml\x00foo";
  24. $headers = ["test@test", "testing openssl_cms_encrypt()"];
  25. $cainfo = [$crt];
  26. $cainfo_null = [$crt_file_null];
  27. $config = __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf';
  28. $config_arg = array('config' => $config);
  29. $dn = [
  30. "countryName" => "BR",
  31. "stateOrProvinceName" => "Rio Grande do Sul",
  32. "localityName" => "Porto Alegre",
  33. "commonName" => "Henrique do N. Angelo",
  34. "emailAddress" => "hnangelo@php.net"
  35. ];
  36. $csr_args = [
  37. "digest_alg" => "sha256",
  38. "private_key_bits" => 2048,
  39. "private_key_type" => OPENSSL_KEYTYPE_DSA,
  40. "encrypt_key" => true,
  41. "config" => $config,
  42. ];
  43. $tests = [
  44. ["openssl_pkey_get_public", $crt_file],
  45. ["openssl_pkey_get_public", $crt_file_null],
  46. ["openssl_pkey_get_private", $crt_file_null],
  47. ["openssl_pkey_export_to_file", $priv_key_file_null, $outfile],
  48. ["openssl_pkey_export_to_file", $priv_key_file, $outfile_null],
  49. ["openssl_pkey_export", $priv_key_file_null, &$out],
  50. ["openssl_pkey_derive", $priv_key_file_null, $priv_key_file],
  51. ["openssl_pkey_derive", $priv_key_file, $priv_key_file_null],
  52. ["openssl_private_encrypt", "test", &$out, $priv_key_file_null],
  53. ["openssl_private_decrypt", "test", &$out, $priv_key_file_null],
  54. ["openssl_public_encrypt", "test", &$out, $priv_key_file_null],
  55. ["openssl_public_decrypt", "test", &$out, $priv_key_file_null],
  56. ["openssl_sign", "test", &$out, $priv_key_file_null],
  57. ["openssl_verify", "test", "sig", $priv_key_file_null],
  58. ["openssl_seal", "test", &$sealed, &$ekeys, [$priv_key_file_null], "AES-128-CBC", &$iv],
  59. ["openssl_open", "test", &$open, "aaa", $priv_key_file_null, "AES-128-CBC", &$iv],
  60. ["openssl_csr_new", $dn, &$priv_key_file_null, $csr_args],
  61. ["openssl_csr_get_subject", $csr_file_null],
  62. ["openssl_csr_get_public_key", $csr_file_null],
  63. ["openssl_x509_fingerprint", $crt_file_null],
  64. ["openssl_x509_export_to_file", $crt_file_null, $outfile],
  65. ["openssl_x509_export_to_file", $crt_file, $outfile_null],
  66. ["openssl_x509_export", $crt_file_null, &$out],
  67. ["openssl_x509_checkpurpose", $crt_file_null, X509_PURPOSE_SSL_CLIENT],
  68. ["openssl_x509_checkpurpose", $crt_file, X509_PURPOSE_SSL_CLIENT, $cainfo_null],
  69. ["openssl_x509_check_private_key", $crt_file_null, $priv_key_file],
  70. ["openssl_x509_check_private_key", $crt_file, $priv_key_file_null],
  71. ["openssl_x509_verify", $crt_file_null, $priv_key_file],
  72. ["openssl_x509_verify", $crt_file, $priv_key_file_null],
  73. ["openssl_x509_parse", $crt_file_null],
  74. ["openssl_x509_read", $crt_file_null],
  75. ["openssl_cms_encrypt", $infile_null, $outfile, $crt_file, $headers],
  76. ["openssl_cms_encrypt", $infile, $outfile_null, $crt_file, $headers],
  77. ["openssl_cms_encrypt", $infile, $outfile, $crt_file_null, $headers],
  78. ["openssl_cms_encrypt", $infile, $outfile, $cainfo_null, $headers],
  79. ["openssl_cms_decrypt", $infile_null, $outfile, $crt_file, $priv_key_file],
  80. ["openssl_cms_decrypt", $infile, $outfile_null, $crt_file, $priv_key_file],
  81. ["openssl_cms_decrypt", $infile, $outfile, $crt_file_null, $priv_key_file],
  82. ["openssl_cms_decrypt", $infile, $outfile, $crt_file, $priv_key_file_null],
  83. ["openssl_cms_sign", $infile_null, "$outfile", $crt_file, $priv_key_file, $headers],
  84. ["openssl_cms_sign", $infile, $outfile_null, $crt_file, $priv_key_file, $headers],
  85. ["openssl_cms_sign", $infile, $outfile, $crt_file_null, $priv_key_file, $headers],
  86. ["openssl_cms_sign", $infile, $outfile, $crt_file, $crt_file_null, $headers],
  87. ["openssl_cms_sign", $infile, $outfile, $crt_file, $crt_file, $headers, 0, OPENSSL_ENCODING_DER, $crt_file_null],
  88. ["openssl_cms_verify", $eml_null, OPENSSL_CMS_NOVERIFY, $outfile, $cainfo, $outfile, $outfile, $outfile],
  89. ["openssl_cms_verify", $eml, OPENSSL_CMS_NOVERIFY, $outfile_null, $cainfo, $outfile, $outfile, $outfile],
  90. ["openssl_cms_verify", $eml, OPENSSL_CMS_NOVERIFY, $outfile, $cainfo_null],
  91. ["openssl_cms_verify", $eml, OPENSSL_CMS_NOVERIFY, $outfile, $cainfo, $outfile_null, $outfile, $outfile],
  92. ["openssl_cms_verify", $eml, OPENSSL_CMS_NOVERIFY, $outfile, $cainfo, $outfile, $outfile_null, $outfile],
  93. ["openssl_cms_verify", $eml, OPENSSL_CMS_NOVERIFY, $outfile, $cainfo, $outfile, $outfile, $outfile_null],
  94. ["openssl_pkcs7_encrypt", $infile_null, $outfile, $crt_file, $headers],
  95. ["openssl_pkcs7_encrypt", $infile, $outfile_null, $crt_file, $headers],
  96. ["openssl_pkcs7_encrypt", $infile, $outfile, $crt_file_null, $headers],
  97. ["openssl_pkcs7_encrypt", $infile, $outfile, $cainfo_null, $headers],
  98. ["openssl_pkcs7_decrypt", $infile_null, $outfile, $crt_file, $priv_key_file],
  99. ["openssl_pkcs7_decrypt", $infile, $outfile_null, $crt_file, $priv_key_file],
  100. ["openssl_pkcs7_decrypt", $infile, $outfile, $crt_file_null, $priv_key_file],
  101. ["openssl_pkcs7_decrypt", $infile, $outfile, $crt_file, $priv_key_file_null],
  102. ["openssl_pkcs7_sign", $infile_null, "$outfile", $crt_file, $priv_key_file, $headers],
  103. ["openssl_pkcs7_sign", $infile, $outfile_null, $crt_file, $priv_key_file, $headers],
  104. ["openssl_pkcs7_sign", $infile, $outfile, $crt_file_null, $priv_key_file, $headers],
  105. ["openssl_pkcs7_sign", $infile, $outfile, $crt_file, $crt_file_null, $headers],
  106. ["openssl_pkcs7_sign", $infile, $outfile, $crt_file, $crt_file, $headers, 0, $crt_file_null],
  107. ["openssl_pkcs7_verify", $eml_null, 0, $outfile, $cainfo, $outfile, $outfile, $outfile],
  108. ["openssl_pkcs7_verify", $eml, 0, $outfile_null, $cainfo, $outfile, $outfile, $outfile],
  109. ["openssl_pkcs7_verify", $eml, 0, $outfile, $cainfo_null],
  110. ["openssl_pkcs7_verify", $eml, 0, $outfile, $cainfo, $outfile_null, $outfile, $outfile],
  111. ["openssl_pkcs7_verify", $eml, 0, $outfile, $cainfo, $outfile, $outfile_null, $outfile],
  112. ["openssl_pkcs7_verify", $eml, 0, $outfile, $cainfo, $outfile, $outfile, $outfile_null],
  113. ["openssl_pkcs12_export", $crt_file_null, &$out, $priv_key_file, "pwd"],
  114. ["openssl_pkcs12_export", $crt_file, &$out, $priv_key_file_null, "pwd"],
  115. ["openssl_pkcs12_export", $crt_file, &$out, $priv_key_file, "pwd", ["extracerts" => [$crt_file_null]]],
  116. ["openssl_pkcs12_export_to_file", $crt_file_null, $outfile, $priv_key_file, "pwd"],
  117. ["openssl_pkcs12_export_to_file", $crt_file, $outfile_null, $priv_key_file_null, "pwd"],
  118. ["openssl_pkcs12_export_to_file", $crt_file, $outfile, $priv_key_file_null, "pwd"],
  119. ["openssl_pkcs12_export_to_file", $crt_file, $outfile, $priv_key_file, "pwd", ["extracerts" => [$crt_file_null]]],
  120. ];
  121. foreach ($tests as $test) {
  122. try {
  123. $key = call_user_func_array($test[0], array_slice($test, 1));
  124. var_dump($key);
  125. }
  126. catch (ValueError $e) {
  127. echo $e->getMessage() . PHP_EOL;
  128. }
  129. }
  130. ?>
  131. --CLEAN--
  132. <?php
  133. $outfile = __DIR__ . '/bug81713.out';
  134. @unlink($outfile);
  135. ?>
  136. --EXPECTF--
  137. object(OpenSSLAsymmetricKey)#1 (0) {
  138. }
  139. openssl_pkey_get_public(): Argument #1 ($public_key) must not contain any null bytes
  140. openssl_pkey_get_private(): Argument #1 ($private_key) must not contain any null bytes
  141. openssl_pkey_export_to_file(): Argument #1 ($key) must not contain any null bytes
  142. openssl_pkey_export_to_file(): Argument #2 ($output_filename) must not contain any null bytes
  143. openssl_pkey_export(): Argument #1 ($key) must not contain any null bytes
  144. openssl_pkey_derive(): Argument #1 ($public_key) must not contain any null bytes
  145. openssl_pkey_derive(): Argument #2 ($private_key) must not contain any null bytes
  146. openssl_private_encrypt(): Argument #3 ($private_key) must not contain any null bytes
  147. openssl_private_decrypt(): Argument #3 ($private_key) must not contain any null bytes
  148. openssl_public_encrypt(): Argument #3 ($public_key) must not contain any null bytes
  149. openssl_public_decrypt(): Argument #3 ($public_key) must not contain any null bytes
  150. openssl_sign(): Argument #3 ($private_key) must not contain any null bytes
  151. openssl_verify(): Argument #3 ($public_key) must not contain any null bytes
  152. openssl_seal(): Argument #4 ($public_key) must not contain any null bytes
  153. openssl_open(): Argument #4 ($private_key) must not contain any null bytes
  154. openssl_csr_new(): Argument #2 ($private_key) must not contain any null bytes
  155. openssl_csr_get_subject(): Argument #1 ($csr) must not contain any null bytes
  156. openssl_csr_get_public_key(): Argument #1 ($csr) must not contain any null bytes
  157. Warning: openssl_x509_fingerprint(): X.509 Certificate cannot be retrieved in %s on line %d
  158. openssl_x509_fingerprint(): Argument #1 ($certificate) must not contain any null bytes
  159. Warning: openssl_x509_export_to_file(): X.509 Certificate cannot be retrieved in %s on line %d
  160. openssl_x509_export_to_file(): Argument #1 ($certificate) must not contain any null bytes
  161. openssl_x509_export_to_file(): Argument #2 ($output_filename) must not contain any null bytes
  162. Warning: openssl_x509_export(): X.509 Certificate cannot be retrieved in %s on line %d
  163. openssl_x509_export(): Argument #1 ($certificate) must not contain any null bytes
  164. openssl_x509_checkpurpose(): Argument #1 ($certificate) must not contain any null bytes
  165. openssl_x509_checkpurpose(): Argument #3 ($ca_info) array item must not contain any null bytes
  166. openssl_x509_check_private_key(): Argument #1 ($certificate) must not contain any null bytes
  167. openssl_x509_check_private_key(): Argument #2 ($private_key) must not contain any null bytes
  168. openssl_x509_verify(): Argument #1 ($certificate) must not contain any null bytes
  169. openssl_x509_verify(): Argument #2 ($public_key) must not contain any null bytes
  170. openssl_x509_parse(): Argument #1 ($certificate) must not contain any null bytes
  171. Warning: openssl_x509_read(): X.509 Certificate cannot be retrieved in %s on line %d
  172. openssl_x509_read(): Argument #1 ($certificate) must not contain any null bytes
  173. openssl_cms_encrypt(): Argument #1 ($input_filename) must not contain any null bytes
  174. openssl_cms_encrypt(): Argument #2 ($output_filename) must not contain any null bytes
  175. openssl_cms_encrypt(): Argument #3 ($certificate) must not contain any null bytes
  176. openssl_cms_encrypt(): Argument #3 ($certificate) array item must not contain any null bytes
  177. openssl_cms_decrypt(): Argument #1 ($input_filename) must not contain any null bytes
  178. openssl_cms_decrypt(): Argument #2 ($output_filename) must not contain any null bytes
  179. Warning: openssl_cms_decrypt(): X.509 Certificate cannot be retrieved in %s on line %d
  180. openssl_cms_decrypt(): Argument #3 ($certificate) must not contain any null bytes
  181. openssl_cms_decrypt(): Argument #4 ($private_key) must not contain any null bytes
  182. openssl_cms_sign(): Argument #1 ($input_filename) must not contain any null bytes
  183. openssl_cms_sign(): Argument #2 ($output_filename) must not contain any null bytes
  184. Warning: openssl_cms_sign(): X.509 Certificate cannot be retrieved in %s on line %d
  185. openssl_cms_sign(): Argument #3 ($certificate) must not contain any null bytes
  186. openssl_cms_sign(): Argument #4 ($private_key) must not contain any null bytes
  187. openssl_cms_sign(): Argument #8 ($untrusted_certificates_filename) must not contain any null bytes
  188. openssl_cms_verify(): Argument #1 ($input_filename) must not contain any null bytes
  189. openssl_cms_verify(): Argument #3 ($certificates) must not contain any null bytes
  190. openssl_cms_verify(): Argument #4 ($ca_info) array item must not contain any null bytes
  191. openssl_cms_verify(): Argument #5 ($untrusted_certificates_filename) must not contain any null bytes
  192. openssl_cms_verify(): Argument #6 ($content) must not contain any null bytes
  193. openssl_cms_verify(): Argument #7 ($pk7) must not contain any null bytes
  194. openssl_pkcs7_encrypt(): Argument #1 ($input_filename) must not contain any null bytes
  195. openssl_pkcs7_encrypt(): Argument #2 ($output_filename) must not contain any null bytes
  196. openssl_pkcs7_encrypt(): Argument #3 ($certificate) must not contain any null bytes
  197. openssl_pkcs7_encrypt(): Argument #3 ($certificate) array item must not contain any null bytes
  198. openssl_pkcs7_decrypt(): Argument #1 ($input_filename) must not contain any null bytes
  199. openssl_pkcs7_decrypt(): Argument #2 ($output_filename) must not contain any null bytes
  200. Warning: openssl_pkcs7_decrypt(): X.509 Certificate cannot be retrieved in %s on line %d
  201. openssl_pkcs7_decrypt(): Argument #3 ($certificate) must not contain any null bytes
  202. openssl_pkcs7_decrypt(): Argument #4 ($private_key) must not contain any null bytes
  203. openssl_pkcs7_sign(): Argument #1 ($input_filename) must not contain any null bytes
  204. openssl_pkcs7_sign(): Argument #2 ($output_filename) must not contain any null bytes
  205. Warning: openssl_pkcs7_sign(): X.509 Certificate cannot be retrieved in %s on line %d
  206. openssl_pkcs7_sign(): Argument #3 ($certificate) must not contain any null bytes
  207. openssl_pkcs7_sign(): Argument #4 ($private_key) must not contain any null bytes
  208. openssl_pkcs7_sign(): Argument #7 ($untrusted_certificates_filename) must not contain any null bytes
  209. openssl_pkcs7_verify(): Argument #1 ($input_filename) must not contain any null bytes
  210. openssl_pkcs7_verify(): Argument #3 ($signers_certificates_filename) must not contain any null bytes
  211. openssl_pkcs7_verify(): Argument #4 ($ca_info) array item must not contain any null bytes
  212. openssl_pkcs7_verify(): Argument #5 ($untrusted_certificates_filename) must not contain any null bytes
  213. openssl_pkcs7_verify(): Argument #6 ($content) must not contain any null bytes
  214. openssl_pkcs7_verify(): Argument #7 ($output_filename) must not contain any null bytes
  215. Warning: openssl_pkcs12_export(): X.509 Certificate cannot be retrieved in %s on line %d
  216. openssl_pkcs12_export(): Argument #1 ($certificate) must not contain any null bytes
  217. openssl_pkcs12_export(): Argument #3 ($private_key) must not contain any null bytes
  218. openssl_pkcs12_export(): Argument #5 ($options) option extracerts array item must not contain any null bytes
  219. Warning: openssl_pkcs12_export_to_file(): X.509 Certificate cannot be retrieved in %s on line %d
  220. openssl_pkcs12_export_to_file(): Argument #1 ($certificate) must not contain any null bytes
  221. openssl_pkcs12_export_to_file(): Argument #2 ($output_filename) must not contain any null bytes
  222. openssl_pkcs12_export_to_file(): Argument #3 ($private_key) must not contain any null bytes
  223. openssl_pkcs12_export_to_file(): Argument #5 ($options) option extracerts array item must not contain any null bytes