3 require_once('include/crypto.php');
7 function get_salmon_key($uri,$keyhash) {
10 logger('Fetching salmon key');
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 logger('slapper called. Data: ' . $slap);
71 // does contact have a salmon endpoint?
77 if(! $owner['sprvkey']) {
78 logger(sprintf("slapper: user '%s' (%d) does not have a salmon private key. Send failed.",
79 $owner['username'],$owner['uid']));
83 // add all namespaces to item
86 <entry xmlns="http://www.w3.org/2005/Atom"
87 xmlns:thr="http://purl.org/syndication/thread/1.0"
88 xmlns:at="http://purl.org/atompub/tombstones/1.0"
89 xmlns:media="http://purl.org/syndication/atommedia"
90 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
91 xmlns:as="http://activitystrea.ms/spec/1.0/"
92 xmlns:georss="http://www.georss.org/georss"
93 xmlns:poco="http://portablecontacts.net/spec/1.0"
94 xmlns:ostatus="http://ostatus.org/schema/1.0"
95 xmlns:statusnet="http://status.net/schema/api/1/" > >
98 $slap = str_replace('<entry>',$namespaces,$slap);
100 // create a magic envelope
102 $data = base64url_encode($slap);
103 $data_type = 'application/atom+xml';
104 $encoding = 'base64url';
105 $algorithm = 'RSA-SHA256';
106 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
108 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
110 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
112 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
114 $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
116 $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
118 $salmon_tpl = get_markup_template('magicsig.tpl');
120 $salmon = replace_macros($salmon_tpl,array(
122 '$encoding' => $encoding,
123 '$algorithm' => $algorithm,
124 '$keyhash' => $keyhash,
125 '$signature' => $signature
129 post_url($url,$salmon, array(
130 'Content-type: application/magic-envelope+xml',
131 'Content-length: ' . strlen($salmon)
135 $return_code = $a->get_curl_code();
137 // check for success, e.g. 2xx
139 if($return_code > 299) {
141 logger('slapper: compliant salmon failed. Falling back to status.net hack2');
143 // Entirely likely that their salmon implementation is
144 // non-compliant. Let's try once more, this time only signing
145 // the data, without stripping '=' chars
147 $salmon = replace_macros($salmon_tpl,array(
149 '$encoding' => $encoding,
150 '$algorithm' => $algorithm,
151 '$keyhash' => $keyhash,
152 '$signature' => $signature2
156 post_url($url,$salmon, array(
157 'Content-type: application/magic-envelope+xml',
158 'Content-length: ' . strlen($salmon)
160 $return_code = $a->get_curl_code();
163 if($return_code > 299) {
165 logger('slapper: compliant salmon failed. Falling back to status.net hack3');
167 // Entirely likely that their salmon implementation is
168 // non-compliant. Let's try once more, this time only signing
169 // the data, without the precomputed blob
171 $salmon = replace_macros($salmon_tpl,array(
173 '$encoding' => $encoding,
174 '$algorithm' => $algorithm,
175 '$keyhash' => $keyhash,
176 '$signature' => $signature3
180 post_url($url,$salmon, array(
181 'Content-type: application/magic-envelope+xml',
182 'Content-length: ' . strlen($salmon)
184 $return_code = $a->get_curl_code();
187 logger('slapper returned ' . $return_code);
190 if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
193 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);