aboutsummaryrefslogtreecommitdiffstats
path: root/include/zotfns.php
blob: a95a064cd11e9d1fa0858db5dcdee99c9082246a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

require_once('include/text.php');

function zot_get($url,$args) {
	$argstr = '';
	foreach($args as $k => $v) {
		if($argstr)
			$argstr .= '&';
		$argstr .= $k . '=' . $v;
	}	
	$s = fetch_url($url . '?' . $argstr);
	if($s) {
		$j = json_decode($s);
		if($j)
			return($j);
	}
	return false;
}

function zot_post($url,$args) {
	$s = post_url($url,$args);
	if($s) {
		$j = json_decode($s);
		if($j)
			return($j);
	}
	return false;
}


function zot_prv_encode($s,$prvkey) {
	$x = '';
	$res = openssl_private_encrypt($s,$x,$prvkey);
	return base64url_encode($y);
}
function zot_pub_encode($s,$pubkey) {
	$x = '';
	$res = openssl_public_encrypt($s,$x,$pubkey);
	return base64url_encode($x);
}

function zot_prv_decode($s,$prvkey) {
	$s = base64url_decode($s);
	$x = '';
	openssl_private_decrypt($s,$x,$prvkey);
	return $x;
}

function zot_pub_decode($s,$pubkey) {
	$s = base64url_decode($s);
	$x = '';
	openssl_public_decrypt($s,$x,$pubkey);
	return $x;
}


function zot_getzid($url,$myaddress,$myprvkey) {
	$ret = array();
	$j = zot_get($url,array('sender' => $myaddress));
	if($j->zid_encoded)
		$ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
	if($j->zkey_encoded)
		$ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
	return $ret;
}

function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
	$ret = array();

	$zinit = random_string(32);

	$j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
	
	$a = get_app();
	if(! $a->get_curl_code())
		return ZCURL_TIMEOUT;
	if(! $j->zinit) {
		logger('zot_post_init: no zinit returned.');
		return false;
	}
	if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
		logger('zot_post_init: incorrect zinit returned.');
		return false;
	}

	if($j->challenge) {
		$s = zot_prv_decode($j->challenge,$myprvkey);
		$s1 = substr($s,0,strpos($s,'.'));
		if($s1 != $zid) {
			logger("zot_post_init: incorrect zid returned");
			return false;
		}
		$ret['result'] = substr($s,strpos($s,'.') + 1);
		$ret['perms'] = $j->perms;
	}
	return $ret;
}


function zot_encrypt_data($data,&$key) {
	$key = random_string();
	return aes_encrypt($data,$key);
}


// encrypt the data prior to calling this function so it only need be done once per message
// regardless of the number of recipients.

function zot_post_data($url,$zid,$myprvkey,$theirpubkey,$encrypted_data,$key, $intro = false) {
	$i = zot_post_init($url,$zid,$myprvkey,$theirpubkey);
	if($i === ZCURL_TIMEOUT)
		return ZCURL_TIMEOUT;

	if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
		return false;
	if((! stristr($i['perms'],'post')) && ($intro === false)) {
		logger("zot_post_data: no permission to post: url=$url zid=$zid");
		return false;
	} 
	$p = array();
	$p['zid'] = $zid;
	$p['result'] = zot_pub_encode($i['result'],$theirpubkey);
	$p['aes_key'] = zot_prv_encode($key,$myprvkey);
	$p['data'] = $encrypted_data;
	$s = zot_post($url,$p);
	$a = get_app();
	if(! $a->get_curl_code())
		return ZCURL_TIMEOUT;

	if($s) {
		$j = json_decode($s); 
		return $j;
	}
	return false;
}
	
function zot_deliver($recipients,$myprvkey,$data) {

	if(is_array($recipients) && count($recipients)) {

		$key = '';
		$encrypted = zot_encrypt_data($data,$key);


		foreach($recipients as $r) {
			$result = zot_post_data(
				$r['post'],
				$r['zid'],
				$myprvkey,
				$r['pubkey'],
				$encrypted,
				$key
			);
			if($result === false) {
				// post failed
				logger('zot_deliver: failed: ' . print_r($r,true));
			}
			elseif($result === ZCURL_TIMEOUT) {
				// queue for redelivery
			}
			elseif($result->error) {
				// failed at other end
				logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
			}
			elseif($result->success) {
				logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
			}
			else
				logger('zot_deliver: unknown failure.');
		}
	}
}


function zot_new_contact($user,$cc) {

	$zid = random_string(32);
	$zkey = random_string(32);

	logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));

	$ret = array();
	$ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
	$ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);
	return $ret;


	


}