3 require_once('include/crypto.php');
7 function get_salmon_key($uri,$keyhash) {
10 logger('Fetching salmon key for '.$uri);
16 if($a['@attributes']['rel'] === 'magic-public-key') {
17 $ret[] = $a['@attributes']['href'];
25 // We have found at least one key URL
26 // If it's inline, parse it - otherwise get the key
29 for($x = 0; $x < count($ret); $x ++) {
30 if(substr($ret[$x],0,5) === 'data:') {
31 if(strstr($ret[$x],','))
32 $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
34 $ret[$x] = substr($ret[$x],5);
37 $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.
56 $hash = base64url_encode(hash('sha256',$a));
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 encoding of data_type, encoding, algorithm concatenated with periods
93 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
95 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
97 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
99 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
101 $salmon_tpl = get_markup_template('magicsig.tpl');
103 $salmon = replace_macros($salmon_tpl,array(
105 '$encoding' => $encoding,
106 '$algorithm' => $algorithm,
107 '$keyhash' => $keyhash,
108 '$signature' => $signature
112 post_url($url,$salmon, array(
113 'Content-type: application/magic-envelope+xml',
114 'Content-length: ' . strlen($salmon)
118 $return_code = $a->get_curl_code();
120 // check for success, e.g. 2xx
122 if($return_code > 299) {
124 logger('compliant salmon failed. Falling back to status.net hack2');
126 // Entirely likely that their salmon implementation is
127 // non-compliant. Let's try once more, this time only signing
128 // the data, without stripping '=' chars
130 $salmon = replace_macros($salmon_tpl,array(
132 '$encoding' => $encoding,
133 '$algorithm' => $algorithm,
134 '$keyhash' => $keyhash,
135 '$signature' => $signature2
139 post_url($url,$salmon, array(
140 'Content-type: application/magic-envelope+xml',
141 'Content-length: ' . strlen($salmon)
143 $return_code = $a->get_curl_code();
146 if($return_code > 299) {
148 logger('compliant salmon failed. Falling back to status.net hack3');
150 // Entirely likely that their salmon implementation is
151 // non-compliant. Let's try once more, this time only signing
152 // the data, without the precomputed blob
154 $salmon = replace_macros($salmon_tpl,array(
156 '$encoding' => $encoding,
157 '$algorithm' => $algorithm,
158 '$keyhash' => $keyhash,
159 '$signature' => $signature3
163 post_url($url,$salmon, array(
164 'Content-type: application/magic-envelope+xml',
165 'Content-length: ' . strlen($salmon)
167 $return_code = $a->get_curl_code();
170 logger('slapper for '.$url.' returned ' . $return_code);
173 if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
176 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);