3 require_once('library/asn1.php');
5 function salmon_key($pubkey) {
6 $lines = explode("\n",$pubkey);
8 unset($lines[count($lines)]);
9 $x = base64_decode(implode('',$lines));
11 $r = ASN_BASE::parseASNString($x);
13 $m = $r[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
14 $e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
17 return 'RSA' . '.' . $m . '.' . $e ;
21 function base64url_encode($s) {
22 $s = strtr(base64_encode($s),'+/','-_');
24 * // placeholder for un-padded base64url_encode
25 * // per latest salmon rev
27 * $s = str_replace('=','',$s);
33 function base64url_decode($s) {
36 * // Placeholder for new rev of salmon which strips base64 padding.
37 * // PHP base64_decode handles the un-padded input without requiring this step
38 * // Uncomment if you find you need it.
41 * if(! strpos($s,'=')) {
51 return base64_decode(strtr($s,'-_','+/'));
54 function get_salmon_key($uri,$keyhash) {
57 logger('Fetching salmon key');
63 if($a['@attributes']['rel'] === 'magic-public-key') {
64 $ret[] = $a['@attributes']['href'];
72 // We have found at least one key URL
73 // If it's inline, parse it - otherwise get the key
76 for($x = 0; $x < count($ret); $x ++) {
77 if(substr($ret[$x],0,5) === 'data:') {
78 if(strstr($ret[$x],','))
79 $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
81 $ret[$x] = substr($ret[$x],5);
84 $ret[$x] = fetch_url($ret[$x]);
89 logger('Key located: ' . print_r($ret,true));
91 if(count($ret) == 1) {
93 // We only found one one key so we don't care if the hash matches.
94 // If it's the wrong key we'll find out soon enough because
95 // message verification will fail. This also covers some older
96 // software which don't supply a keyhash. As long as they only
97 // have one key we'll be right.
102 foreach($ret as $a) {
103 $hash = base64url_encode(hash('sha256',$a));
104 if($hash == $keyhash)
114 function slapper($owner,$url,$slap) {
116 logger('slapper called. Data: ' . $slap);
118 // does contact have a salmon endpoint?
123 // add all namespaces to item
125 $namespaces = <<< EOT
126 <entry xmlns="http://www.w3.org/2005/Atom"
127 xmlns:thr="http://purl.org/syndication/thread/1.0"
128 xmlns:at="http://purl.org/atompub/tombstones/1.0"
129 xmlns:media="http://purl.org/syndication/atommedia"
130 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
131 xmlns:as="http://activitystrea.ms/spec/1.0/"
132 xmlns:georss="http://www.georss.org/georss"
133 xmlns:poco="http://portablecontacts.net/spec/1.0" >
136 $slap = str_replace('<entry>',$namespaces,$slap);
138 // create a magic envelope
140 $data = base64url_encode($slap);
141 $data_type = 'application/atom+xml';
142 $encoding = 'base64url';
143 $algorithm = 'RSA-SHA256';
144 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])));
146 // Setup RSA stuff to PKCS#1 sign the data
148 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
150 require_once('phpsec/Crypt/RSA.php');
152 $rsa = new CRYPT_RSA();
153 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
154 $rsa->setHash('sha256');
155 $rsa->loadKey($owner['sprvkey']);
157 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
159 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
161 $signature = base64url_encode($rsa->sign($data . $precomputed));
163 $signature2 = base64url_encode($rsa->sign($data));
165 $salmon_tpl = load_view_file('view/magicsig.tpl');
166 $salmon = replace_macros($salmon_tpl,array(
168 '$encoding' => $encoding,
169 '$algorithm' => $algorithm,
170 '$keyhash' => $keyhash,
171 '$signature' => $signature
175 post_url($url,$salmon, array(
176 'Content-type: application/magic-envelope+xml',
177 'Content-length: ' . strlen($salmon)
181 $return_code = $a->get_curl_code();
183 // check for success, e.g. 2xx
185 if($return_code > 299) {
187 logger('slapper: compliant salmon failed. Falling back to status.net hack');
189 // Entirely likely that their salmon implementation is
190 // non-compliant. Let's try once more, this time only signing
191 // the data, without the precomputed blob
193 $salmon = replace_macros($salmon_tpl,array(
195 '$encoding' => $encoding,
196 '$algorithm' => $algorithm,
197 '$keyhash' => $keyhash,
198 '$signature' => $signature2
202 post_url($url,$salmon, array(
203 'Content-type: application/magic-envelope+xml',
204 'Content-length: ' . strlen($salmon)
206 $return_code = $a->get_curl_code();
209 logger('slapper returned ' . $return_code);
212 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);