简体   繁体   English

如何使用php从应用程序证明对象中提取证书?

[英]How to extract certificates from app attestation object using php?

I tried to set up app attestation between my app and php but I rarely find any other source of explaination than Apple's own documentation, which let me stuck quite at an early state.我试图在我的应用程序和 php 之间设置应用程序证明,但除了 Apple 自己的文档之外,我几乎找不到任何其他解释来源,这让我陷入了早期状态。 So far I got the following steps:到目前为止,我得到了以下步骤:

On the client side, following https://developer.apple.com/documentation/devicecheck/establishing_your_app_s_integrity , I creted my attestation as a base64 encoded string:在客户端,按照https://developer.apple.com/documentation/devicecheck/establishing_your_app_s_integrity ,我将我的证明作为 base64 编码的字符串:

attestation.base64EncodedString()

I then send that string to the server, following https://developer.apple.com/documentation/devicecheck/validating_apps_that_connect_to_your_server from now on.然后我将该字符串发送到服务器,从现在开始遵循https://developer.apple.com/documentation/devicecheck/validating_apps_that_connect_to_your_server

The documentation says, that the attestation is in the CBOR format.文档说,证明采用 CBOR 格式。 I therefor first decode the base64 encoded string and parse it using ( https://github.com/Spomky-Labs/cbor-php ).因此,我首先解码 base64 编码的字符串并使用( https://github.com/Spomky-Labs/cbor-php )解析它。

<?php
use CBOR\Decoder;
use CBOR\OtherObject;
use CBOR\Tag;
use CBOR\StringStream;

$otherObjectManager = new OtherObject\OtherObjectManager();
$tagManager = new Tag\TagObjectManager();

$decoder = new Decoder($tagManager, $otherObjectManager);
$data = base64_decode(/* .. base64 encoded attestation string as send from the client (see swift snippet above) */);

$stream = new StringStream($data);
$object = $decoder->decode($stream);

$norm = $object->getNormalizedData();
$fmt = $norm['fmt'];
$x5c = $norm['attStmt']['x5c'];

From the documentation, the normalized object should have the following format:从文档中,规范化对象应具有以下格式:

{
   fmt: 'apple-appattest',
   attStmt: {
     x5c: [
       <Buffer 30 82 02 cc ... >,
       <Buffer 30 82 02 36 ... >
     ],
     receipt: <Buffer 30 80 06 09 ... >
   },
   authData: <Buffer 21 c9 9e 00 ... >
 }

which it does:它的作用是:

$fmt == "apple-appattest" // true

Then the next according to the documentation is described as:然后根据文档的下一个描述为:

Verify that the x5c array contains the intermediate and leaf certificates for App Attest, starting from the credential certificate in the first data buffer in the array (credcert).验证 x5c 阵列是否包含 App Attest 的中间和叶证书,从阵列中第一个数据缓冲区 (credcert) 中的凭证证书开​​始。 Verify the validity of the certificates using Apple's App Attest root certificate .使用Apple 的 App Attest 根证书验证证书的有效性。

However, I don't know how to proceed further on this.但是,我不知道如何进一步处理此问题。 The content of eg $norm['attStmt']['x5c'][0] is a mix of readable chars and glyphs.例如$norm['attStmt']['x5c'][0]是可读字符和字形的混合。 To give you an idea, this is a random substring from the content of $norm['attStmt']['x5c'][0] : "Certification Authority10U Apple Inc.10 UUS0Y0* H =* H =B c } ".给你一个想法,这是$norm['attStmt']['x5c'][0]内容中的一个随机子字符串:“Certification Authority10U Apple Inc.10 UUS0Y0* H =* H =B c } ”。 That's why I'm not really sure wheather I have to perform any further encodeing/decoding steps.这就是为什么我不确定是否必须执行任何进一步的编码/解码步骤。

I tried parsing the certificate but without any luck (both var_dump return false):我尝试解析证书但没有任何运气(var_dump 都返回 false):

 $cert = openssl_x509_read($x5c[0]);
 var_dump($cert); // false - indicating that reading the cert failed
 
 $parsedCert = openssl_x509_parse($cert, false);
 var_dump($parsedCert); // false - of course, since the prior step did not succeed

Any ideas, guidance or alternative ressources are highly appreciated.任何想法、指导或替代资源都受到高度赞赏。 Thank you!谢谢!

After a while I came up with the following solution.过了一会儿,我想出了以下解决方案。 The $x5c field contains a list of certificates, all in binary form. $x5c 字段包含证书列表,全部为二进制形式。 I wrote the folowing converter to create a ready-to-use certificate in PEM format, which does the following:我编写了以下转换器来创建 PEM 格式的即用型证书,它执行以下操作:

  1. base64 encode the binary data base64 编码二进制数据
  2. break lines after 64 bytes在 64 个字节后换行
  3. add BEGIN and END markers (also note the trailing line-break on the end certificate line)添加 BEGIN 和 END 标记(还要注意结尾证书行上的尾随换行符)

function makeCert($bindata) {
     $beginpem = "-----BEGIN CERTIFICATE-----\n";
    $endpem = "-----END CERTIFICATE-----\n";

    $pem = $beginpem;
    $cbenc = base64_encode($bindata);
    for($i = 0; $i < strlen($cbenc); $i++) {
        $pem .= $cbenc[$i];
        if (($i + 1) % 64 == 0)
            $pem .= "\n";
    }
    $pem .= "\n".$endpem;

    return $pem;
}

the following then works:下面的工作:

openssl_x509_read(makeCert($x5c[0]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM