X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FSalmon.php;h=5047375ea0fb81c89caccb69a6cc1b87e6d25418;hb=dc69116572c7ab45987c6ecb19edfa1bca5068b7;hp=cf202ba13c0823b19ea0321880b9afe5c45ed151;hpb=e299fc67c8809ecf1018e6680a3a5284bcf7d8d9;p=friendica.git diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index cf202ba13c..5047375ea0 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -1,6 +1,6 @@ $ret]); if (count($ret) == 1) { - // We only found one one key so we don't care if the hash matches. - // If it's the wrong key we'll find out soon enough because - // message verification will fail. This also covers some older - // software which don't supply a keyhash. As long as they only - // have one key we'll be right. - - return $ret[0]; + /* We only found one key so we don't care if the hash matches. + * If it's the wrong key we'll find out soon enough because + * message verification will fail. This also covers some older + * software which don't supply a keyhash. As long as they only + * have one key we'll be right. + */ + return (string) $ret[0]; } else { foreach ($ret as $a) { $hash = Strings::base64UrlEncode(hash('sha256', $a)); @@ -109,18 +111,18 @@ class Salmon * @return integer * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function slapper($owner, $url, $slap) + public static function slapper(array $owner, string $url, string $slap): int { // does contact have a salmon endpoint? if (!strlen($url)) { - return; + return -1; } if (!$owner['sprvkey']) { Logger::notice(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.", $owner['name'], $owner['uid'])); - return; + return -1; } Logger::info('slapper called for '.$url.'. Data: ' . $slap); @@ -145,19 +147,23 @@ class Salmon $signature3 = Strings::base64UrlEncode(Crypto::rsaSign($data, $owner['sprvkey'])); // At first try the non compliant method that works for GNU Social - $xmldata = ["me:env" => ["me:data" => $data, - "@attributes" => ["type" => $data_type], - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature, - "@attributes2" => ["key_id" => $keyhash]]]; + $xmldata = [ + 'me:env' => [ + 'me:data' => $data, + '@attributes' => ['type' => $data_type], + 'me:encoding' => $encoding, + 'me:alg' => $algorithm, + 'me:sig' => $signature, + '@attributes2' => ['key_id' => $keyhash], + ] + ]; - $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"]; + $namespaces = ['me' => ActivityNamespace::SALMON_ME]; - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -170,19 +176,21 @@ class Salmon Logger::notice('GNU Social salmon failed. Falling back to compliant mode'); // Now try the compliant mode that normally isn't used for GNU Social - $xmldata = ["me:env" => ["me:data" => $data, - "@attributes" => ["type" => $data_type], - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature2, - "@attributes2" => ["key_id" => $keyhash]]]; - - $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"]; - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $xmldata = [ + 'me:env' => [ + 'me:data' => $data, + '@attributes' => ['type' => $data_type], + 'me:encoding' => $encoding, + 'me:alg' => $algorithm, + 'me:sig' => $signature2, + '@attributes2' => ['key_id' => $keyhash] + ] + ]; + + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon), ]); @@ -193,19 +201,21 @@ class Salmon Logger::notice('compliant salmon failed. Falling back to old status.net'); // Last try. This will most likely fail as well. - $xmldata = ["me:env" => ["me:data" => $data, - "@attributes" => ["type" => $data_type], - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature3, - "@attributes2" => ["key_id" => $keyhash]]]; - - $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"]; - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $xmldata = [ + 'me:env' => [ + 'me:data' => $data, + '@attributes' => ['type' => $data_type], + 'me:encoding' => $encoding, + 'me:alg' => $algorithm, + 'me:sig' => $signature3, + '@attributes2' => ['key_id' => $keyhash], + ] + ]; + + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them - $postResult = DI::httpClient()->post($url, $salmon, HttpClientAccept::DEFAULT, [ + $postResult = DI::httpClient()->post($url, $salmon, [ 'Content-type' => 'application/magic-envelope+xml', 'Content-length' => strlen($salmon)]); $return_code = $postResult->getReturnCode(); @@ -229,9 +239,21 @@ class Salmon * @return string * @throws \Exception */ - public static function salmonKey($pubkey) + public static function salmonKey(string $pubkey): string { - Crypto::pemToMe($pubkey, $modulus, $exponent); - return 'RSA' . '.' . Strings::base64UrlEncode($modulus, true) . '.' . Strings::base64UrlEncode($exponent, true); + \phpseclib3\Crypt\RSA::addFileFormat(Magic::class); + + return PublicKeyLoader::load($pubkey)->toString('Magic'); + } + + /** + * @param string $magic Magic key format starting with "RSA." + * @return string + */ + public static function magicKeyToPem(string $magic): string + { + \phpseclib3\Crypt\RSA::addFileFormat(Magic::class); + + return (string) PublicKeyLoader::load($magic); } }