Changeset 52244 for trunk/src/wp-includes/Requests/Ssl.php
- Timestamp:
- 11/25/2021 01:10:30 AM (5 years ago)
- File:
-
- 1 moved
-
trunk/src/wp-includes/Requests/Ssl.php (moved) (moved from trunk/src/wp-includes/Requests/SSL.php ) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Ssl.php
r52243 r52244 3 3 * SSL utilities for Requests 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\Utilities 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception\InvalidArgument; 11 use WpOrg\Requests\Utility\InputValidator; 8 12 9 13 /** … … 12 16 * Collection of utilities for working with and verifying SSL certificates. 13 17 * 14 * @package Requests 15 * @subpackage Utilities 18 * @package Requests\Utilities 16 19 */ 17 class Requests_SSL{20 final class Ssl { 18 21 /** 19 22 * Verify the certificate against common name and subject alternative names … … 22 25 * names, leading things like 'https://www.github.com/' to be invalid. 23 26 * 24 * @ seehttps://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.127 * @link https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 25 28 * 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 29 * @param string|Stringable $host Host name to verify against 28 30 * @param array $cert Certificate data from openssl_x509_parse() 29 31 * @return bool 32 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $host argument is not a string or a stringable object. 33 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cert argument is not an array or array accessible. 30 34 */ 31 35 public static function verify_certificate($host, $cert) { 36 if (InputValidator::is_string_or_stringable($host) === false) { 37 throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host)); 38 } 39 40 if (InputValidator::has_array_access($cert) === false) { 41 throw InvalidArgument::create(2, '$cert', 'array|ArrayAccess', gettype($cert)); 42 } 43 32 44 $has_dns_alt = false; 33 45 34 46 // Check the subjectAltName 35 if (!empty($cert['extensions'] ) && !empty($cert['extensions']['subjectAltName'])) {47 if (!empty($cert['extensions']['subjectAltName'])) { 36 48 $altnames = explode(',', $cert['extensions']['subjectAltName']); 37 49 foreach ($altnames as $altname) { … … 51 63 } 52 64 } 65 66 if ($has_dns_alt === true) { 67 return false; 68 } 53 69 } 54 70 55 71 // Fall back to checking the common name if we didn't get any dNSName 56 72 // alt names, as per RFC2818 57 if (! $has_dns_alt && !empty($cert['subject']['CN'])) {73 if (!empty($cert['subject']['CN'])) { 58 74 // Check for a match 59 if (self::match_domain($host, $cert['subject']['CN']) === true) { 60 return true; 61 } 75 return (self::match_domain($host, $cert['subject']['CN']) === true); 62 76 } 63 77 … … 78 92 * the third rule. 79 93 * 80 * @param string $reference Reference dNSName94 * @param string|Stringable $reference Reference dNSName 81 95 * @return boolean Is the name valid? 96 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object. 82 97 */ 83 98 public static function verify_reference_name($reference) { 99 if (InputValidator::is_string_or_stringable($reference) === false) { 100 throw InvalidArgument::create(1, '$reference', 'string|Stringable', gettype($reference)); 101 } 102 103 if ($reference === '') { 104 return false; 105 } 106 107 if (preg_match('`\s`', $reference) > 0) { 108 // Whitespace detected. This can never be a dNSName. 109 return false; 110 } 111 84 112 $parts = explode('.', $reference); 113 if ($parts !== array_filter($parts)) { 114 // DNSName cannot contain two dots next to each other. 115 return false; 116 } 85 117 86 118 // Check the first part of the name … … 113 145 * Match a hostname against a dNSName reference 114 146 * 115 * @param string $host Requested host116 * @param string $reference dNSName to match against147 * @param string|Stringable $host Requested host 148 * @param string|Stringable $reference dNSName to match against 117 149 * @return boolean Does the domain match? 150 * @throws \WpOrg\Requests\Exception\InvalidArgument When either of the passed arguments is not a string or a stringable object. 118 151 */ 119 152 public static function match_domain($host, $reference) { 153 if (InputValidator::is_string_or_stringable($host) === false) { 154 throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host)); 155 } 156 120 157 // Check if the reference is blocklisted first 121 158 if (self::verify_reference_name($reference) !== true) { … … 124 161 125 162 // Check for a direct match 126 if ( $host ===$reference) {163 if ((string) $host === (string) $reference) { 127 164 return true; 128 165 } 129 166 130 167 // Calculate the valid wildcard match if the host is not an IP address 131 // Also validates that the host has 3 parts or more, as per Firefox's 132 // ruleset. 168 // Also validates that the host has 3 parts or more, as per Firefox's ruleset, 169 // as a wildcard reference is only allowed with 3 parts or more, so the 170 // comparison will never match if host doesn't contain 3 parts or more as well. 133 171 if (ip2long($host) === false) { 134 172 $parts = explode('.', $host); 135 173 $parts[0] = '*'; 136 174 $wildcard = implode('.', $parts); 137 if ($wildcard === $reference) {175 if ($wildcard === (string) $reference) { 138 176 return true; 139 177 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)