setasign/cloud-kms-csr

用于创建和更新由云密钥管理系统(AWS KMS 和 Google Cloud KMS)提供密钥的证书签名请求 (CSRs) 和自签名证书的工具

v1.0.0 2021-01-25 14:02 UTC

This package is auto-updated.

Last update: 2024-09-06 14:21:04 UTC


README

本项目提供了一些 PHP 类,用于使用存储在 Amazon KMSGoogle Cloud KMS 中的密钥来创建证书签名请求 (CSRs) 和自签名证书(用于测试目的)。

它基于 SetaPDF-Signer 组件的功能。该 SetaPDF-Signer 组件是纯 PHP 的 PDF 文档数字签名解决方案。

AWS KMS 和 Google Cloud KMS 都允许您将密钥存储在硬件安全模块 (HSM) 上。通过这种方式,您可以请求证书授权机构颁发的证书,这些证书通过 Adobe 批准信任列表 (AATL) 进行验证。

生成的证书然后可以与 SetaPDF-Signer 组件的模块一起使用

安装

将以下内容添加到您的 composer.json 中

{
    "require": {
        "setasign/cloud-kms-csr": "^1.0"
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://www.setasign.com/downloads/"
        }
    ]
}

然后执行 composer update。您需要定义 repository 以评估对 SetaPDF-Signer 组件的依赖项(有关更多详细信息,请参阅 此处)。

Setasign 存储库需要身份验证数据:您可以使用您在 setasign.com 的账户凭据进行身份验证,您的许可证已分配给该账户。在 composer 运行期间将要求您提供这些凭据。有关使用 composer 进行身份验证的更多选项,请参阅 此处

根据您要使用的 KMS 服务,请确保已设置它们的身份验证

为了演示目的,我们一直在使用环境变量中的身份验证数据。

工作原理

我们实现了两个类,分别代表 CSR 和 X.509 证书实例。它们需要通过现有的 CSR 或证书进行初始化。对于创建新的 CSRs 或证书,两个类中都有一个静态的 create() 方法,它使用标准的 OpenSSL 函数创建 CSR 和证书。

然后有一个 update() 方法,它接受 AwsKMS\UpdaterGoogleCloudKMS\Updater 实例作为其参数。

然后使用存储在 KMS 中的密钥内部更新所有密钥信息、算法和签名。

我们使用官方客户端库与 KMS 服务进行通信

创建自签名证书

在您开始从证书授权机构请求真实证书或您只想测试 KMS 服务之前,您可以通过以下方式创建自签名证书

Google Cloud KMS

在Google Cloud KMS中,所有像算法、哈希和填充这样的设置都直接在密钥中完成。因此,创建自签名证书非常简单。

<?php

use setasign\CloudKmsCsr\Certificate;
use setasign\CloudKmsCsr\GoogleCloudKMS;

require_once 'vendor/autoload.php';

$projectId = '<YOUR-PROJECT-ID>';
$locationId = '<YOUR-LOCATION-ID>';
$keyRingId = '<YOUR-KEY-RING-ID>';
$keyId = '<YOUR-KEY-ID>';
$versionId = '<YOUR-KEY-VERSION-ID>';

// create an updater instance
$updater = new GoogleCloudKMS\Updater($projectId, $locationId, $keyRingId, $keyId, $versionId);

// create a new Certificate
$certificate = Certificate::create([
    'commonName' => 'Test and Development',
    'organizationName' => 'Setasign GmbH & Co. KG'
]);
// or
//$certificate = new Certificate(file_get_contents('existing-x509-certificate.pem'));

// update it by the key in the KMS
$certificate->update($updater);

// verify the certifcate
echo 'Verified: ' . ($certificate->verify() ? 'YES' : 'NO');
echo "\n\n";

// output PEM encoded certifcate
echo $certificate->get();

AWS KMS

