4 require_once('include/salmon.php');
5 require_once('include/crypto.php');
8 function zot_get($url,$args) {
10 foreach($args as $k => $v) {
13 $argstr .= $k . '=' . $v;
15 $s = fetch_url($url . '?' . $argstr);
24 function zot_post($url,$args) {
25 $s = post_url($url,$args);
35 function zot_prv_encode($s,$prvkey) {
37 $res = openssl_private_encrypt($s,$x,$prvkey);
38 return base64url_encode($y);
40 function zot_pub_encode($s,$pubkey) {
42 $res = openssl_public_encrypt($s,$x,$pubkey);
43 return base64url_encode($x);
46 function zot_prv_decode($s,$prvkey) {
47 $s = base64url_decode($s);
49 openssl_private_decrypt($s,$x,$prvkey);
53 function zot_pub_decode($s,$pubkey) {
54 $s = base64url_decode($s);
56 openssl_public_decrypt($s,$x,$pubkey);
61 function zot_getzid($url,$myaddress,$myprvkey) {
63 $j = zot_get($url,array('sender' => $myaddress));
65 $ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
67 $ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
71 function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
74 $zinit = random_string(32);
76 $j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
79 if(! $a->get_curl_code())
82 logger('zot_post_init: no zinit returned.');
85 if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
86 logger('zot_post_init: incorrect zinit returned.');
91 $s = zot_prv_decode($j->challenge,$myprvkey);
92 $s1 = substr($s,0,strpos($s,'.'));
94 logger("zot_post_init: incorrect zid returned");
97 $ret['result'] = substr($s,strpos($s,'.') + 1);
98 $ret['perms'] = $j->perms;
104 function zot_encrypt_data($data,&$key) {
105 $key = random_string();
106 return aes_encrypt($data,$key);
110 // encrypt the data prior to calling this function so it only need be done once per message
111 // regardless of the number of recipients.
113 function zot_post_data($url,$zid,$myprvkey,$theirpubkey,$encrypted_data,$key, $intro = false) {
114 $i = zot_post_init($url,$zid,$myprvkey,$theirpubkey);
115 if($i === ZCURL_TIMEOUT)
116 return ZCURL_TIMEOUT;
118 if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
120 if((! stristr($i['perms'],'post')) && ($intro === false)) {
121 logger("zot_post_data: no permission to post: url=$url zid=$zid");
126 $p['result'] = zot_pub_encode($i['result'],$theirpubkey);
127 $p['aes_key'] = zot_prv_encode($key,$myprvkey);
128 $p['data'] = $encrypted_data;
129 $s = zot_post($url,$p);
131 if(! $a->get_curl_code())
132 return ZCURL_TIMEOUT;
135 $j = json_decode($s);
141 function zot_deliver($recipients,$myprvkey,$data) {
143 if(is_array($recipients) && count($recipients)) {
146 $encrypted = zot_encrypt_data($data,$key);
149 foreach($recipients as $r) {
150 $result = zot_post_data(
158 if($result === false) {
160 logger('zot_deliver: failed: ' . print_r($r,true));
162 elseif($result === ZCURL_TIMEOUT) {
163 // queue for redelivery
165 elseif($result->error) {
166 // failed at other end
167 logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
169 elseif($result->success) {
170 logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
173 logger('zot_deliver: unknown failure.');
179 function zot_new_contact($user,$cc) {
181 $zid = random_string(32);
182 $zkey = random_string(32);
184 logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));
187 $ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
188 $ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);