3 require_once('include/crypto.php');
4 require_once('include/Probe.php');
6 function get_salmon_key($uri, $keyhash) {
9 logger('Fetching salmon key for '.$uri);
11 $arr = Probe::lrdd($uri);
14 foreach ($arr as $a) {
15 if ($a['@attributes']['rel'] === 'magic-public-key') {
16 $ret[] = $a['@attributes']['href'];
23 // We have found at least one key URL
24 // If it's inline, parse it - otherwise get the key
26 if (count($ret) > 0) {
27 for ($x = 0; $x < count($ret); $x ++) {
28 if (substr($ret[$x], 0, 5) === 'data:') {
29 if (strstr($ret[$x], ',')) {
30 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
32 $ret[$x] = substr($ret[$x], 5);
34 } elseif (normalise_link($ret[$x]) == 'http://') {
35 $ret[$x] = fetch_url($ret[$x]);
41 logger('Key located: ' . print_r($ret, true));
43 if (count($ret) == 1) {
45 // We only found one one key so we don't care if the hash matches.
46 // If it's the wrong key we'll find out soon enough because
47 // message verification will fail. This also covers some older
48 // software which don't supply a keyhash. As long as they only
49 // have one key we'll be right.
53 foreach ($ret as $a) {
54 $hash = base64url_encode(hash('sha256', $a));
55 if ($hash == $keyhash) {
66 function slapper($owner, $url, $slap) {
68 // does contact have a salmon endpoint?
74 if (! $owner['sprvkey']) {
75 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
76 $owner['username'], $owner['uid']));
80 logger('slapper called for '.$url.'. Data: ' . $slap);
82 // create a magic envelope
84 $data = base64url_encode($slap);
85 $data_type = 'application/atom+xml';
86 $encoding = 'base64url';
87 $algorithm = 'RSA-SHA256';
88 $keyhash = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
90 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
93 $signature = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
96 $signature2 = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
98 // Old Status.net format
99 $signature3 = base64url_encode(rsa_sign($data, $owner['sprvkey']));
101 // At first try the non compliant method that works for GNU Social
102 $xmldata = array("me:env" => array("me:data" => $data,
103 "@attributes" => array("type" => $data_type),
104 "me:encoding" => $encoding,
105 "me:alg" => $algorithm,
106 "me:sig" => $signature,
107 "@attributes2" => array("key_id" => $keyhash)));
109 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
111 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
114 post_url($url, $salmon, array(
115 'Content-type: application/magic-envelope+xml',
116 'Content-length: ' . strlen($salmon)
120 $return_code = $a->get_curl_code();
122 // check for success, e.g. 2xx
124 if ($return_code > 299) {
126 logger('GNU Social salmon failed. Falling back to compliant mode');
128 // Now try the compliant mode that normally isn't used for GNU Social
129 $xmldata = array("me:env" => array("me:data" => $data,
130 "@attributes" => array("type" => $data_type),
131 "me:encoding" => $encoding,
132 "me:alg" => $algorithm,
133 "me:sig" => $signature2,
134 "@attributes2" => array("key_id" => $keyhash)));
136 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
138 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
141 post_url($url, $salmon, array(
142 'Content-type: application/magic-envelope+xml',
143 'Content-length: ' . strlen($salmon)
145 $return_code = $a->get_curl_code();
148 if ($return_code > 299) {
149 logger('compliant salmon failed. Falling back to old status.net');
151 // Last try. This will most likely fail as well.
152 $xmldata = array("me:env" => array("me:data" => $data,
153 "@attributes" => array("type" => $data_type),
154 "me:encoding" => $encoding,
155 "me:alg" => $algorithm,
156 "me:sig" => $signature3,
157 "@attributes2" => array("key_id" => $keyhash)));
159 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
161 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
164 post_url($url, $salmon, array(
165 'Content-type: application/magic-envelope+xml',
166 'Content-length: ' . strlen($salmon)
168 $return_code = $a->get_curl_code();
171 logger('slapper for '.$url.' returned ' . $return_code);
172 if (! $return_code) {
175 if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
178 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);