X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FCrypto.php;h=2ab97e5373b5a53fd276ba5ab859af6f0fbf5c2c;hb=9a5f9829c11998284620a804eee15049fccc0757;hp=7dd0dee5c0dc9c603114540e635808894aeedb24;hpb=e8891e9ba4f72d75c2325917d57ffc808cab7e99;p=friendica.git diff --git a/src/Util/Crypto.php b/src/Util/Crypto.php index 7dd0dee5c0..2ab97e5373 100644 --- a/src/Util/Crypto.php +++ b/src/Util/Crypto.php @@ -4,8 +4,8 @@ */ namespace Friendica\Util; -use Friendica\Core\Addon; use Friendica\Core\Config; +use Friendica\Core\Hook; use Friendica\Core\Logger; use ASN_BASE; use ASNValue; @@ -42,7 +42,7 @@ class Crypto /** * @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) @@ -149,6 +149,7 @@ class Crypto * @param string $m modulo reference * @param object $e exponent reference * @return void + * @throws \Exception */ private static function pubRsaToMe($key, &$m, &$e) { @@ -159,13 +160,14 @@ class Crypto $r = ASN_BASE::parseASNString($x); - $m = base64url_decode($r[0]->asnData[0]->asnData); - $e = base64url_decode($r[0]->asnData[1]->asnData); + $m = Strings::base64UrlDecode($r[0]->asnData[0]->asnData); + $e = Strings::base64UrlDecode($r[0]->asnData[1]->asnData); } /** * @param string $key key * @return string + * @throws \Exception */ public static function rsaToPem($key) { @@ -176,6 +178,7 @@ class Crypto /** * @param string $key key * @return string + * @throws \Exception */ private static function pemToRsa($key) { @@ -188,6 +191,7 @@ class Crypto * @param string $m modulo reference * @param string $e exponent reference * @return void + * @throws \Exception */ public static function pemToMe($key, &$m, &$e) { @@ -198,8 +202,8 @@ class Crypto $r = ASN_BASE::parseASNString($x); - $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData); - $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData); + $m = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData); + $e = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData); } /** @@ -217,6 +221,7 @@ class Crypto /** * @param integer $bits number of bits * @return mixed + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function newKeypair($bits) { @@ -318,14 +323,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') { @@ -336,14 +342,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) { @@ -355,7 +362,7 @@ class Crypto $result = ['encrypted' => true]; $key = random_bytes(256); $iv = random_bytes(256); - $result['data'] = base64url_encode(self::$fn($data, $key, $iv), true); + $result['data'] = Strings::base64UrlEncode(self::$fn($data, $key, $iv), true); // log the offending call so we can track it down if (!openssl_public_encrypt($key, $k, $pubkey)) { @@ -364,27 +371,28 @@ class Crypto } $result['alg'] = $alg; - $result['key'] = base64url_encode($k, true); + $result['key'] = Strings::base64UrlEncode($k, true); openssl_public_encrypt($iv, $i, $pubkey); - $result['iv'] = base64url_encode($i, true); + $result['iv'] = Strings::base64UrlEncode($i, true); 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) { @@ -395,7 +403,7 @@ class Crypto $key = random_bytes(32); $iv = random_bytes(16); $result = ['encrypted' => true]; - $result['data'] = base64url_encode(self::encryptAES256CBC($data, $key, $iv), true); + $result['data'] = Strings::base64UrlEncode(self::encryptAES256CBC($data, $key, $iv), true); // log the offending call so we can track it down if (!openssl_public_encrypt($key, $k, $pubkey)) { @@ -404,23 +412,24 @@ class Crypto } $result['alg'] = 'aes256cbc'; - $result['key'] = base64url_encode($k, true); + $result['key'] = Strings::base64UrlEncode($k, true); openssl_public_encrypt($iv, $i, $pubkey); - $result['iv'] = base64url_encode($i, true); + $result['iv'] = Strings::base64UrlEncode($i, true); return $result; } /** - * + * * 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; @@ -428,53 +437,55 @@ 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); if (method_exists(__CLASS__, $fn)) { - openssl_private_decrypt(base64url_decode($data['key']), $k, $prvkey); - openssl_private_decrypt(base64url_decode($data['iv']), $i, $prvkey); + openssl_private_decrypt(Strings::base64UrlDecode($data['key']), $k, $prvkey); + openssl_private_decrypt(Strings::base64UrlDecode($data['iv']), $i, $prvkey); - return self::$fn(base64url_decode($data['data']), $k, $i); + 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) { - openssl_private_decrypt(base64url_decode($data['key']), $k, $prvkey); - openssl_private_decrypt(base64url_decode($data['iv']), $i, $prvkey); + openssl_private_decrypt(Strings::base64UrlDecode($data['key']), $k, $prvkey); + openssl_private_decrypt(Strings::base64UrlDecode($data['iv']), $i, $prvkey); - return self::decryptAES256CBC(base64url_decode($data['data']), $k, $i); + return self::decryptAES256CBC(Strings::base64UrlDecode($data['data']), $k, $i); }