3 * SSL utilities for Requests
6 * @subpackage Utilities
10 * SSL utilities for Requests
12 * Collection of utilities for working with and verifying SSL certificates.
15 * @subpackage Utilities
19 * Verify the certificate against common name and subject alternative names
21 * Unfortunately, PHP doesn't check the certificate against the alternative
22 * names, leading things like 'https://www.github.com/' to be invalid.
25 * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1
27 * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`)
28 * @param string $host Host name to verify against
29 * @param array $cert Certificate data from openssl_x509_parse()
32 public static function verify_certificate($host, $cert) {
33 // Calculate the valid wildcard match if the host is not an IP address
34 $parts = explode('.', $host);
35 if (ip2long($host) === false) {
38 $wildcard = implode('.', $parts);
42 // Check the subjectAltName
43 if (!empty($cert['extensions']) && !empty($cert['extensions']['subjectAltName'])) {
44 $altnames = explode(',', $cert['extensions']['subjectAltName']);
45 foreach ($altnames as $altname) {
46 $altname = trim($altname);
47 if (strpos($altname, 'DNS:') !== 0) {
53 // Strip the 'DNS:' prefix and trim whitespace
54 $altname = trim(substr($altname, 4));
57 if (self::match_domain($host, $altname) === true) {
63 // Fall back to checking the common name if we didn't get any dNSName
64 // alt names, as per RFC2818
65 if (!$has_dns_alt && !empty($cert['subject']['CN'])) {
67 if (self::match_domain($host, $cert['subject']['CN']) === true) {
76 * Verify that a reference name is valid
78 * Verifies a dNSName for HTTPS usage, (almost) as per Firefox's rules:
79 * - Wildcards can only occur in a name with more than 3 components
80 * - Wildcards can only occur as the last character in the first
82 * - Wildcards may be preceded by additional characters
84 * We modify these rules to be a bit stricter and only allow the wildcard
85 * character to be the full first component; that is, with the exclusion of
88 * @param string $reference Reference dNSName
89 * @return boolean Is the name valid?
91 public static function verify_reference_name($reference) {
92 $parts = explode('.', $reference);
94 // Check the first part of the name
95 $first = array_shift($parts);
97 if (strpos($first, '*') !== false) {
98 // Check that the wildcard is the full part
103 // Check that we have at least 3 components (including first)
104 if (count($parts) < 2) {
109 // Check the remaining parts
110 foreach ($parts as $part) {
111 if (strpos($part, '*') !== false) {
116 // Nothing found, verified!
121 * Match a hostname against a dNSName reference
123 * @param string $host Requested host
124 * @param string $reference dNSName to match against
125 * @return boolean Does the domain match?
127 public static function match_domain($host, $reference) {
128 // Check if the reference is blacklisted first
129 if (self::verify_reference_name($reference) !== true) {
133 // Check for a direct match
134 if ($host === $reference) {
138 // Calculate the valid wildcard match if the host is not an IP address
139 // Also validates that the host has 3 parts or more, as per Firefox's
141 if (ip2long($host) === false) {
142 $parts = explode('.', $host);
144 $wildcard = implode('.', $parts);
145 if ($wildcard === $reference) {