WP_Http_Streams::verify_ssl_certificate( resource $stream, string $host ): bool
- Since
- 3.7.0
- Source
wp-includes/class-wp-http-streams.php:433
Description
PHP's SSL verifications only verify that it's a valid Certificate, it doesn't verify if the certificate is valid for the hostname which was requested.
This function verifies the requested hostname against certificate's subjectAltName field, if that is empty, or contains no DNS entries, a fallback to the Common Name field is used.
IP Address support is included if the request is being made to an IP address.
Parameters
$streamresource- The PHP Stream which the SSL request is being made over
$hoststring- The hostname being requested
Return
bool- If the certificate presented in $stream is valid for $host
Cost profile
Measured from the compiled opcodes, not from a stopwatch. Every number here is identical on any machine that runs the same PHP version, and every function in core is ranked by these figures.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 10–60
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 98.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- regexregular expression over the whole input
preg_split()called directly
What one call costs · 11 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Http_Streams::verify_ssl_certificate() can have, taken from its control-flow graph on PHP 8.4.
| When | Instructions | Calls it makes |
|---|---|---|
empty($value) | 10 | stream_context_get_options() |
!empty($value) | 17 | stream_context_get_options()openssl_x509_parse() |
| always | 36–39 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower() |
| always | 43–46 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower() |
!empty($value) | 43–47 | stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower() |
!$certificate_hostnames && $host_type !== "ip" | 44–45 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()substr_count() |
!$certificate_hostnames && $host_type !== "ip" | 51–52 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()substr_count()strtolower() |
!$certificate_hostnames && $host_type !== "ip" | 51–52 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower()substr_count() |
!empty($value) && !$certificate_hostnames && $host_type !== "ip" | 51–53 | stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower()substr_count() |
!$certificate_hostnames && $host_type !== "ip" | 58–59 | stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower()substr_count()strtolower() |
!empty($value) && !$certificate_hostnames && $host_type !== "ip" | 58–60 | stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower()substr_count()strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
7.4 | 112 | 10–70 | 11 | More instructions than PHP 8.4 |
8.1 | 112 | 10–70 | 11 | More instructions than PHP 8.4 |
8.2 | 112 | 10–70 | 11 | More instructions than PHP 8.4 |
8.3 | 112 | 10–70 | 11 | More instructions than PHP 8.4 |
8.4 | 98 | 10–60 | 11 | Compiles to the same instructions |
Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.
Uses · 1
- WP_Http::is_ip_address()Determines if a specified string represents an IP address or not.
Used by · 1
- WP_Http_Streams::request()Send a HTTP request to a URI using PHP Streams.
Source
public static function verify_ssl_certificate( $stream, $host ) { $context_options = stream_context_get_options( $stream ); if ( empty( $context_options['ssl']['peer_certificate'] ) ) { return false; } $cert = openssl_x509_parse( $context_options['ssl']['peer_certificate'] ); if ( ! $cert ) { return false; } /* * If the request is being made to an IP address, we'll validate against IP fields * in the cert (if they exist) */ $host_type = ( WP_Http::is_ip_address( $host ) ? 'ip' : 'dns' ); $certificate_hostnames = array(); if ( ! empty( $cert['extensions']['subjectAltName'] ) ) { $match_against = preg_split( '/,\s*/', $cert['extensions']['subjectAltName'] ); foreach ( $match_against as $match ) { list( $match_type, $match_host ) = explode( ':', $match ); if ( strtolower( trim( $match_type ) ) === $host_type ) { // IP: or DNS: $certificate_hostnames[] = strtolower( trim( $match_host ) ); } } } elseif ( ! empty( $cert['subject']['CN'] ) ) { // Only use the CN when the certificate includes no subjectAltName extension. $certificate_hostnames[] = strtolower( $cert['subject']['CN'] ); } // Exact hostname/IP matches. if ( in_array( strtolower( $host ), $certificate_hostnames, true ) ) { return true; } // IP's can't be wildcards, Stop processing. if ( 'ip' === $host_type ) { return false; } // Test to see if the domain is at least 2 deep for wildcard support. if ( substr_count( $host, '.' ) < 2 ) { return false; } // Wildcard subdomains certs (*.example.com) are valid for a.example.com but not a.b.example.com. $wildcard_host = preg_replace( '/^[^.]+\./', '*.', $host ); return in_array( strtolower( $wildcard_host ), $certificate_hostnames, true ); }History
Introduced in 3.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/class-wp-http-streams.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.