SSL.php 3.9 KB

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