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);
34 } elseif (normalise_link($ret[$x]) == 'http://')
35 $ret[$x] = fetch_url($ret[$x]);
40 logger('Key located: ' . print_r($ret,true));
42 if(count($ret) == 1) {
44 // We only found one one key so we don't care if the hash matches.
45 // If it's the wrong key we'll find out soon enough because
46 // message verification will fail. This also covers some older
47 // software which don't supply a keyhash. As long as they only
48 // have one key we'll be right.
54 $hash = base64url_encode(hash('sha256',$a));
65 function slapper($owner,$url,$slap) {
67 // does contact have a salmon endpoint?
73 if(! $owner['sprvkey']) {
74 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
75 $owner['username'],$owner['uid']));
79 logger('slapper called for '.$url.'. Data: ' . $slap);
81 // create a magic envelope
83 $data = base64url_encode($slap);
84 $data_type = 'application/atom+xml';
85 $encoding = 'base64url';
86 $algorithm = 'RSA-SHA256';
87 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
89 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
91 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
93 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
95 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
97 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
99 $salmon_tpl = get_markup_template('magicsig.tpl');
101 $salmon = replace_macros($salmon_tpl,array(
103 '$encoding' => $encoding,
104 '$algorithm' => $algorithm,
105 '$keyhash' => $keyhash,
106 '$signature' => $signature
110 post_url($url,$salmon, array(
111 'Content-type: application/magic-envelope+xml',
112 'Content-length: ' . strlen($salmon)
116 $return_code = $a->get_curl_code();
118 // check for success, e.g. 2xx
120 if($return_code > 299) {
122 logger('compliant salmon failed. Falling back to status.net hack2');
124 // Entirely likely that their salmon implementation is
125 // non-compliant. Let's try once more, this time only signing
126 // the data, without stripping '=' chars
128 $salmon = replace_macros($salmon_tpl,array(
130 '$encoding' => $encoding,
131 '$algorithm' => $algorithm,
132 '$keyhash' => $keyhash,
133 '$signature' => $signature2
137 post_url($url,$salmon, array(
138 'Content-type: application/magic-envelope+xml',
139 'Content-length: ' . strlen($salmon)
141 $return_code = $a->get_curl_code();
144 if($return_code > 299) {
146 logger('compliant salmon failed. Falling back to status.net hack3');
148 // Entirely likely that their salmon implementation is
149 // non-compliant. Let's try once more, this time only signing
150 // the data, without the precomputed blob
152 $salmon = replace_macros($salmon_tpl,array(
154 '$encoding' => $encoding,
155 '$algorithm' => $algorithm,
156 '$keyhash' => $keyhash,
157 '$signature' => $signature3
161 post_url($url,$salmon, array(
162 'Content-type: application/magic-envelope+xml',
163 'Content-length: ' . strlen($salmon)
165 $return_code = $a->get_curl_code();
168 logger('slapper for '.$url.' returned ' . $return_code);
171 if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
174 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);