]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Crypto.php
Fix edited messages for Diaspora
[friendica.git] / src / Util / Crypto.php
index 3426babe36e27aa2102bf6d36152f80a939903ed..78f7c349a71eb855e2f05ebb3d8c528b4adcc83e 100644 (file)
@@ -4,10 +4,10 @@
  */
 namespace Friendica\Util;
 
-use Friendica\Core\Addon;
 use Friendica\Core\Config;
+use Friendica\Core\Hook;
 use Friendica\Core\Logger;
-use Friendica\Util\Strings;
+use Friendica\Core\System;
 use ASN_BASE;
 use ASNValue;
 
@@ -25,6 +25,9 @@ class Crypto
         */
        public static function rsaSign($data, $key, $alg = 'sha256')
        {
+               if (empty($key)) {
+                       Logger::warning('Empty key parameter', ['callstack' => System::callstack()]);
+               }
                openssl_sign($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
                return $sig;
        }
@@ -38,12 +41,15 @@ class Crypto
         */
        public static function rsaVerify($data, $sig, $key, $alg = 'sha256')
        {
+               if (empty($key)) {
+                       Logger::warning('Empty key parameter', ['callstack' => System::callstack()]);
+               }
                return openssl_verify($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
        }
 
        /**
         * @param string $Der     der formatted string
-        * @param string $Private key type optional, default false
+        * @param bool   $Private key type optional, default false
         * @return string
         */
        private static function DerToPem($Der, $Private = false)
@@ -150,6 +156,7 @@ class Crypto
         * @param string $m   modulo reference
         * @param object $e   exponent reference
         * @return void
+        * @throws \Exception
         */
        private static function pubRsaToMe($key, &$m, &$e)
        {
@@ -167,6 +174,7 @@ class Crypto
        /**
         * @param string $key key
         * @return string
+        * @throws \Exception
         */
        public static function rsaToPem($key)
        {
@@ -177,6 +185,7 @@ class Crypto
        /**
         * @param string $key key
         * @return string
+        * @throws \Exception
         */
        private static function pemToRsa($key)
        {
@@ -189,6 +198,7 @@ class Crypto
         * @param string $m   modulo reference
         * @param string $e   exponent reference
         * @return void
+        * @throws \Exception
         */
        public static function pemToMe($key, &$m, &$e)
        {
@@ -218,6 +228,7 @@ class Crypto
        /**
         * @param integer $bits number of bits
         * @return mixed
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function newKeypair($bits)
        {
@@ -319,14 +330,15 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param string $data
         * @param string $pubkey The public key.
         * @param string $alg    The algorithm used for encryption.
-        * 
+        *
         * @return array
+        * @throws \Exception
         */
        public static function encapsulate($data, $pubkey, $alg = 'aes256cbc')
        {
@@ -337,14 +349,15 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param type $data
-        * @param type $pubkey The public key.
-        * @param type $alg    The algorithm used for encryption.
-        * 
+        *
+        * @param string $data
+        * @param string $pubkey The public key.
+        * @param string $alg    The algorithm used for encryption.
+        *
         * @return array
+        * @throws \Exception
         */
        private static function encapsulateOther($data, $pubkey, $alg)
        {
@@ -372,20 +385,21 @@ class Crypto
                        return $result;
                } else {
                        $x = ['data' => $data, 'pubkey' => $pubkey, 'alg' => $alg, 'result' => $data];
-                       Addon::callHooks('other_encapsulate', $x);
+                       Hook::callAll('other_encapsulate', $x);
 
                        return $x['result'];
                }
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param string $data
         * @param string $pubkey
-        * 
+        *
         * @return array
+        * @throws \Exception
         */
        private static function encapsulateAes($data, $pubkey)
        {
@@ -413,15 +427,16 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param string $data
-        * @param string $prvkey  The private key used for decryption.
-        * 
+        *
+        * @param array $data ['iv' => $iv, 'key' => $key, 'alg' => $alg, 'data' => $data]
+        * @param string $prvkey The private key used for decryption.
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Exception
         */
-       public static function unencapsulate($data, $prvkey)
+       public static function unencapsulate(array $data, $prvkey)
        {
                if (!$data) {
                        return;
@@ -429,22 +444,23 @@ class Crypto
 
                $alg = ((array_key_exists('alg', $data)) ? $data['alg'] : 'aes256cbc');
                if ($alg === 'aes256cbc') {
-                       return self::encapsulateAes($data, $prvkey);
+                       return self::encapsulateAes($data['data'], $prvkey);
                }
-               return self::encapsulateOther($data, $prvkey, $alg);
+               return self::encapsulateOther($data['data'], $prvkey, $alg);
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param string $data
-        * @param string $prvkey  The private key used for decryption.
+        *
+        * @param array $data
+        * @param string $prvkey The private key used for decryption.
         * @param string $alg
-        * 
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function unencapsulateOther($data, $prvkey, $alg)
+       private static function unencapsulateOther(array $data, $prvkey, $alg)
        {
                $fn = 'decrypt' . strtoupper($alg);
 
@@ -455,20 +471,21 @@ class Crypto
                        return self::$fn(Strings::base64UrlDecode($data['data']), $k, $i);
                } else {
                        $x = ['data' => $data, 'prvkey' => $prvkey, 'alg' => $alg, 'result' => $data];
-                       Addon::callHooks('other_unencapsulate', $x);
+                       Hook::callAll('other_unencapsulate', $x);
 
                        return $x['result'];
                }
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param array  $data
-        * @param string $prvkey  The private key used for decryption.
-        * 
+        * @param string $prvkey The private key used for decryption.
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Exception
         */
        private static function unencapsulateAes($data, $prvkey)
        {