2019-05-11 20:27:21 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* @category Network
|
|
|
|
* @package Nautilus
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-05-11 20:27:21 +09:00
|
|
|
* @author Aaron Parecki <aaron@parecki.com>
|
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @see https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php
|
2019-05-11 20:27:21 +09:00
|
|
|
*/
|
2020-10-16 09:07:01 +09:00
|
|
|
class httpsignature
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* Sign a message with an Actor
|
|
|
|
*
|
2020-10-16 09:07:01 +09:00
|
|
|
* @param Profile $user Actor signing
|
|
|
|
* @param string $url Inbox url
|
|
|
|
* @param bool|string $body Data to sign (optional)
|
|
|
|
* @param array $addlHeaders Additional headers (optional)
|
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @throws Exception Attempted to sign something that belongs to an Actor we don't own
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @return array Headers to be used in curl
|
2019-12-11 07:52:28 +09:00
|
|
|
*/
|
|
|
|
public static function sign(Profile $user, string $url, $body = false, array $addlHeaders = []): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
$digest = false;
|
|
|
|
if ($body) {
|
|
|
|
$digest = self::_digest($body);
|
|
|
|
}
|
2020-10-16 09:07:01 +09:00
|
|
|
$headers = self::_headersToSign($url, $digest);
|
|
|
|
$headers = array_merge($headers, $addlHeaders);
|
|
|
|
$stringToSign = self::_headersToSigningString($headers);
|
|
|
|
$signedHeaders = implode(' ', array_map('strtolower', array_keys($headers)));
|
2019-05-11 20:27:21 +09:00
|
|
|
$actor_private_key = new Activitypub_rsa();
|
2019-12-11 07:52:28 +09:00
|
|
|
// Intentionally unhandled exception, we want this to explode if that happens as it would be a bug
|
2019-05-11 20:27:21 +09:00
|
|
|
$actor_private_key = $actor_private_key->get_private_key($user);
|
2020-10-16 09:07:01 +09:00
|
|
|
$key = openssl_pkey_get_private($actor_private_key);
|
2019-05-11 20:27:21 +09:00
|
|
|
openssl_sign($stringToSign, $signature, $key, OPENSSL_ALGO_SHA256);
|
2020-10-16 09:07:01 +09:00
|
|
|
$signature = base64_encode($signature);
|
2019-12-11 07:27:32 +09:00
|
|
|
$signatureHeader = 'keyId="' . $user->getUri() . '#public-key' . '",headers="' . $signedHeaders . '",algorithm="rsa-sha256",signature="' . $signature . '"';
|
2019-05-11 20:27:21 +09:00
|
|
|
unset($headers['(request-target)']);
|
|
|
|
$headers['Signature'] = $signatureHeader;
|
|
|
|
|
|
|
|
return self::_headersToCurlArray($headers);
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param mixed $body
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _digest($body): string
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
if (is_array($body)) {
|
|
|
|
$body = json_encode($body);
|
|
|
|
}
|
|
|
|
return base64_encode(hash('sha256', $body, true));
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param string $url
|
2020-10-16 09:07:01 +09:00
|
|
|
* @param mixed $digest
|
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @throws Exception
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
|
|
|
* @return array
|
2019-12-11 07:52:28 +09:00
|
|
|
*/
|
|
|
|
protected static function _headersToSign(string $url, $digest = false): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
$date = new DateTime('UTC');
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'(request-target)' => 'post ' . parse_url($url, PHP_URL_PATH),
|
2020-10-16 09:07:01 +09:00
|
|
|
'Date' => $date->format('D, d M Y H:i:s \G\M\T'),
|
|
|
|
'Host' => parse_url($url, PHP_URL_HOST),
|
|
|
|
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, application/json',
|
|
|
|
'User-Agent' => 'GNU social ActivityPub Plugin - ' . GNUSOCIAL_ENGINE_URL,
|
|
|
|
'Content-Type' => 'application/activity+json',
|
2019-05-11 20:27:21 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($digest) {
|
|
|
|
$headers['Digest'] = 'SHA-256=' . $digest;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param array $headers
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _headersToSigningString(array $headers): string
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
return implode("\n", array_map(function ($k, $v) {
|
|
|
|
return strtolower($k) . ': ' . $v;
|
|
|
|
}, array_keys($headers), $headers));
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param array $headers
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function _headersToCurlArray(array $headers): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
|
|
|
return array_map(function ($k, $v) {
|
2020-10-16 09:07:01 +09:00
|
|
|
return "{$k}: {$v}";
|
2019-05-11 20:27:21 +09:00
|
|
|
}, array_keys($headers), $headers);
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param string $signature
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function parseSignatureHeader(string $signature): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2020-10-16 09:07:01 +09:00
|
|
|
$parts = explode(',', $signature);
|
2019-05-11 20:27:21 +09:00
|
|
|
$signatureData = [];
|
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
if (preg_match('/(.+)="(.+)"/', $part, $match)) {
|
|
|
|
$signatureData[$match[1]] = $match[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($signatureData['keyId'])) {
|
|
|
|
return [
|
2020-10-16 09:07:01 +09:00
|
|
|
'error' => 'No keyId was found in the signature header. Found: ' . implode(', ', array_keys($signatureData)),
|
2019-05-11 20:27:21 +09:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filter_var($signatureData['keyId'], FILTER_VALIDATE_URL)) {
|
|
|
|
return [
|
2020-10-16 09:07:01 +09:00
|
|
|
'error' => 'keyId is not a URL: ' . $signatureData['keyId'],
|
2019-05-11 20:27:21 +09:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($signatureData['headers']) || !isset($signatureData['signature'])) {
|
|
|
|
return [
|
2020-10-16 09:07:01 +09:00
|
|
|
'error' => 'Signature is missing headers or signature parts',
|
2019-05-11 20:27:21 +09:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $signatureData;
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:52:28 +09:00
|
|
|
/**
|
|
|
|
* @param $publicKey
|
|
|
|
* @param $signatureData
|
|
|
|
* @param $inputHeaders
|
|
|
|
* @param $path
|
|
|
|
* @param $body
|
2020-10-16 09:07:01 +09:00
|
|
|
*
|
2019-12-11 07:52:28 +09:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function verify($publicKey, $signatureData, $inputHeaders, $path, $body): array
|
2019-05-11 20:27:21 +09:00
|
|
|
{
|
2020-06-28 08:58:16 +09:00
|
|
|
// We need this because the used Request headers fields specified by Signature are in lower case.
|
|
|
|
$headersContent = array_change_key_case($inputHeaders, CASE_LOWER);
|
2020-10-16 09:07:01 +09:00
|
|
|
$digest = 'SHA-256=' . base64_encode(hash('sha256', $body, true));
|
|
|
|
$headersToSign = [];
|
2019-05-11 20:27:21 +09:00
|
|
|
foreach (explode(' ', $signatureData['headers']) as $h) {
|
|
|
|
if ($h == '(request-target)') {
|
|
|
|
$headersToSign[$h] = 'post ' . $path;
|
|
|
|
} elseif ($h == 'digest') {
|
|
|
|
$headersToSign[$h] = $digest;
|
2020-06-28 08:58:16 +09:00
|
|
|
} elseif (isset($headersContent[$h][0])) {
|
|
|
|
$headersToSign[$h] = $headersContent[$h];
|
2019-05-11 20:27:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$signingString = self::_headersToSigningString($headersToSign);
|
|
|
|
|
|
|
|
$verified = openssl_verify($signingString, base64_decode($signatureData['signature']), $publicKey, OPENSSL_ALGO_SHA256);
|
|
|
|
|
|
|
|
return [$verified, $signingString];
|
|
|
|
}
|
|
|
|
}
|