]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge branch 'friendica:develop' into bug-noLocalPosts
[friendica.git] / src / Protocol / Diaspora.php
index ffffaf6199a02e0177d2b05fdb0e97d550bf56ff..54f09e9d970c8ce209249a39a609b17d2c989b90 100644 (file)
@@ -54,7 +54,8 @@ use GuzzleHttp\Psr7\Uri;
 use SimpleXMLElement;
 
 /**
- * This class contain functions to create and send Diaspora XML files
+ * This class contains functions to communicate via the Diaspora protocol
+ * @see https://diaspora.github.io/diaspora_federation/
  */
 class Diaspora
 {
@@ -223,14 +224,34 @@ class Diaspora
 
                // Is it a private post? Then decrypt the outer Salmon
                if (is_object($data)) {
-                       $encrypted_aes_key_bundle = base64_decode($data->aes_key);
-                       $ciphertext = base64_decode($data->encrypted_magic_envelope);
+                       try {
+                               if (!isset($data->aes_key) || !isset($data->encrypted_magic_envelope)) {
+                                       Logger::info('Missing keys "aes_key" and/or "encrypted_magic_envelope"', ['data' => $data]);
+                                       throw new \RuntimeException('Missing keys "aes_key" and/or "encrypted_magic_envelope"');
+                               }
 
-                       $outer_key_bundle = '';
-                       @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
-                       $j_outer_key_bundle = json_decode($outer_key_bundle);
+                               $encrypted_aes_key_bundle = base64_decode($data->aes_key);
+                               $ciphertext = base64_decode($data->encrypted_magic_envelope);
+
+                               $outer_key_bundle = '';
+                               @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
+                               $j_outer_key_bundle = json_decode($outer_key_bundle);
 
-                       if (!is_object($j_outer_key_bundle)) {
+                               if (!is_object($j_outer_key_bundle)) {
+                                       Logger::info('Unable to decode outer key bundle', ['outer_key_bundle' => $outer_key_bundle]);
+                                       throw new \RuntimeException('Unable to decode outer key bundle');
+                               }
+
+                               if (!isset($j_outer_key_bundle->iv) || !isset($j_outer_key_bundle->key)) {
+                                       Logger::info('Missing keys "iv" and/or "key" from outer Salmon', ['j_outer_key_bundle' => $j_outer_key_bundle]);
+                                       throw new \RuntimeException('Missing keys "iv" and/or "key" from outer Salmon');
+                               }
+
+                               $outer_iv = base64_decode($j_outer_key_bundle->iv);
+                               $outer_key = base64_decode($j_outer_key_bundle->key);
+
+                               $xml = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
+                       } catch (\Throwable $e) {
                                Logger::notice('Outer Salmon did not verify. Discarding.');
                                if ($no_exit) {
                                        return false;
@@ -238,11 +259,6 @@ class Diaspora
                                        throw new \Friendica\Network\HTTPException\BadRequestException();
                                }
                        }
-
-                       $outer_iv = base64_decode($j_outer_key_bundle->iv);
-                       $outer_key = base64_decode($j_outer_key_bundle->key);
-
-                       $xml = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
                } else {
                        $xml = $raw;
                }
@@ -3200,7 +3216,7 @@ class Diaspora
         */
        public static function getReshareDetails(array $item): array
        {
-               $reshared = DI::contentItem()->getSharedPost($item, ['network', 'author-addr']);
+               $reshared = DI::contentItem()->getSharedPost($item, ['guid', 'network', 'author-addr']);
                if (empty($reshared)) {
                        return [];
                }
@@ -3212,7 +3228,7 @@ class Diaspora
 
                return [
                        'root_handle' => strtolower($reshared['post']['author-addr']),
-                       'root_guid'   => $reshared['guid']
+                       'root_guid'   => $reshared['post']['guid'],
                ];
        }
 
@@ -3637,7 +3653,7 @@ class Diaspora
 
                Logger::info('Got relayable data ' . $type . ' for item ' . $item['guid'] . ' (' . $item['id'] . ')');
 
-               $msg = json_decode($item['signed_text'], true);
+               $msg = json_decode($item['signed_text'] ?? '', true);
 
                $message = [];
                if (is_array($msg)) {