-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
337 lines (273 loc) · 9.12 KB
/
utils.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "utils.h"
#include "xz/xz.h"
#define SODIUM_STATIC
#include <sodium.h>
#include <ws2tcpip.h>
#include <time.h>
#include <winsock2.h>
#define close(x) closesocket(x)
static uint32_t inflate(void *dest, void *src, uint32_t dest_size, uint32_t src_len) {
xz_crc32_init();
struct xz_dec *dec = xz_dec_init(XZ_SINGLE, 0);
if (!dec) {
return 0;
}
struct xz_buf buf = {
.in = src,
.in_pos = 0,
.in_size = src_len,
.out = dest,
.out_pos = 0,
.out_size = dest_size,
};
int r = xz_dec_run(dec, &buf);
xz_dec_end(dec);
LOG_TO_FILE("%i\n", r);
/* out_pos is only set on success*/
return buf.out_pos;
}
static void* checksignature(void *data, uint32_t dlen, const uint8_t *self_public_key, size_t *downloaded_len) {
void *mdata;
int r;
mdata = malloc(dlen);
if (!mdata) {
LOG_TO_FILE("malloc failed\n");
free(data);
return NULL;
}
r = crypto_sign_ed25519_open(mdata, (unsigned long long*)downloaded_len, data, dlen, self_public_key);
free(data);
if (r == -1) {
LOG_TO_FILE("invalid signature\n");
free(mdata);
return NULL;
}
LOG_TO_FILE("Signature cleared\n");
return mdata;
}
static void* download( struct sockaddr_storage *sock_addr,
size_t addr_len,
char *request,
uint16_t request_len,
uint32_t *downloaded_length)
{
uint32_t sock, len, rlen, dlen;
char *data = 0;
bool header = 0;
sock = socket(sock_addr->ss_family, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
LOG_TO_FILE("socket failed\n");
return NULL;
}
if (connect(sock, (struct sockaddr *)sock_addr, addr_len) != 0) {
LOG_TO_FILE("connect failed\n");
close(sock);
return NULL;
}
if (send(sock, request, request_len, 0) != request_len) {
LOG_TO_FILE("send failed\n");
close(sock);
return NULL;
}
uint8_t recvbuf[0x10000];
while ((len = recv(sock, (char*)recvbuf, 0xFFFF, 0)) > 0) {
if (!header) {
/* work with a null-terminated buffer */
recvbuf[len] = 0;
/* check for "Not Found" response (todo: only check first line of response)*/
if (strstr((char*)recvbuf, "404 Not Found\r\n")) {
LOG_TO_FILE("Not Found\n");
break;
}
/* find the length field */
char *str = strstr((char*)recvbuf, "Content-Length: ");
if (!str) {
LOG_TO_FILE("invalid HTTP response (1)\n");
break;
}
/* parse the length field */
str += sizeof("Content-Length: ") - 1;
dlen = strtol(str, NULL, 10);
/* find the end of the http response header */
str = strstr(str, "\r\n\r\n");
if (!str) {
LOG_TO_FILE("invalid HTTP response (2)\n");
break;
}
str += sizeof("\r\n\r\n") - 1;
/* allocate buffer to read into) */
data = malloc(dlen);
if (!data) {
LOG_TO_FILE("malloc failed (1) (%u)\n", dlen);
break;
}
LOG_TO_FILE("Download size: %u\n", dlen);
/* read the first piece */
rlen = len - (str - (char*)recvbuf);
memcpy(data, str, rlen);
header = 1;
continue;
}
/* check if received too much */
if (rlen + len > dlen) {
LOG_TO_FILE("bad download\n");
break;
}
memcpy(data + rlen, recvbuf, len);
rlen += len;
set_download_progress((rlen * 100) / dlen);
}
close(sock);
if (!header) {
/* read nothing or invalid header */
LOG_TO_FILE("download() failed\n");
return NULL;
} else if (rlen != dlen) {
LOG_TO_FILE("number of bytes read does not match (%u)\n", rlen);
free(data);
return NULL;
}
*downloaded_length = dlen;
return data;
}
static int generate_request(char *out,
size_t out_len,
const char *host,
size_t host_len,
const char *filename,
size_t filename_len)
{
char host_terminated[host_len + 1];
memcpy(host_terminated, host, host_len);
host_terminated[host_len] = 0;
char filename_terminated[filename_len + 1];
memcpy(filename_terminated, filename, filename_len);
filename_terminated[filename_len] = 0;
int len = snprintf(out, out_len, "GET /%s HTTP/1.0\r\n""Host: %s\r\n\r\n", filename_terminated, host_terminated);
if (len > out_len + 1 || len <= 0) {
return -1;
}
return len;
}
void* download_signed( void *sock_addr,
size_t addr_len,
const char *host,
size_t host_len,
const char *filename,
size_t filename_len,
uint32_t *downloaded_len,
const uint8_t *self_public_key)
{
void *data, *mdata;
uint32_t len, t;
time_t now;
size_t mlen;
char request[512];
int request_len = generate_request(request, sizeof(request), host, host_len, filename, filename_len);
if (request_len == -1)
return NULL;
data = download(sock_addr, addr_len, request, request_len, &len);
if (!data) {
return NULL;
}
mdata = checksignature(data, len, self_public_key, &mlen);
if (!mdata) {
return NULL;
}
time(&now);
memcpy(&t, mdata, 4);
LOG_TO_FILE("signed %u, now %u\n", (uint32_t)t, (uint32_t)now);
if (t < now && now - t >= 60 * 60 * 24 * UPDATE_EXPIRE_DAYS) {
/* build is more than 14 days old: expired */
LOG_TO_FILE("expired signature (%u)\n", (uint32_t)(now - t));
free(mdata);
return NULL;
}
*downloaded_len = mlen;
return mdata;
}
void* download_signed_compressed( void *sock_addr,
size_t addr_len,
const char *host,
size_t host_len,
const char *filename,
size_t filename_len,
uint32_t *downloaded_len,
uint32_t downloaded_len_max,
const uint8_t *self_public_key)
{
char *data, *mdata;
uint32_t len, mlen;
mdata = download_signed(sock_addr, addr_len, host, host_len, filename, filename_len,
&mlen, self_public_key);
if (!mdata) {
LOG_TO_FILE("file download failed\n");
return NULL;
}
/* inflate */
data = malloc(downloaded_len_max);
if (!data) {
LOG_TO_FILE("malloc failed (2) (%u)\n", downloaded_len_max);
free(mdata);
return NULL;
}
len = inflate(data, mdata + 4, downloaded_len_max, mlen - 4);
free(mdata);
if (len == 0) {
LOG_TO_FILE("inflate failed\n");
free(data);
return NULL;
}
*downloaded_len = len;
return data;
}
#define TRY_TIMES 2
void *download_from_host(bool compressed,
const char *host,
const char *filename,
size_t filename_len,
uint32_t *downloaded_len,
const uint8_t *self_public_key)
{
struct addrinfo *root;
if (getaddrinfo(host, "80", NULL, &root) != 0) {
LOG_TO_FILE("getaddrinfo failed for host [%s]\n", host);
return NULL;
}
for (struct addrinfo *info = root; info; info = info->ai_next) {
if (info->ai_socktype && info->ai_socktype != SOCK_STREAM) {
continue;
}
LOG_TO_FILE("Trying host %s\n", host);
void *data = 0;
uint32_t dled_len = 0;
if (compressed) {
data = download_signed_compressed(info->ai_addr,
info->ai_addrlen,
host,
strlen(host),
filename, filename_len,
&dled_len,
UINT32_MAX,
self_public_key);
} else {
data = download_signed(info->ai_addr,
info->ai_addrlen,
host,
strlen(host),
filename,
filename_len,
&dled_len,
self_public_key);
}
if (!data) {
LOG_TO_FILE("data is NULL\n");
continue;
}
*downloaded_len = dled_len;
freeaddrinfo(root);
return data;
}
freeaddrinfo(root);
return NULL;
}