httpsignature.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * @category Network
  16. * @package Nautilus
  17. * @author Aaron Parecki <aaron@parecki.com>
  18. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  19. * @link https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php
  20. */
  21. class HttpSignature
  22. {
  23. public static function sign($user, $url, $body = false, $addlHeaders = [])
  24. {
  25. $digest = false;
  26. if ($body) {
  27. $digest = self::_digest($body);
  28. }
  29. $headers = self::_headersToSign($url, $digest);
  30. $headers = array_merge($headers, $addlHeaders);
  31. $stringToSign = self::_headersToSigningString($headers);
  32. $signedHeaders = implode(' ', array_map('strtolower', array_keys($headers)));
  33. $actor_private_key = new Activitypub_rsa();
  34. $actor_private_key = $actor_private_key->get_private_key($user);
  35. $key = openssl_pkey_get_private($actor_private_key);
  36. openssl_sign($stringToSign, $signature, $key, OPENSSL_ALGO_SHA256);
  37. $signature = base64_encode($signature);
  38. $signatureHeader = 'keyId="' . ActivityPubPlugin::actor_uri($user).'#public-key' . '",headers="' . $signedHeaders . '",algorithm="rsa-sha256",signature="' . $signature . '"';
  39. unset($headers['(request-target)']);
  40. $headers['Signature'] = $signatureHeader;
  41. return self::_headersToCurlArray($headers);
  42. }
  43. private static function _digest($body)
  44. {
  45. if (is_array($body)) {
  46. $body = json_encode($body);
  47. }
  48. return base64_encode(hash('sha256', $body, true));
  49. }
  50. protected static function _headersToSign($url, $digest = false)
  51. {
  52. $date = new DateTime('UTC');
  53. $headers = [
  54. '(request-target)' => 'post ' . parse_url($url, PHP_URL_PATH),
  55. 'Date' => $date->format('D, d M Y H:i:s \G\M\T'),
  56. 'Host' => parse_url($url, PHP_URL_HOST),
  57. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, application/json',
  58. 'User-Agent' => 'GNU social ActivityPub Plugin - https://gnu.io/social',
  59. 'Content-Type' => 'application/activity+json'
  60. ];
  61. if ($digest) {
  62. $headers['Digest'] = 'SHA-256=' . $digest;
  63. }
  64. return $headers;
  65. }
  66. private static function _headersToSigningString($headers)
  67. {
  68. return implode("\n", array_map(function ($k, $v) {
  69. return strtolower($k) . ': ' . $v;
  70. }, array_keys($headers), $headers));
  71. }
  72. private static function _headersToCurlArray($headers)
  73. {
  74. return array_map(function ($k, $v) {
  75. return "$k: $v";
  76. }, array_keys($headers), $headers);
  77. }
  78. public static function parseSignatureHeader($signature)
  79. {
  80. $parts = explode(',', $signature);
  81. $signatureData = [];
  82. foreach ($parts as $part) {
  83. if (preg_match('/(.+)="(.+)"/', $part, $match)) {
  84. $signatureData[$match[1]] = $match[2];
  85. }
  86. }
  87. if (!isset($signatureData['keyId'])) {
  88. return [
  89. 'error' => 'No keyId was found in the signature header. Found: ' . implode(', ', array_keys($signatureData))
  90. ];
  91. }
  92. if (!filter_var($signatureData['keyId'], FILTER_VALIDATE_URL)) {
  93. return [
  94. 'error' => 'keyId is not a URL: ' . $signatureData['keyId']
  95. ];
  96. }
  97. if (!isset($signatureData['headers']) || !isset($signatureData['signature'])) {
  98. return [
  99. 'error' => 'Signature is missing headers or signature parts'
  100. ];
  101. }
  102. return $signatureData;
  103. }
  104. public static function verify($publicKey, $signatureData, $inputHeaders, $path, $body)
  105. {
  106. $digest = 'SHA-256=' . base64_encode(hash('sha256', $body, true));
  107. $headersToSign = [];
  108. foreach (explode(' ', $signatureData['headers']) as $h) {
  109. if ($h == '(request-target)') {
  110. $headersToSign[$h] = 'post ' . $path;
  111. } elseif ($h == 'digest') {
  112. $headersToSign[$h] = $digest;
  113. } elseif (isset($inputHeaders[$h][0])) {
  114. $headersToSign[$h] = $inputHeaders[$h];
  115. }
  116. }
  117. $signingString = self::_headersToSigningString($headersToSign);
  118. $verified = openssl_verify($signingString, base64_decode($signatureData['signature']), $publicKey, OPENSSL_ALGO_SHA256);
  119. return [$verified, $signingString];
  120. }
  121. }