3 require_once('include/crypto.php');
5 function get_diaspora_key($uri) {
8 logger('Fetching diaspora key for: ' . $uri);
14 if($a['@attributes']['rel'] === 'diaspora-public-key') {
15 $key = base64_decode($a['@attributes']['href']);
24 return rsatopem($key);
29 function diaspora_base_message($type,$data) {
31 $tpl = get_markup_template('diaspora_' . $type . '.tpl');
34 return replace_macros($tpl,$data);
39 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey) {
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);
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);
52 $handle = 'acct:' . $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
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);
57 $b64_data = base64_encode($inner_encrypted);
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';
69 $signable_data = $data . '.' . base64url_encode($type) . "\n" . '.'
70 . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
72 $signature = rsa_sign($signable_data,$prvkey);
73 $sig = base64url_encode($signature);
75 $decrypted_header = <<< EOT
78 <aes_key>$b_inner_aes_key</aes_key>
80 <name>{$contact['name']}</name>
86 $decrypted_header = pkcs5_pad($decrypted_header,16);
88 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
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);
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>';
100 <?xml version='1.0' encoding='UTF-8'?>
101 <entry xmlns='http://www.w3.org/2005/Atom'>
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>
117 function diaspora_decode($importer,$xml) {
119 $basedom = parse_xml_string($xml);
121 $atom = $basedom->children(NAMESPACE_ATOM1);
123 $encrypted_header = json_decode(base64_decode($atom->encrypted_header));
125 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
126 $ciphertext = base64_decode($encrypted_header->ciphertext);
128 $outer_key_bundle = '';
129 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
131 $j_outer_key_bundle = json_decode($outer_key_bundle);
133 $outer_iv = base64_decode($j_outer_key_bundle->iv);
134 $outer_key = base64_decode($j_outer_key_bundle->key);
136 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
138 $decrypted = pkcs5_unpad($decrypted);
141 * $decrypted now contains something like
144 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
145 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
147 * <name>Ryan Hughes</name>
148 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
150 * </decrypted_header>
153 $idom = parse_xml_string($decrypted,false);
155 $inner_iv = base64_decode($idom->iv);
156 $inner_aes_key = base64_decode($idom->aes_key);
158 $author_link = str_replace('acct:','',$idom->author->uri);
160 $dom = $basedom->children(NAMESPACE_SALMON_ME);
162 // figure out where in the DOM tree our data is hiding
164 if($dom->provenance->data)
165 $base = $dom->provenance;
166 elseif($dom->env->data)
172 logger('mod-diaspora: unable to locate salmon data in xml ');
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);
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);
189 // stash away some other stuff for later
191 $type = $base->data[0]->attributes()->type[0];
192 $keyhash = $base->sig[0]->attributes()->keyhash[0];
193 $encoding = $base->encoding;
196 $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
200 $data = base64url_decode($data);
202 // Now pull out the inner encrypted blob
204 $inner_encrypted = base64_decode($data);
207 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
209 $inner_decrypted = pkcs5_unpad($inner_decrypted);
212 logger('mod-diaspora: Could not retrieve author URI.');
213 http_status_exit(400);
216 // Once we have the author URI, go to the web and try to find their public key
217 // *** or look it up locally ***
219 logger('mod-diaspora: Fetching key for ' . $author_link );
221 // Get diaspora public key (pkcs#1) and convert to pkcs#8
222 $key = get_diaspora_key($author_link);
225 logger('mod-diaspora: Could not retrieve author key.');
226 http_status_exit(400);
229 $verify = rsa_verify($signed_data,$signature,$key);
232 logger('mod-diaspora: Message did not verify. Discarding.');
233 http_status_exit(400);
236 logger('mod-diaspora: Message verified.');
238 return $inner_decrypted;
245 function diaspora_request($importer,$contact,$xml) {
247 $sender_handle = $xml->sender_handle;
248 $recipient_handle = $xml->recipient_handle;
250 if(! $sender_handle || ! $recipient_handle)
254 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
255 intval(CONTACT_IS_FRIEND),
256 intval($contact['id']),
257 intval($importer['uid'])
263 require_once('include/Scrape.php');
264 $ret = probe_url($sender_handle);
266 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
267 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
282 function diaspora_post($importer,$contact,$xml) {
286 function diaspora_comment($importer,$contact,$xml) {
290 function diaspora_like($importer,$contact,$xml) {
294 function diaspora_retraction($importer,$contact,$xml) {