Ssl.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * SSL utilities for Requests
  4. *
  5. * @package Requests\Utilities
  6. */
  7. namespace WpOrg\Requests;
  8. use WpOrg\Requests\Exception\InvalidArgument;
  9. use WpOrg\Requests\Utility\InputValidator;
  10. /**
  11. * SSL utilities for Requests
  12. *
  13. * Collection of utilities for working with and verifying SSL certificates.
  14. *
  15. * @package Requests\Utilities
  16. */
  17. final class Ssl {
  18. /**
  19. * Verify the certificate against common name and subject alternative names
  20. *
  21. * Unfortunately, PHP doesn't check the certificate against the alternative
  22. * names, leading things like 'https://www.github.com/' to be invalid.
  23. *
  24. * @link https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1
  25. *
  26. * @param string|Stringable $host Host name to verify against
  27. * @param array $cert Certificate data from openssl_x509_parse()
  28. * @return bool
  29. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $host argument is not a string or a stringable object.
  30. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cert argument is not an array or array accessible.
  31. */
  32. public static function verify_certificate($host, $cert) {
  33. if (InputValidator::is_string_or_stringable($host) === false) {
  34. throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host));
  35. }
  36. if (InputValidator::has_array_access($cert) === false) {
  37. throw InvalidArgument::create(2, '$cert', 'array|ArrayAccess', gettype($cert));
  38. }
  39. $has_dns_alt = false;
  40. // Check the subjectAltName
  41. if (!empty($cert['extensions']['subjectAltName'])) {
  42. $altnames = explode(',', $cert['extensions']['subjectAltName']);
  43. foreach ($altnames as $altname) {
  44. $altname = trim($altname);
  45. if (strpos($altname, 'DNS:') !== 0) {
  46. continue;
  47. }
  48. $has_dns_alt = true;
  49. // Strip the 'DNS:' prefix and trim whitespace
  50. $altname = trim(substr($altname, 4));
  51. // Check for a match
  52. if (self::match_domain($host, $altname) === true) {
  53. return true;
  54. }
  55. }
  56. if ($has_dns_alt === true) {
  57. return false;
  58. }
  59. }
  60. // Fall back to checking the common name if we didn't get any dNSName
  61. // alt names, as per RFC2818
  62. if (!empty($cert['subject']['CN'])) {
  63. // Check for a match
  64. return (self::match_domain($host, $cert['subject']['CN']) === true);
  65. }
  66. return false;
  67. }
  68. /**
  69. * Verify that a reference name is valid
  70. *
  71. * Verifies a dNSName for HTTPS usage, (almost) as per Firefox's rules:
  72. * - Wildcards can only occur in a name with more than 3 components
  73. * - Wildcards can only occur as the last character in the first
  74. * component
  75. * - Wildcards may be preceded by additional characters
  76. *
  77. * We modify these rules to be a bit stricter and only allow the wildcard
  78. * character to be the full first component; that is, with the exclusion of
  79. * the third rule.
  80. *
  81. * @param string|Stringable $reference Reference dNSName
  82. * @return boolean Is the name valid?
  83. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object.
  84. */
  85. public static function verify_reference_name($reference) {
  86. if (InputValidator::is_string_or_stringable($reference) === false) {
  87. throw InvalidArgument::create(1, '$reference', 'string|Stringable', gettype($reference));
  88. }
  89. if ($reference === '') {
  90. return false;
  91. }
  92. if (preg_match('`\s`', $reference) > 0) {
  93. // Whitespace detected. This can never be a dNSName.
  94. return false;
  95. }
  96. $parts = explode('.', $reference);
  97. if ($parts !== array_filter($parts)) {
  98. // DNSName cannot contain two dots next to each other.
  99. return false;
  100. }
  101. // Check the first part of the name
  102. $first = array_shift($parts);
  103. if (strpos($first, '*') !== false) {
  104. // Check that the wildcard is the full part
  105. if ($first !== '*') {
  106. return false;
  107. }
  108. // Check that we have at least 3 components (including first)
  109. if (count($parts) < 2) {
  110. return false;
  111. }
  112. }
  113. // Check the remaining parts
  114. foreach ($parts as $part) {
  115. if (strpos($part, '*') !== false) {
  116. return false;
  117. }
  118. }
  119. // Nothing found, verified!
  120. return true;
  121. }
  122. /**
  123. * Match a hostname against a dNSName reference
  124. *
  125. * @param string|Stringable $host Requested host
  126. * @param string|Stringable $reference dNSName to match against
  127. * @return boolean Does the domain match?
  128. * @throws \WpOrg\Requests\Exception\InvalidArgument When either of the passed arguments is not a string or a stringable object.
  129. */
  130. public static function match_domain($host, $reference) {
  131. if (InputValidator::is_string_or_stringable($host) === false) {
  132. throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host));
  133. }
  134. // Check if the reference is blocklisted first
  135. if (self::verify_reference_name($reference) !== true) {
  136. return false;
  137. }
  138. // Check for a direct match
  139. if ((string) $host === (string) $reference) {
  140. return true;
  141. }
  142. // Calculate the valid wildcard match if the host is not an IP address
  143. // Also validates that the host has 3 parts or more, as per Firefox's ruleset,
  144. // as a wildcard reference is only allowed with 3 parts or more, so the
  145. // comparison will never match if host doesn't contain 3 parts or more as well.
  146. if (ip2long($host) === false) {
  147. $parts = explode('.', $host);
  148. $parts[0] = '*';
  149. $wildcard = implode('.', $parts);
  150. if ($wildcard === (string) $reference) {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. }