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?
76 if(! $owner['sprvkey']) {
77 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
78 $owner['username'],$owner['uid']));
82 logger('slapper called for '.$url.'. Data: ' . $slap);
84 // create a magic envelope
86 $data = base64url_encode($slap);
87 $data_type = 'application/atom+xml';
88 $encoding = 'base64url';
89 $algorithm = 'RSA-SHA256';
90 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
92 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
94 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
96 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
98 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
100 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
102 $salmon_tpl = get_markup_template('magicsig.tpl');
104 $salmon = replace_macros($salmon_tpl,array(
106 '$encoding' => $encoding,
107 '$algorithm' => $algorithm,
108 '$keyhash' => $keyhash,
109 '$signature' => $signature
113 post_url($url,$salmon, array(
114 'Content-type: application/magic-envelope+xml',
115 'Content-length: ' . strlen($salmon)
119 $return_code = $a->get_curl_code();
121 // check for success, e.g. 2xx
123 if($return_code > 299) {
125 logger('compliant salmon failed. Falling back to status.net hack2');
127 // Entirely likely that their salmon implementation is
128 // non-compliant. Let's try once more, this time only signing
129 // the data, without stripping '=' chars
131 $salmon = replace_macros($salmon_tpl,array(
133 '$encoding' => $encoding,
134 '$algorithm' => $algorithm,
135 '$keyhash' => $keyhash,
136 '$signature' => $signature2
140 post_url($url,$salmon, array(
141 'Content-type: application/magic-envelope+xml',
142 'Content-length: ' . strlen($salmon)
144 $return_code = $a->get_curl_code();
147 if($return_code > 299) {
149 logger('compliant salmon failed. Falling back to status.net hack3');
151 // Entirely likely that their salmon implementation is
152 // non-compliant. Let's try once more, this time only signing
153 // the data, without the precomputed blob
155 $salmon = replace_macros($salmon_tpl,array(
157 '$encoding' => $encoding,
158 '$algorithm' => $algorithm,
159 '$keyhash' => $keyhash,
160 '$signature' => $signature3
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);
174 if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
177 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);