3 * @copyright Copyright (C) 2010-2021, 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\Probe;
27 use Friendica\Util\Crypto;
28 use Friendica\Util\Strings;
29 use Friendica\Util\XML;
32 * Salmon Protocol class
34 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
35 * and annotations made against newsfeed articles such as blog posts.
40 * @param string $uri Uniform Resource Identifier
41 * @param string $keyhash encoded key
43 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
45 public static function getKey($uri, $keyhash)
49 Logger::log('Fetching salmon key for '.$uri);
51 $arr = Probe::lrdd($uri);
54 foreach ($arr as $a) {
55 if ($a['@attributes']['rel'] === 'magic-public-key') {
56 $ret[] = $a['@attributes']['href'];
63 // We have found at least one key URL
64 // If it's inline, parse it - otherwise get the key
66 if (count($ret) > 0) {
67 for ($x = 0; $x < count($ret); $x ++) {
68 if (substr($ret[$x], 0, 5) === 'data:') {
69 if (strstr($ret[$x], ',')) {
70 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
72 $ret[$x] = substr($ret[$x], 5);
74 } elseif (Strings::normaliseLink($ret[$x]) == 'http://') {
75 $ret[$x] = DI::httpRequest()->fetch($ret[$x]);
81 Logger::notice('Key located', ['ret' => $ret]);
83 if (count($ret) == 1) {
84 // We only found one one key so we don't care if the hash matches.
85 // If it's the wrong key we'll find out soon enough because
86 // message verification will fail. This also covers some older
87 // software which don't supply a keyhash. As long as they only
88 // have one key we'll be right.
92 foreach ($ret as $a) {
93 $hash = Strings::base64UrlEncode(hash('sha256', $a));
94 if ($hash == $keyhash) {
104 * @param array $owner owner
105 * @param string $url url
106 * @param string $slap slap
108 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
110 public static function slapper($owner, $url, $slap)
112 // does contact have a salmon endpoint?
118 if (!$owner['sprvkey']) {
119 Logger::log(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
120 $owner['name'], $owner['uid']));
124 Logger::log('slapper called for '.$url.'. Data: ' . $slap);
126 // create a magic envelope
128 $data = Strings::base64UrlEncode($slap);
129 $data_type = 'application/atom+xml';
130 $encoding = 'base64url';
131 $algorithm = 'RSA-SHA256';
132 $keyhash = Strings::base64UrlEncode(hash('sha256', self::salmonKey($owner['spubkey'])), true);
134 $precomputed = '.' . Strings::base64UrlEncode($data_type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($algorithm);
137 $signature = Strings::base64UrlEncode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
140 $signature2 = Strings::base64UrlEncode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
142 // Old Status.net format
143 $signature3 = Strings::base64UrlEncode(Crypto::rsaSign($data, $owner['sprvkey']));
145 // At first try the non compliant method that works for GNU Social
146 $xmldata = ["me:env" => ["me:data" => $data,
147 "@attributes" => ["type" => $data_type],
148 "me:encoding" => $encoding,
149 "me:alg" => $algorithm,
150 "me:sig" => $signature,
151 "@attributes2" => ["key_id" => $keyhash]]];
153 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
155 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
158 $postResult = DI::httpRequest()->post($url, $salmon, [
159 'Content-type: application/magic-envelope+xml',
160 'Content-length: ' . strlen($salmon)
163 $return_code = $postResult->getReturnCode();
165 // check for success, e.g. 2xx
167 if ($return_code > 299) {
168 Logger::log('GNU Social salmon failed. Falling back to compliant mode');
170 // Now try the compliant mode that normally isn't used for GNU Social
171 $xmldata = ["me:env" => ["me:data" => $data,
172 "@attributes" => ["type" => $data_type],
173 "me:encoding" => $encoding,
174 "me:alg" => $algorithm,
175 "me:sig" => $signature2,
176 "@attributes2" => ["key_id" => $keyhash]]];
178 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
180 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
183 $postResult = DI::httpRequest()->post($url, $salmon, [
184 'Content-type: application/magic-envelope+xml',
185 'Content-length: ' . strlen($salmon)
187 $return_code = $postResult->getReturnCode();
190 if ($return_code > 299) {
191 Logger::log('compliant salmon failed. Falling back to old status.net');
193 // Last try. This will most likely fail as well.
194 $xmldata = ["me:env" => ["me:data" => $data,
195 "@attributes" => ["type" => $data_type],
196 "me:encoding" => $encoding,
197 "me:alg" => $algorithm,
198 "me:sig" => $signature3,
199 "@attributes2" => ["key_id" => $keyhash]]];
201 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
203 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
206 $postResult = DI::httpRequest()->post($url, $salmon, [
207 'Content-type: application/magic-envelope+xml',
208 'Content-length: ' . strlen($salmon)]);
209 $return_code = $postResult->getReturnCode();
212 Logger::log('slapper for '.$url.' returned ' . $return_code);
214 if (! $return_code) {
218 if (($return_code == 503) && $postResult->inHeader('retry-after')) {
222 return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1;
226 * @param string $pubkey public key
230 public static function salmonKey($pubkey)
232 Crypto::pemToMe($pubkey, $modulus, $exponent);
233 return 'RSA' . '.' . Strings::base64UrlEncode($modulus, true) . '.' . Strings::base64UrlEncode($exponent, true);