spatie/ssl-certificate

一个用于轻松查询SSL证书属性的类

资助包维护!
spatie
其他

安装次数: 3,103,639

依赖关系: 31

建议者: 5

安全: 0

星标: 707

关注者: 19

分支: 133

2.6.8 2024-09-20 13:11 UTC

README

Latest Version on Packagist MIT Licensed run-tests Quality Score Total Downloads

此包提供的类使得查询SSL证书上的属性变得非常简单。我们提供了三种获取证书的方式。以下是一个示例

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->isValid(); // returns true if the certificate is currently valid

$certificate->expirationDate(); // returns a Carbon instance Carbon
$certificate->validFromDate(); // returns a Carbon instance Carbon

$certificate->daysUntilExpirationDate(); // returns the amount of days between today and expirationDate
$certificate->lifespanInDays(); // return the amount of days between validFromDate and expirationDate

$certificate->getIssuer(); // returns "Let's Encrypt Authority X3"
$certificate->getOrganization(); // returns the organization name when available
$certificate->getPublicKeyAlgorithm(); // returns the public key algorithm
$certificate->getPublicKeySize(); // returns the public key algorithm
$certificate->getSignatureAlgorithm(); // returns the signature algorithm

下载无效证书

如果您想要下载无效的证书(例如,如果它们已过期),您可以将一个布尔值 $verifyCertificate 传递给 SslCertificate::createFromHostname() 作为第三个参数,例如

$certificate = SslCertificate::createForHostName('expired.badssl.com', $timeoutInSeconds, false);

关于我们

Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

支持我们

我们投入了大量资源来创建 最佳的开源软件包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从您的家乡寄给我们明信片,并说明您正在使用我们的哪些包。您可以在我们的 联系页面 上找到我们的地址。我们将发布所有收到的明信片在我们的 虚拟明信片墙上

安装

您可以通过 composer 安装此包

composer require spatie/ssl-certificate

重要提示

目前,此包 不会检查 证书是否由受信任的机构签署。我们将在下一个版本中添加此检查。

用法

您可以使用此命名构造函数创建 Spatie\SslCertificate\SslCertificate 的实例

$certificate = SslCertificate::createForHostName('spatie.be');

您可以通过此命名构造函数传递端口来创建 Spatie\SslCertificate\SslCertificate 的实例

$certificate = SslCertificate::createForHostName('spatie.be:443');

您可以使用这种流畅风格指定要连接到的特定端口

SslCertificate::download()
   ->usingPort($customPort)
   ->forHost($hostName);

您可以使用相同的方式使用不同的IP地址检查证书

SslCertificate::download()
   ->fromIpAddress($ipAddress)
   ->forHost($hostName);

这也适用于IPv6地址

SslCertificate::download()
    ->fromIpAddress('2a00:1450:4001:80e::200e')
    ->forHost('google.com');

您可以指定 套接字上下文选项

SslCertificate::download()
   ->withSocketContextOptions([
      'option' => 'value',
   ])
   ->forHost($hostName);

如果给定的 ipAddress 无效,将抛出 Spatie\SslCertificate\Exceptions\InvalidIpAddress

如果给定的 hostName 无效,将抛出 Spatie\SslCertificate\Exceptions\InvalidUrl

如果给定的 hostName 有效,但在下载证书时出现问题时,将抛出 Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate

获取颁发者名称

$certificate->getIssuer(); // returns "Let's Encrypt Authority X3"

获取域名

返回证书的主域名

$certificate->getDomain(); // returns "spatie.be"

获取证书的签名算法

返回用于签名证书的算法

$certificate->getSignatureAlgorithm(); // returns "RSA-SHA256"

获取证书的组织

返回属于证书的组织

$certificate->getOrganization(); // returns "Spatie BVBA"

获取附加域名

证书可以覆盖多个(子)域名。以下是如何获取它们的方法。

$certificate->getAdditionalDomains(); // returns ["spatie.be", "www.spatie.be]

此方法返回的域名名可以以 * 开头,表示它适用于该域的所有子域名。

获取指纹

$certificate->getFingerprint(); // returns a fingerprint for the certificate

获取SHA256指纹

$certificate->getFingerprintSha256(); // returns a SHA256 fingerprint for the certificate

获取证书开始生效的日期

$certificate->validFromDate(); // returns an instance of Carbon

获取证书到期日期

$certificate->expirationDate(); // returns an instance of Carbon

确定证书是否仍然有效

如果当前日期和时间在 validFromDateexpirationDate 之间,则返回 true。

$certificate->isValid(); // returns a boolean

您也可以使用这种方法来判断给定的域名是否被证书覆盖。当然,它还会继续检查当前日期和时间是否在validFromDateexpirationDate之间。

$certificate->isValid('spatie.be'); // returns true;
$certificate->isValid('laravel.com'); // returns false;

确定证书在给定日期之前是否仍然有效

如果证书有效并且expirationDate在给定日期之后,则返回true。

$certificate->isValidUntil(Carbon::now()->addDays(7)); // returns a boolean

确定证书是否已过期

$certificate->isExpired(); // returns a boolean if expired

将证书转换为数组

您可以使用toArray方法将证书转换为数组。

$certificateProperties = $certificate->toArray();

可以使用这些属性来创建证书的新实例。

\Spatie\SslCertificate\SslCertificate::createFromArray($certificateProperties);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

Postcardware

您可以自由使用这个包,但如果它进入您的生产环境,我们非常感激您从家乡给我们寄来一张明信片,说明您正在使用我们的哪个包。

我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。

我们将所有收到的明信片发布在我们的公司网站上

致谢

辅助函数和测试是从Laravel 框架中复制的。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件