ActivitypubRsa.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category ActivityPub
  24. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\ActivityPub\Entity;
  30. use App\Core\DB\DB;
  31. use App\Core\Entity;
  32. use App\Core\Log;
  33. use App\Entity\Actor;
  34. use App\Util\Exception\ServerException;
  35. use DateTimeInterface;
  36. /**
  37. * ActivityPub Keys System
  38. *
  39. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class ActivitypubRsa extends Entity
  43. {
  44. // {{{ Autocode
  45. // @codeCoverageIgnoreStart
  46. private int $actor_id;
  47. private ?string $private_key = null;
  48. private string $public_key;
  49. private DateTimeInterface $created;
  50. private DateTimeInterface $modified;
  51. public function setActorId(int $actor_id): self
  52. {
  53. $this->actor_id = $actor_id;
  54. return $this;
  55. }
  56. public function getActorId(): int
  57. {
  58. return $this->actor_id;
  59. }
  60. public function setPrivateKey(?string $private_key): self
  61. {
  62. $this->private_key = $private_key;
  63. return $this;
  64. }
  65. public function getPrivateKey(): ?string
  66. {
  67. return $this->private_key;
  68. }
  69. public function setPublicKey(string $public_key): self
  70. {
  71. $this->public_key = $public_key;
  72. return $this;
  73. }
  74. public function getPublicKey(): string
  75. {
  76. return $this->public_key;
  77. }
  78. public function setCreated(DateTimeInterface $created): self
  79. {
  80. $this->created = $created;
  81. return $this;
  82. }
  83. public function getCreated(): DateTimeInterface
  84. {
  85. return $this->created;
  86. }
  87. public function setModified(DateTimeInterface $modified): self
  88. {
  89. $this->modified = $modified;
  90. return $this;
  91. }
  92. public function getModified(): DateTimeInterface
  93. {
  94. return $this->modified;
  95. }
  96. // @codeCoverageIgnoreEnd
  97. // }}} Autocode
  98. /**
  99. * Return table definition for Schema setup and Entity usage.
  100. *
  101. * @return array array of column definitions
  102. */
  103. public static function schemaDef(): array
  104. {
  105. return [
  106. 'name' => 'activitypub_rsa',
  107. 'fields' => [
  108. 'actor_id' => ['type' => 'int', 'not null' => true],
  109. 'private_key' => ['type' => 'text'],
  110. 'public_key' => ['type' => 'text', 'not null' => true],
  111. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  112. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  113. ],
  114. 'primary key' => ['actor_id'],
  115. 'foreign keys' => [
  116. 'activitypub_rsa_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
  117. ],
  118. ];
  119. }
  120. /**
  121. * Guarantees RSA keys for a given actor.
  122. *
  123. * @param bool $fetch =true Should attempt to fetch keys from a remote profile?
  124. *
  125. * @throws ServerException It should never occur, but if so, we break everything!
  126. *
  127. * @return ActivitypubRsa The keys (private key is null for remote actors)
  128. */
  129. public static function getByActor(Actor $gsactor, bool $fetch = true): self
  130. {
  131. $apRSA = self::getByPK(['actor_id' => ($actor_id = $gsactor->getId())]);
  132. if (\is_null($apRSA)) {
  133. // Nonexistent key pair for this profile
  134. if ($gsactor->getIsLocal()) {
  135. self::generateKeys($private_key, $public_key);
  136. $apRSA = self::create([
  137. 'actor_id' => $actor_id,
  138. 'private_key' => $private_key,
  139. 'public_key' => $public_key,
  140. ]);
  141. DB::wrapInTransaction(fn () => DB::persist($apRSA));
  142. } else {
  143. // ASSERT: This should never happen, but try to recover!
  144. Log::error('Activitypub_rsa: An impossible thing has happened... Please let the devs know.');
  145. if ($fetch) {
  146. //$res = Activitypub_explorer::get_remote_user_activity($profile->getUri());
  147. //Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']);
  148. //return self::ensure_public_key($profile, false);
  149. } else {
  150. throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!');
  151. }
  152. }
  153. }
  154. return $apRSA;
  155. }
  156. /**
  157. * Generates a pair of RSA keys.
  158. *
  159. * @param null|string $private_key out
  160. * @param null|string $public_key out
  161. *
  162. * @author PHP Manual Contributed Notes <dirt@awoms.com>
  163. */
  164. private static function generateKeys(?string &$private_key, ?string &$public_key): void
  165. {
  166. $config = [
  167. 'digest_alg' => 'sha512',
  168. 'private_key_bits' => 2048,
  169. 'private_key_type' => \OPENSSL_KEYTYPE_RSA,
  170. ];
  171. // Create the private and public key
  172. $res = openssl_pkey_new($config);
  173. // Extract the private key from $res to $private_key
  174. openssl_pkey_export($res, $private_key);
  175. // Extract the public key from $res to $pubKey
  176. $pubKey = openssl_pkey_get_details($res);
  177. $public_key = $pubKey['key'];
  178. unset($pubKey);
  179. }
  180. }