3 * @file src/Protocol/Salmon.php
5 namespace Friendica\Protocol;
7 use Friendica\Network\Probe;
8 use Friendica\Util\Crypto;
9 use Friendica\Util\Network;
10 use Friendica\Util\XML;
13 * @brief Salmon Protocol class
14 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
15 * and annotations made against newsfeed articles such as blog posts.
20 * @param string $uri Uniform Resource Identifier
21 * @param string $keyhash encoded key
24 public static function getKey($uri, $keyhash)
28 logger('Fetching salmon key for '.$uri);
30 $arr = Probe::lrdd($uri);
33 foreach ($arr as $a) {
34 if ($a['@attributes']['rel'] === 'magic-public-key') {
35 $ret[] = $a['@attributes']['href'];
42 // We have found at least one key URL
43 // If it's inline, parse it - otherwise get the key
45 if (count($ret) > 0) {
46 for ($x = 0; $x < count($ret); $x ++) {
47 if (substr($ret[$x], 0, 5) === 'data:') {
48 if (strstr($ret[$x], ',')) {
49 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
51 $ret[$x] = substr($ret[$x], 5);
53 } elseif (normalise_link($ret[$x]) == 'http://') {
54 $ret[$x] = Network::fetchUrl($ret[$x]);
60 logger('Key located: ' . print_r($ret, true));
62 if (count($ret) == 1) {
63 // We only found one one key so we don't care if the hash matches.
64 // If it's the wrong key we'll find out soon enough because
65 // message verification will fail. This also covers some older
66 // software which don't supply a keyhash. As long as they only
67 // have one key we'll be right.
71 foreach ($ret as $a) {
72 $hash = base64url_encode(hash('sha256', $a));
73 if ($hash == $keyhash) {
83 * @param array $owner owner
84 * @param string $url url
85 * @param string $slap slap
88 public static function slapper($owner, $url, $slap)
90 // does contact have a salmon endpoint?
96 if (! $owner['sprvkey']) {
97 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
98 $owner['username'], $owner['uid']));
102 logger('slapper called for '.$url.'. Data: ' . $slap);
104 // create a magic envelope
106 $data = base64url_encode($slap);
107 $data_type = 'application/atom+xml';
108 $encoding = 'base64url';
109 $algorithm = 'RSA-SHA256';
110 $keyhash = base64url_encode(hash('sha256', self::salmonKey($owner['spubkey'])), true);
112 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
115 $signature = base64url_encode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
118 $signature2 = base64url_encode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
120 // Old Status.net format
121 $signature3 = base64url_encode(Crypto::rsaSign($data, $owner['sprvkey']));
123 // At first try the non compliant method that works for GNU Social
124 $xmldata = ["me:env" => ["me:data" => $data,
125 "@attributes" => ["type" => $data_type],
126 "me:encoding" => $encoding,
127 "me:alg" => $algorithm,
128 "me:sig" => $signature,
129 "@attributes2" => ["key_id" => $keyhash]]];
131 $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
133 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
136 Network::post($url, $salmon, [
137 'Content-type: application/magic-envelope+xml',
138 'Content-length: ' . strlen($salmon)
142 $return_code = $a->get_curl_code();
144 // check for success, e.g. 2xx
146 if ($return_code > 299) {
147 logger('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 Network::post($url, $salmon, [
163 'Content-type: application/magic-envelope+xml',
164 'Content-length: ' . strlen($salmon)
166 $return_code = $a->get_curl_code();
169 if ($return_code > 299) {
170 logger('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 Network::post($url, $salmon, [
186 'Content-type: application/magic-envelope+xml',
187 'Content-length: ' . strlen($salmon)]);
188 $return_code = $a->get_curl_code();
191 logger('slapper for '.$url.' returned ' . $return_code);
193 if (! $return_code) {
197 if (($return_code == 503) && (stristr($a->get_curl_headers(), '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);