]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Merge branch 'master' of git://github.com/friendika/friendika
[friendica.git] / include / salmon.php
1 <?php
2
3 require_once('library/asn1.php');
4
5 function salmon_key($pubkey) {
6         $lines = explode("\n",$pubkey);
7         unset($lines[0]);
8         unset($lines[count($lines)]);
9         $x = base64_decode(implode('',$lines));
10
11         $r = ASN_BASE::parseASNString($x);
12
13         $m = $r[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
14         $e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
15
16
17         return 'RSA' . '.' . $m . '.' . $e ;
18 }
19
20
21 function get_salmon_key($uri,$keyhash) {
22         $ret = array();
23
24         logger('Fetching salmon key');
25
26         $arr = lrdd($uri);
27
28         if(is_array($arr)) {
29                 foreach($arr as $a) {
30                         if($a['@attributes']['rel'] === 'magic-public-key') {
31                                 $ret[] = $a['@attributes']['href'];
32                         }
33                 }
34         }
35         else {
36                 return '';
37         }
38
39         // We have found at least one key URL
40         // If it's inline, parse it - otherwise get the key
41
42         if(count($ret)) {
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);
47                                 else
48                                         $ret[$x] = substr($ret[$x],5);
49                         }
50                         else
51                                 $ret[$x] = fetch_url($ret[$x]);
52                 }
53         }
54
55
56         logger('Key located: ' . print_r($ret,true));
57
58         if(count($ret) == 1) {
59
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. 
65
66                 return $ret[0];
67         }
68         else {
69                 foreach($ret as $a) {
70                         $hash = base64url_encode(hash('sha256',$a));
71                         if($hash == $keyhash)
72                                 return $a;
73                 }
74         }
75
76         return '';
77 }
78
79         
80                 
81 function slapper($owner,$url,$slap) {
82
83         logger('slapper called. Data: ' . $slap);
84
85         // does contact have a salmon endpoint? 
86
87         if(! strlen($url))
88                 return;
89
90         // add all namespaces to item
91
92 $namespaces = <<< EOT
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/" >                                                                                                   >
103 EOT;
104
105         $slap = str_replace('<entry>',$namespaces,$slap);
106         
107         // create a magic envelope
108
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'])),true);
114
115         // Setup RSA stuff to PKCS#1 sign the data
116
117         require_once('library/phpsec/Crypt/RSA.php');
118
119     $rsa = new CRYPT_RSA();
120     $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
121     $rsa->setHash('sha256');
122         $rsa->loadKey($owner['sprvkey']);
123
124         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
125
126         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
127
128         $signature   = base64url_encode($rsa->sign(str_replace('=','',$data . $precomputed),true));
129
130         $signature2  = base64url_encode($rsa->sign($data . $precomputed));
131
132         $signature3  = base64url_encode($rsa->sign($data));
133
134         $salmon_tpl = get_markup_template('magicsig.tpl');
135
136         $salmon = replace_macros($salmon_tpl,array(
137                 '$data'      => $data,
138                 '$encoding'  => $encoding,
139                 '$algorithm' => $algorithm,
140                 '$keyhash'   => $keyhash,
141                 '$signature' => $signature
142         ));
143
144         // slap them 
145         post_url($url,$salmon, array(
146                 'Content-type: application/magic-envelope+xml',
147                 'Content-length: ' . strlen($salmon)
148         ));
149
150         $a = get_app();
151         $return_code = $a->get_curl_code();
152
153         // check for success, e.g. 2xx
154
155         if($return_code > 299) {
156
157                 logger('slapper: compliant salmon failed. Falling back to status.net hack2');
158
159                 // Entirely likely that their salmon implementation is
160                 // non-compliant. Let's try once more, this time only signing
161                 // the data, without stripping '=' chars
162
163                 $salmon = replace_macros($salmon_tpl,array(
164                         '$data'      => $data,
165                         '$encoding'  => $encoding,
166                         '$algorithm' => $algorithm,
167                         '$keyhash'   => $keyhash,
168                         '$signature' => $signature2
169                 ));
170
171                 // slap them 
172                 post_url($url,$salmon, array(
173                         'Content-type: application/magic-envelope+xml',
174                         'Content-length: ' . strlen($salmon)
175                 ));
176                 $return_code = $a->get_curl_code();
177
178
179                 if($return_code > 299) {
180
181                         logger('slapper: compliant salmon failed. Falling back to status.net hack3');
182
183                         // Entirely likely that their salmon implementation is
184                         // non-compliant. Let's try once more, this time only signing
185                         // the data, without the precomputed blob 
186
187                         $salmon = replace_macros($salmon_tpl,array(
188                                 '$data'      => $data,
189                                 '$encoding'  => $encoding,
190                                 '$algorithm' => $algorithm,
191                                 '$keyhash'   => $keyhash,
192                                 '$signature' => $signature3
193                         ));
194
195                         // slap them 
196                         post_url($url,$salmon, array(
197                                 'Content-type: application/magic-envelope+xml',
198                                 'Content-length: ' . strlen($salmon)
199                         ));
200                         $return_code = $a->get_curl_code();
201                 }
202         }
203         logger('slapper returned ' . $return_code); 
204         if(! $return_code)
205                 return(-1);
206         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
207 }
208