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
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);
36 $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.
55 $hash = base64url_encode(hash('sha256',$a));
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 encoding of data_type, encoding, algorithm concatenated with periods
92 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
94 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
96 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
98 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
100 $salmon_tpl = get_markup_template('magicsig.tpl');
102 $salmon = replace_macros($salmon_tpl,array(
104 '$encoding' => $encoding,
105 '$algorithm' => $algorithm,
106 '$keyhash' => $keyhash,
107 '$signature' => $signature
111 post_url($url,$salmon, array(
112 'Content-type: application/magic-envelope+xml',
113 'Content-length: ' . strlen($salmon)
117 $return_code = $a->get_curl_code();
119 // check for success, e.g. 2xx
121 if($return_code > 299) {
123 logger('compliant salmon failed. Falling back to status.net hack2');
125 // Entirely likely that their salmon implementation is
126 // non-compliant. Let's try once more, this time only signing
127 // the data, without stripping '=' chars
129 $salmon = replace_macros($salmon_tpl,array(
131 '$encoding' => $encoding,
132 '$algorithm' => $algorithm,
133 '$keyhash' => $keyhash,
134 '$signature' => $signature2
138 post_url($url,$salmon, array(
139 'Content-type: application/magic-envelope+xml',
140 'Content-length: ' . strlen($salmon)
142 $return_code = $a->get_curl_code();
145 if($return_code > 299) {
147 logger('compliant salmon failed. Falling back to status.net hack3');
149 // Entirely likely that their salmon implementation is
150 // non-compliant. Let's try once more, this time only signing
151 // the data, without the precomputed blob
153 $salmon = replace_macros($salmon_tpl,array(
155 '$encoding' => $encoding,
156 '$algorithm' => $algorithm,
157 '$keyhash' => $keyhash,
158 '$signature' => $signature3
162 post_url($url,$salmon, array(
163 'Content-type: application/magic-envelope+xml',
164 'Content-length: ' . strlen($salmon)
166 $return_code = $a->get_curl_code();
169 logger('slapper for '.$url.' returned ' . $return_code);
172 if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
175 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);