*/
namespace Friendica\Util;
-use Friendica\Core\Config;
-use Friendica\Core\Hook;
-use Friendica\Core\Logger;
use ASN_BASE;
use ASNValue;
+use Friendica\Core\Hook;
+use Friendica\Core\Logger;
+use Friendica\Core\System;
+use Friendica\DI;
/**
- * @brief Crypto class
+ * Crypto class
*/
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;
}
*/
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)
* @param string $m modulo reference
* @param object $e exponent reference
* @return void
+ * @throws \Exception
*/
private static function pubRsaToMe($key, &$m, &$e)
{
/**
* @param string $key key
* @return string
+ * @throws \Exception
*/
public static function rsaToPem($key)
{
/**
* @param string $key key
* @return string
+ * @throws \Exception
*/
private static function pemToRsa($key)
{
* @param string $m modulo reference
* @param string $e exponent reference
* @return void
+ * @throws \Exception
*/
public static function pemToMe($key, &$m, &$e)
{
$r = ASN_BASE::parseASNString($x);
- $m = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
- $e = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
+ if (isset($r[0])) {
+ $m = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
+ $e = Strings::base64UrlDecode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
+ }
}
/**
/**
* @param integer $bits number of bits
* @return mixed
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function newKeypair($bits)
{
'encrypt_key' => false
];
- $conf = Config::get('system', 'openssl_conf_file');
+ $conf = DI::config()->get('system', 'openssl_conf_file');
if ($conf) {
$openssl_options['config'] = $conf;
}
}
/**
- *
+ *
* 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')
{
}
/**
- *
+ *
* 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)
{
}
/**
- *
+ *
* 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)
{
}
/**
- *
+ *
* 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;
$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);
}
/**
- *
+ *
* 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)
{