How to get complete issuer name from cert.pem in php?

by arnoldo.moen , in category: PHP General , 6 months ago

How to get complete issuer name from cert.pem in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 6 months ago

@arnoldo.moen 

To get the complete issuer name from a certificate file (cert.pem) in PHP, you can use the openssl functions. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Path to cert.pem file
$certFile = '/path/to/cert.pem';

// Read the contents of the cert.pem file
$certContents = file_get_contents($certFile);

// Parse the certificate
$certResource = openssl_x509_read($certContents);

// Get the issuer name
$issuer = openssl_x509_parse($certResource)['issuer'];

// Convert the issuer name fields to a string
$issuerName = [];
foreach ($issuer as $key => $value) {
    $issuerName[] = "$key=$value";
}

// Join the issuer name fields with commas to form the complete issuer name
$issuerName = implode(', ', $issuerName);

// Output the complete issuer name
echo $issuerName;

// Free the certificate resource
openssl_x509_free($certResource);


Make sure to replace /path/to/cert.pem with the actual path to your cert.pem file.


This code uses the openssl_x509_parse function to parse the certificate and retrieve the issuer information. The issuer is then converted to a string by concatenating the key-value pairs, separated by commas. Finally, the complete issuer name is outputted.


Note: The openssl extension must be enabled in your PHP configuration in order to use these functions.