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 $debugging = get_config('system','debugging');
34 file_put_contents('salmon.out', "\n" . 'Fetch key' . "\n", FILE_APPEND);
40 if($a['@attributes']['rel'] === 'magic-public-key') {
41 $ret[] = $a['@attributes']['href'];
49 // We have found at least one key URL
50 // If it's inline, parse it - otherwise get the key
53 for($x = 0; $x < count($ret); $x ++) {
54 if(substr($ret[$x],0,5) === 'data:') {
55 if(strstr($ret[$x],','))
56 $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
58 $ret[$x] = substr($ret[$x],5);
61 $ret[$x] = fetch_url($ret[$x]);
66 file_put_contents('salmon.out', "\n" . 'Key located: ' . print_r($ret,true) . "\n", FILE_APPEND);
68 if(count($ret) == 1) {
70 // We only found one one key so we don't care if the hash matches.
71 // If it's the wrong key we'll find out soon enough because
72 // message verification will fail. This also covers some older
73 // software which don't supply a keyhash. As long as they only
74 // have one key we'll be right.
80 $hash = base64url_encode(hash('sha256',$a));
91 function slapper($owner,$contact,$slap) {
94 // does contact have a salmon endpoint?
96 if(! strlen($contact['notify']))
99 // add all namespaces to item
101 $namespaces = <<< EOT
102 <entry xmlns="http://www.w3.org/2005/Atom"
103 xmlns:thr="http://purl.org/syndication/thread/1.0"
104 xmlns:at="http://purl.org/atompub/tombstones/1.0"
105 xmlns:media="http://purl.org/syndication/atommedia"
106 xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
107 xmlns:as="http://activitystrea.ms/spec/1.0/"
108 xmlns:georss="http://www.georss.org/georss"
109 xmlns:poco="http://portablecontacts.net/spec/1.0" >
112 $slap = str_replace('<entry>',$namespaces,$slap);
114 // create a magic envelope
116 $data = base64url_encode($slap);
117 $data_type = 'application/atom+xml';
118 $encoding = 'base64url';
119 $algorithm = 'RSA-SHA256';
120 $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])));
122 // Setup RSA stuff to PKCS#1 sign the data
124 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
126 require_once('phpsec/Crypt/RSA.php');
128 $rsa = new CRYPT_RSA();
129 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
130 $rsa->setHash('sha256');
131 $rsa->loadKey($owner['sprvkey']);
133 // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
135 $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
137 $signature = base64url_encode($rsa->sign($data . $precomputed));
139 $signature2 = base64url_encode($rsa->sign($data));
141 $salmon_tpl = load_view_file('view/magicsig.tpl');
142 $salmon = replace_macros($salmon_tpl,array(
144 '$encoding' => $encoding,
145 '$algorithm' => $algorithm,
146 '$keyhash' => $keyhash,
147 '$signature' => $signature
151 post_url($contact['notify'],$salmon, array(
152 'Content-type: application/magic-envelope+xml',
153 'Content-length: ' . strlen($salmon)
157 $return_code = trim($a->get_curl_code());
159 // check for success, e.g. 2xx
161 if(substr($return_code,0,1) !== '2') {
163 // Entirely likely that their salmon implementation is
164 // non-compliant. Let's try once more, this time only signing
165 // the data, without the precomputed blob
167 $salmon = replace_macros($salmon_tpl,array(
169 '$encoding' => $encoding,
170 '$algorithm' => $algorithm,
171 '$keyhash' => $keyhash,
172 '$signature' => $signature2
176 post_url($contact['notify'],$salmon, array(
177 'Content-type: application/magic-envelope+xml',
178 'Content-length: ' . strlen($salmon)
180 $return_code = trim($a->get_curl_code());