Activitypub_rsa.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * GNU social - a federating social network
  4. *
  5. * ActivityPubPlugin implementation for GNU Social
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2018 Free Software Foundation http://fsf.org
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link https://www.gnu.org/software/social/
  26. */
  27. if (!defined('GNUSOCIAL')) {
  28. exit(1);
  29. }
  30. /**
  31. * ActivityPub Keys System
  32. *
  33. * @category Plugin
  34. * @package GNUsocial
  35. * @author Diogo Cordeiro <diogo@fc.up.pt>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://www.gnu.org/software/social/
  38. */
  39. class Activitypub_rsa extends Managed_DataObject
  40. {
  41. public $__table = 'Activitypub_rsa';
  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' => 'integer'],
  53. 'private_key' => ['type' => 'text'],
  54. 'public_key' => ['type' => 'text', 'not null' => true],
  55. 'created' => ['type' => 'datetime', 'not null' => true],
  56. 'modified' => ['type' => 'datetime', 'not null' => true],
  57. ],
  58. 'primary key' => ['profile_id'],
  59. 'unique keys' => [
  60. 'Activitypub_rsa_profile_id_key' => ['profile_id'],
  61. ],
  62. 'foreign keys' => [
  63. 'Activitypub_profile_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
  64. ],
  65. ];
  66. }
  67. public function get_private_key($profile)
  68. {
  69. $this->profile_id = $profile->getID();
  70. $apRSA = self::getKV('profile_id', $this->profile_id);
  71. if (!$apRSA instanceof Activitypub_rsa) {
  72. // No existing key pair for this profile
  73. if ($profile->isLocal()) {
  74. self::generate_keys($this->private_key, $this->public_key);
  75. $this->store_keys();
  76. } else {
  77. throw new Exception('This is a remote Profile, there is no Private Key for this Profile.');
  78. }
  79. }
  80. return $apRSA->private_key;
  81. }
  82. /**
  83. * Guarantees a Public Key for a given profile.
  84. *
  85. * @author Diogo Cordeiro <diogo@fc.up.pt>
  86. * @param Profile $profile
  87. * @return string The public key
  88. * @throws ServerException It should never occur, but if so, we break everything!
  89. */
  90. public function ensure_public_key($profile, $fetch = true)
  91. {
  92. $this->profile_id = $profile->getID();
  93. $apRSA = self::getKV('profile_id', $this->profile_id);
  94. if (!$apRSA instanceof Activitypub_rsa) {
  95. // No existing key pair for this profile
  96. if ($profile->isLocal()) {
  97. self::generate_keys($this->private_key, $this->public_key);
  98. $this->store_keys();
  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 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. * @author Diogo Cordeiro <diogo@fc.up.pt>
  154. * @param Profile $profile
  155. * @param string $public_key
  156. */
  157. public static function update_public_key($profile, $public_key)
  158. {
  159. // Public Key
  160. $apRSA = new Activitypub_rsa();
  161. $apRSA->profile_id = $profile->getID();
  162. $apRSA->public_key = $public_key;
  163. $apRSA->modified = common_sql_now();
  164. if(!$apRSA->update()) {
  165. $apRSA->insert();
  166. }
  167. }
  168. }