Skip to content

Latest commit

 

History

History
528 lines (467 loc) · 13.2 KB

ssl.c

File metadata and controls

528 lines (467 loc) · 13.2 KB
 
Sep 22, 2008
Sep 22, 2008
1
/*
Nov 20, 2008
Nov 20, 2008
2
* OpenConnect (SSL + DTLS) VPN client
Sep 22, 2008
Sep 22, 2008
3
*
May 13, 2012
May 13, 2012
4
* Copyright © 2008-2012 Intel Corporation.
Sep 22, 2008
Sep 22, 2008
5
*
Nov 20, 2008
Nov 20, 2008
6
7
8
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
Oct 4, 2008
Oct 4, 2008
9
* modify it under the terms of the GNU Lesser General Public License
Nov 20, 2008
Nov 20, 2008
10
* version 2.1, as published by the Free Software Foundation.
Sep 22, 2008
Sep 22, 2008
11
*
Nov 20, 2008
Nov 20, 2008
12
* This program is distributed in the hope that it will be useful, but
Oct 4, 2008
Oct 4, 2008
13
14
15
16
17
18
19
20
21
22
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
Sep 22, 2008
Sep 22, 2008
23
*/
Jun 1, 2009
Jun 1, 2009
24
25
#include <sys/types.h>
Sep 22, 2008
Sep 22, 2008
26
#include <sys/socket.h>
Jun 17, 2012
Jun 17, 2012
27
#include <sys/stat.h>
Jun 1, 2012
Jun 1, 2012
28
29
#include <netinet/in.h>
#include <arpa/inet.h>
Sep 22, 2008
Sep 22, 2008
30
31
#include <netdb.h>
#include <unistd.h>
Sep 22, 2008
Sep 22, 2008
32
#include <fcntl.h>
Jun 1, 2009
Jun 1, 2009
33
#include <string.h>
Nov 6, 2009
Nov 6, 2009
34
#include <stdio.h>
May 29, 2012
May 29, 2012
35
#include <errno.h>
May 29, 2012
May 29, 2012
36
#include <stdlib.h>
May 31, 2012
May 31, 2012
37
#include <stdarg.h>
Jun 15, 2012
Jun 15, 2012
38
#if defined(__linux__) || defined(ANDROID)
Jun 1, 2009
Jun 1, 2009
39
#include <sys/vfs.h>
Apr 9, 2010
Apr 9, 2010
40
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__APPLE__)
Jun 1, 2009
Jun 1, 2009
41
42
#include <sys/param.h>
#include <sys/mount.h>
Mar 10, 2013
Mar 10, 2013
43
#elif defined(__sun__) || defined(__NetBSD__) || defined(__DragonFly__)
Nov 6, 2009
Nov 6, 2009
44
#include <sys/statvfs.h>
Mar 10, 2013
Mar 10, 2013
45
#elif defined(__GNU__)
Aug 16, 2011
Aug 16, 2011
46
#include <sys/statfs.h>
Jun 1, 2009
Jun 1, 2009
47
#endif
Sep 22, 2008
Sep 22, 2008
48
Mar 9, 2011
Mar 9, 2011
49
#include "openconnect-internal.h"
Sep 22, 2008
Sep 22, 2008
50
Mar 3, 2013
Mar 3, 2013
51
52
53
54
#ifdef ANDROID_KEYSTORE
#include <sys/un.h>
#endif
Feb 23, 2010
Feb 23, 2010
55
56
57
58
59
/* OSX < 1.6 doesn't have AI_NUMERICSERV */
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
May 12, 2012
May 12, 2012
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
static int cancellable_connect(struct openconnect_info *vpninfo, int sockfd,
const struct sockaddr *addr, socklen_t addrlen)
{
struct sockaddr_storage peer;
socklen_t peerlen = sizeof(peer);
fd_set wr_set, rd_set;
int maxfd = sockfd;
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
if (connect(sockfd, addr, addrlen) < 0 && errno != EINPROGRESS)
return -1;
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
FD_SET(sockfd, &wr_set);
if (vpninfo->cancel_fd != -1) {
FD_SET(vpninfo->cancel_fd, &rd_set);
if (vpninfo->cancel_fd > sockfd)
maxfd = vpninfo->cancel_fd;
}
/* Later we'll render this whole exercise non-pointless by
including a 'cancelfd' here too. */
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
if (vpninfo->cancel_fd != -1 && FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
May 18, 2012
May 18, 2012
86
vpn_progress(vpninfo, PRG_ERR, _("Socket connect cancelled\n"));
May 12, 2012
May 12, 2012
87
88
89
90
91
92
93
94
95
errno = EINTR;
return -1;
}
/* Check whether connect() succeeded or failed by using
getpeername(). See http://cr.yp.to/docs/connect.html */
return getpeername(sockfd, (void *)&peer, &peerlen);
}
May 29, 2012
May 29, 2012
96
int connect_https_socket(struct openconnect_info *vpninfo)
Sep 22, 2008
Sep 22, 2008
97
{
Jun 3, 2009
Jun 3, 2009
98
int ssl_sock = -1;
Sep 22, 2008
Sep 22, 2008
99
100
int err;
Dec 23, 2009
Dec 23, 2009
101
102
103
if (!vpninfo->port)
vpninfo->port = 443;
Dec 7, 2009
Dec 7, 2009
104
if (vpninfo->peer_addr) {
May 17, 2012
May 17, 2012
105
106
107
108
109
110
111
112
113
114
115
#ifdef SOCK_CLOEXEC
ssl_sock = socket(vpninfo->peer_addr->sa_family, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_IP);
if (ssl_sock < 0)
#endif
{
ssl_sock = socket(vpninfo->peer_addr->sa_family, SOCK_STREAM, IPPROTO_IP);
if (ssl_sock < 0)
goto reconn_err;
fcntl(ssl_sock, F_SETFD, fcntl(ssl_sock, F_GETFD) | FD_CLOEXEC);
}
if (cancellable_connect(vpninfo, ssl_sock, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
Dec 7, 2009
Dec 7, 2009
116
reconn_err:
Sep 22, 2011
Sep 22, 2011
117
if (vpninfo->proxy) {
Sep 26, 2012
Sep 26, 2012
118
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
119
120
121
_("Failed to reconnect to proxy %s\n"),
vpninfo->proxy);
} else {
Sep 26, 2012
Sep 26, 2012
122
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
123
124
125
_("Failed to reconnect to host %s\n"),
vpninfo->hostname);
}
Sep 26, 2012
Sep 26, 2012
126
127
if (ssl_sock >= 0)
close(ssl_sock);
Dec 7, 2009
Dec 7, 2009
128
return -EINVAL;
Sep 22, 2008
Sep 22, 2008
129
}
Dec 7, 2009
Dec 7, 2009
130
131
} else {
struct addrinfo hints, *result, *rp;
Jan 1, 2010
Jan 1, 2010
132
char *hostname;
Dec 23, 2009
Dec 23, 2009
133
char port[6];
Dec 7, 2009
Dec 7, 2009
134
135
136
137
138
139
140
141
142
143
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
Jan 1, 2010
Jan 1, 2010
144
145
146
147
/* The 'port' variable is a string because it's easier
this way than if we pass NULL to getaddrinfo() and
then try to fill in the numeric value into
different types of returned sockaddr_in{6,}. */
Sep 22, 2011
Sep 22, 2011
148
#ifdef LIBPROXY_HDR
Jan 2, 2010
Jan 2, 2010
149
150
151
152
153
if (vpninfo->proxy_factory) {
char *url;
char **proxies;
int i = 0;
Jan 2, 2010
Jan 2, 2010
154
155
free(vpninfo->proxy_type);
vpninfo->proxy_type = NULL;
Jan 2, 2010
Jan 2, 2010
156
157
158
159
free(vpninfo->proxy);
vpninfo->proxy = NULL;
if (vpninfo->port == 443)
Jan 5, 2010
Jan 5, 2010
160
161
i = asprintf(&url, "https://%s/%s", vpninfo->hostname,
vpninfo->urlpath?:"");
Jan 2, 2010
Jan 2, 2010
162
else
Jan 5, 2010
Jan 5, 2010
163
164
165
166
i = asprintf(&url, "https://%s:%d/%s", vpninfo->hostname,
vpninfo->port, vpninfo->urlpath?:"");
if (i == -1)
return -ENOMEM;
Jan 2, 2010
Jan 2, 2010
167
168
169
170
proxies = px_proxy_factory_get_proxies(vpninfo->proxy_factory,
url);
Dec 3, 2011
Dec 3, 2011
171
i = 0;
Jan 2, 2010
Jan 2, 2010
172
while (proxies && proxies[i]) {
Jan 2, 2010
Jan 2, 2010
173
174
175
176
if (!vpninfo->proxy &&
(!strncmp(proxies[i], "http://", 7) ||
!strncmp(proxies[i], "socks://", 8) ||
!strncmp(proxies[i], "socks5://", 9)))
Mar 9, 2011
Mar 9, 2011
177
internal_parse_url(proxies[i], &vpninfo->proxy_type,
Jan 2, 2010
Jan 2, 2010
178
179
&vpninfo->proxy, &vpninfo->proxy_port,
NULL, 0);
Jan 2, 2010
Jan 2, 2010
180
181
182
183
184
i++;
}
free(url);
free(proxies);
if (vpninfo->proxy)
Sep 22, 2011
Sep 22, 2011
185
186
187
vpn_progress(vpninfo, PRG_TRACE,
_("Proxy from libproxy: %s://%s:%d/\n"),
vpninfo->proxy_type, vpninfo->proxy, vpninfo->port);
Jan 2, 2010
Jan 2, 2010
188
189
}
#endif
Jan 1, 2010
Jan 1, 2010
190
191
if (vpninfo->proxy) {
hostname = vpninfo->proxy;
Feb 22, 2010
Feb 22, 2010
192
snprintf(port, 6, "%d", vpninfo->proxy_port);
Jan 1, 2010
Jan 1, 2010
193
194
} else {
hostname = vpninfo->hostname;
Feb 22, 2010
Feb 22, 2010
195
snprintf(port, 6, "%d", vpninfo->port);
Jan 1, 2010
Jan 1, 2010
196
197
}
Jan 1, 2010
Jan 1, 2010
198
if (hostname[0] == '[' && hostname[strlen(hostname)-1] == ']') {
Jan 24, 2010
Jan 24, 2010
199
200
201
202
/* Solaris has no strndup(). */
int len = strlen(hostname) - 2;
char *new_hostname = malloc(len + 1);
if (!new_hostname)
Jan 1, 2010
Jan 1, 2010
203
return -ENOMEM;
Jan 24, 2010
Jan 24, 2010
204
205
206
207
memcpy(new_hostname, hostname + 1, len);
new_hostname[len] = 0;
hostname = new_hostname;
Jan 1, 2010
Jan 1, 2010
208
209
210
hints.ai_flags |= AI_NUMERICHOST;
}
Jan 1, 2010
Jan 1, 2010
211
err = getaddrinfo(hostname, port, &hints, &result);
Jan 1, 2010
Jan 1, 2010
212
Dec 7, 2009
Dec 7, 2009
213
if (err) {
Sep 22, 2011
Sep 22, 2011
214
215
216
vpn_progress(vpninfo, PRG_ERR,
_("getaddrinfo failed for host '%s': %s\n"),
hostname, gai_strerror(err));
Sep 26, 2012
Sep 26, 2012
217
218
if (hints.ai_flags & AI_NUMERICHOST)
free(hostname);
Dec 7, 2009
Dec 7, 2009
219
220
return -EINVAL;
}
Sep 26, 2012
Sep 26, 2012
221
222
if (hints.ai_flags & AI_NUMERICHOST)
free(hostname);
Dec 7, 2009
Dec 7, 2009
223
224
for (rp = result; rp ; rp = rp->ai_next) {
Jan 1, 2010
Jan 1, 2010
225
226
char host[80];
Feb 4, 2013
Feb 4, 2013
227
host[0] = 0;
Jan 1, 2010
Jan 1, 2010
228
229
if (!getnameinfo(rp->ai_addr, rp->ai_addrlen, host,
sizeof(host), NULL, 0, NI_NUMERICHOST))
Sep 23, 2012
Sep 23, 2012
230
231
232
vpn_progress(vpninfo, PRG_INFO, vpninfo->proxy_type?
_("Attempting to connect to proxy %s%s%s:%s\n"):
_("Attempting to connect to server %s%s%s:%s\n"),
Sep 22, 2011
Sep 22, 2011
233
234
235
236
rp->ai_family == AF_INET6?"[":"",
host,
rp->ai_family == AF_INET6?"]":"",
port);
Sep 23, 2012
Sep 23, 2012
237
Dec 7, 2009
Dec 7, 2009
238
239
240
241
ssl_sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (ssl_sock < 0)
continue;
May 12, 2012
May 12, 2012
242
if (cancellable_connect(vpninfo, ssl_sock, rp->ai_addr, rp->ai_addrlen) >= 0) {
Dec 7, 2009
Dec 7, 2009
243
244
245
246
/* Store the peer address we actually used, so that DTLS can
use it again later */
vpninfo->peer_addr = malloc(rp->ai_addrlen);
if (!vpninfo->peer_addr) {
Sep 22, 2011
Sep 22, 2011
247
248
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate sockaddr storage\n"));
Dec 7, 2009
Dec 7, 2009
249
250
251
252
253
close(ssl_sock);
return -ENOMEM;
}
vpninfo->peer_addrlen = rp->ai_addrlen;
memcpy(vpninfo->peer_addr, rp->ai_addr, rp->ai_addrlen);
Feb 4, 2013
Feb 4, 2013
254
255
256
257
/* If no proxy, and if more than one address for the hostname,
ensure that we output the same IP address in authentication
results (from libopenconnect or --authenticate). */
if (!vpninfo->proxy && (rp != result || rp->ai_next) && host[0]) {
Feb 5, 2013
Feb 5, 2013
258
char *p = malloc(strlen(host) + 3);
Feb 4, 2013
Feb 4, 2013
259
if (p) {
Feb 22, 2013
Feb 22, 2013
260
261
free(vpninfo->unique_hostname);
vpninfo->unique_hostname = p;
Feb 4, 2013
Feb 4, 2013
262
263
264
265
266
267
if (rp->ai_family == AF_INET6)
*p++ = '[';
memcpy(p, host, strlen(host));
p += strlen(host);
if (rp->ai_family == AF_INET6)
*p++ = ']';
Feb 5, 2013
Feb 5, 2013
268
*p = 0;
Feb 4, 2013
Feb 4, 2013
269
270
}
}
Dec 7, 2009
Dec 7, 2009
271
272
273
274
275
276
277
278
break;
}
close(ssl_sock);
ssl_sock = -1;
}
freeaddrinfo(result);
if (ssl_sock < 0) {
Sep 22, 2011
Sep 22, 2011
279
280
281
vpn_progress(vpninfo, PRG_ERR,
_("Failed to connect to host %s\n"),
vpninfo->proxy?:vpninfo->hostname);
Dec 7, 2009
Dec 7, 2009
282
283
return -EINVAL;
}
Sep 22, 2008
Sep 22, 2008
284
285
}
Jan 1, 2010
Jan 1, 2010
286
if (vpninfo->proxy) {
Jan 2, 2010
Jan 2, 2010
287
err = process_proxy(vpninfo, ssl_sock);
Jan 1, 2010
Jan 1, 2010
288
289
290
291
292
293
if (err) {
close(ssl_sock);
return err;
}
}
May 29, 2012
May 29, 2012
294
295
296
return ssl_sock;
}
May 31, 2012
May 31, 2012
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
int __attribute__ ((format (printf, 2, 3)))
openconnect_SSL_printf(struct openconnect_info *vpninfo, const char *fmt, ...)
{
char buf[1024];
va_list args;
buf[1023] = 0;
va_start(args, fmt);
vsnprintf(buf, 1023, fmt, args);
va_end(args);
return openconnect_SSL_write(vpninfo, buf, strlen(buf));
}
Jun 13, 2012
Jun 13, 2012
312
int request_passphrase(struct openconnect_info *vpninfo, const char *label,
Jun 4, 2012
Jun 4, 2012
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
char **response, const char *fmt, ...)
{
struct oc_auth_form f;
struct oc_form_opt o;
char buf[1024];
va_list args;
int ret;
if (!vpninfo->process_auth_form)
return -EINVAL;
buf[1023] = 0;
memset(&f, 0, sizeof(f));
va_start(args, fmt);
vsnprintf(buf, 1023, fmt, args);
va_end(args);
Jun 13, 2012
Jun 13, 2012
330
f.auth_id = (char *)label;
Jun 4, 2012
Jun 4, 2012
331
332
333
334
f.opts = &o;
o.next = NULL;
o.type = OC_FORM_OPT_PASSWORD;
Jun 13, 2012
Jun 13, 2012
335
o.name = (char *)label;
Jun 4, 2012
Jun 4, 2012
336
337
338
o.label = buf;
o.value = NULL;
Jun 8, 2012
Jun 8, 2012
339
ret = vpninfo->process_auth_form(vpninfo->cbdata, &f);
Jun 4, 2012
Jun 4, 2012
340
341
342
343
344
345
346
347
if (!ret) {
*response = o.value;
return 0;
}
return -EIO;
}
May 7, 2010
May 7, 2010
348
#if defined(__sun__) || defined(__NetBSD__) || defined(__DragonFly__)
Nov 16, 2010
Nov 16, 2010
349
int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo)
Nov 3, 2009
Nov 3, 2009
350
351
352
353
354
{
struct statvfs buf;
if (statvfs(vpninfo->sslkey, &buf)) {
int err = errno;
Sep 22, 2011
Sep 22, 2011
355
356
vpn_progress(vpninfo, PRG_ERR, _("statvfs: %s\n"),
strerror(errno));
Nov 3, 2009
Nov 3, 2009
357
358
return -err;
}
Nov 6, 2009
Nov 6, 2009
359
360
if (asprintf(&vpninfo->cert_password, "%lx", buf.f_fsid))
return -ENOMEM;
Nov 3, 2009
Nov 3, 2009
361
362
363
return 0;
}
#else
Nov 16, 2010
Nov 16, 2010
364
int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo)
May 28, 2009
May 28, 2009
365
366
367
368
369
370
371
{
struct statfs buf;
unsigned *fsid = (unsigned *)&buf.f_fsid;
unsigned long long fsid64;
if (statfs(vpninfo->sslkey, &buf)) {
int err = errno;
Sep 22, 2011
Sep 22, 2011
372
373
vpn_progress(vpninfo, PRG_ERR, _("statfs: %s\n"),
strerror(errno));
May 28, 2009
May 28, 2009
374
375
376
return -err;
}
fsid64 = ((unsigned long long)fsid[0] << 32) | fsid[1];
Nov 6, 2009
Nov 6, 2009
377
378
379
if (asprintf(&vpninfo->cert_password, "%llx", fsid64))
return -ENOMEM;
May 28, 2009
May 28, 2009
380
381
return 0;
}
Nov 3, 2009
Nov 3, 2009
382
#endif
Jun 11, 2012
Jun 11, 2012
383
Mar 10, 2013
Mar 10, 2013
384
#if defined(OPENCONNECT_OPENSSL) || defined(DTLS_OPENSSL)
Jun 11, 2012
Jun 11, 2012
385
386
/* We put this here rather than in openssl.c because it might be needed
for OpenSSL DTLS support even when GnuTLS is being used for HTTPS */
Jun 14, 2012
Jun 14, 2012
387
int openconnect_print_err_cb(const char *str, size_t len, void *ptr)
Jun 11, 2012
Jun 11, 2012
388
389
390
391
392
393
394
{
struct openconnect_info *vpninfo = ptr;
vpn_progress(vpninfo, PRG_ERR, "%s", str);
return 0;
}
#endif
Jun 17, 2012
Jun 17, 2012
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#ifdef FAKE_ANDROID_KEYSTORE
char *keystore_strerror(int err)
{
return (char *)strerror(-err);
}
int keystore_fetch(const char *key, unsigned char **result)
{
unsigned char *data;
struct stat st;
int fd;
int ret;
fd = open(key, O_RDONLY);
if (fd < 0)
return -errno;
if (fstat(fd, &st)) {
ret = -errno;
goto out_fd;
}
Jun 19, 2012
Jun 19, 2012
418
data = malloc(st.st_size + 1);
Jun 17, 2012
Jun 17, 2012
419
420
421
422
423
424
425
426
427
428
if (!data) {
ret = -ENOMEM;
goto out_fd;
}
if (read(fd, data, st.st_size) != st.st_size) {
ret = -EIO;
free(data);
goto out_fd;
}
Jun 19, 2012
Jun 19, 2012
429
430
data[st.st_size] = 0;
Jun 17, 2012
Jun 17, 2012
431
432
433
434
435
436
*result = data;
ret = st.st_size;
out_fd:
close(fd);
return ret;
}
Mar 10, 2013
Mar 10, 2013
437
#elif defined(ANDROID_KEYSTORE)
Mar 3, 2013
Mar 3, 2013
438
439
440
441
442
443
444
445
446
447
448
449
450
/* keystore.h isn't in the NDK so we need to define these */
#define NO_ERROR 1
#define LOCKED 2
#define UNINITIALIZED 3
#define SYSTEM_ERROR 4
#define PROTOCOL_ERROR 5
#define PERMISSION_DENIED 6
#define KEY_NOT_FOUND 7
#define VALUE_CORRUPTED 8
#define UNDEFINED_ACTION 9
#define WRONG_PASSWORD 10
const char *keystore_strerror(int err)
Jun 17, 2012
Jun 17, 2012
451
452
453
{
switch (-err) {
case NO_ERROR: return _("No error");
Oct 17, 2012
Oct 17, 2012
454
case LOCKED: return _("Keystore locked");
Jun 17, 2012
Jun 17, 2012
455
456
457
458
459
460
461
case UNINITIALIZED: return _("Keystore uninitialized");
case SYSTEM_ERROR: return _("System error");
case PROTOCOL_ERROR: return _("Protocol error");
case PERMISSION_DENIED: return _("Permission denied");
case KEY_NOT_FOUND: return _("Key not found");
case VALUE_CORRUPTED: return _("Value corrupted");
case UNDEFINED_ACTION: return _("Undefined action");
Mar 3, 2013
Mar 3, 2013
462
463
464
465
case WRONG_PASSWORD:
case WRONG_PASSWORD+1:
case WRONG_PASSWORD+2:
case WRONG_PASSWORD+3: return _("Wrong password");
Jun 17, 2012
Jun 17, 2012
466
467
468
469
470
471
472
473
default: return _("Unknown error");
}
}
/* Returns length, or a negative errno in its own namespace (handled by its
own strerror function above). The numbers are from Android's keystore.h */
int keystore_fetch(const char *key, unsigned char **result)
{
Mar 3, 2013
Mar 3, 2013
474
475
struct sockaddr_un sa = { AF_UNIX, "/dev/socket/keystore" };
socklen_t sl = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path) + 1;
Jun 17, 2012
Jun 17, 2012
476
477
unsigned char *data, *p;
unsigned char buf[3];
Mar 6, 2013
Mar 6, 2013
478
int len, fd;
Jun 17, 2012
Jun 17, 2012
479
480
int ret = -SYSTEM_ERROR;
Mar 3, 2013
Mar 3, 2013
481
fd = socket(AF_UNIX, SOCK_STREAM, 0);
Jun 17, 2012
Jun 17, 2012
482
483
484
if (fd < 0)
return -SYSTEM_ERROR;
Mar 3, 2013
Mar 3, 2013
485
486
487
488
if (connect(fd, (void *)&sa, sl)) {
close(fd);
return -SYSTEM_ERROR;
}
Jun 17, 2012
Jun 17, 2012
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
len = strlen(key);
buf[0] = 'g';
buf[1] = len >> 8;
buf[2] = len & 0xff;
if (send(fd, buf, 3, 0) != 3 || send(fd, key, len, 0) != len ||
shutdown(fd, SHUT_WR) || recv(fd, buf, 1, 0) != 1)
goto out;
if (buf[0] != NO_ERROR) {
/* Should never be zero */
ret = buf[0] ? -buf[0] : -PROTOCOL_ERROR;
goto out;
}
if (recv(fd, buf, 2, 0) != 2)
goto out;
len = (buf[0] << 8) + buf[1];
data = malloc(len);
if (!data)
goto out;
p = data;
ret = len;
while (len) {
int got = recv(fd, p, len, 0);
if (got <= 0) {
free(data);
ret = -PROTOCOL_ERROR;
goto out;
}
len -= got;
p += got;
}
*result = data;
out:
close(fd);
return ret;
}
#endif