3 * @file src/Protocol/Salmon.php
5 namespace Friendica\Protocol;
7 use Friendica\Network\Probe;
8 use Friendica\Util\Crypto;
9 use Friendica\Util\XML;
12 * @brief Salmon Protocol class
13 * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
14 * and annotations made against newsfeed articles such as blog posts.
19 * @param string $uri Uniform Resource Identifier
20 * @param string $keyhash encoded key
23 public static function getKey($uri, $keyhash)
27 logger('Fetching salmon key for '.$uri);
29 $arr = Probe::lrdd($uri);
32 foreach ($arr as $a) {
33 if ($a['@attributes']['rel'] === 'magic-public-key') {
34 $ret[] = $a['@attributes']['href'];
41 // We have found at least one key URL
42 // If it's inline, parse it - otherwise get the key
44 if (count($ret) > 0) {
45 for ($x = 0; $x < count($ret); $x ++) {
46 if (substr($ret[$x], 0, 5) === 'data:') {
47 if (strstr($ret[$x], ',')) {
48 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
50 $ret[$x] = substr($ret[$x], 5);
52 } elseif (normalise_link($ret[$x]) == 'http://') {
53 $ret[$x] = fetch_url($ret[$x]);
59 logger('Key located: ' . print_r($ret, true));
61 if (count($ret) == 1) {
62 // We only found one one key so we don't care if the hash matches.
63 // If it's the wrong key we'll find out soon enough because
64 // message verification will fail. This also covers some older
65 // software which don't supply a keyhash. As long as they only
66 // have one key we'll be right.
70 foreach ($ret as $a) {
71 $hash = base64url_encode(hash('sha256', $a));
72 if ($hash == $keyhash) {
82 * @param array $owner owner
83 * @param string $url url
84 * @param string $slap slap
87 public static function slapper($owner, $url, $slap)
89 // does contact have a salmon endpoint?
95 if (! $owner['sprvkey']) {
96 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
97 $owner['username'], $owner['uid']));
101 logger('slapper called for '.$url.'. Data: ' . $slap);
103 // create a magic envelope
105 $data = base64url_encode($slap);
106 $data_type = 'application/atom+xml';
107 $encoding = 'base64url';
108 $algorithm = 'RSA-SHA256';
109 $keyhash = base64url_encode(hash('sha256', Crypto::salmonKey($owner['spubkey'])), true);
111 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
114 $signature = base64url_encode(Crypto::rsaSign($data . $precomputed, $owner['sprvkey']));
117 $signature2 = base64url_encode(Crypto::rsaSign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
119 // Old Status.net format
120 $signature3 = base64url_encode(Crypto::rsaSign($data, $owner['sprvkey']));
122 // At first try the non compliant method that works for GNU Social
123 $xmldata = array("me:env" => array("me:data" => $data,
124 "@attributes" => array("type" => $data_type),
125 "me:encoding" => $encoding,
126 "me:alg" => $algorithm,
127 "me:sig" => $signature,
128 "@attributes2" => array("key_id" => $keyhash)));
130 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
132 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
135 post_url($url, $salmon, array(
136 'Content-type: application/magic-envelope+xml',
137 'Content-length: ' . strlen($salmon)
141 $return_code = $a->get_curl_code();
143 // check for success, e.g. 2xx
145 if ($return_code > 299) {
146 logger('GNU Social salmon failed. Falling back to compliant mode');
148 // Now try the compliant mode that normally isn't used for GNU Social
149 $xmldata = array("me:env" => array("me:data" => $data,
150 "@attributes" => array("type" => $data_type),
151 "me:encoding" => $encoding,
152 "me:alg" => $algorithm,
153 "me:sig" => $signature2,
154 "@attributes2" => array("key_id" => $keyhash)));
156 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
158 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
161 post_url($url, $salmon, array(
162 'Content-type: application/magic-envelope+xml',
163 'Content-length: ' . strlen($salmon)
165 $return_code = $a->get_curl_code();
168 if ($return_code > 299) {
169 logger('compliant salmon failed. Falling back to old status.net');
171 // Last try. This will most likely fail as well.
172 $xmldata = array("me:env" => array("me:data" => $data,
173 "@attributes" => array("type" => $data_type),
174 "me:encoding" => $encoding,
175 "me:alg" => $algorithm,
176 "me:sig" => $signature3,
177 "@attributes2" => array("key_id" => $keyhash)));
179 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
181 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
184 post_url($url, $salmon, array(
185 'Content-type: application/magic-envelope+xml',
186 '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;