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);
15 if ($a['@attributes']['rel'] === 'magic-public-key') {
16 $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.
55 foreach ($ret as $a) {
56 $hash = base64url_encode(hash('sha256',$a));
57 if ($hash == $keyhash) {
68 function slapper($owner,$url,$slap) {
70 // does contact have a salmon endpoint?
77 if (! $owner['sprvkey']) {
78 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
79 $owner['username'],$owner['uid']));
83 logger('slapper called for '.$url.'. Data: ' . $slap);
85 // create a magic envelope
87 $data = base64url_encode($slap);
88 $data_type = 'application/atom+xml';
89 $encoding = 'base64url';
90 $algorithm = 'RSA-SHA256';
91 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
93 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
95 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
97 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
99 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
101 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
103 $salmon_tpl = get_markup_template('magicsig.tpl');
105 $salmon = replace_macros($salmon_tpl,array(
107 '$encoding' => $encoding,
108 '$algorithm' => $algorithm,
109 '$keyhash' => $keyhash,
110 '$signature' => $signature
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('compliant salmon failed. Falling back to status.net hack2');
128 // Entirely likely that their salmon implementation is
129 // non-compliant. Let's try once more, this time only signing
130 // the data, without stripping '=' chars
132 $salmon = replace_macros($salmon_tpl,array(
134 '$encoding' => $encoding,
135 '$algorithm' => $algorithm,
136 '$keyhash' => $keyhash,
137 '$signature' => $signature2
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) {
150 logger('compliant salmon failed. Falling back to status.net hack3');
152 // Entirely likely that their salmon implementation is
153 // non-compliant. Let's try once more, this time only signing
154 // the data, without the precomputed blob
156 $salmon = replace_macros($salmon_tpl,array(
158 '$encoding' => $encoding,
159 '$algorithm' => $algorithm,
160 '$keyhash' => $keyhash,
161 '$signature' => $signature3
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);
173 if (! $return_code) {
176 if (($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after'))) {
180 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);