截流自动化的商城平台
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * SSL utilities for Requests
  4. *
  5. * @package Requests
  6. * @subpackage Utilities
  7. */
  8. /**
  9. * SSL utilities for Requests
  10. *
  11. * Collection of utilities for working with and verifying SSL certificates.
  12. *
  13. * @package Requests
  14. * @subpackage Utilities
  15. */
  16. class Requests_SSL {
  17. /**
  18. * Verify the certificate against common name and subject alternative names
  19. *
  20. * Unfortunately, PHP doesn't check the certificate against the alternative
  21. * names, leading things like 'https://www.github.com/' to be invalid.
  22. *
  23. * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1
  24. *
  25. * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`)
  26. * @param string $host Host name to verify against
  27. * @param array $cert Certificate data from openssl_x509_parse()
  28. * @return bool
  29. */
  30. public static function verify_certificate($host, $cert) {
  31. $has_dns_alt = false;
  32. // Check the subjectAltName
  33. if (!empty($cert['extensions']) && !empty($cert['extensions']['subjectAltName'])) {
  34. $altnames = explode(',', $cert['extensions']['subjectAltName']);
  35. foreach ($altnames as $altname) {
  36. $altname = trim($altname);
  37. if (strpos($altname, 'DNS:') !== 0) {
  38. continue;
  39. }
  40. $has_dns_alt = true;
  41. // Strip the 'DNS:' prefix and trim whitespace
  42. $altname = trim(substr($altname, 4));
  43. // Check for a match
  44. if (self::match_domain($host, $altname) === true) {
  45. return true;
  46. }
  47. }
  48. }
  49. // Fall back to checking the common name if we didn't get any dNSName
  50. // alt names, as per RFC2818
  51. if (!$has_dns_alt && !empty($cert['subject']['CN'])) {
  52. // Check for a match
  53. if (self::match_domain($host, $cert['subject']['CN']) === true) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. /**
  60. * Verify that a reference name is valid
  61. *
  62. * Verifies a dNSName for HTTPS usage, (almost) as per Firefox's rules:
  63. * - Wildcards can only occur in a name with more than 3 components
  64. * - Wildcards can only occur as the last character in the first
  65. * component
  66. * - Wildcards may be preceded by additional characters
  67. *
  68. * We modify these rules to be a bit stricter and only allow the wildcard
  69. * character to be the full first component; that is, with the exclusion of
  70. * the third rule.
  71. *
  72. * @param string $reference Reference dNSName
  73. * @return boolean Is the name valid?
  74. */
  75. public static function verify_reference_name($reference) {
  76. $parts = explode('.', $reference);
  77. // Check the first part of the name
  78. $first = array_shift($parts);
  79. if (strpos($first, '*') !== false) {
  80. // Check that the wildcard is the full part
  81. if ($first !== '*') {
  82. return false;
  83. }
  84. // Check that we have at least 3 components (including first)
  85. if (count($parts) < 2) {
  86. return false;
  87. }
  88. }
  89. // Check the remaining parts
  90. foreach ($parts as $part) {
  91. if (strpos($part, '*') !== false) {
  92. return false;
  93. }
  94. }
  95. // Nothing found, verified!
  96. return true;
  97. }
  98. /**
  99. * Match a hostname against a dNSName reference
  100. *
  101. * @param string $host Requested host
  102. * @param string $reference dNSName to match against
  103. * @return boolean Does the domain match?
  104. */
  105. public static function match_domain($host, $reference) {
  106. // Check if the reference is blocklisted first
  107. if (self::verify_reference_name($reference) !== true) {
  108. return false;
  109. }
  110. // Check for a direct match
  111. if ($host === $reference) {
  112. return true;
  113. }
  114. // Calculate the valid wildcard match if the host is not an IP address
  115. // Also validates that the host has 3 parts or more, as per Firefox's
  116. // ruleset.
  117. if (ip2long($host) === false) {
  118. $parts = explode('.', $host);
  119. $parts[0] = '*';
  120. $wildcard = implode('.', $parts);
  121. if ($wildcard === $reference) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. }