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 return strtr(base64_encode($s),'+/','-_');
25 function base64url_decode($s) {
26 return base64_decode(strtr($s,'-_','+/'));
29 function get_salmon_key($uri,$keyhash) {
32 logger('Fetching salmon key');
38 if($a['@attributes']['rel'] === 'magic-public-key') {
39 $ret[] = $a['@attributes']['href'];
47 // We have found at least one key URL
48 // If it's inline, parse it - otherwise get the key
51 for($x = 0; $x < count($ret); $x ++) {
52 if(substr($ret[$x],0,5) === 'data:') {
53 if(strstr($ret[$x],','))
54 $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
56 $ret[$x] = substr($ret[$x],5);
59 $ret[$x] = fetch_url($ret[$x]);
64 logger('Key located: ' . print_r($ret,true));
66 if(count($ret) == 1) {
68 // We only found one one key so we don't care if the hash matches.
69 // If it's the wrong key we'll find out soon enough because
70 // message verification will fail. This also covers some older
71 // software which don't supply a keyhash. As long as they only
72 // have one key we'll be right.
78 $hash = base64url_encode(hash('sha256',$a));
89 function slapper($owner,$url,$slap) {
91 logger('slapper called. Data: ' . $slap);
93 // does contact have a salmon endpoint?
98 // add all namespaces to item
100 $namespaces = <<< EOT
101 <entry xmlns="http://www.w3.org/2005/Atom"
102 xmlns:thr="http://purl.org/syndication/thread/1.0"
103 xmlns:at="http://purl.org/atompub/tombstones/1.0"
104 xmlns:media="http://purl.org/syndication/atommedia"
105 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
106 xmlns:as="http://activitystrea.ms/spec/1.0/"
107 xmlns:georss="http://www.georss.org/georss"
108 xmlns:poco="http://portablecontacts.net/spec/1.0" >
111 $slap = str_replace('<entry>',$namespaces,$slap);
113 // create a magic envelope
115 $data = base64url_encode($slap);
116 $data_type = 'application/atom+xml';
117 $encoding = 'base64url';
118 $algorithm = 'RSA-SHA256';
119 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])));
121 // Setup RSA stuff to PKCS#1 sign the data
123 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
125 require_once('phpsec/Crypt/RSA.php');
127 $rsa = new CRYPT_RSA();
128 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
129 $rsa->setHash('sha256');
130 $rsa->loadKey($owner['sprvkey']);
132 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
134 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
136 $signature = base64url_encode($rsa->sign($data . $precomputed));
138 $signature2 = base64url_encode($rsa->sign($data));
140 $salmon_tpl = load_view_file('view/magicsig.tpl');
141 $salmon = replace_macros($salmon_tpl,array(
143 '$encoding' => $encoding,
144 '$algorithm' => $algorithm,
145 '$keyhash' => $keyhash,
146 '$signature' => $signature
150 post_url($url,$salmon, array(
151 'Content-type: application/magic-envelope+xml',
152 'Content-length: ' . strlen($salmon)
156 $return_code = trim($a->get_curl_code());
158 // check for success, e.g. 2xx
160 if(substr($return_code,0,1) !== '2') {
162 // Entirely likely that their salmon implementation is
163 // non-compliant. Let's try once more, this time only signing
164 // the data, without the precomputed blob
166 $salmon = replace_macros($salmon_tpl,array(
168 '$encoding' => $encoding,
169 '$algorithm' => $algorithm,
170 '$keyhash' => $keyhash,
171 '$signature' => $signature2
175 post_url($url,$salmon, array(
176 'Content-type: application/magic-envelope+xml',
177 'Content-length: ' . strlen($salmon)
179 $return_code = trim($a->get_curl_code());
182 logger('slapper returned ' . $return_code);