Skip to content

Latest commit

 

History

History
1508 lines (1307 loc) · 38.2 KB

openssl.c

File metadata and controls

1508 lines (1307 loc) · 38.2 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2012 Intel Corporation.
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* 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
*/
May 29, 2012
May 29, 2012
25
#include <errno.h>
Jun 11, 2012
Jun 11, 2012
26
#include <sys/types.h>
May 29, 2012
May 29, 2012
27
28
#include <netinet/in.h>
#include <arpa/inet.h>
Jun 1, 2012
Jun 1, 2012
29
#include <ctype.h>
May 29, 2012
May 29, 2012
30
May 29, 2012
May 29, 2012
31
32
33
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/engine.h>
34
#include <openssl/evp.h>
May 29, 2012
May 29, 2012
35
#include <openssl/rand.h>
May 29, 2012
May 29, 2012
36
37
38
39
#include <openssl/pkcs12.h>
#include <openssl/x509v3.h>
#include <openssl/x509.h>
#include <openssl/bio.h>
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "openconnect-internal.h"
int openconnect_sha1(unsigned char *result, void *data, int len)
{
EVP_MD_CTX c;
EVP_MD_CTX_init(&c);
EVP_Digest(data, len, result, NULL, EVP_sha1(), NULL);
EVP_MD_CTX_cleanup(&c);
return 0;
}
May 29, 2012
May 29, 2012
53
54
int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
Jun 8, 2012
Jun 8, 2012
55
OPENCONNECT_X509 *cert, unsigned char **buf)
May 29, 2012
May 29, 2012
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
BIO *bp = BIO_new(BIO_s_mem());
BUF_MEM *certinfo;
size_t l;
if (!i2d_X509_bio(bp, cert)) {
BIO_free(bp);
return -EIO;
}
BIO_get_mem_ptr(bp, &certinfo);
l = certinfo->length;
*buf = malloc(l);
if (!*buf) {
BIO_free(bp);
return -ENOMEM;
}
memcpy(*buf, certinfo->data, l);
BIO_free(bp);
return l;
}
May 29, 2012
May 29, 2012
77
78
79
80
81
82
83
int openconnect_random(void *bytes, int len)
{
if (RAND_bytes(bytes, len) != 1)
return -EIO;
return 0;
}
May 29, 2012
May 29, 2012
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
/* Helper functions for reading/writing lines over SSL.
We could use cURL for the HTTP stuff, but it's overkill */
int openconnect_SSL_write(struct openconnect_info *vpninfo, char *buf, size_t len)
{
size_t orig_len = len;
while (len) {
int done = SSL_write(vpninfo->https_ssl, buf, len);
if (done > 0)
len -= done;
else {
int err = SSL_get_error(vpninfo->https_ssl, done);
fd_set wr_set, rd_set;
int maxfd = vpninfo->ssl_fd;
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
if (err == SSL_ERROR_WANT_READ)
FD_SET(vpninfo->ssl_fd, &rd_set);
else if (err == SSL_ERROR_WANT_WRITE)
FD_SET(vpninfo->ssl_fd, &wr_set);
else {
vpn_progress(vpninfo, PRG_ERR, _("Failed to write to SSL socket"));
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
if (vpninfo->cancel_fd != -1) {
FD_SET(vpninfo->cancel_fd, &rd_set);
if (vpninfo->cancel_fd > vpninfo->ssl_fd)
maxfd = vpninfo->cancel_fd;
}
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
if (vpninfo->cancel_fd != -1 &&
FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL write cancelled\n"));
return -EINTR;
}
}
}
return orig_len;
}
int openconnect_SSL_read(struct openconnect_info *vpninfo, char *buf, size_t len)
{
int done;
while ((done = SSL_read(vpninfo->https_ssl, buf, len)) == -1) {
int err = SSL_get_error(vpninfo->https_ssl, done);
fd_set wr_set, rd_set;
int maxfd = vpninfo->ssl_fd;
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
if (err == SSL_ERROR_WANT_READ)
FD_SET(vpninfo->ssl_fd, &rd_set);
else if (err == SSL_ERROR_WANT_WRITE)
FD_SET(vpninfo->ssl_fd, &wr_set);
else {
vpn_progress(vpninfo, PRG_ERR, _("Failed to read from SSL socket"));
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
if (vpninfo->cancel_fd != -1) {
FD_SET(vpninfo->cancel_fd, &rd_set);
if (vpninfo->cancel_fd > vpninfo->ssl_fd)
maxfd = vpninfo->cancel_fd;
}
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
if (vpninfo->cancel_fd != -1 &&
FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
return -EINTR;
}
}
return done;
}
int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len)
{
int i = 0;
int ret;
if (len < 2)
return -EINVAL;
while (1) {
ret = SSL_read(vpninfo->https_ssl, buf + i, 1);
if (ret == 1) {
if (buf[i] == '\n') {
buf[i] = 0;
if (i && buf[i-1] == '\r') {
buf[i-1] = 0;
i--;
}
return i;
}
i++;
if (i >= len - 1) {
buf[i] = 0;
return i;
}
} else {
fd_set rd_set, wr_set;
int maxfd = vpninfo->ssl_fd;
FD_ZERO(&rd_set);
FD_ZERO(&wr_set);
ret = SSL_get_error(vpninfo->https_ssl, ret);
if (ret == SSL_ERROR_WANT_READ)
FD_SET(vpninfo->ssl_fd, &rd_set);
else if (ret == SSL_ERROR_WANT_WRITE)
FD_SET(vpninfo->ssl_fd, &wr_set);
else {
vpn_progress(vpninfo, PRG_ERR, _("Failed to read from SSL socket\n"));
openconnect_report_ssl_errors(vpninfo);
ret = -EIO;
break;
}
if (vpninfo->cancel_fd != -1) {
FD_SET(vpninfo->cancel_fd, &rd_set);
if (vpninfo->cancel_fd > vpninfo->ssl_fd)
maxfd = vpninfo->cancel_fd;
}
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
if (vpninfo->cancel_fd != -1 &&
FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
ret = -EINTR;
break;
}
}
}
buf[i] = 0;
return i ?: ret;
}
Jun 7, 2012
Jun 7, 2012
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
/* UI handling. All this just to handle the PIN callback from the TPM ENGINE,
and turn it into a call to our ->process_auth_form function */
struct ui_data {
struct openconnect_info *vpninfo;
struct oc_form_opt **last_opt;
struct oc_auth_form form;
};
struct ui_form_opt {
struct oc_form_opt opt;
UI_STRING *uis;
};
/* Ick. But there is no way to pass this sanely through OpenSSL */
static struct openconnect_info *ui_vpninfo;
static int ui_open(UI *ui)
{
struct openconnect_info *vpninfo = ui_vpninfo; /* Ick */
struct ui_data *ui_data;
if (!vpninfo || !vpninfo->process_auth_form)
return 0;
ui_data = malloc(sizeof(*ui_data));
if (!ui_data)
return 0;
memset(ui_data, 0, sizeof(*ui_data));
ui_data->last_opt = &ui_data->form.opts;
ui_data->vpninfo = vpninfo;
Jun 13, 2012
Jun 13, 2012
260
ui_data->form.auth_id = (char *)"openssl_ui";
Jun 7, 2012
Jun 7, 2012
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
UI_add_user_data(ui, ui_data);
return 1;
}
static int ui_write(UI *ui, UI_STRING *uis)
{
struct ui_data *ui_data = UI_get0_user_data(ui);
struct ui_form_opt *opt;
switch(UI_get_string_type(uis)) {
case UIT_ERROR:
ui_data->form.error = (char *)UI_get0_output_string(uis);
break;
case UIT_INFO:
ui_data->form.message = (char *)UI_get0_output_string(uis);
break;
case UIT_PROMPT:
opt = malloc(sizeof(*opt));
if (!opt)
return 1;
memset(opt, 0, sizeof(*opt));
opt->uis = uis;
opt->opt.label = opt->opt.name = (char *)UI_get0_output_string(uis);
if (UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO)
opt->opt.type = OC_FORM_OPT_TEXT;
else
opt->opt.type = OC_FORM_OPT_PASSWORD;
*(ui_data->last_opt) = &opt->opt;
ui_data->last_opt = &opt->opt.next;
break;
default:
fprintf(stderr, "Unhandled SSL UI request type %d\n",
UI_get_string_type(uis));
return 0;
}
return 1;
}
static int ui_flush(UI *ui)
{
struct ui_data *ui_data = UI_get0_user_data(ui);
struct openconnect_info *vpninfo = ui_data->vpninfo;
struct ui_form_opt *opt;
int ret;
Jun 8, 2012
Jun 8, 2012
308
ret = vpninfo->process_auth_form(vpninfo->cbdata, &ui_data->form);
Jun 7, 2012
Jun 7, 2012
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
if (ret)
return 0;
for (opt = (struct ui_form_opt *)ui_data->form.opts; opt;
opt = (struct ui_form_opt *)opt->opt.next) {
if (opt->opt.value && opt->uis)
UI_set_result(ui, opt->uis, opt->opt.value);
}
return 1;
}
static int ui_close(UI *ui)
{
struct ui_data *ui_data = UI_get0_user_data(ui);
struct ui_form_opt *opt, *next_opt;
opt = (struct ui_form_opt *)ui_data->form.opts;
while (opt) {
next_opt = (struct ui_form_opt *)opt->opt.next;
if (opt->opt.value)
free(opt->opt.value);
free(opt);
opt = next_opt;
}
free(ui_data);
UI_add_user_data(ui, NULL);
return 1;
}
static UI_METHOD *create_openssl_ui(struct openconnect_info *vpninfo)
{
UI_METHOD *ui_method = UI_create_method((char *)"AnyConnect VPN UI");
/* There is a race condition here because of the use of the
static ui_vpninfo pointer. This sucks, but it's OpenSSL's
fault and in practice it's *never* going to hurt us.
This UI is only used for loading certificates from a TPM; for
PKCS#12 and PEM files we hook the passphrase request differently.
The ui_vpninfo variable is set here, and is used from ui_open()
when the TPM ENGINE decides it needs to ask the user for a PIN.
The race condition exists because theoretically, there
could be more than one thread using libopenconnect and
trying to authenticate to a VPN server, within the *same*
process. And if *both* are using certificates from the TPM,
and *both* manage to be within that short window of time
between setting ui_vpninfo and invoking ui_open() to fetch
the PIN, then one connection's ->process_auth_form() could
get a PIN request for the *other* connection.
However, the only thing that ever does run libopenconnect more
than once from the same process is KDE's NetworkManager support,
and NetworkManager doesn't *support* having more than one VPN
connected anyway, so first that would have to be fixed and then
you'd have to connect to two VPNs simultaneously by clicking
'connect' on both at *exactly* the same time and then getting
*really* unlucky.
Oh, and the KDE support won't be using OpenSSL anyway because of
licensing conflicts... so although this sucks, I'm not going to
lose sleep over it.
*/
ui_vpninfo = vpninfo;
/* Set up a UI method of our own for password/passphrase requests */
UI_method_set_opener(ui_method, ui_open);
UI_method_set_writer(ui_method, ui_write);
UI_method_set_flusher(ui_method, ui_flush);
UI_method_set_closer(ui_method, ui_close);
return ui_method;
}
May 29, 2012
May 29, 2012
384
385
386
static int pem_pw_cb(char *buf, int len, int w, void *v)
{
struct openconnect_info *vpninfo = v;
Jun 5, 2012
Jun 5, 2012
387
388
char *pass = NULL;
int plen;
May 29, 2012
May 29, 2012
389
Jun 5, 2012
Jun 5, 2012
390
391
392
if (vpninfo->cert_password) {
pass = vpninfo->cert_password;
vpninfo->cert_password = NULL;
Jun 13, 2012
Jun 13, 2012
393
394
} else if (request_passphrase(vpninfo, "openconnect_pem",
&pass, _("Enter PEM pass phrase:")))
Jun 5, 2012
Jun 5, 2012
395
396
397
return -1;
plen = strlen(pass);
May 29, 2012
May 29, 2012
398
Jun 5, 2012
Jun 5, 2012
399
if (len <= plen) {
May 29, 2012
May 29, 2012
400
vpn_progress(vpninfo, PRG_ERR,
Jun 5, 2012
Jun 5, 2012
401
402
403
_("PEM password too long (%d >= %d)\n"),
plen, len);
free(pass);
May 29, 2012
May 29, 2012
404
405
return -1;
}
Jun 5, 2012
Jun 5, 2012
406
407
408
409
memcpy(buf, pass, plen+1);
free(pass);
return plen;
May 29, 2012
May 29, 2012
410
411
412
413
414
415
416
417
}
static int load_pkcs12_certificate(struct openconnect_info *vpninfo, PKCS12 *p12)
{
EVP_PKEY *pkey = NULL;
X509 *cert = NULL;
STACK_OF(X509) *ca;
int ret = 0;
Jun 4, 2012
Jun 4, 2012
418
char *pass;
May 29, 2012
May 29, 2012
419
Jun 4, 2012
Jun 4, 2012
420
421
pass = vpninfo->cert_password;
vpninfo->cert_password = NULL;
May 29, 2012
May 29, 2012
422
423
424
425
426
427
retrypass:
/* We do this every time round the loop, to work around a bug in
OpenSSL < 1.0.0-beta2 -- where the stack at *ca will be freed
when PKCS12_parse() returns an error, but *ca is left pointing
to the freed memory. */
ca = NULL;
Jun 13, 2012
Jun 13, 2012
428
if (!pass && request_passphrase(vpninfo, "openconnect_pkcs12", &pass,
Jun 4, 2012
Jun 4, 2012
429
430
431
_("Enter PKCS#12 pass phrase:")) < 0) {
PKCS12_free(p12);
return -EINVAL;
May 29, 2012
May 29, 2012
432
}
Jun 4, 2012
Jun 4, 2012
433
if (!PKCS12_parse(p12, pass, &pkey, &cert, &ca)) {
May 29, 2012
May 29, 2012
434
435
436
437
438
439
440
441
442
unsigned long err = ERR_peek_error();
openconnect_report_ssl_errors(vpninfo);
if (ERR_GET_LIB(err) == ERR_LIB_PKCS12 &&
ERR_GET_FUNC(err) == PKCS12_F_PKCS12_PARSE &&
ERR_GET_REASON(err) == PKCS12_R_MAC_VERIFY_FAILURE) {
vpn_progress(vpninfo, PRG_ERR,
_("Parse PKCS#12 failed (wrong passphrase?)\n"));
Jun 4, 2012
Jun 4, 2012
443
444
free(pass);
pass = NULL;
May 29, 2012
May 29, 2012
445
446
447
448
449
450
goto retrypass;
}
vpn_progress(vpninfo, PRG_ERR,
_("Parse PKCS#12 failed (see above errors)\n"));
PKCS12_free(p12);
Jun 12, 2012
Jun 12, 2012
451
free(pass);
May 29, 2012
May 29, 2012
452
453
return -EINVAL;
}
Jun 12, 2012
Jun 12, 2012
454
free(pass);
May 29, 2012
May 29, 2012
455
if (cert) {
Jun 11, 2012
Jun 11, 2012
456
char buf[200];
May 29, 2012
May 29, 2012
457
458
vpninfo->cert_x509 = cert;
SSL_CTX_use_certificate(vpninfo->https_ctx, cert);
Jun 11, 2012
Jun 11, 2012
459
460
461
X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
vpn_progress(vpninfo, PRG_INFO,
_("Using client certificate '%s'\n"), buf);
May 29, 2012
May 29, 2012
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
} else {
vpn_progress(vpninfo, PRG_ERR,
_("PKCS#12 contained no certificate!"));
ret = -EINVAL;
}
if (pkey) {
SSL_CTX_use_PrivateKey(vpninfo->https_ctx, pkey);
EVP_PKEY_free(pkey);
} else {
vpn_progress(vpninfo, PRG_ERR,
_("PKCS#12 contained no private key!"));
ret = -EINVAL;
}
/* Only include supporting certificates which are actually necessary */
if (ca) {
int i;
next:
for (i = 0; i < sk_X509_num(ca); i++) {
X509 *cert2 = sk_X509_value(ca, i);
if (X509_check_issued(cert2, cert) == X509_V_OK) {
char buf[200];
if (cert2 == cert)
break;
Jun 1, 2012
Jun 1, 2012
488
489
if (X509_check_issued(cert2, cert2) == X509_V_OK)
break;
May 29, 2012
May 29, 2012
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
X509_NAME_oneline(X509_get_subject_name(cert2),
buf, sizeof(buf));
vpn_progress(vpninfo, PRG_DEBUG,
_("Extra cert from PKCS#12: '%s'\n"), buf);
CRYPTO_add(&cert2->references, 1, CRYPTO_LOCK_X509);
SSL_CTX_add_extra_chain_cert(vpninfo->https_ctx, cert2);
cert = cert2;
goto next;
}
}
sk_X509_pop_free(ca, X509_free);
}
PKCS12_free(p12);
return ret;
}
#ifdef HAVE_ENGINE
static int load_tpm_certificate(struct openconnect_info *vpninfo)
{
ENGINE *e;
EVP_PKEY *key;
Jun 7, 2012
Jun 7, 2012
513
UI_METHOD *meth = NULL;
Jun 12, 2012
Jun 12, 2012
514
515
int ret = 0;
May 29, 2012
May 29, 2012
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
ENGINE_load_builtin_engines();
e = ENGINE_by_id("tpm");
if (!e) {
vpn_progress(vpninfo, PRG_ERR, _("Can't load TPM engine.\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
if (!ENGINE_init(e) || !ENGINE_set_default_RSA(e) ||
!ENGINE_set_default_RAND(e)) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to init TPM engine\n"));
openconnect_report_ssl_errors(vpninfo);
ENGINE_free(e);
return -EINVAL;
}
if (vpninfo->cert_password) {
if (!ENGINE_ctrl_cmd(e, "PIN", strlen(vpninfo->cert_password),
vpninfo->cert_password, NULL, 0)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TPM SRK password\n"));
openconnect_report_ssl_errors(vpninfo);
}
Jun 12, 2012
Jun 12, 2012
539
540
vpninfo->cert_password = NULL;
free(vpninfo->cert_password);
Jun 7, 2012
Jun 7, 2012
541
542
543
} else {
/* Provide our own UI method to handle the PIN callback. */
meth = create_openssl_ui(vpninfo);
May 29, 2012
May 29, 2012
544
}
Jun 7, 2012
Jun 7, 2012
545
546
547
key = ENGINE_load_private_key(e, vpninfo->sslkey, meth, NULL);
if (meth)
UI_destroy_method(meth);
May 29, 2012
May 29, 2012
548
549
550
551
if (!key) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM private key\n"));
openconnect_report_ssl_errors(vpninfo);
Jun 12, 2012
Jun 12, 2012
552
553
ret = -EINVAL;
goto out;
May 29, 2012
May 29, 2012
554
555
556
557
}
if (!SSL_CTX_use_PrivateKey(vpninfo->https_ctx, key)) {
vpn_progress(vpninfo, PRG_ERR, _("Add key from TPM failed\n"));
openconnect_report_ssl_errors(vpninfo);
Jun 12, 2012
Jun 12, 2012
558
ret = -EINVAL;
May 29, 2012
May 29, 2012
559
}
Jun 12, 2012
Jun 12, 2012
560
561
562
563
564
EVP_PKEY_free(key);
out:
ENGINE_finish(e);
ENGINE_free(e);
return ret;
May 29, 2012
May 29, 2012
565
566
567
568
569
570
571
572
573
574
575
576
577
}
#else
static int load_tpm_certificate(struct openconnect_info *vpninfo)
{
vpn_progress(vpninfo, PRG_ERR,
_("This version of OpenConnect was built without TPM support\n"));
return -EINVAL;
}
#endif
static int reload_pem_cert(struct openconnect_info *vpninfo)
{
BIO *b = BIO_new(BIO_s_file_internal());
Jun 11, 2012
Jun 11, 2012
578
char buf[200];
May 29, 2012
May 29, 2012
579
580
581
582
583
584
585
586
587
588
589
590
591
if (!b)
return -ENOMEM;
if (BIO_read_filename(b, vpninfo->cert) <= 0) {
err:
BIO_free(b);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to reload X509 cert for expiry check\n"));
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
vpninfo->cert_x509 = PEM_read_bio_X509_AUX(b, NULL, NULL, NULL);
Jun 12, 2012
Jun 12, 2012
592
BIO_free(b);
May 29, 2012
May 29, 2012
593
594
595
if (!vpninfo->cert_x509)
goto err;
Jun 11, 2012
Jun 11, 2012
596
597
598
599
X509_NAME_oneline(X509_get_subject_name(vpninfo->cert_x509), buf, sizeof(buf));
vpn_progress(vpninfo, PRG_INFO,
_("Using client certificate '%s'\n"), buf);
May 29, 2012
May 29, 2012
600
601
602
return 0;
}
Jun 15, 2012
Jun 15, 2012
603
604
605
#ifdef ANDROID_KEYSTORE
static BIO *BIO_from_keystore(struct openconnect_info *vpninfo, const char *item)
{
Jun 17, 2012
Jun 17, 2012
606
unsigned char *content;
Jun 15, 2012
Jun 15, 2012
607
608
609
610
611
612
613
614
615
616
BIO *b;
int len;
const char *p = item + 9;
/* Skip first two slashes if the user has given it as
keystore://foo ... */
if (*p == '/')
p++;
if (*p == '/')
p++;
Jun 17, 2012
Jun 17, 2012
617
618
619
len = keystore_fetch(p, &content);
if (len < 0) {
Jun 15, 2012
Jun 15, 2012
620
vpn_progress(vpninfo, PRG_ERR,
Jun 17, 2012
Jun 17, 2012
621
622
_("Failed to load item '%s' from keystore: %s\n"),
p, keystore_strerror(len));
Jun 15, 2012
Jun 15, 2012
623
624
625
626
627
628
return NULL;
}
if (!(b = BIO_new(BIO_s_mem())) || BIO_write(b, content, len) != len) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create BIO for keystore item '%s'\n"),
p);
Jun 17, 2012
Jun 17, 2012
629
free(content);
Jun 15, 2012
Jun 15, 2012
630
631
632
BIO_free(b);
return NULL;
}
Jun 17, 2012
Jun 17, 2012
633
free(content);
Jun 15, 2012
Jun 15, 2012
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
return b;
}
#endif
static int is_pem_password_error(struct openconnect_info *vpninfo)
{
unsigned long err = ERR_peek_error();
openconnect_report_ssl_errors(vpninfo);
#ifndef EVP_F_EVP_DECRYPTFINAL_EX
#define EVP_F_EVP_DECRYPTFINAL_EX EVP_F_EVP_DECRYPTFINAL
#endif
/* If the user fat-fingered the passphrase, try again */
if (ERR_GET_LIB(err) == ERR_LIB_EVP &&
ERR_GET_FUNC(err) == EVP_F_EVP_DECRYPTFINAL_EX &&
ERR_GET_REASON(err) == EVP_R_BAD_DECRYPT) {
vpn_progress(vpninfo, PRG_ERR,
_("Loading private key failed (wrong passphrase?)\n"));
Jun 15, 2012
Jun 15, 2012
653
ERR_clear_error();
Jun 15, 2012
Jun 15, 2012
654
655
656
657
658
659
660
661
return 1;
}
vpn_progress(vpninfo, PRG_ERR,
_("Loading private key failed (see above errors)\n"));
return 0;
}
May 29, 2012
May 29, 2012
662
663
static int load_certificate(struct openconnect_info *vpninfo)
{
Jun 11, 2012
Jun 11, 2012
664
665
666
667
668
669
670
if (!strncmp(vpninfo->sslkey, "pkcs11:", 7) ||
!strncmp(vpninfo->cert, "pkcs11:", 7)) {
vpn_progress(vpninfo, PRG_ERR,
_("This binary built without PKCS#11 support\n"));
return -EINVAL;
}
May 29, 2012
May 29, 2012
671
672
673
vpn_progress(vpninfo, PRG_TRACE,
_("Using certificate file %s\n"), vpninfo->cert);
Jun 15, 2012
Jun 15, 2012
674
675
676
if (strncmp(vpninfo->cert, "keystore:", 9) &&
(vpninfo->cert_type == CERT_TYPE_PKCS12 ||
vpninfo->cert_type == CERT_TYPE_UNKNOWN)) {
May 29, 2012
May 29, 2012
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
FILE *f;
PKCS12 *p12;
f = fopen(vpninfo->cert, "r");
if (!f) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open certificate file %s: %s\n"),
vpninfo->cert, strerror(errno));
return -ENOENT;
}
p12 = d2i_PKCS12_fp(f, NULL);
fclose(f);
if (p12)
return load_pkcs12_certificate(vpninfo, p12);
/* Not PKCS#12 */
if (vpninfo->cert_type == CERT_TYPE_PKCS12) {
vpn_progress(vpninfo, PRG_ERR, _("Read PKCS#12 failed\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
/* Clear error and fall through to see if it's a PEM file... */
ERR_clear_error();
}
/* It's PEM or TPM now, and either way we need to load the plain cert: */
Jun 15, 2012
Jun 15, 2012
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#ifdef ANDROID_KEYSTORE
if (!strncmp(vpninfo->cert, "keystore:", 9)) {
BIO *b = BIO_from_keystore(vpninfo, vpninfo->cert);
if (!b)
return -EINVAL;
vpninfo->cert_x509 = PEM_read_bio_X509_AUX(b, NULL, pem_pw_cb, vpninfo);
BIO_free(b);
if (!vpninfo->cert_x509) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load X509 certificate from keystore\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
if (!SSL_CTX_use_certificate(vpninfo->https_ctx, vpninfo->cert_x509)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to use X509 certificate from keystore\n"));
openconnect_report_ssl_errors(vpninfo);
X509_free(vpninfo->cert_x509);
vpninfo->cert_x509 = NULL;
return -EINVAL;
}
} else
#endif /* ANDROID_KEYSTORE */
{
if (!SSL_CTX_use_certificate_chain_file(vpninfo->https_ctx,
vpninfo->cert)) {
vpn_progress(vpninfo, PRG_ERR,
_("Loading certificate failed\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
/* Ew, we can't get it back from the OpenSSL CTX in any sane fashion */
reload_pem_cert(vpninfo);
May 29, 2012
May 29, 2012
737
738
}
Jun 15, 2012
Jun 15, 2012
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
#ifdef ANDROID_KEYSTORE
if (!strncmp(vpninfo->sslkey, "keystore:", 9)) {
EVP_PKEY *key;
BIO *b;
again_android:
b = BIO_from_keystore(vpninfo, vpninfo->sslkey);
if (!b)
return -EINVAL;
key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, vpninfo);
BIO_free(b);
if (!key) {
if (is_pem_password_error(vpninfo))
goto again_android;
return -EINVAL;
}
if (!SSL_CTX_use_PrivateKey(vpninfo->https_ctx, key)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to use private key from keystore\n"));
EVP_PKEY_free(key);
X509_free(vpninfo->cert_x509);
vpninfo->cert_x509 = NULL;
return -EINVAL;
}
return 0;
}
#endif /* ANDROID */
May 29, 2012
May 29, 2012
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
if (vpninfo->cert_type == CERT_TYPE_UNKNOWN) {
FILE *f = fopen(vpninfo->sslkey, "r");
char buf[256];
if (!f) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open private key file %s: %s\n"),
vpninfo->cert, strerror(errno));
return -ENOENT;
}
buf[255] = 0;
while (fgets(buf, 255, f)) {
if (!strcmp(buf, "-----BEGIN TSS KEY BLOB-----\n")) {
vpninfo->cert_type = CERT_TYPE_TPM;
break;
} else if (!strcmp(buf, "-----BEGIN RSA PRIVATE KEY-----\n") ||
!strcmp(buf, "-----BEGIN DSA PRIVATE KEY-----\n") ||
!strcmp(buf, "-----BEGIN ENCRYPTED PRIVATE KEY-----\n")) {
vpninfo->cert_type = CERT_TYPE_PEM;
break;
}
}
fclose(f);
if (vpninfo->cert_type == CERT_TYPE_UNKNOWN) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to identify private key type in '%s'\n"),
vpninfo->sslkey);
return -EINVAL;
}
}
if (vpninfo->cert_type == CERT_TYPE_TPM)
return load_tpm_certificate(vpninfo);
/* Standard PEM certificate */
Jun 5, 2012
Jun 5, 2012
803
804
SSL_CTX_set_default_passwd_cb(vpninfo->https_ctx, pem_pw_cb);
SSL_CTX_set_default_passwd_cb_userdata(vpninfo->https_ctx, vpninfo);
May 29, 2012
May 29, 2012
805
806
807
again:
if (!SSL_CTX_use_RSAPrivateKey_file(vpninfo->https_ctx, vpninfo->sslkey,
SSL_FILETYPE_PEM)) {
Jun 15, 2012
Jun 15, 2012
808
if (is_pem_password_error(vpninfo))
May 29, 2012
May 29, 2012
809
810
811
812
813
814
815
goto again;
return -EINVAL;
}
return 0;
}
static int get_cert_fingerprint(struct openconnect_info *vpninfo,
Jun 8, 2012
Jun 8, 2012
816
OPENCONNECT_X509 *cert, const EVP_MD *type,
May 29, 2012
May 29, 2012
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
char *buf)
{
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int i, n;
if (!X509_digest(cert, type, md, &n))
return -ENOMEM;
for (i=0; i < n; i++)
sprintf(&buf[i*2], "%02X", md[i]);
return 0;
}
int get_cert_md5_fingerprint(struct openconnect_info *vpninfo,
Jun 8, 2012
Jun 8, 2012
832
OPENCONNECT_X509 *cert, char *buf)
May 29, 2012
May 29, 2012
833
834
835
836
837
{
return get_cert_fingerprint(vpninfo, cert, EVP_md5(), buf);
}
int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
Jun 8, 2012
Jun 8, 2012
838
OPENCONNECT_X509 *cert, char *buf)
May 29, 2012
May 29, 2012
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
return get_cert_fingerprint(vpninfo, cert, EVP_sha1(), buf);
}
static int check_server_cert(struct openconnect_info *vpninfo, X509 *cert)
{
char fingerprint[EVP_MAX_MD_SIZE * 2 + 1];
int ret;
ret = openconnect_get_cert_sha1(vpninfo, cert, fingerprint);
if (ret)
return ret;
if (strcasecmp(vpninfo->servercert, fingerprint)) {
vpn_progress(vpninfo, PRG_ERR,
_("Server SSL certificate didn't match: %s\n"), fingerprint);
return -EINVAL;
}
return 0;
}
static int match_hostname_elem(const char *hostname, int helem_len,
const char *match, int melem_len)
{
if (!helem_len && !melem_len)
return 0;
if (!helem_len || !melem_len)
return -1;
if (match[0] == '*') {
int i;
for (i = 1 ; i <= helem_len; i++) {
if (!match_hostname_elem(hostname + i, helem_len - i,
match + 1, melem_len - 1))
return 0;
}
return -1;
}
/* From the NetBSD (5.1) man page for ctype(3):
Values of type char or signed char must first be cast to unsigned char,
to ensure that the values are within the correct range. The result
should then be cast to int to avoid warnings from some compilers.
We do indeed get warning "array subscript has type 'char'" without
the casts. Ick. */
if (toupper((int)(unsigned char)hostname[0]) ==
toupper((int)(unsigned char)match[0]))
return match_hostname_elem(hostname + 1, helem_len - 1,
match + 1, melem_len - 1);
return -1;
}
static int match_hostname(const char *hostname, const char *match)
{
while (*match) {
const char *h_dot, *m_dot;
int helem_len, melem_len;
h_dot = strchr(hostname, '.');
m_dot = strchr(match, '.');
if (h_dot && m_dot) {
helem_len = h_dot - hostname + 1;
melem_len = m_dot - match + 1;
} else if (!h_dot && !m_dot) {
helem_len = strlen(hostname);
melem_len = strlen(match);
} else
return -1;
if (match_hostname_elem(hostname, helem_len,
match, melem_len))
return -1;
hostname += helem_len;
match += melem_len;
}
if (*hostname)
return -1;
return 0;
}
/* cf. RFC2818 and RFC2459 */
static int match_cert_hostname(struct openconnect_info *vpninfo, X509 *peer_cert)
{
STACK_OF(GENERAL_NAME) *altnames;
X509_NAME *subjname;
ASN1_STRING *subjasn1;
char *subjstr = NULL;
int addrlen = 0;
int i, altdns = 0;
char addrbuf[sizeof(struct in6_addr)];
int ret;
/* Allow GEN_IP in the certificate only if we actually connected
by IP address rather than by name. */
if (inet_pton(AF_INET, vpninfo->hostname, addrbuf) > 0)
addrlen = 4;
else if (inet_pton(AF_INET6, vpninfo->hostname, addrbuf) > 0)
addrlen = 16;
else if (vpninfo->hostname[0] == '[' &&
vpninfo->hostname[strlen(vpninfo->hostname)-1] == ']') {
char *p = &vpninfo->hostname[strlen(vpninfo->hostname)-1];
*p = 0;
if (inet_pton(AF_INET6, vpninfo->hostname + 1, addrbuf) > 0)
addrlen = 16;
*p = ']';
}
altnames = X509_get_ext_d2i(peer_cert, NID_subject_alt_name,
NULL, NULL);
for (i = 0; i < sk_GENERAL_NAME_num(altnames); i++) {
const GENERAL_NAME *this = sk_GENERAL_NAME_value(altnames, i);
if (this->type == GEN_DNS) {
char *str;
int len = ASN1_STRING_to_UTF8((void *)&str, this->d.ia5);
if (len < 0)
continue;
altdns = 1;
/* We don't like names with embedded NUL */
if (strlen(str) != len)
continue;
if (!match_hostname(vpninfo->hostname, str)) {
vpn_progress(vpninfo, PRG_TRACE,
_("Matched DNS altname '%s'\n"),
str);
GENERAL_NAMES_free(altnames);
OPENSSL_free(str);
return 0;
} else {
vpn_progress(vpninfo, PRG_TRACE,
_("No match for altname '%s'\n"),
str);
}
OPENSSL_free(str);
} else if (this->type == GEN_IPADD && addrlen) {
char host[80];
int family;
if (this->d.ip->length == 4) {
family = AF_INET;
} else if (this->d.ip->length == 16) {
family = AF_INET6;
} else {
vpn_progress(vpninfo, PRG_ERR,
_("Certificate has GEN_IPADD altname with bogus length %d\n"),
this->d.ip->length);
continue;
}
/* We only do this for the debug messages */