AWS KMS几乎与此相同。您只需要自行定义签名算法。请参阅这里这里查看所有可用的算法。请注意,这些算法需要被使用的密钥所支持。

<?php

use Aws\Kms\KmsClient;
use setasign\CloudKmsCsr\Certificate;
use setasign\CloudKmsCsr\AwsKMS;

require_once 'vendor/autoload.php';

$region = '<REGION>';
$version = '<VERSION>';
$keyId = '<KEY-ID>';
$signatureAlgorithm = 'RSASSA_PKCS1_V1_5_SHA_512';

$kmsClient = new KmsClient([
    'region' => $region,
    'version' => $version
]);

$updater = new AwsKms\Updater($keyId, $kmsClient);
$updater->setSignatureAlgorithm($signatureAlgorithm);

$certificate = Certificate::create([
    'commonName' => 'Test and Development',
    'organizationName' => 'Setasign GmbH & Co. KG'
]);
// or
//$certificate = new Certificate(file_get_contents('existing-x509-certificate.pem'));

// update it by the key in the KMS
$certificate->update($updater);

// verify the certifcate
echo 'Verified: ' . ($certificate->verify() ? 'YES' : 'NO');
echo "\n\n";

// output PEM encoded certifcate
echo $certificate->get();

创建CSR

与上面的例子非常相似,只需用Csr代替Certificate

Google Cloud KMS

<?php

use setasign\CloudKmsCsr\Csr;
use setasign\CloudKmsCsr\GoogleCloudKMS;

require_once 'vendor/autoload.php';

$projectId = '<YOUR-PROJECT-ID>';
$locationId = '<YOUR-LOCATION-ID>';
$keyRingId = '<YOUR-KEY-RING-ID>';
$keyId = '<YOUR-KEY-ID>';
$versionId = '<YOUR-KEY-VERSION-ID>';

// create an updater instance
$updater = new GoogleCloudKMS\Updater($projectId, $locationId, $keyRingId, $keyId, $versionId);

// create a new CSR
$csr = Csr::create([
    'countryName' => 'DE',
    'stateOrProvinceName' => 'Niedersachen',
    'localityName' => 'Helmstedt',
    'organizationName' => 'Setasign GmbH & Co. KG',
    'organizationalUnitName' => 'Testing and Development',
    'commonName' => 'SetaPDF-Signer',
    'emailAddress' => 'setapdf-demos@setasign.com'
]);
// or
//$csr = new Csr(file_get_contents('existing-csr.pem'));

// update it by the key in the KMS
$csr->update($updater);

// verify the CSR
echo 'Verified: ' . ($csr->verify() ? 'YES' : 'NO');
echo "\n\n";

// output PEM encoded CSR
echo $csr->get();

AWS KMS

<?php

use Aws\Kms\KmsClient;
use setasign\CloudKmsCsr\Csr;
use setasign\CloudKmsCsr\AwsKMS;

require_once 'vendor/autoload.php';

$region = '<REGION>';
$version = '<VERSION>';
$keyId = '<KEY-ID>';
$signatureAlgorithm = 'RSASSA_PKCS1_V1_5_SHA_512';

$kmsClient = new KmsClient([
    'region' => $region,
    'version' => $version
]);

$updater = new AwsKms\Updater($keyId, $kmsClient);
$updater->setSignatureAlgorithm($signatureAlgorithm);

$csr = Csr::create([
    'countryName' => 'DE',
    'stateOrProvinceName' => 'Niedersachen',
    'localityName' => 'Helmstedt',
    'organizationName' => 'Setasign GmbH & Co. KG',
    'organizationalUnitName' => 'Testing and Development',
    'commonName' => 'SetaPDF-Signer',
    'emailAddress' => 'setapdf-demos@setasign.com'
]);
// update it by the key in the KMS
$csr->update($updater);

// verify the CSR
echo 'Verified: ' . ($csr->verify() ? 'YES' : 'NO');
echo "\n\n";

// output PEM encoded CSR
echo $csr->get();