3 * @file include/salmon.php
5 use Friendica\Network\Probe;
6 use Friendica\Util\XML;
8 require_once 'include/crypto.php';
10 function get_salmon_key($uri, $keyhash)
14 logger('Fetching salmon key for '.$uri);
16 $arr = Probe::lrdd($uri);
19 foreach ($arr as $a) {
20 if ($a['@attributes']['rel'] === 'magic-public-key') {
21 $ret[] = $a['@attributes']['href'];
28 // We have found at least one key URL
29 // If it's inline, parse it - otherwise get the key
31 if (count($ret) > 0) {
32 for ($x = 0; $x < count($ret); $x ++) {
33 if (substr($ret[$x], 0, 5) === 'data:') {
34 if (strstr($ret[$x], ',')) {
35 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
37 $ret[$x] = substr($ret[$x], 5);
39 } elseif (normalise_link($ret[$x]) == 'http://') {
40 $ret[$x] = fetch_url($ret[$x]);
46 logger('Key located: ' . print_r($ret, true));
48 if (count($ret) == 1) {
49 // We only found one one key so we don't care if the hash matches.
50 // If it's the wrong key we'll find out soon enough because
51 // message verification will fail. This also covers some older
52 // software which don't supply a keyhash. As long as they only
53 // have one key we'll be right.
57 foreach ($ret as $a) {
58 $hash = base64url_encode(hash('sha256', $a));
59 if ($hash == $keyhash) {
70 function slapper($owner, $url, $slap)
72 // does contact have a salmon endpoint?
78 if (! $owner['sprvkey']) {
79 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
80 $owner['username'], $owner['uid']));
84 logger('slapper called for '.$url.'. Data: ' . $slap);
86 // create a magic envelope
88 $data = base64url_encode($slap);
89 $data_type = 'application/atom+xml';
90 $encoding = 'base64url';
91 $algorithm = 'RSA-SHA256';
92 $keyhash = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
94 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
97 $signature = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
100 $signature2 = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
102 // Old Status.net format
103 $signature3 = base64url_encode(rsa_sign($data, $owner['sprvkey']));
105 // At first try the non compliant method that works for GNU Social
106 $xmldata = array("me:env" => array("me:data" => $data,
107 "@attributes" => array("type" => $data_type),
108 "me:encoding" => $encoding,
109 "me:alg" => $algorithm,
110 "me:sig" => $signature,
111 "@attributes2" => array("key_id" => $keyhash)));
113 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
115 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
118 post_url($url, $salmon, array(
119 'Content-type: application/magic-envelope+xml',
120 'Content-length: ' . strlen($salmon)
124 $return_code = $a->get_curl_code();
126 // check for success, e.g. 2xx
128 if ($return_code > 299) {
129 logger('GNU Social salmon failed. Falling back to compliant mode');
131 // Now try the compliant mode that normally isn't used for GNU Social
132 $xmldata = array("me:env" => array("me:data" => $data,
133 "@attributes" => array("type" => $data_type),
134 "me:encoding" => $encoding,
135 "me:alg" => $algorithm,
136 "me:sig" => $signature2,
137 "@attributes2" => array("key_id" => $keyhash)));
139 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
141 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
144 post_url($url, $salmon, array(
145 'Content-type: application/magic-envelope+xml',
146 'Content-length: ' . strlen($salmon)
148 $return_code = $a->get_curl_code();
151 if ($return_code > 299) {
152 logger('compliant salmon failed. Falling back to old status.net');
154 // Last try. This will most likely fail as well.
155 $xmldata = array("me:env" => array("me:data" => $data,
156 "@attributes" => array("type" => $data_type),
157 "me:encoding" => $encoding,
158 "me:alg" => $algorithm,
159 "me:sig" => $signature3,
160 "@attributes2" => array("key_id" => $keyhash)));
162 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
164 $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
167 post_url($url, $salmon, array(
168 'Content-type: application/magic-envelope+xml',
169 'Content-length: ' . strlen($salmon))
171 $return_code = $a->get_curl_code();
174 logger('slapper for '.$url.' returned ' . $return_code);
176 if (! $return_code) {
180 if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
184 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);