]> git.mxchange.org Git - friendica.git/blob - include/diaspora.php
diaspora function dispatcher
[friendica.git] / include / diaspora.php
1 <?php
2
3 require_once('include/crypto.php');
4
5 function get_diaspora_key($uri) {
6         $key = '';
7
8         logger('Fetching diaspora key for: ' . $uri);
9
10         $arr = lrdd($uri);
11
12         if(is_array($arr)) {
13                 foreach($arr as $a) {
14                         if($a['@attributes']['rel'] === 'diaspora-public-key') {
15                                 $key = base64_decode($a['@attributes']['href']);
16                         }
17                 }
18         }
19         else {
20                 return '';
21         }
22
23         if($key)
24                 return rsatopem($key);
25         return '';
26 }
27
28
29 function diaspora_base_message($type,$data) {
30
31         $tpl = get_markup_template('diaspora_' . $type . '.tpl');
32         if(! $tpl) 
33                 return '';
34         return replace_macros($tpl,$data);
35
36 }
37
38
39 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey) {
40         $a = get_app();
41
42         $inner_aes_key = random_string(32);
43         $b_inner_aes_key = base64_encode($inner_aes_key);
44         $inner_iv = random_string(32);
45         $b_inner_iv = base64_encode($inner_iv);
46
47         $outer_aes_key = random_string(32);
48         $b_outer_aes_key = base64_encode($outer_aes_key);
49         $outer_iv = random_string(32);
50         $b_outer_iv = base64_encode($outer_iv);
51         
52         $handle = 'acct:' . $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
53
54         $padded_data = pkcs5_pad($msg,16);
55         $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
56
57         $b64_data = base64_encode($inner_encrypted);
58
59
60         $b64url_data = base64url_encode($b64_data);
61         $b64url_stripped = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
62     $lines = str_split($b64url_stripped,60);
63     $data = implode("\n",$lines);
64         $data = $data . (($data[-1] != "\n") ? "\n" : '') ;
65         $type = 'application/atom+xml';
66         $encoding = 'base64url';
67         $alg = 'RSA-SHA256';
68
69         $signable_data = $data  . '.' . base64url_encode($type) . "\n" . '.' 
70                 . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
71
72         $signature = rsa_sign($signable_data,$prvkey);
73         $sig = base64url_encode($signature);
74
75 $decrypted_header = <<< EOT
76 <decrypted_header>
77   <iv>$b_inner_iv</iv>
78   <aes_key>$b_inner_aes_key</aes_key>
79   <author>
80     <name>{$contact['name']}</name>
81     <uri>$handle</uri>
82   </author>
83 </decrypted_header>
84 EOT;
85
86         $decrypted_header = pkcs5_pad($decrypted_header,16);
87
88         $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
89
90         $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
91         $encrypted_outer_key_bundle = '';
92         openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
93         
94         $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
95         $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle), 
96                 'ciphertext' => base64_encode($ciphertext)));
97         $encrypted_header = '<encrypted_header>' . base64_encode($encrypted_header_json_object) . '</encrypted_header>';
98
99 $magic_env = <<< EOT
100 <?xml version='1.0' encoding='UTF-8'?>
101 <entry xmlns='http://www.w3.org/2005/Atom'>
102   $encrypted_header
103   <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
104     <me:encoding>base64url</me:encoding>
105     <me:alg>RSA-SHA256</me:alg>
106     <me:data type="application/atom+xml">$data</me:data>
107     <me:sig>$sig</me:sig>
108   </me:env>
109 </entry>
110 EOT;
111
112         return $magic_env;
113
114 }
115
116
117 function diaspora_decode($importer,$xml) {
118
119         $basedom = parse_xml_string($xml);
120
121         $atom = $basedom->children(NAMESPACE_ATOM1);
122
123         $encrypted_header = json_decode(base64_decode($atom->encrypted_header));
124         
125         $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
126         $ciphertext = base64_decode($encrypted_header->ciphertext);
127
128         $outer_key_bundle = '';
129         openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
130
131         $j_outer_key_bundle = json_decode($outer_key_bundle);
132
133         $outer_iv = base64_decode($j_outer_key_bundle->iv);
134         $outer_key = base64_decode($j_outer_key_bundle->key);
135
136         $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
137
138         $decrypted = pkcs5_unpad($decrypted);
139
140         /**
141          * $decrypted now contains something like
142          *
143          *  <decrypted_header>
144          *     <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
145          *     <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
146          *     <author>
147          *       <name>Ryan Hughes</name>
148          *       <uri>acct:galaxor@diaspora.pirateship.org</uri>
149          *     </author>
150          *  </decrypted_header>
151          */
152
153         $idom = parse_xml_string($decrypted,false);
154
155         $inner_iv = base64_decode($idom->iv);
156         $inner_aes_key = base64_decode($idom->aes_key);
157
158         $author_link = str_replace('acct:','',$idom->author->uri);
159
160         $dom = $basedom->children(NAMESPACE_SALMON_ME);
161
162         // figure out where in the DOM tree our data is hiding
163
164         if($dom->provenance->data)
165                 $base = $dom->provenance;
166         elseif($dom->env->data)
167                 $base = $dom->env;
168         elseif($dom->data)
169                 $base = $dom;
170         
171         if(! $base) {
172                 logger('mod-diaspora: unable to locate salmon data in xml ');
173                 dt_return(400);
174         }
175
176
177         // Stash the signature away for now. We have to find their key or it won't be good for anything.
178         $signature = base64url_decode($base->sig);
179
180         // unpack the  data
181
182         // strip whitespace so our data element will return to one big base64 blob
183         $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
184         // Add back the 60 char linefeeds
185     $lines = str_split($data,60);
186     $data = implode("\n",$lines);
187
188
189         // stash away some other stuff for later
190
191         $type = $base->data[0]->attributes()->type[0];
192         $keyhash = $base->sig[0]->attributes()->keyhash[0];
193         $encoding = $base->encoding;
194         $alg = $base->alg;
195
196         $signed_data = $data  . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
197
198
199         // decode the data
200         $data = base64url_decode($data);
201
202         // Now pull out the inner encrypted blob
203
204         $inner_encrypted = base64_decode($data);
205
206         $inner_decrypted = 
207         $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
208
209         $inner_decrypted = pkcs5_unpad($inner_decrypted);
210
211         if(! $author_link) {
212                 logger('mod-diaspora: Could not retrieve author URI.');
213                 http_status_exit(400);
214         }
215
216         // Once we have the author URI, go to the web and try to find their public key
217         // *** or look it up locally ***
218
219         logger('mod-diaspora: Fetching key for ' . $author_link );
220
221         // Get diaspora public key (pkcs#1) and convert to pkcs#8
222         $key = get_diaspora_key($author_link);
223
224         if(! $key) {
225                 logger('mod-diaspora: Could not retrieve author key.');
226                 http_status_exit(400);
227         }
228
229         $verify = rsa_verify($signed_data,$signature,$key);
230
231         if(! $verify) {
232                 logger('mod-diaspora: Message did not verify. Discarding.');
233                 http_status_exit(400);
234         }
235
236         logger('mod-diaspora: Message verified.');
237
238         return $inner_decrypted;
239
240 }
241
242
243
244
245 function diaspora_request($importer,$contact,$xml) {
246
247 }
248
249 function diaspora_post($importer,$contact,$xml) {
250
251 }
252
253 function diaspora_comment($importer,$contact,$xml) {
254
255 }
256
257 function diaspora_like($importer,$contact,$xml) {
258
259 }
260
261 function diaspora_retraction($importer,$contact,$xml) {
262
263 }
264