使用PHP为Apple Wallet通行证创建PKCS#7分离签名


Creating a PKCS #7 detached signature for Apple Wallet passes using PHP

这对我来说是一个全新的概念,所以我在黑暗中拍摄。

要创建签名文件,请对清单文件,使用与签名关联的私钥证明书将WWDR中间证书作为签名您可以从苹果的网站下载此证书。将签名写入通行证顶层的文件签名包裹包括使用S/MIME签名时间属性。

我的理解:

要创建签名文件,请对清单文件进行PKCS#7分离签名

我将使用标志PKCS7_DETACHEDopenssl_pkcs7_sign函数。

使用与签名证书关联的私钥。

我将使用ssl cert.pem文件的位置作为signcert参数,使用cert.key文件的位置为privkey参数。

将WWDR中间证书作为签名的一部分。

我将在extracerts参数中包含WWDR证书的路径

包括使用S/MIME签名时间属性对通行证进行签名的日期和时间。

我将包含一个数组,该数组的关键字为signing-timeheaders参数的值类似于2015-05-03 10:40:00

我的代码:

private function createSignature($dir)
{
    $cert = '/etc/ssl/cert.pem';
    $key = '/etc/ssl/private/cert.key';
    $wwdr = '/location/of/apple/wwdr/cert.cer';
    $headers = [
        'signing-time' => (new DateTime())->format('o-m-d H:i:s'),
    ];
    return openssl_pkcs7_sign("$dir/manifest.json", "$dir/signature", $cert, $key, $headers, PKCS7_DETACHED, $wwdr);
}

其他问题:

我在openssl_pkcs7_sign函数的文档示例中注意到,文件的某些位置以file://为前缀。为什么会这样?

  1. 在生成通行证类型IDhttps://developer.apple.com/account/ios/identifier/passTypeId
  2. 在为该通行证类型ID创建证书https://developer.apple.com/account/ios/certificate/create/
  3. 下载证书并将其放入密钥链中
  4. 在密钥链中查找证书并将其导出为不带密码的Certificates.p12
  5. 打开终端,运行openssl pkcs12 -in Certificates.p12 -clcerts -nokeys -out pass_cert.pem -passin pass:生成证书
  6. 在终端中,运行openssl pkcs12 -in Certificates.p12 -nocerts -out pass_key.pem -passin pass: -passout pass:YourPassword生成密钥
  7. 从下载WWDR证书https://www.apple.com/certificateauthority/把它放在你的钥匙扣里
  8. 将密钥链中的WWDR证书导出为wwdr.pem

创建分离签名的函数:

public function createSignature()
{
    $cert = "file://location/of/pass_cert.pem";
    $key = "file://location/of/pass_key.pem";
    $wwdr = "/location/of/wwdr.pem";
    openssl_pkcs7_sign("/location/of/manifest.json", "/location/of/signature",
        $cert, [$key, 'YourPassword'], [], PKCS7_BINARY | PKCS7_DETACHED, $wwdr);
    // convert pem to der
    $signature = file_get_contents("/location/of/signature");
    $begin = 'filename="smime.p7s"';
    $end = '------';
    $signature = substr($signature, strpos($signature, $begin) + strlen($begin));
    $signature = substr($signature, 0, strpos($signature, $end));
    $signature = trim($signature);
    $signature = base64_decode($signature);
    file_put_contents("/location/of/signature", $signature);
}

参考文献:

  • https://www.raywenderlich.com/20734/beginning-passbook-part-1
  • https://github.com/tschoffelen/PHP-PKPass/blob/master/PKPass.php