Activitypub_rsa.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub Keys System
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_rsa extends Managed_DataObject
  35. {
  36. public $__table = 'activitypub_rsa';
  37. public $profile_id; // int(4) primary_key not_null
  38. public $private_key; // text() not_null
  39. public $public_key; // text() not_null
  40. public $created; // datetime() not_null default_CURRENT_TIMESTAMP
  41. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  42. /**
  43. * Return table definition for Schema setup and DB_DataObject usage.
  44. *
  45. * @author Diogo Cordeiro <diogo@fc.up.pt>
  46. * @return array array of column definitions
  47. */
  48. public static function schemaDef()
  49. {
  50. return [
  51. 'fields' => [
  52. 'profile_id' => ['type' => 'int', 'not null' => true],
  53. 'private_key' => ['type' => 'text'],
  54. 'public_key' => ['type' => 'text', 'not null' => true],
  55. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  56. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  57. ],
  58. 'primary key' => ['profile_id'],
  59. 'foreign keys' => [
  60. 'activitypub_profile_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
  61. ],
  62. ];
  63. }
  64. public function get_private_key($profile)
  65. {
  66. $this->profile_id = $profile->getID();
  67. $apRSA = self::getKV('profile_id', $this->profile_id);
  68. if (!$apRSA instanceof Activitypub_rsa) {
  69. // No existing key pair for this profile
  70. if ($profile->isLocal()) {
  71. self::generate_keys($this->private_key, $this->public_key);
  72. $this->store_keys();
  73. $apRSA->private_key = $this->private_key;
  74. } else {
  75. throw new Exception('This is a remote Profile, there is no Private Key for this Profile.');
  76. }
  77. }
  78. return $apRSA->private_key;
  79. }
  80. /**
  81. * Guarantees a Public Key for a given profile.
  82. *
  83. * @param Profile $profile
  84. * @param bool $fetch
  85. * @return string The public key
  86. * @throws ServerException It should never occur, but if so, we break everything!
  87. * @author Diogo Cordeiro <diogo@fc.up.pt>
  88. */
  89. public function ensure_public_key($profile, $fetch = true)
  90. {
  91. $this->profile_id = $profile->getID();
  92. $apRSA = self::getKV('profile_id', $this->profile_id);
  93. if (!$apRSA instanceof Activitypub_rsa) {
  94. // No existing key pair for this profile
  95. if ($profile->isLocal()) {
  96. self::generate_keys($this->private_key, $this->public_key);
  97. $this->store_keys();
  98. $apRSA->public_key = $this->public_key;
  99. } else {
  100. // This should never happen, but try to recover!
  101. if ($fetch) {
  102. $res = Activitypub_explorer::get_remote_user_activity(ActivityPubPlugin::actor_uri($profile));
  103. Activitypub_rsa::update_public_key($profile, $res['publicKey']['publicKeyPem']);
  104. return self::ensure_public_key($profile, false);
  105. } else {
  106. throw new ServerException('Activitypub_rsa: Failed to find keys for given profile. That should have not happened!');
  107. }
  108. }
  109. }
  110. return $apRSA->public_key;
  111. }
  112. /**
  113. * Insert the current object variables into the database.
  114. *
  115. * @author Diogo Cordeiro <diogo@fc.up.pt>
  116. * @access public
  117. * @throws ServerException
  118. */
  119. public function store_keys()
  120. {
  121. $this->created = $this->modified = common_sql_now();
  122. $ok = $this->insert();
  123. if ($ok === false) {
  124. throw new ServerException('Cannot save ActivityPub RSA.');
  125. }
  126. }
  127. /**
  128. * Generates a pair of RSA keys.
  129. *
  130. * @author PHP Manual Contributed Notes <dirt@awoms.com>
  131. * @param string $private_key in/out
  132. * @param string $public_key in/out
  133. */
  134. public static function generate_keys(&$private_key, &$public_key)
  135. {
  136. $config = [
  137. 'digest_alg' => 'sha512',
  138. 'private_key_bits' => 2048,
  139. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  140. ];
  141. // Create the private and public key
  142. $res = openssl_pkey_new($config);
  143. // Extract the private key from $res to $private_key
  144. openssl_pkey_export($res, $private_key);
  145. // Extract the public key from $res to $pubKey
  146. $pubKey = openssl_pkey_get_details($res);
  147. $public_key = $pubKey["key"];
  148. unset($pubKey);
  149. }
  150. /**
  151. * Update public key.
  152. *
  153. * @param Profile $profile
  154. * @param string $public_key
  155. * @throws Exception
  156. * @author Diogo Cordeiro <diogo@fc.up.pt>
  157. */
  158. public static function update_public_key($profile, $public_key)
  159. {
  160. // Public Key
  161. $apRSA = new Activitypub_rsa();
  162. $apRSA->profile_id = $profile->getID();
  163. $apRSA->public_key = $public_key;
  164. $apRSA->modified = common_sql_now();
  165. if (!$apRSA->update()) {
  166. $apRSA->insert();
  167. }
  168. }
  169. }