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?
76 // add all namespaces to item
79 <entry xmlns="http://www.w3.org/2005/Atom"
80 xmlns:thr="http://purl.org/syndication/thread/1.0"
81 xmlns:at="http://purl.org/atompub/tombstones/1.0"
82 xmlns:media="http://purl.org/syndication/atommedia"
83 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
84 xmlns:as="http://activitystrea.ms/spec/1.0/"
85 xmlns:georss="http://www.georss.org/georss"
86 xmlns:poco="http://portablecontacts.net/spec/1.0"
87 xmlns:ostatus="http://ostatus.org/schema/1.0"
88 xmlns:statusnet="http://status.net/schema/api/1/" > >
91 $slap = str_replace('<entry>',$namespaces,$slap);
93 // create a magic envelope
95 $data = base64url_encode($slap);
96 $data_type = 'application/atom+xml';
97 $encoding = 'base64url';
98 $algorithm = 'RSA-SHA256';
99 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
101 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
103 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
105 $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),true),$owner['sprvkey']);
107 $signature2 = base64url_encode(rsa_sign($data . $precomputed),$owner['sprvkey']);
109 $signature3 = base64url_encode(rsa_sign($data),$owner['sprvkey']);
111 $salmon_tpl = get_markup_template('magicsig.tpl');
113 $salmon = replace_macros($salmon_tpl,array(
115 '$encoding' => $encoding,
116 '$algorithm' => $algorithm,
117 '$keyhash' => $keyhash,
118 '$signature' => $signature
122 post_url($url,$salmon, array(
123 'Content-type: application/magic-envelope+xml',
124 'Content-length: ' . strlen($salmon)
128 $return_code = $a->get_curl_code();
130 // check for success, e.g. 2xx
132 if($return_code > 299) {
134 logger('slapper: compliant salmon failed. Falling back to status.net hack2');
136 // Entirely likely that their salmon implementation is
137 // non-compliant. Let's try once more, this time only signing
138 // the data, without stripping '=' chars
140 $salmon = replace_macros($salmon_tpl,array(
142 '$encoding' => $encoding,
143 '$algorithm' => $algorithm,
144 '$keyhash' => $keyhash,
145 '$signature' => $signature2
149 post_url($url,$salmon, array(
150 'Content-type: application/magic-envelope+xml',
151 'Content-length: ' . strlen($salmon)
153 $return_code = $a->get_curl_code();
156 if($return_code > 299) {
158 logger('slapper: compliant salmon failed. Falling back to status.net hack3');
160 // Entirely likely that their salmon implementation is
161 // non-compliant. Let's try once more, this time only signing
162 // the data, without the precomputed blob
164 $salmon = replace_macros($salmon_tpl,array(
166 '$encoding' => $encoding,
167 '$algorithm' => $algorithm,
168 '$keyhash' => $keyhash,
169 '$signature' => $signature3
173 post_url($url,$salmon, array(
174 'Content-type: application/magic-envelope+xml',
175 'Content-length: ' . strlen($salmon)
177 $return_code = $a->get_curl_code();
180 logger('slapper returned ' . $return_code);
183 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);