openssl_x509_verify

(PHP 7 >= 7.4.0, PHP 8)

openssl_x509_verifyVerifies digital signature of x509 certificate against a public key

Description

openssl_x509_verify(OpenSSLCertificate|string $certificate, OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $public_key): int

openssl_x509_verify() verifies that the certificate certificate was signed by the private key corresponding to public key public_key.

Parameters

x509

See Key/Certificate parameters for a list of valid values.

public_key

OpenSSLAsymmetricKey - a key, returned by openssl_get_publickey()

string - a PEM formatted key, example, "-----BEGIN PUBLIC KEY----- MIIBCgK..."

Return Values

Returns 1 if the signature is correct, 0 if it is incorrect, and -1 on error.

Changelog

Version Description
8.0.0 certificate accepts an OpenSSLCertificate instance now; previously, a resource of type OpenSSL X.509 was accepted.
8.0.0 public_key accepts an OpenSSLAsymmetricKey or OpenSSLCertificate instance now; previously, a resource of type OpenSSL key or OpenSSL X.509 was accepted.

Examples

Example #1 openssl_x509_verify() example

<?php
$hostname 
"news.php.net";
$ssloptions = array(
    
"capture_peer_cert" => true
    
"capture_peer_cert_chain" => true
    
"allow_self_signed"=> false
    
"CN_match" => $hostname,
    
"verify_peer" => true,
    
"SNI_enabled" => true,
    
"SNI_server_name" => $hostname,
);
 
$ctx stream_context_create( array("ssl" => $ssloptions) );
$result stream_socket_client("ssl://$hostname:443"$errno$errstr30STREAM_CLIENT_CONNECT$ctx);
$cont stream_context_get_params($result);
$x509 $cont["options"]["ssl"]["peer_certificate"];
$certparsed openssl_x509_parse($x509);

foreach(
$cont["options"]["ssl"]["peer_certificate_chain"] as $chaincert)
{
    
$chainparsed openssl_x509_parse($chaincert);
    
$chain_public_key openssl_get_publickey($chaincert);
    
$r openssl_x509_verify($x509$chain_public_key);   
    if (
$r==1)
    {
        echo 
$certparsed['subject']['CN'];
        echo 
" was digitally signed by ";
        echo 
$chainparsed['subject']['CN']."\n";
    }
}
?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Category: Software & Web-Development

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial