Stars: 674
Forks: 129
Pull Requests: 114
Issues: 0
Watchers: 18
Last Updated: 2023-09-04 08:08:34
A class to validate SSL certificates
License: MIT License
Languages: PHP
The class provided by this package makes it incredibly easy to query the properties on an ssl certificate. We have three options for fetching a certficate. Here's an example:
use Spatie\SslCertificate\SslCertificate;
// fetch the certificate using an url
$certificate = SslCertificate::createForHostName('spatie.be');
// or from a certificate file
$certificate = SslCertificate::createFromFile($pathToCertificateFile);
// or from a string
$certificate = SslCertificate::createFromString($certificateData);
$certificate->getIssuer(); // returns "Let's Encrypt Authority X3"
$certificate->isValid(); // returns true if the certificate is currently valid
$certificate->validFromDate(); // returns a Carbon instance Carbon
$certificate->expirationDate(); // returns a Carbon instance Carbon
$certificate->lifespanInDays(); // return the amount of days between validFromDate and expirationDate
$certificate->expirationDate()->diffInDays(); // returns an int
$certificate->getSignatureAlgorithm(); // returns a string
$certificate->getOrganization(); // returns the organization name when available
If you want to download certificates even if they are invalid (for example, if they are expired), you can pass a $verifyCertificate
boolean to SslCertificate::createFromHostname()
as the third argument, for example:
$certificate = SslCertificate::createForHostName('expired.badssl.com', $timeoutInSeconds, false);
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/ssl-certificate
Currently, this package does not check if the certificate is signed by a trusted authority. We'll add this check soon in a next point release.
You can create an instance of Spatie\SslCertificate\SslCertificate
with this named constructor:
$certificate = SslCertificate::createForHostName('spatie.be');
You can create an instance of Spatie\SslCertificate\SslCertificate
passing the port with this named constructor:
$certificate = SslCertificate::createForHostName('spatie.be:443');
You can use this fluent style to specify a specific port to connect to.
SslCertificate::download()
->usingPort($customPort)
->forHost($hostName);
You can check the certificate on a different IP address using the same style.
SslCertificate::download()
->fromIpAddress($ipAddress)
->forHost($hostName);
This also works with IPv6 addresses
SslCertificate::download()
->fromIpAddress('2a00:1450:4001:80e::200e')
->forHost('google.com');
You can specify socket context options.
SslCertificate::download()
->withSocketContextOptions([
'option' => 'value',
])
->forHost($hostName);
If the given ipAddress
is invalid Spatie\SslCertificate\Exceptions\InvalidIpAddress
will be thrown.
If the given hostName
is invalid Spatie\SslCertificate\Exceptions\InvalidUrl
will be thrown.
If the given hostName
is valid but there was a problem downloading the certifcate Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate
will be thrown.
$certificate->getIssuer(); // returns "Let's Encrypt Authority X3"
Returns the primary domain name for the certificate
$certificate->getDomain(); // returns "spatie.be"
Returns the algorithm used for signing the certificate
$certificate->getSignatureAlgorithm(); // returns "RSA-SHA256"
Returns the organization belonging to the certificate
$certificate->getOrganization(); // returns "Spatie BVBA"
A certificate can cover multiple (sub)domains. Here's how to get them.
$certificate->getAdditionalDomains(); // returns ["spatie.be", "www.spatie.be]
A domain name return with this method can start with *
meaning it is valid for all subdomains of that domain.
$certificate->getFingerprint(); // returns a fingerprint for the certificate
$certificate->getFingerprintSha256(); // returns a SHA256 fingerprint for the certificate
$certificate->validFromDate(); // returns an instance of Carbon
$certificate->expirationDate(); // returns an instance of Carbon
Returns true if the current Date and time is between validFromDate
and expirationDate
.
$certificate->isValid(); // returns a boolean
You also use this method to determine if a given domain is covered by the certificate. Of course it'll keep checking if the current Date and time is between validFromDate
and expirationDate
.
$certificate->isValid('spatie.be'); // returns true;
$certificate->isValid('laravel.com'); // returns false;
Returns true if the certificate is valid and if the expirationDate
is after the given date.
$certificate->isValidUntil(Carbon::now()->addDays(7)); // returns a boolean
$certificate->isExpired(); // returns a boolean if expired
You can convert a certificate to an array using the toArray
method.
$certificateProperties = $certificate->toArray();
The properties can be used to create a new instance of the certificate.
\Spatie\SslCertificate\SslCertificate::createFromArray($certificateProperties);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The helper functions and tests were copied from the Laravel Framework.
The MIT License (MIT). Please see License File for more information.