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\XML;
14 * @brief Salmon Protocol class
15 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
16 * and annotations made against newsfeed articles such as blog posts.
21 * @param string $uri Uniform Resource Identifier
22 * @param string $keyhash encoded key
25 public static function getKey($uri, $keyhash)
29 Logger::log('Fetching salmon key for '.$uri);
31 $arr = Probe::lrdd($uri);
34 foreach ($arr as $a) {
35 if ($a['@attributes']['rel'] === 'magic-public-key') {
36 $ret[] = $a['@attributes']['href'];
43 // We have found at least one key URL
44 // If it's inline, parse it - otherwise get the key
46 if (count($ret) > 0) {
47 for ($x = 0; $x < count($ret); $x ++) {
48 if (substr($ret[$x], 0, 5) === 'data:') {
49 if (strstr($ret[$x], ',')) {
50 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
52 $ret[$x] = substr($ret[$x], 5);
54 } elseif (normalise_link($ret[$x]) == 'http://') {
55 $ret[$x] = Network::fetchUrl($ret[$x]);
61 Logger::log('Key located: ' . print_r($ret, true));
63 if (count($ret) == 1) {
64 // We only found one one key so we don't care if the hash matches.
65 // If it's the wrong key we'll find out soon enough because
66 // message verification will fail. This also covers some older
67 // software which don't supply a keyhash. As long as they only
68 // have one key we'll be right.
72 foreach ($ret as $a) {
73 $hash = base64url_encode(hash('sha256', $a));
74 if ($hash == $keyhash) {
84 * @param array $owner owner
85 * @param string $url url
86 * @param string $slap slap
89 public static function slapper($owner, $url, $slap)
91 // does contact have a salmon endpoint?
97 if (! $owner['sprvkey']) {
98 Logger::log(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
99 $owner['username'], $owner['uid']));
103 Logger::log('slapper called for '.$url.'. Data: ' . $slap);
105 // create a magic envelope
107 $data = base64url_encode($slap);
108 $data_type = 'application/atom+xml';
109 $encoding = 'base64url';
110 $algorithm = 'RSA-SHA256';
111 $keyhash = base64url_encode(hash('sha256', self::salmonKey($owner['spubkey'])), true);
113 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
116 $signature = base64url_encode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
119 $signature2 = base64url_encode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
121 // Old Status.net format
122 $signature3 = base64url_encode(Crypto::rsaSign($data, $owner['sprvkey']));
124 // At first try the non compliant method that works for GNU Social
125 $xmldata = ["me:env" => ["me:data" => $data,
126 "@attributes" => ["type" => $data_type],
127 "me:encoding" => $encoding,
128 "me:alg" => $algorithm,
129 "me:sig" => $signature,
130 "@attributes2" => ["key_id" => $keyhash]]];
132 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
134 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
137 $postResult = Network::post($url, $salmon, [
138 'Content-type: application/magic-envelope+xml',
139 'Content-length: ' . strlen($salmon)
142 $return_code = $postResult->getReturnCode();
144 // check for success, e.g. 2xx
146 if ($return_code > 299) {
147 Logger::log('GNU Social salmon failed. Falling back to compliant mode');
149 // Now try the compliant mode that normally isn't used for GNU Social
150 $xmldata = ["me:env" => ["me:data" => $data,
151 "@attributes" => ["type" => $data_type],
152 "me:encoding" => $encoding,
153 "me:alg" => $algorithm,
154 "me:sig" => $signature2,
155 "@attributes2" => ["key_id" => $keyhash]]];
157 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
159 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
162 $postResult = Network::post($url, $salmon, [
163 'Content-type: application/magic-envelope+xml',
164 'Content-length: ' . strlen($salmon)
166 $return_code = $postResult->getReturnCode();
169 if ($return_code > 299) {
170 Logger::log('compliant salmon failed. Falling back to old status.net');
172 // Last try. This will most likely fail as well.
173 $xmldata = ["me:env" => ["me:data" => $data,
174 "@attributes" => ["type" => $data_type],
175 "me:encoding" => $encoding,
176 "me:alg" => $algorithm,
177 "me:sig" => $signature3,
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 = Network::post($url, $salmon, [
186 'Content-type: application/magic-envelope+xml',
187 'Content-length: ' . strlen($salmon)]);
188 $return_code = $postResult->getReturnCode();
191 Logger::log('slapper for '.$url.' returned ' . $return_code);
193 if (! $return_code) {
197 if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
201 return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1;
205 * @param string $pubkey public key
208 public static function salmonKey($pubkey)
210 Crypto::pemToMe($pubkey, $m, $e);
211 return 'RSA' . '.' . base64url_encode($m, true) . '.' . base64url_encode($e, true);