3 * @file src/Protocol/Salmon.php
5 namespace Friendica\Protocol;
7 use Friendica\Core\Logger;
8 use Friendica\Network\Probe;
9 use Friendica\Util\Crypto;
10 use Friendica\Util\Network;
11 use Friendica\Util\Strings;
12 use Friendica\Util\XML;
15 * @brief Salmon Protocol class
16 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
17 * and annotations made against newsfeed articles such as blog posts.
22 * @param string $uri Uniform Resource Identifier
23 * @param string $keyhash encoded key
25 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
27 public static function getKey($uri, $keyhash)
31 Logger::log('Fetching salmon key for '.$uri);
33 $arr = Probe::lrdd($uri);
36 foreach ($arr as $a) {
37 if ($a['@attributes']['rel'] === 'magic-public-key') {
38 $ret[] = $a['@attributes']['href'];
45 // We have found at least one key URL
46 // If it's inline, parse it - otherwise get the key
48 if (count($ret) > 0) {
49 for ($x = 0; $x < count($ret); $x ++) {
50 if (substr($ret[$x], 0, 5) === 'data:') {
51 if (strstr($ret[$x], ',')) {
52 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
54 $ret[$x] = substr($ret[$x], 5);
56 } elseif (Strings::normaliseLink($ret[$x]) == 'http://') {
57 $ret[$x] = Network::fetchUrl($ret[$x]);
63 Logger::log('Key located: ' . print_r($ret, true));
65 if (count($ret) == 1) {
66 // We only found one one key so we don't care if the hash matches.
67 // If it's the wrong key we'll find out soon enough because
68 // message verification will fail. This also covers some older
69 // software which don't supply a keyhash. As long as they only
70 // have one key we'll be right.
74 foreach ($ret as $a) {
75 $hash = Strings::base64UrlEncode(hash('sha256', $a));
76 if ($hash == $keyhash) {
86 * @param array $owner owner
87 * @param string $url url
88 * @param string $slap slap
90 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
92 public static function slapper($owner, $url, $slap)
94 // does contact have a salmon endpoint?
100 if (! $owner['sprvkey']) {
101 Logger::log(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
102 $owner['username'], $owner['uid']));
106 Logger::log('slapper called for '.$url.'. Data: ' . $slap);
108 // create a magic envelope
110 $data = Strings::base64UrlEncode($slap);
111 $data_type = 'application/atom+xml';
112 $encoding = 'base64url';
113 $algorithm = 'RSA-SHA256';
114 $keyhash = Strings::base64UrlEncode(hash('sha256', self::salmonKey($owner['spubkey'])), true);
116 $precomputed = '.' . Strings::base64UrlEncode($data_type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($algorithm);
119 $signature = Strings::base64UrlEncode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
122 $signature2 = Strings::base64UrlEncode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
124 // Old Status.net format
125 $signature3 = Strings::base64UrlEncode(Crypto::rsaSign($data, $owner['sprvkey']));
127 // At first try the non compliant method that works for GNU Social
128 $xmldata = ["me:env" => ["me:data" => $data,
129 "@attributes" => ["type" => $data_type],
130 "me:encoding" => $encoding,
131 "me:alg" => $algorithm,
132 "me:sig" => $signature,
133 "@attributes2" => ["key_id" => $keyhash]]];
135 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
137 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
140 $postResult = Network::post($url, $salmon, [
141 'Content-type: application/magic-envelope+xml',
142 'Content-length: ' . strlen($salmon)
145 $return_code = $postResult->getReturnCode();
147 // check for success, e.g. 2xx
149 if ($return_code > 299) {
150 Logger::log('GNU Social salmon failed. Falling back to compliant mode');
152 // Now try the compliant mode that normally isn't used for GNU Social
153 $xmldata = ["me:env" => ["me:data" => $data,
154 "@attributes" => ["type" => $data_type],
155 "me:encoding" => $encoding,
156 "me:alg" => $algorithm,
157 "me:sig" => $signature2,
158 "@attributes2" => ["key_id" => $keyhash]]];
160 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
162 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
165 $postResult = Network::post($url, $salmon, [
166 'Content-type: application/magic-envelope+xml',
167 'Content-length: ' . strlen($salmon)
169 $return_code = $postResult->getReturnCode();
172 if ($return_code > 299) {
173 Logger::log('compliant salmon failed. Falling back to old status.net');
175 // Last try. This will most likely fail as well.
176 $xmldata = ["me:env" => ["me:data" => $data,
177 "@attributes" => ["type" => $data_type],
178 "me:encoding" => $encoding,
179 "me:alg" => $algorithm,
180 "me:sig" => $signature3,
181 "@attributes2" => ["key_id" => $keyhash]]];
183 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
185 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
188 $postResult = Network::post($url, $salmon, [
189 'Content-type: application/magic-envelope+xml',
190 'Content-length: ' . strlen($salmon)]);
191 $return_code = $postResult->getReturnCode();
194 Logger::log('slapper for '.$url.' returned ' . $return_code);
196 if (! $return_code) {
200 if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
204 return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1;
208 * @param string $pubkey public key
212 public static function salmonKey($pubkey)
214 Crypto::pemToMe($pubkey, $m, $e);
215 return 'RSA' . '.' . Strings::base64UrlEncode($m, true) . '.' . Strings::base64UrlEncode($e, true);