ActivitypubRsa.php 6.3 KB

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