geoip redirection for superforex.com.au
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
14KB

  1. <?php
  2. /*
  3. * This file is part of composer/ca-bundle.
  4. *
  5. * (c) Composer <https://github.com/composer>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. namespace Composer\CaBundle;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Process\PhpProcess;
  13. /**
  14. * @author Chris Smith <chris@cs278.org>
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class CaBundle
  18. {
  19. private static $caPath;
  20. private static $caFileValidity = array();
  21. private static $useOpensslParse;
  22. /**
  23. * Returns the system CA bundle path, or a path to the bundled one
  24. *
  25. * This method was adapted from Sslurp.
  26. * https://github.com/EvanDotPro/Sslurp
  27. *
  28. * (c) Evan Coury <me@evancoury.com>
  29. *
  30. * For the full copyright and license information, please see below:
  31. *
  32. * Copyright (c) 2013, Evan Coury
  33. * All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without modification,
  36. * are permitted provided that the following conditions are met:
  37. *
  38. * * Redistributions of source code must retain the above copyright notice,
  39. * this list of conditions and the following disclaimer.
  40. *
  41. * * Redistributions in binary form must reproduce the above copyright notice,
  42. * this list of conditions and the following disclaimer in the documentation
  43. * and/or other materials provided with the distribution.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  46. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  47. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  48. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  49. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  52. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  53. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  54. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. * @param LoggerInterface $logger optional logger for information about which CA files were loaded
  57. * @return string path to a CA bundle file or directory
  58. */
  59. public static function getSystemCaRootBundlePath(LoggerInterface $logger = null)
  60. {
  61. if (self::$caPath !== null) {
  62. return self::$caPath;
  63. }
  64. $caBundlePaths = array();
  65. // If SSL_CERT_FILE env variable points to a valid certificate/bundle, use that.
  66. // This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
  67. $caBundlePaths[] = self::getEnvVariable('SSL_CERT_FILE');
  68. // If SSL_CERT_DIR env variable points to a valid certificate/bundle, use that.
  69. // This mimics how OpenSSL uses the SSL_CERT_FILE env variable.
  70. $caBundlePaths[] = self::getEnvVariable('SSL_CERT_DIR');
  71. $caBundlePaths[] = ini_get('openssl.cafile');
  72. $caBundlePaths[] = ini_get('openssl.capath');
  73. $otherLocations = array(
  74. '/etc/pki/tls/certs/ca-bundle.crt', // Fedora, RHEL, CentOS (ca-certificates package)
  75. '/etc/ssl/certs/ca-certificates.crt', // Debian, Ubuntu, Gentoo, Arch Linux (ca-certificates package)
  76. '/etc/ssl/ca-bundle.pem', // SUSE, openSUSE (ca-certificates package)
  77. '/usr/local/share/certs/ca-root-nss.crt', // FreeBSD (ca_root_nss_package)
  78. '/usr/ssl/certs/ca-bundle.crt', // Cygwin
  79. '/opt/local/share/curl/curl-ca-bundle.crt', // OS X macports, curl-ca-bundle package
  80. '/usr/local/share/curl/curl-ca-bundle.crt', // Default cURL CA bunde path (without --with-ca-bundle option)
  81. '/usr/share/ssl/certs/ca-bundle.crt', // Really old RedHat?
  82. '/etc/ssl/cert.pem', // OpenBSD
  83. '/usr/local/etc/ssl/cert.pem', // FreeBSD 10.x
  84. '/usr/local/etc/openssl/cert.pem', // OS X homebrew, openssl package
  85. '/usr/local/etc/openssl@1.1/cert.pem', // OS X homebrew, openssl@1.1 package
  86. );
  87. foreach($otherLocations as $location) {
  88. $otherLocations[] = dirname($location);
  89. }
  90. $caBundlePaths = array_merge($caBundlePaths, $otherLocations);
  91. foreach ($caBundlePaths as $caBundle) {
  92. if (self::caFileUsable($caBundle, $logger)) {
  93. return self::$caPath = $caBundle;
  94. }
  95. if (self::caDirUsable($caBundle)) {
  96. return self::$caPath = $caBundle;
  97. }
  98. }
  99. return self::$caPath = static::getBundledCaBundlePath(); // Bundled CA file, last resort
  100. }
  101. /**
  102. * Returns the path to the bundled CA file
  103. *
  104. * In case you don't want to trust the user or the system, you can use this directly
  105. *
  106. * @return string path to a CA bundle file
  107. */
  108. public static function getBundledCaBundlePath()
  109. {
  110. $caBundleFile = __DIR__.'/../res/cacert.pem';
  111. // cURL does not understand 'phar://' paths
  112. // see https://github.com/composer/ca-bundle/issues/10
  113. if (0 === strpos($caBundleFile, 'phar://')) {
  114. file_put_contents(
  115. $tempCaBundleFile = tempnam(sys_get_temp_dir(), 'openssl-ca-bundle-'),
  116. file_get_contents($caBundleFile)
  117. );
  118. register_shutdown_function(function() use ($tempCaBundleFile) {
  119. @unlink($tempCaBundleFile);
  120. });
  121. $caBundleFile = $tempCaBundleFile;
  122. }
  123. return $caBundleFile;
  124. }
  125. /**
  126. * Validates a CA file using opensl_x509_parse only if it is safe to use
  127. *
  128. * @param string $filename
  129. * @param LoggerInterface $logger optional logger for information about which CA files were loaded
  130. *
  131. * @return bool
  132. */
  133. public static function validateCaFile($filename, LoggerInterface $logger = null)
  134. {
  135. static $warned = false;
  136. if (isset(self::$caFileValidity[$filename])) {
  137. return self::$caFileValidity[$filename];
  138. }
  139. $contents = file_get_contents($filename);
  140. // assume the CA is valid if php is vulnerable to
  141. // https://www.sektioneins.de/advisories/advisory-012013-php-openssl_x509_parse-memory-corruption-vulnerability.html
  142. if (!static::isOpensslParseSafe()) {
  143. if (!$warned && $logger) {
  144. $logger->warning(sprintf(
  145. 'Your version of PHP, %s, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.',
  146. PHP_VERSION
  147. ));
  148. $warned = true;
  149. }
  150. $isValid = !empty($contents);
  151. } else {
  152. $isValid = (bool) openssl_x509_parse($contents);
  153. }
  154. if ($logger) {
  155. $logger->debug('Checked CA file '.realpath($filename).': '.($isValid ? 'valid' : 'invalid'));
  156. }
  157. return self::$caFileValidity[$filename] = $isValid;
  158. }
  159. /**
  160. * Test if it is safe to use the PHP function openssl_x509_parse().
  161. *
  162. * This checks if OpenSSL extensions is vulnerable to remote code execution
  163. * via the exploit documented as CVE-2013-6420.
  164. *
  165. * @return bool
  166. */
  167. public static function isOpensslParseSafe()
  168. {
  169. if (null !== self::$useOpensslParse) {
  170. return self::$useOpensslParse;
  171. }
  172. if (PHP_VERSION_ID >= 50600) {
  173. return self::$useOpensslParse = true;
  174. }
  175. // Vulnerable:
  176. // PHP 5.3.0 - PHP 5.3.27
  177. // PHP 5.4.0 - PHP 5.4.22
  178. // PHP 5.5.0 - PHP 5.5.6
  179. if (
  180. (PHP_VERSION_ID < 50400 && PHP_VERSION_ID >= 50328)
  181. || (PHP_VERSION_ID < 50500 && PHP_VERSION_ID >= 50423)
  182. || (PHP_VERSION_ID < 50600 && PHP_VERSION_ID >= 50507)
  183. ) {
  184. // This version of PHP has the fix for CVE-2013-6420 applied.
  185. return self::$useOpensslParse = true;
  186. }
  187. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  188. // Windows is probably insecure in this case.
  189. return self::$useOpensslParse = false;
  190. }
  191. $compareDistroVersionPrefix = function ($prefix, $fixedVersion) {
  192. $regex = '{^'.preg_quote($prefix).'([0-9]+)$}';
  193. if (preg_match($regex, PHP_VERSION, $m)) {
  194. return ((int) $m[1]) >= $fixedVersion;
  195. }
  196. return false;
  197. };
  198. // Hard coded list of PHP distributions with the fix backported.
  199. if (
  200. $compareDistroVersionPrefix('5.3.3-7+squeeze', 18) // Debian 6 (Squeeze)
  201. || $compareDistroVersionPrefix('5.4.4-14+deb7u', 7) // Debian 7 (Wheezy)
  202. || $compareDistroVersionPrefix('5.3.10-1ubuntu3.', 9) // Ubuntu 12.04 (Precise)
  203. ) {
  204. return self::$useOpensslParse = true;
  205. }
  206. // Symfony Process component is missing so we assume it is unsafe at this point
  207. if (!class_exists('Symfony\Component\Process\PhpProcess')) {
  208. return self::$useOpensslParse = false;
  209. }
  210. // This is where things get crazy, because distros backport security
  211. // fixes the chances are on NIX systems the fix has been applied but
  212. // it's not possible to verify that from the PHP version.
  213. //
  214. // To verify exec a new PHP process and run the issue testcase with
  215. // known safe input that replicates the bug.
  216. // Based on testcase in https://github.com/php/php-src/commit/c1224573c773b6845e83505f717fbf820fc18415
  217. // changes in https://github.com/php/php-src/commit/76a7fd893b7d6101300cc656058704a73254d593
  218. $cert = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVwRENDQTR5Z0F3SUJBZ0lKQUp6dThyNnU2ZUJjTUEwR0NTcUdTSWIzRFFFQkJRVUFNSUhETVFzd0NRWUQKVlFRR0V3SkVSVEVjTUJvR0ExVUVDQXdUVG05eVpISm9aV2x1TFZkbGMzUm1ZV3hsYmpFUU1BNEdBMVVFQnd3SApTOE9Ed3Jac2JqRVVNQklHQTFVRUNnd0xVMlZyZEdsdmJrVnBibk14SHpBZEJnTlZCQXNNRmsxaGJHbGphVzkxCmN5QkRaWEowSUZObFkzUnBiMjR4SVRBZkJnTlZCQU1NR0cxaGJHbGphVzkxY3k1elpXdDBhVzl1WldsdWN5NWsKWlRFcU1DZ0dDU3FHU0liM0RRRUpBUlliYzNSbFptRnVMbVZ6YzJWeVFITmxhM1JwYjI1bGFXNXpMbVJsTU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mZGSXQ5RGJHdkhUY3lPdU4rblpMUEJtM3ZXeG50eUlJdlZBZ01CQUFHalFqQkFNQWtHQTFVZEV3UUNNQUF3CkVRWUpZSVpJQVliNFFnRUJCQVFEQWdlQU1Bc0dBMVVkRHdRRUF3SUZvREFUQmdOVkhTVUVEREFLQmdnckJnRUYKQlFjREFqQU5CZ2txaGtpRzl3MEJBUVVGQUFPQ0FRRUFHMGZaWVlDVGJkajFYWWMrMVNub2FQUit2SThDOENhRAo4KzBVWWhkbnlVNGdnYTBCQWNEclk5ZTk0ZUVBdTZacXljRjZGakxxWFhkQWJvcHBXb2NyNlQ2R0QxeDMzQ2tsClZBcnpHL0t4UW9oR0QySmVxa2hJTWxEb214SE83a2EzOStPYThpMnZXTFZ5alU4QVp2V01BcnVIYTRFRU55RzcKbFcyQWFnYUZLRkNyOVRuWFRmcmR4R1ZFYnY3S1ZRNmJkaGc1cDVTanBXSDErTXEwM3VSM1pYUEJZZHlWODMxOQpvMGxWajFLRkkyRENML2xpV2lzSlJvb2YrMWNSMzVDdGQwd1lCY3BCNlRac2xNY09QbDc2ZHdLd0pnZUpvMlFnClpzZm1jMnZDMS9xT2xOdU5xLzBUenprVkd2OEVUVDNDZ2FVK1VYZTRYT1Z2a2NjZWJKbjJkZz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K';
  219. $script = <<<'EOT'
  220. error_reporting(-1);
  221. $info = openssl_x509_parse(base64_decode('%s'));
  222. var_dump(PHP_VERSION, $info['issuer']['emailAddress'], $info['validFrom_time_t']);
  223. EOT;
  224. $script = '<'."?php\n".sprintf($script, $cert);
  225. try {
  226. $process = new PhpProcess($script);
  227. $process->mustRun();
  228. } catch (\Exception $e) {
  229. // In the case of any exceptions just accept it is not possible to
  230. // determine the safety of openssl_x509_parse and bail out.
  231. return self::$useOpensslParse = false;
  232. }
  233. $output = preg_split('{\r?\n}', trim($process->getOutput()));
  234. $errorOutput = trim($process->getErrorOutput());
  235. if (
  236. count($output) === 3
  237. && $output[0] === sprintf('string(%d) "%s"', strlen(PHP_VERSION), PHP_VERSION)
  238. && $output[1] === 'string(27) "stefan.esser@sektioneins.de"'
  239. && $output[2] === 'int(-1)'
  240. && preg_match('{openssl_x509_parse\(\): illegal (?:ASN1 data type for|length in) timestamp in - on line \d+}', $errorOutput)
  241. ) {
  242. // This PHP has the fix backported probably by a distro security team.
  243. return self::$useOpensslParse = true;
  244. }
  245. return self::$useOpensslParse = false;
  246. }
  247. /**
  248. * Resets the static caches
  249. */
  250. public static function reset()
  251. {
  252. self::$caFileValidity = array();
  253. self::$caPath = null;
  254. self::$useOpensslParse = null;
  255. }
  256. private static function getEnvVariable($name)
  257. {
  258. if (isset($_SERVER[$name])) {
  259. return (string) $_SERVER[$name];
  260. }
  261. if (PHP_SAPI === 'cli' && ($value = getenv($name)) !== false && $value !== null) {
  262. return (string) $value;
  263. }
  264. return false;
  265. }
  266. private static function caFileUsable($certFile, LoggerInterface $logger = null)
  267. {
  268. return $certFile && @is_file($certFile) && @is_readable($certFile) && static::validateCaFile($certFile, $logger);
  269. }
  270. private static function caDirUsable($certDir)
  271. {
  272. return $certDir && @is_dir($certDir) && @is_readable($certDir) && glob($certDir . '/*');
  273. }
  274. }