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 get_salmon_key($uri,$keyhash) {
24 logger('Fetching salmon key');
30 if($a['@attributes']['rel'] === 'magic-public-key') {
31 $ret[] = $a['@attributes']['href'];
39 // We have found at least one key URL
40 // If it's inline, parse it - otherwise get the key
43 for($x = 0; $x < count($ret); $x ++) {
44 if(substr($ret[$x],0,5) === 'data:') {
45 if(strstr($ret[$x],','))
46 $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
48 $ret[$x] = substr($ret[$x],5);
51 $ret[$x] = fetch_url($ret[$x]);
56 logger('Key located: ' . print_r($ret,true));
58 if(count($ret) == 1) {
60 // We only found one one key so we don't care if the hash matches.
61 // If it's the wrong key we'll find out soon enough because
62 // message verification will fail. This also covers some older
63 // software which don't supply a keyhash. As long as they only
64 // have one key we'll be right.
70 $hash = base64url_encode(hash('sha256',$a));
81 function slapper($owner,$url,$slap) {
83 logger('slapper called. Data: ' . $slap);
85 // does contact have a salmon endpoint?
90 // add all namespaces to item
93 <entry xmlns="http://www.w3.org/2005/Atom"
94 xmlns:thr="http://purl.org/syndication/thread/1.0"
95 xmlns:at="http://purl.org/atompub/tombstones/1.0"
96 xmlns:media="http://purl.org/syndication/atommedia"
97 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
98 xmlns:as="http://activitystrea.ms/spec/1.0/"
99 xmlns:georss="http://www.georss.org/georss"
100 xmlns:poco="http://portablecontacts.net/spec/1.0"
101 xmlns:ostatus="http://ostatus.org/schema/1.0"
102 xmlns:statusnet="http://status.net/schema/api/1/" > >
105 $slap = str_replace('<entry>',$namespaces,$slap);
107 // create a magic envelope
109 $data = base64url_encode($slap);
110 $data_type = 'application/atom+xml';
111 $encoding = 'base64url';
112 $algorithm = 'RSA-SHA256';
113 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])));
115 // Setup RSA stuff to PKCS#1 sign the data
117 set_include_path(get_include_path() . PATH_SEPARATOR . 'library/phpsec');
119 require_once('library/phpsec/Crypt/RSA.php');
121 $rsa = new CRYPT_RSA();
122 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
123 $rsa->setHash('sha256');
124 $rsa->loadKey($owner['sprvkey']);
126 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
128 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
130 $signature = base64url_encode($rsa->sign($data . $precomputed));
132 $signature2 = base64url_encode($rsa->sign($data));
134 $salmon_tpl = get_markup_template('magicsig.tpl');
135 $salmon = replace_macros($salmon_tpl,array(
137 '$encoding' => $encoding,
138 '$algorithm' => $algorithm,
139 '$keyhash' => $keyhash,
140 '$signature' => $signature
144 post_url($url,$salmon, array(
145 'Content-type: application/magic-envelope+xml',
146 'Content-length: ' . strlen($salmon)
150 $return_code = $a->get_curl_code();
152 // check for success, e.g. 2xx
154 if($return_code > 299) {
156 logger('slapper: compliant salmon failed. Falling back to status.net hack');
158 // Entirely likely that their salmon implementation is
159 // non-compliant. Let's try once more, this time only signing
160 // the data, without the precomputed blob
162 $salmon = replace_macros($salmon_tpl,array(
164 '$encoding' => $encoding,
165 '$algorithm' => $algorithm,
166 '$keyhash' => $keyhash,
167 '$signature' => $signature2
171 post_url($url,$salmon, array(
172 'Content-type: application/magic-envelope+xml',
173 'Content-length: ' . strlen($salmon)
175 $return_code = $a->get_curl_code();
178 logger('slapper returned ' . $return_code);
181 return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);