3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
24 use Friendica\Core\Logger;
26 use Friendica\Network\HTTPClient\Client\HttpClient;
27 use Friendica\Network\Probe;
28 use Friendica\Util\Crypto;
29 use Friendica\Util\Strings;
30 use Friendica\Util\XML;
33 * Salmon Protocol class
35 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
36 * and annotations made against newsfeed articles such as blog posts.
41 * @param string $uri Uniform Resource Identifier
42 * @param string $keyhash encoded key
44 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46 public static function getKey($uri, $keyhash)
50 Logger::info('Fetching salmon key for '.$uri);
52 $arr = Probe::lrdd($uri);
55 foreach ($arr as $a) {
56 if ($a['@attributes']['rel'] === 'magic-public-key') {
57 $ret[] = $a['@attributes']['href'];
64 // We have found at least one key URL
65 // If it's inline, parse it - otherwise get the key
67 if (count($ret) > 0) {
68 for ($x = 0; $x < count($ret); $x ++) {
69 if (substr($ret[$x], 0, 5) === 'data:') {
70 if (strstr($ret[$x], ',')) {
71 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
73 $ret[$x] = substr($ret[$x], 5);
75 } elseif (Strings::normaliseLink($ret[$x]) == 'http://') {
76 $ret[$x] = DI::httpClient()->fetch($ret[$x], 0, HttpClient::ACCEPT_MAGIC_KEY);
77 Logger::debug('Fetched public key', ['url' => $ret[$x]]);
83 Logger::notice('Key located', ['ret' => $ret]);
85 if (count($ret) == 1) {
86 // We only found one one key so we don't care if the hash matches.
87 // If it's the wrong key we'll find out soon enough because
88 // message verification will fail. This also covers some older
89 // software which don't supply a keyhash. As long as they only
90 // have one key we'll be right.
94 foreach ($ret as $a) {
95 $hash = Strings::base64UrlEncode(hash('sha256', $a));
96 if ($hash == $keyhash) {
106 * @param array $owner owner
107 * @param string $url url
108 * @param string $slap slap
110 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112 public static function slapper($owner, $url, $slap)
114 // does contact have a salmon endpoint?
120 if (!$owner['sprvkey']) {
121 Logger::notice(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
122 $owner['name'], $owner['uid']));
126 Logger::info('slapper called for '.$url.'. Data: ' . $slap);
128 // create a magic envelope
130 $data = Strings::base64UrlEncode($slap);
131 $data_type = 'application/atom+xml';
132 $encoding = 'base64url';
133 $algorithm = 'RSA-SHA256';
134 $keyhash = Strings::base64UrlEncode(hash('sha256', self::salmonKey($owner['spubkey'])), true);
136 $precomputed = '.' . Strings::base64UrlEncode($data_type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($algorithm);
139 $signature = Strings::base64UrlEncode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
142 $signature2 = Strings::base64UrlEncode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
144 // Old Status.net format
145 $signature3 = Strings::base64UrlEncode(Crypto::rsaSign($data, $owner['sprvkey']));
147 // At first try the non compliant method that works for GNU Social
148 $xmldata = ["me:env" => ["me:data" => $data,
149 "@attributes" => ["type" => $data_type],
150 "me:encoding" => $encoding,
151 "me:alg" => $algorithm,
152 "me:sig" => $signature,
153 "@attributes2" => ["key_id" => $keyhash]]];
155 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
157 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
160 $postResult = DI::httpClient()->post($url, $salmon, [
161 'Content-type' => 'application/magic-envelope+xml',
162 'Content-length' => strlen($salmon),
165 $return_code = $postResult->getReturnCode();
167 // check for success, e.g. 2xx
169 if ($return_code > 299) {
170 Logger::notice('GNU Social salmon failed. Falling back to compliant mode');
172 // Now try the compliant mode that normally isn't used for GNU Social
173 $xmldata = ["me:env" => ["me:data" => $data,
174 "@attributes" => ["type" => $data_type],
175 "me:encoding" => $encoding,
176 "me:alg" => $algorithm,
177 "me:sig" => $signature2,
178 "@attributes2" => ["key_id" => $keyhash]]];
180 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
182 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
185 $postResult = DI::httpClient()->post($url, $salmon, [
186 'Content-type' => 'application/magic-envelope+xml',
187 'Content-length' => strlen($salmon),
189 $return_code = $postResult->getReturnCode();
192 if ($return_code > 299) {
193 Logger::notice('compliant salmon failed. Falling back to old status.net');
195 // Last try. This will most likely fail as well.
196 $xmldata = ["me:env" => ["me:data" => $data,
197 "@attributes" => ["type" => $data_type],
198 "me:encoding" => $encoding,
199 "me:alg" => $algorithm,
200 "me:sig" => $signature3,
201 "@attributes2" => ["key_id" => $keyhash]]];
203 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
205 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
208 $postResult = DI::httpClient()->post($url, $salmon, [
209 'Content-type' => 'application/magic-envelope+xml',
210 'Content-length' => strlen($salmon)]);
211 $return_code = $postResult->getReturnCode();
214 Logger::info('slapper for '.$url.' returned ' . $return_code);
216 if (! $return_code) {
220 if (($return_code == 503) && $postResult->inHeader('retry-after')) {
224 return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1;
228 * @param string $pubkey public key
232 public static function salmonKey($pubkey)
234 Crypto::pemToMe($pubkey, $modulus, $exponent);
235 return 'RSA' . '.' . Strings::base64UrlEncode($modulus, true) . '.' . Strings::base64UrlEncode($exponent, true);