HTTPSignature.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * @category Network
  17. * @package Nautilus
  18. *
  19. * @author Aaron Parecki <aaron@parecki.com>
  20. * @author Diogo Peralta Cordeiro <@diogo.site>
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  22. *
  23. * @see https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php
  24. */
  25. namespace Plugin\ActivityPub\Util;
  26. use App\Entity\Actor;
  27. use DateTime;
  28. use Exception;
  29. use Plugin\ActivityPub\ActivityPub;
  30. use Plugin\ActivityPub\Entity\ActivitypubRsa;
  31. class HTTPSignature
  32. {
  33. /**
  34. * Sign a message with an Actor
  35. *
  36. * @param Actor $user Actor signing
  37. * @param string $url Inbox url
  38. * @param bool|string $body Data to sign (optional)
  39. * @param array $addlHeaders Additional headers (optional)
  40. *
  41. * @throws Exception Attempted to sign something that belongs to an Actor we don't own
  42. *
  43. * @return array Headers to be used in request
  44. */
  45. public static function sign(Actor $user, string $url, string|bool $body = false, array $addlHeaders = [], string $method = 'post'): array
  46. {
  47. $digest = false;
  48. if ($body) {
  49. $digest = self::_digest($body);
  50. }
  51. $headers = self::_headersToSign($url, $digest, $method);
  52. $headers = array_merge($headers, $addlHeaders);
  53. $stringToSign = self::_headersToSigningString($headers);
  54. $signedHeaders = implode(' ', array_map('strtolower', array_keys($headers)));
  55. $actor_private_key = ActivitypubRsa::getByActor($user)->getPrivateKey();
  56. // Intentionally unhandled exception, we want this to explode if that happens as it would be a bug
  57. $key = openssl_pkey_get_private($actor_private_key);
  58. openssl_sign($stringToSign, $signature, $key, \OPENSSL_ALGO_SHA256);
  59. $signature = base64_encode($signature);
  60. $signatureHeader = 'keyId="' . $user->getUri() . '#public-key' . '",headers="' . $signedHeaders . '",algorithm="rsa-sha256",signature="' . $signature . '"';
  61. unset($headers['(request-target)']);
  62. $headers['Signature'] = $signatureHeader;
  63. return $headers;
  64. }
  65. /**
  66. * @param array|string $body array or json string $body
  67. */
  68. private static function _digest(array|string $body): string
  69. {
  70. if (\is_array($body)) {
  71. $body = json_encode($body);
  72. }
  73. return base64_encode(hash('sha256', $body, true));
  74. }
  75. /**
  76. * Return a canonical array of http headers, ready to be signed.
  77. *
  78. * @param string $uri uri of destination
  79. * @param bool|string $digest digest of the request body to add to the `Digest` header (optional)
  80. * @param string $method http method (GET, POST, etc) that the request will use.
  81. * This will be used in the `(request-target)` part of the signature.
  82. *
  83. * @throws Exception
  84. *
  85. * @return array headers to be signed
  86. */
  87. protected static function _headersToSign(string $url, string|bool $digest = false, string $method = 'GET'): array
  88. {
  89. $date = new DateTime('UTC');
  90. $headers = [
  91. '(request-target)' => mb_strtolower($method) . ' ' . parse_url($url, \PHP_URL_PATH),
  92. 'Date' => $date->format('D, d M Y H:i:s \G\M\T'),
  93. 'Host' => parse_url($url, \PHP_URL_HOST),
  94. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/activity+json, application/json',
  95. 'User-Agent' => 'GNU social ActivityPub Plugin - v' . ActivityPub::version() . ' - ' . GNUSOCIAL_ENGINE_URL,
  96. 'Content-Type' => 'application/activity+json',
  97. ];
  98. if ($digest) {
  99. $headers['Digest'] = 'SHA-256=' . $digest;
  100. }
  101. return $headers;
  102. }
  103. private static function _headersToSigningString(array $headers): string
  104. {
  105. return implode("\n", array_map(fn ($k, $v) => mb_strtolower($k) . ': ' . $v, array_keys($headers), $headers));
  106. }
  107. public static function parseSignatureHeader(string $signature): array
  108. {
  109. $parts = explode(',', $signature);
  110. $signatureData = [];
  111. foreach ($parts as $part) {
  112. if (preg_match('/(.+)="(.+)"/', $part, $match)) {
  113. $signatureData[$match[1]] = $match[2];
  114. }
  115. }
  116. if (!isset($signatureData['keyId'])) {
  117. return [
  118. 'error' => 'No keyId was found in the signature header. Found: ' . implode(', ', array_keys($signatureData)),
  119. ];
  120. }
  121. if (!filter_var($signatureData['keyId'], \FILTER_VALIDATE_URL)) {
  122. return [
  123. 'error' => 'keyId is not a URL: ' . $signatureData['keyId'],
  124. ];
  125. }
  126. if (!isset($signatureData['headers']) || !isset($signatureData['signature'])) {
  127. return [
  128. 'error' => 'Signature is missing headers or signature parts',
  129. ];
  130. }
  131. return $signatureData;
  132. }
  133. public static function verify(string $publicKey, array $signatureData, array $inputHeaders, string $path, string $body): array
  134. {
  135. // We need this because the used Request headers fields specified by Signature are in lower case.
  136. $headersContent = array_change_key_case($inputHeaders, \CASE_LOWER);
  137. $digest = 'SHA-256=' . base64_encode(hash('sha256', $body, true));
  138. $headersToSign = [];
  139. foreach (explode(' ', $signatureData['headers']) as $h) {
  140. if ($h == '(request-target)') {
  141. $headersToSign[$h] = 'post ' . $path;
  142. } elseif ($h == '(created)') {
  143. $headersToSign[$h] = $signatureData['created'];
  144. } elseif ($h == '(expires)') {
  145. $headersToSign[$h] = $signatureData['expires'];
  146. } elseif ($h == 'digest') {
  147. $headersToSign[$h] = $digest;
  148. } elseif (\array_key_exists($h, $headersContent)) {
  149. $headersToSign[$h] = $headersContent[$h];
  150. }
  151. }
  152. $signingString = self::_headersToSigningString($headersToSign);
  153. $verified = openssl_verify($signingString, base64_decode($signatureData['signature']), $publicKey, \OPENSSL_ALGO_SHA256);
  154. return [$verified, $signingString];
  155. }
  156. }