PHP作曲家下载签名验证

时间:2016-03-11 20:27:46

标签: composer-php

我需要安装PHP编写器,但我还没准备好curl | php,我想根据签名或校验和验证下载的包。

网站上的download links是我想去的方式。开发者还在网站上发布了他们的public keys。并且$ {download} .sig提供了包签名(仅通过查找它们找到它们),但我无法弄清楚如何使用这些签名进行验证。

例如,这些是当前最新的PHAR和sig文件:

sig文件包含:

{"sha384":"FGC1jaYN4TCnaaeha3aeHx1W8gn/GyBaNk09TEOxLXjhWdwFb7psJBtZgXEsrq1Sm0J91j3l2AZDWofQ1s1FHD6A4cZY5H2KQ7KleqIFmDWDVyASHc6tjvaPtlQJ4BCVJEOPsRHX2NTH1roCs48t7S+MbVj5j5K8oVggEw9IOG4uurABUiadOLj/gQ3UpXz1+oflkr358qCkQuUW2upMuHDto8BNLSDYrCLgct1i8aCTCgKo6BYMBGSZxQdGY/dDyRX6rHbR1/CzfJmECgA9qGgeXxDRyjFg/93wsQfuFPCijd6vTpRmsFKYpwfQXMult8t+0mPh4PcvX1GzKtYcLxmsA2MmyPVfX80KtGp2EF5ExRAxOqZtd3ZtwqqOxUeNUfESKrXif1v0PxVGlER4KX5MBvCH9UvwwUPOzyplJ8N+4ybtNGfHiOD3MpPsiVBVoWkQouI5qbHRT39kAGKfMQBDWounrwMGGQV2Ca2/bFMcnInYkXFyLD12yekluoktpBcyFyZcHOJXXbbMGeXLZn3cepBwneUPklB4Q6zkouIdCkZZIzyOkLp4XgCP55idmD+DNmeoaGNlqDJmN+2wTWQv5GBj9DEEXBFHZ5f4hfn6ZEYWO7GlgOz0YeijuknvtvdCR+Iqr3Vn72UKhtoBQd2L74YwzCG/4CBYhGtknMc"}

这个签名的主体似乎是base64编码的,但是解码它的时间太长而不能成为sha384校验和。它似乎也不是GPG签名。

如何验证包裹?

1 个答案:

答案 0 :(得分:3)

可以在github上找到sign script并包含以下代码:

openssl_sign(file_get_contents($_SERVER['argv'][1]), $sha384sig, $pkeyid, OPENSSL_ALGO_SHA384)
// ...
$sha384sig = trim(base64_encode($sha384sig), '=');

因此签名确实是base64编码的sha384校验和。

请注意,用于下载composer phar的安装程序也会检查签名。在{GitHub上也可以找到It's code

$signature = $httpClient->get($url.'.sig');
if (!$signature) {
    out('Download failed: '.$errorHandler->message, 'error');
} else {
    $signature = json_decode($signature, true);
    $signature = base64_decode($signature['sha384']);
}

// ...

if (false === $disableTls) {
    $pubkeyid = openssl_pkey_get_public('file://'.$home.'/' . ($version ? 'keys.tags.pub' : 'keys.dev.pub'));
    $algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
    if (!in_array('SHA384', openssl_get_md_methods())) {
        out('SHA384 is not supported by your openssl extension, could not verify the phar file integrity', 'error');
        exit(1);
    }
    $verified = 1 === openssl_verify(file_get_contents($file), $signature, $pubkeyid, $algo);
    openssl_free_key($pubkeyid);
    if (!$verified) {
        out('Signature mismatch, could not verify the phar file integrity', 'error');
        exit(1);
    }
}