3 use Friendica\Network\Probe;
5 require_once 'include/crypto.php';
7 function get_salmon_key($uri, $keyhash) {
10 logger('Fetching salmon key for '.$uri);
12 $arr = Probe::lrdd($uri);
15 foreach ($arr as $a) {
16 if ($a['@attributes']['rel'] === 'magic-public-key') {
17 $ret[] = $a['@attributes']['href'];
24 // We have found at least one key URL
25 // If it's inline, parse it - otherwise get the key
27 if (count($ret) > 0) {
28 for ($x = 0; $x < count($ret); $x ++) {
29 if (substr($ret[$x], 0, 5) === 'data:') {
30 if (strstr($ret[$x], ',')) {
31 $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
33 $ret[$x] = substr($ret[$x], 5);
35 } elseif (normalise_link($ret[$x]) == 'http://') {
36 $ret[$x] = fetch_url($ret[$x]);
42 logger('Key located: ' . print_r($ret, true));
44 if (count($ret) == 1) {
46 // We only found one one key so we don't care if the hash matches.
47 // If it's the wrong key we'll find out soon enough because
48 // message verification will fail. This also covers some older
49 // software which don't supply a keyhash. As long as they only
50 // have one key we'll be right.
54 foreach ($ret as $a) {
55 $hash = base64url_encode(hash('sha256', $a));
56 if ($hash == $keyhash) {
67 function slapper($owner, $url, $slap) {
69 // does contact have a salmon endpoint?
75 if (! $owner['sprvkey']) {
76 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
77 $owner['username'], $owner['uid']));
81 logger('slapper called for '.$url.'. Data: ' . $slap);
83 // create a magic envelope
85 $data = base64url_encode($slap);
86 $data_type = 'application/atom+xml';
87 $encoding = 'base64url';
88 $algorithm = 'RSA-SHA256';
89 $keyhash = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
91 $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
94 $signature = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
97 $signature2 = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
99 // Old Status.net format
100 $signature3 = base64url_encode(rsa_sign($data, $owner['sprvkey']));
102 // At first try the non compliant method that works for GNU Social
103 $xmldata = array("me:env" => array("me:data" => $data,
104 "@attributes" => array("type" => $data_type),
105 "me:encoding" => $encoding,
106 "me:alg" => $algorithm,
107 "me:sig" => $signature,
108 "@attributes2" => array("key_id" => $keyhash)));
110 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
112 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
115 post_url($url, $salmon, array(
116 'Content-type: application/magic-envelope+xml',
117 'Content-length: ' . strlen($salmon)
121 $return_code = $a->get_curl_code();
123 // check for success, e.g. 2xx
125 if ($return_code > 299) {
127 logger('GNU Social salmon failed. Falling back to compliant mode');
129 // Now try the compliant mode that normally isn't used for GNU Social
130 $xmldata = array("me:env" => array("me:data" => $data,
131 "@attributes" => array("type" => $data_type),
132 "me:encoding" => $encoding,
133 "me:alg" => $algorithm,
134 "me:sig" => $signature2,
135 "@attributes2" => array("key_id" => $keyhash)));
137 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
139 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
142 post_url($url, $salmon, array(
143 'Content-type: application/magic-envelope+xml',
144 'Content-length: ' . strlen($salmon)
146 $return_code = $a->get_curl_code();
149 if ($return_code > 299) {
150 logger('compliant salmon failed. Falling back to old status.net');
152 // Last try. This will most likely fail as well.
153 $xmldata = array("me:env" => array("me:data" => $data,
154 "@attributes" => array("type" => $data_type),
155 "me:encoding" => $encoding,
156 "me:alg" => $algorithm,
157 "me:sig" => $signature3,
158 "@attributes2" => array("key_id" => $keyhash)));
160 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
162 $salmon = xml::from_array($xmldata, $xml, false, $namespaces);
165 post_url($url, $salmon, array(
166 'Content-type: application/magic-envelope+xml',
167 'Content-length: ' . strlen($salmon)
169 $return_code = $a->get_curl_code();
172 logger('slapper for '.$url.' returned ' . $return_code);
174 if (! $return_code) {
178 if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
182 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);