wppaste
WordPress

WP_Http_Streams::verify_ssl_certificate( resource $stream, string $host ): bool

Since
3.7.0
Source
wp-includes/class-wp-http-streams.php:433
Verifies the received SSL certificate against its Common Names and subjectAltName fields.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–60

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 98.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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 inputpreg_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.

WhenInstructionsCalls it makes
empty($value)10stream_context_get_options()
!empty($value)17stream_context_get_options()openssl_x509_parse()
always36–39stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()
always43–46stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower()
!empty($value)43–47stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower()
!$certificate_hostnames && $host_type !== "ip"44–45stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()substr_count()
!$certificate_hostnames && $host_type !== "ip"51–52stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()substr_count()strtolower()
!$certificate_hostnames && $host_type !== "ip"51–52stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower()substr_count()
!empty($value) && !$certificate_hostnames && $host_type !== "ip"51–53stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower()substr_count()
!$certificate_hostnames && $host_type !== "ip"58–59stream_context_get_options()openssl_x509_parse()::WP_Http()strtolower()strtolower()substr_count()strtolower()
!empty($value) && !$certificate_hostnames && $host_type !== "ip"58–60stream_context_get_options()openssl_x509_parse()::WP_Http()preg_split()strtolower()substr_count()strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.411210–7011More instructions than PHP 8.4
8.111210–7011More instructions than PHP 8.4
8.211210–7011More instructions than PHP 8.4
8.311210–7011More instructions than PHP 8.4
8.49810–6011Compiles 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

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.