Skip to content

Latest commit

 

History

History
1738 lines (1532 loc) · 47.7 KB

gnutls.c

File metadata and controls

1738 lines (1532 loc) · 47.7 KB
 
May 29, 2012
May 29, 2012
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
/*
* 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
*/
#include <sys/types.h>
May 31, 2012
May 31, 2012
26
#include <sys/stat.h>
May 29, 2012
May 29, 2012
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/crypto.h>
May 31, 2012
May 31, 2012
43
#include <gnutls/pkcs12.h>
Jun 10, 2012
Jun 10, 2012
44
#include <gnutls/abstract.h>
May 29, 2012
May 29, 2012
45
Jun 13, 2012
Jun 13, 2012
46
47
48
49
#ifdef HAVE_TROUSERS
#include <trousers/tss.h>
#include <trousers/trousers.h>
#endif
Jun 8, 2012
Jun 8, 2012
50
51
#ifdef HAVE_P11KIT
#include <p11-kit/p11-kit.h>
Jun 10, 2012
Jun 10, 2012
52
#include <p11-kit/pkcs11.h>
Jun 8, 2012
Jun 8, 2012
53
54
55
56
57
58
59
60
#include <p11-kit/pin.h>
static P11KitPin *pin_callback(const char *pin_source, P11KitUri *pin_uri,
const char *pin_description,
P11KitPinFlags flags,
void *_vpninfo);
#endif
May 29, 2012
May 29, 2012
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
#include "openconnect-internal.h"
/* 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 = gnutls_record_send(vpninfo->https_sess, buf, len);
if (done > 0)
len -= done;
else if (done != GNUTLS_E_AGAIN) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to write to SSL socket: %s"),
gnutls_strerror(done));
return -EIO;
} else {
fd_set wr_set, rd_set;
int maxfd = vpninfo->ssl_fd;
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
if (gnutls_record_get_direction(vpninfo->https_sess))
FD_SET(vpninfo->ssl_fd, &wr_set);
else
FD_SET(vpninfo->ssl_fd, &rd_set);
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 = gnutls_record_recv(vpninfo->https_sess, buf, len)) < 0) {
fd_set wr_set, rd_set;
int maxfd = vpninfo->ssl_fd;
if (done != GNUTLS_E_AGAIN) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to read from SSL socket: %s"),
gnutls_strerror(done));
return -EIO;
} else {
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
if (gnutls_record_get_direction(vpninfo->https_sess))
FD_SET(vpninfo->ssl_fd, &wr_set);
else
FD_SET(vpninfo->ssl_fd, &rd_set);
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 = gnutls_record_recv(vpninfo->https_sess, 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 if (ret != GNUTLS_E_AGAIN) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to read from SSL socket: %s\n"),
gnutls_strerror(ret));
ret = -EIO;
break;
} else {
fd_set rd_set, wr_set;
int maxfd = vpninfo->ssl_fd;
FD_ZERO(&rd_set);
FD_ZERO(&wr_set);
if (gnutls_record_get_direction(vpninfo->https_sess))
FD_SET(vpninfo->ssl_fd, &wr_set);
else
FD_SET(vpninfo->ssl_fd, &rd_set);
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;
}
May 31, 2012
May 31, 2012
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
static int check_certificate_expiry(struct openconnect_info *vpninfo, gnutls_x509_crt_t cert)
{
const char *reason = NULL;
time_t expires = gnutls_x509_crt_get_expiration_time(cert);
time_t now = time(NULL);
if (expires == -1) {
vpn_progress(vpninfo, PRG_ERR,
_("Could not extract expiration time of certificate\n"));
return -EINVAL;
}
if (expires < now)
reason = _("Client certificate has expired at");
else if (expires < now + vpninfo->cert_expire_warning)
reason = _("Client certificate expires soon at");
if (reason) {
struct tm tm;
char buf[80];
gmtime_r(&expires, &tm);
strftime(buf, 80, "%a, %d %b %Y %T %Z", &tm);
vpn_progress(vpninfo, PRG_ERR, "%s: %s\n", reason, buf);
}
return 0;
}
May 31, 2012
May 31, 2012
232
233
234
235
236
237
/* For systems that don't support O_CLOEXEC, just don't bother.
It's not open for long anyway. */
#ifndef O_CLOEXEC
#define O_CLOEXEC
#endif
May 31, 2012
May 31, 2012
238
239
240
241
242
243
244
245
246
247
static int load_datum(struct openconnect_info *vpninfo,
gnutls_datum_t *datum, const char *fname)
{
struct stat st;
int fd, err;
fd = open(fname, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
err = errno;
vpn_progress(vpninfo, PRG_ERR,
Jun 9, 2012
Jun 9, 2012
248
249
_("Failed to open key/certificate file %s: %s\n"),
fname, strerror(err));
May 31, 2012
May 31, 2012
250
251
252
253
254
return -ENOENT;
}
if (fstat(fd, &st)) {
err = errno;
vpn_progress(vpninfo, PRG_ERR,
Jun 9, 2012
Jun 9, 2012
255
256
_("Failed to stat key/certificate file %s: %s\n"),
fname, strerror(err));
May 31, 2012
May 31, 2012
257
258
close(fd);
return -EIO;
May 31, 2012
May 31, 2012
259
}
May 31, 2012
May 31, 2012
260
datum->size = st.st_size;
May 31, 2012
May 31, 2012
261
datum->data = gnutls_malloc(st.st_size + 1);
May 31, 2012
May 31, 2012
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
if (!datum->data) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate certificate buffer\n"));
close(fd);
return -ENOMEM;
}
errno = EAGAIN;
if (read(fd, datum->data, datum->size) != datum->size) {
err = errno;
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read certificate into memory: %s\n"),
strerror(err));
close(fd);
gnutls_free(datum->data);
return -EIO;
}
May 31, 2012
May 31, 2012
278
datum->data[st.st_size] = 0;
May 31, 2012
May 31, 2012
279
280
281
282
close(fd);
return 0;
}
Jun 7, 2012
Jun 7, 2012
283
284
285
#ifndef HAVE_GNUTLS_PKCS12_SIMPLE_PARSE
/* If we're using a version of GnuTLS from before this was
exported, pull in our local copy. */
May 31, 2012
May 31, 2012
286
#include "gnutls_pkcs12.c"
Jun 7, 2012
Jun 7, 2012
287
#endif
May 31, 2012
May 31, 2012
288
May 31, 2012
May 31, 2012
289
290
291
292
/* A non-zero, non-error return to make load_certificate() continue and
interpreting the file as other types */
#define NOT_PKCS12 1
May 31, 2012
May 31, 2012
293
static int load_pkcs12_certificate(struct openconnect_info *vpninfo,
May 31, 2012
May 31, 2012
294
295
gnutls_datum_t *datum,
gnutls_x509_privkey_t *key,
Jun 9, 2012
Jun 9, 2012
296
297
gnutls_x509_crt_t **chain,
unsigned int *chain_len,
May 31, 2012
May 31, 2012
298
gnutls_x509_crt_t **extra_certs,
Jun 9, 2012
Jun 9, 2012
299
unsigned int *extra_certs_len,
May 31, 2012
May 31, 2012
300
gnutls_x509_crl_t *crl)
May 31, 2012
May 31, 2012
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{
gnutls_pkcs12_t p12;
char *pass;
int err;
err = gnutls_pkcs12_init(&p12);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to setup PKCS#12 data structure: %s\n"),
gnutls_strerror(err));
return -EIO;
}
err = gnutls_pkcs12_import(p12, datum, GNUTLS_X509_FMT_DER, 0);
if (err) {
gnutls_pkcs12_deinit(p12);
if (vpninfo->cert_type == CERT_TYPE_UNKNOWN)
May 31, 2012
May 31, 2012
318
return NOT_PKCS12;
May 31, 2012
May 31, 2012
319
320
321
322
323
324
325
vpn_progress(vpninfo, PRG_ERR,
_("Failed to import PKCS#12 file: %s\n"),
gnutls_strerror(err));
return -EINVAL;
}
pass = vpninfo->cert_password;
May 31, 2012
May 31, 2012
326
while ((err = gnutls_pkcs12_verify_mac(p12, pass)) == GNUTLS_E_MAC_VERIFY_FAILED) {
May 31, 2012
May 31, 2012
327
328
329
330
if (pass)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to decrypt PKCS#12 certificate file\n"));
free(pass);
May 31, 2012
May 31, 2012
331
vpninfo->cert_password = NULL;
Jun 13, 2012
Jun 13, 2012
332
err = request_passphrase(vpninfo, "openconnect_pkcs12", &pass,
May 31, 2012
May 31, 2012
333
334
335
336
337
338
_("Enter PKCS#12 pass phrase:"));
if (err) {
gnutls_pkcs12_deinit(p12);
return -EINVAL;
}
}
May 31, 2012
May 31, 2012
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* If it wasn't GNUTLS_E_MAC_VERIFY_FAILED, then the problem wasn't just a
bad password. Give up. */
if (err) {
int level = PRG_ERR;
int ret = -EINVAL;
gnutls_pkcs12_deinit(p12);
/* If the first attempt, and we didn't know for sure it was PKCS#12
anyway, bail out and try loading it as something different. */
if (pass == vpninfo->cert_password &&
vpninfo->cert_type == CERT_TYPE_UNKNOWN) {
/* Make it non-fatal... */
level = PRG_TRACE;
ret = NOT_PKCS12;
}
vpn_progress(vpninfo, level,
_("Failed to process PKCS#12 file: %s\n"),
gnutls_strerror(err));
return ret;
}
Jun 9, 2012
Jun 9, 2012
361
362
err = gnutls_pkcs12_simple_parse(p12, pass, key, chain, chain_len,
extra_certs, extra_certs_len, crl, 0);
Jun 12, 2012
Jun 12, 2012
363
364
365
free(pass);
vpninfo->cert_password = NULL;
May 31, 2012
May 31, 2012
366
367
368
369
370
371
372
373
374
375
gnutls_pkcs12_deinit(p12);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load PKCS#12 certificate: %s\n"),
gnutls_strerror(err));
return -EINVAL;
}
return 0;
}
May 31, 2012
May 31, 2012
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Older versions of GnuTLS didn't actually bother to check this, so we'll
do it for them. */
static int check_issuer_sanity(gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer)
{
#if GNUTLS_VERSION_NUMBER > 0x300014
return 0;
#else
unsigned char id1[512], id2[512];
size_t id1_size = 512, id2_size = 512;
int err;
err = gnutls_x509_crt_get_authority_key_id(cert, id1, &id1_size, NULL);
if (err)
return 0;
err = gnutls_x509_crt_get_subject_key_id(issuer, id2, &id2_size, NULL);
if (err)
return 0;
if (id1_size == id2_size && !memcmp(id1, id2, id1_size))
return 0;
/* EEP! */
return -EIO;
#endif
}
May 31, 2012
May 31, 2012
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
static int count_x509_certificates(gnutls_datum_t *datum)
{
int count = 0;
char *p = (char *)datum->data;
while (p) {
p = strstr(p, "-----BEGIN ");
if (!p)
break;
p += 11;
if (!strncmp(p, "CERTIFICATE", 11) ||
!strncmp(p, "X509 CERTIFICATE", 16))
count++;
}
return count;
}
Jun 11, 2012
Jun 11, 2012
419
420
421
422
423
424
425
426
427
428
429
430
static int get_cert_name(gnutls_x509_crt_t cert, char *name, size_t namelen)
{
if (gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME,
0, 0, name, &namelen) &&
gnutls_x509_crt_get_dn(cert, name, &namelen)) {
name[namelen-1] = 0;
snprintf(name, namelen-1, "<unknown>");
return -EINVAL;
}
return 0;
}
Jun 13, 2012
Jun 13, 2012
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#ifdef HAVE_TROUSERS
/* TPM code based on client-tpm.c from Carolin Latze <latze@angry-red-pla.net>
and Tobias Soder */
static int tpm_sign_fn(gnutls_privkey_t key, void *_vpninfo,
const gnutls_datum_t *data, gnutls_datum_t *sig)
{
struct openconnect_info *vpninfo = _vpninfo;
TSS_HHASH hash;
int err;
vpn_progress(vpninfo, PRG_TRACE,
_("TPM sign function called for %d bytes.\n"),
data->size);
err = Tspi_Context_CreateObject(vpninfo->tpm_context, TSS_OBJECT_TYPE_HASH,
TSS_HASH_OTHER, &hash);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
Jun 13, 2012
Jun 13, 2012
450
451
_("Failed to create TPM hash object: %s\n"),
Trspi_Error_String(err));
Jun 13, 2012
Jun 13, 2012
452
453
454
455
456
return GNUTLS_E_PK_SIGN_FAILED;
}
err = Tspi_Hash_SetHashValue(hash, data->size, data->data);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
Jun 13, 2012
Jun 13, 2012
457
458
_("Failed to set value in TPM hash object: %s\n"),
Trspi_Error_String(err));
Jun 13, 2012
Jun 13, 2012
459
460
461
462
463
464
Tspi_Context_CloseObject(vpninfo->tpm_context, hash);
return GNUTLS_E_PK_SIGN_FAILED;
}
err = Tspi_Hash_Sign(hash, vpninfo->tpm_key, &sig->size, &sig->data);
Tspi_Context_CloseObject(vpninfo->tpm_context, hash);
if (err) {
Jun 13, 2012
Jun 13, 2012
465
466
467
468
if (vpninfo->tpm_key_policy || err != TPM_E_AUTHFAIL)
vpn_progress(vpninfo, PRG_ERR,
_("TPM hash signature failed: %s\n"),
Trspi_Error_String(err));
Jun 13, 2012
Jun 13, 2012
469
470
471
472
if (err == TPM_E_AUTHFAIL)
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
else
return GNUTLS_E_PK_SIGN_FAILED;
Jun 13, 2012
Jun 13, 2012
473
474
475
476
}
return 0;
}
Jun 13, 2012
Jun 13, 2012
477
478
static int load_tpm_key(struct openconnect_info *vpninfo, gnutls_datum_t *fdata, gnutls_privkey_t *pkey,
gnutls_datum_t *pkey_sig)
Jun 13, 2012
Jun 13, 2012
479
480
481
482
483
484
485
486
487
488
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
{
static const TSS_UUID SRK_UUID = TSS_UUID_SRK;
gnutls_datum_t asn1;
unsigned int tss_len;
char *pass;
int ofs, err;
err = gnutls_pem_base64_decode_alloc("TSS KEY BLOB", fdata, &asn1);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error decoding TSS key blob: %s\n"),
gnutls_strerror(err));
return -EINVAL;
}
/* Ick. We have to parse the ASN1 OCTET_STRING for ourselves. */
if (asn1.size < 2 || asn1.data[0] != 0x04 /* OCTET_STRING */) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
tss_len = asn1.data[1];
ofs = 2;
if (tss_len & 0x80) {
int lenlen = tss_len & 0x7f;
if (asn1.size < 2 + lenlen || lenlen > 3) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
tss_len = 0;
while (lenlen) {
tss_len <<= 8;
tss_len |= asn1.data[ofs++];
lenlen--;
}
}
if (tss_len + ofs != asn1.size) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
err = Tspi_Context_Create(&vpninfo->tpm_context);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create TPM context: %s\n"),
Trspi_Error_String(err));
goto out_blob;
}
err = Tspi_Context_Connect(vpninfo->tpm_context, NULL);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to connect TPM context: %s\n"),
Trspi_Error_String(err));
goto out_context;
}
err = Tspi_Context_LoadKeyByUUID(vpninfo->tpm_context, TSS_PS_TYPE_SYSTEM,
SRK_UUID, &vpninfo->srk);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM SRK key: %s\n"),
Trspi_Error_String(err));
goto out_context;
}
err = Tspi_GetPolicyObject(vpninfo->srk, TSS_POLICY_USAGE, &vpninfo->srk_policy);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM SRK policy object: %s\n"),
Trspi_Error_String(err));
goto out_srk;
}
pass = vpninfo->cert_password;
vpninfo->cert_password = NULL;
while (1) {
Jun 13, 2012
Jun 13, 2012
557
558
static const char nullpass[20];
Jun 13, 2012
Jun 13, 2012
559
/* We don't seem to get the error here... */
Jun 13, 2012
Jun 13, 2012
560
561
562
563
564
565
if (pass)
err = Tspi_Policy_SetSecret(vpninfo->srk_policy, TSS_SECRET_MODE_PLAIN,
strlen(pass), (BYTE *)pass);
else /* Well-known NULL key */
err = Tspi_Policy_SetSecret(vpninfo->srk_policy, TSS_SECRET_MODE_SHA1,
sizeof(nullpass), (BYTE *)nullpass);
Jun 13, 2012
Jun 13, 2012
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TPM PIN: %s\n"),
Trspi_Error_String(err));
goto out_srkpol;
}
free(pass);
/* ... we get it here instead. */
err = Tspi_Context_LoadKeyByBlob(vpninfo->tpm_context, vpninfo->srk,
tss_len, asn1.data + ofs, &vpninfo->tpm_key);
if (!err)
break;
Jun 13, 2012
Jun 13, 2012
581
582
583
584
if (pass)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM key blob: %s\n"),
Trspi_Error_String(err));
Jun 13, 2012
Jun 13, 2012
585
586
587
if (err != TPM_E_AUTHFAIL)
goto out_srkpol;
Jun 13, 2012
Jun 13, 2012
588
Jun 13, 2012
Jun 13, 2012
589
590
err = request_passphrase(vpninfo, "openconnect_tpm_srk",
&pass, _("Enter TPM SRK PIN:"));
Jun 13, 2012
Jun 13, 2012
591
592
if (err)
goto out_srkpol;
Jun 13, 2012
Jun 13, 2012
593
594
595
596
597
598
599
600
}
gnutls_privkey_init(pkey);
/* This would be nicer if there was a destructor callback. I could
allocate a data structure with the TPM handles and the vpninfo
pointer, and destroy that properly when the key is destroyed. */
gnutls_privkey_import_ext(*pkey, GNUTLS_PK_RSA, vpninfo, tpm_sign_fn, NULL, 0);
Jun 13, 2012
Jun 13, 2012
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
retry_sign:
err = gnutls_privkey_sign_data(*pkey, GNUTLS_DIG_SHA1, 0, fdata, pkey_sig);
if (err == GNUTLS_E_INSUFFICIENT_CREDENTIALS) {
if (!vpninfo->tpm_key_policy) {
err = Tspi_Context_CreateObject(vpninfo->tpm_context,
TSS_OBJECT_TYPE_POLICY,
TSS_POLICY_USAGE,
&vpninfo->tpm_key_policy);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create key policy object: %s\n"),
Trspi_Error_String(err));
goto out_key;
}
err = Tspi_Policy_AssignToObject(vpninfo->tpm_key_policy,
vpninfo->tpm_key);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to assign policy to key: %s\n"),
Trspi_Error_String(err));
goto out_key_policy;
}
}
Jun 13, 2012
Jun 13, 2012
624
625
err = request_passphrase(vpninfo, "openconnect_tpm_key",
&pass, _("Enter TPM key PIN:"));
Jun 13, 2012
Jun 13, 2012
626
627
628
629
630
631
if (err)
goto out_key_policy;
err = Tspi_Policy_SetSecret(vpninfo->tpm_key_policy,
TSS_SECRET_MODE_PLAIN,
strlen(pass), (void *)pass);
Jun 14, 2012
Jun 14, 2012
632
633
free (pass);
Jun 13, 2012
Jun 13, 2012
634
635
636
637
638
639
640
641
642
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set key PIN: %s\n"),
Trspi_Error_String(err));
goto out_key_policy;
}
goto retry_sign;
}
Jun 13, 2012
Jun 13, 2012
643
644
free (asn1.data);
return 0;
Jun 13, 2012
Jun 13, 2012
645
646
647
648
649
650
out_key_policy:
Tspi_Context_CloseObject(vpninfo->tpm_context, vpninfo->tpm_key_policy);
vpninfo->tpm_key_policy = 0;
out_key:
Tspi_Context_CloseObject(vpninfo->tpm_context, vpninfo->tpm_key);
vpninfo->tpm_key = 0;
Jun 13, 2012
Jun 13, 2012
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
out_srkpol:
Tspi_Context_CloseObject(vpninfo->tpm_context, vpninfo->srk_policy);
vpninfo->srk_policy = 0;
out_srk:
Tspi_Context_CloseObject(vpninfo->tpm_context, vpninfo->srk);
vpninfo->srk = 0;
out_context:
Tspi_Context_Close(vpninfo->tpm_context);
vpninfo->tpm_context = 0;
out_blob:
free (asn1.data);
return -EIO;
}
#endif /* HAVE_TROUSERS */
May 29, 2012
May 29, 2012
666
667
static int load_certificate(struct openconnect_info *vpninfo)
{
May 31, 2012
May 31, 2012
668
gnutls_datum_t fdata;
May 31, 2012
May 31, 2012
669
gnutls_x509_privkey_t key = NULL;
Jun 12, 2012
Jun 12, 2012
670
#ifdef HAVE_GNUTLS_CERTIFICATE_SET_KEY
Jun 10, 2012
Jun 10, 2012
671
gnutls_privkey_t pkey = NULL;
Jun 13, 2012
Jun 13, 2012
672
673
gnutls_datum_t pkey_sig = {NULL, 0};
void *dummy_hash_data = &load_certificate;
Jun 12, 2012
Jun 12, 2012
674
675
#endif
#ifdef HAVE_P11KIT
Jun 10, 2012
Jun 10, 2012
676
677
678
char *cert_url = (char *)vpninfo->cert;
char *key_url = (char *)vpninfo->sslkey;
#endif
May 31, 2012
May 31, 2012
679
gnutls_x509_crl_t crl = NULL;
May 31, 2012
May 31, 2012
680
681
gnutls_x509_crt_t last_cert, cert = NULL;
gnutls_x509_crt_t *extra_certs = NULL, *supporting_certs = NULL;
Jun 9, 2012
Jun 9, 2012
682
683
unsigned int nr_supporting_certs = 0, nr_extra_certs = 0;
unsigned int certs_to_free = 0; /* How many of supporting_certs */
May 31, 2012
May 31, 2012
684
int err; /* GnuTLS error */
Jun 13, 2012
Jun 13, 2012
685
int ret;
May 31, 2012
May 31, 2012
686
int i;
Jun 10, 2012
Jun 10, 2012
687
int cert_is_p11 = 0, key_is_p11 = 0;
May 31, 2012
May 31, 2012
688
689
unsigned char key_id[20];
size_t key_id_size = sizeof(key_id);
Jun 11, 2012
Jun 11, 2012
690
char name[80];
May 29, 2012
May 29, 2012
691
Jun 9, 2012
Jun 9, 2012
692
693
fdata.data = NULL;
Jun 10, 2012
Jun 10, 2012
694
695
696
697
698
key_is_p11 = !strncmp(vpninfo->sslkey, "pkcs11:", 7);
cert_is_p11 = !strncmp(vpninfo->cert, "pkcs11:", 7);
/* Install PIN handler if either certificate or key are coming from PKCS#11 */
if (key_is_p11 || cert_is_p11) {
Jun 8, 2012
Jun 8, 2012
699
#ifdef HAVE_P11KIT
Jun 10, 2012
Jun 10, 2012
700
701
CK_OBJECT_CLASS class;
CK_ATTRIBUTE attr;
Jun 8, 2012
Jun 8, 2012
702
703
704
705
char pin_source[40];
P11KitUri *uri;
sprintf(pin_source, "openconnect:%p", vpninfo);
Jun 10, 2012
Jun 10, 2012
706
p11_kit_pin_register_callback(pin_source, pin_callback, vpninfo, NULL);
Jun 8, 2012
Jun 8, 2012
707
708
uri = p11_kit_uri_new();
Jun 10, 2012
Jun 10, 2012
709
Jun 10, 2012
Jun 10, 2012
710
711
712
713
714
715
attr.type = CKA_CLASS;
attr.pValue = &class;
attr.ulValueLen = sizeof(class);
/* Add appropriate pin-source and object-type attributes to
both certificate and key URLs, unless they already exist. */
Jun 10, 2012
Jun 10, 2012
716
if (cert_is_p11 &&
Jun 10, 2012
Jun 10, 2012
717
718
719
720
721
722
723
!p11_kit_uri_parse(cert_url, P11_KIT_URI_FOR_OBJECT, uri)) {
if (!p11_kit_uri_get_pin_source(uri))
p11_kit_uri_set_pin_source(uri, pin_source);
if (!p11_kit_uri_get_attribute(uri, CKA_CLASS)) {
class = CKO_CERTIFICATE;
p11_kit_uri_set_attribute(uri, &attr);
}
Jun 8, 2012
Jun 8, 2012
724
725
726
p11_kit_uri_format(uri, P11_KIT_URI_FOR_OBJECT, &cert_url);
}
Jun 10, 2012
Jun 10, 2012
727
if (key_is_p11 &&
Jun 10, 2012
Jun 10, 2012
728
729
730
731
732
733
734
!p11_kit_uri_parse(key_url, P11_KIT_URI_FOR_OBJECT, uri)) {
if (!p11_kit_uri_get_pin_source(uri))
p11_kit_uri_set_pin_source(uri, pin_source);
if (!p11_kit_uri_get_attribute(uri, CKA_CLASS)) {
class = CKO_PRIVATE_KEY;
p11_kit_uri_set_attribute(uri, &attr);
}
Jun 8, 2012
Jun 8, 2012
735
736
p11_kit_uri_format(uri, P11_KIT_URI_FOR_OBJECT, &key_url);
}
Jun 10, 2012
Jun 10, 2012
737
Jun 8, 2012
Jun 8, 2012
738
p11_kit_uri_free(uri);
Jun 10, 2012
Jun 10, 2012
739
740
741
742
#else
vpn_progress(vpninfo, PRG_ERR,
_("This binary built without PKCS#11 support\n"));
return -EINVAL;
Jun 8, 2012
Jun 8, 2012
743
#endif
Jun 10, 2012
Jun 10, 2012
744
}
May 31, 2012
May 31, 2012
745
Jun 10, 2012
Jun 10, 2012
746
747
/* Load certificate(s) first... */
#ifdef HAVE_P11KIT
Jun 12, 2012
Jun 12, 2012
748
#ifndef HAVE_GNUTLS_CERTIFICATE_SET_KEY
Jun 10, 2012
Jun 10, 2012
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
if (key_is_p11) {
/* With GnuTLS 2.12 we can't *see* the key so we can't
do the expiry check or fill in intermediate CAs. */
err = gnutls_certificate_set_x509_key_file(vpninfo->https_cred,
cert_url, key_url,
GNUTLS_X509_FMT_PEM);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error loading PKCS#11 certificate: %s\n"),
gnutls_strerror(err));
ret = -EIO;
goto out;
}
ret = 0;
goto out;
}
Jun 12, 2012
Jun 12, 2012
765
766
767
768
#endif /* PKCS#11 for GnuTLS v2.12 */
/* GnuTLS 2.12 *can* handle the cert being in PKCS#11, if the key
isn't. Although it's not clear why anyone would ever do that. */
Jun 10, 2012
Jun 10, 2012
769
770
771
if (cert_is_p11) {
vpn_progress(vpninfo, PRG_TRACE,
_("Using PKCS#11 certificate %s\n"), cert_url);
Jun 8, 2012
Jun 8, 2012
772
Jun 10, 2012
Jun 10, 2012
773
774
775
776
777
778
779
780
781
err = gnutls_x509_crt_init(&cert);
if (err) {
ret = -ENOMEM;
goto out;
}
err = gnutls_x509_crt_import_pkcs11_url(cert, cert_url, 0);
if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
err = gnutls_x509_crt_import_pkcs11_url(cert, cert_url,
GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
May 31, 2012
May 31, 2012
782
783
if (err) {
vpn_progress(vpninfo, PRG_ERR,
Jun 10, 2012
Jun 10, 2012
784
_("Error loading certificate from PKCS#11: %s\n"),
May 31, 2012
May 31, 2012
785
gnutls_strerror(err));
Jun 10, 2012
Jun 10, 2012
786
787
ret = -EIO;
goto out;
May 31, 2012
May 31, 2012
788
}
Jun 10, 2012
Jun 10, 2012
789
goto got_certs;
May 31, 2012
May 31, 2012
790
}
Jun 12, 2012
Jun 12, 2012
791
#endif /* HAVE_P11KIT */
May 31, 2012
May 31, 2012
792
May 29, 2012
May 29, 2012
793
794
vpn_progress(vpninfo, PRG_TRACE,
_("Using certificate file %s\n"), vpninfo->cert);
May 31, 2012
May 31, 2012
795
796
797
798
ret = load_datum(vpninfo, &fdata, vpninfo->cert);
if (ret)
return ret;
May 31, 2012
May 31, 2012
799
Jun 10, 2012
Jun 10, 2012
800
801
802
if (!key_is_p11 && (vpninfo->cert_type == CERT_TYPE_PKCS12 ||
vpninfo->cert_type == CERT_TYPE_UNKNOWN)) {
/* PKCS#12 should actually contain certificates *and* private key */
Jun 9, 2012
Jun 9, 2012
803
804
805
806
ret = load_pkcs12_certificate(vpninfo, &fdata, &key,
&supporting_certs, &nr_supporting_certs,
&extra_certs, &nr_extra_certs,
&crl);
May 31, 2012
May 31, 2012
807
808
if (ret < 0)
goto out;
Jun 9, 2012
Jun 9, 2012
809
810
811
else if (!ret) {
if (nr_supporting_certs) {
cert = supporting_certs[0];
Jun 10, 2012
Jun 10, 2012
812
goto got_key;
Jun 9, 2012
Jun 9, 2012
813
814
815
816
817
818
}
vpn_progress(vpninfo, PRG_ERR,
_("PKCS#11 file contained no certificate\n"));
ret = -EINVAL;
goto out;
}
May 31, 2012
May 31, 2012
819
May 31, 2012
May 31, 2012
820
821
/* It returned NOT_PKCS12.
Fall through to try PEM formats. */
May 31, 2012
May 31, 2012
822
823
}
May 31, 2012
May 31, 2012
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* We need to know how many there are in *advance*; it won't just allocate
the array for us :( */
nr_extra_certs = count_x509_certificates(&fdata);
if (!nr_extra_certs)
nr_extra_certs = 1; /* wtf? Oh well, we'll fail later... */
extra_certs = calloc(nr_extra_certs, sizeof(cert));
if (!extra_certs) {
nr_extra_certs = 0;
ret = -ENOMEM;
goto out;
}
err = gnutls_x509_crt_list_import(extra_certs, &nr_extra_certs, &fdata,
GNUTLS_X509_FMT_PEM, 0);
if (err <= 0) {
const char *reason;
if (!err || err == GNUTLS_E_NO_CERTIFICATE_FOUND)
reason = _("No certificate found in file");
else
reason = gnutls_strerror(err);
May 29, 2012
May 29, 2012
845
vpn_progress(vpninfo, PRG_ERR,
May 31, 2012
May 31, 2012
846
_("Loading certificate failed: %s\n"),
May 31, 2012
May 31, 2012
847
848
849
reason);
ret = -EINVAL;
goto out;
May 29, 2012
May 29, 2012
850
}
May 31, 2012
May 31, 2012
851
852
nr_extra_certs = err;
err = 0;
May 31, 2012
May 31, 2012
853
Jun 10, 2012
Jun 10, 2012
854
goto got_certs;
Jun 10, 2012
Jun 10, 2012
855
got_certs:
Jun 10, 2012
Jun 10, 2012
856
/* Now we have the certificate(s) and we're looking for the private key... */
Jun 12, 2012
Jun 12, 2012
857
#if defined (HAVE_P11KIT) && defined (HAVE_GNUTLS_CERTIFICATE_SET_KEY)
Jun 10, 2012
Jun 10, 2012
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
if (key_is_p11) {
gnutls_pkcs11_privkey_t p11key = NULL;
vpn_progress(vpninfo, PRG_TRACE,
_("Using PKCS#11 key %s\n"), key_url);
err = gnutls_pkcs11_privkey_init(&p11key);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error initialising PKCS#11 key structure: %s\n"),
gnutls_strerror(err));
ret = -EIO;
goto out;
}
err = gnutls_pkcs11_privkey_import_url(p11key, key_url, 0);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error importing PKCS#11 URL %s: %s\n"),
key_url, gnutls_strerror(err));
gnutls_pkcs11_privkey_deinit(p11key);
ret = -EIO;
goto out;
}
err = gnutls_privkey_init(&pkey);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error initialising private key structure: %s\n"),
gnutls_strerror(err));
gnutls_pkcs11_privkey_deinit(p11key);
ret = -EIO;
goto out;
}
err = gnutls_privkey_import_pkcs11(pkey, p11key, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error importing PKCS#11 key into private key structure: %s\n"),
gnutls_strerror(err));
gnutls_pkcs11_privkey_deinit(p11key);
ret = -EIO;
goto out;
}
Jun 13, 2012
Jun 13, 2012
903
goto match_cert;
Jun 10, 2012
Jun 10, 2012
904
905
906
907
908
909
}
#endif
/* We're loading the private key from a file. Load the file into memory
unless it's the same as the certificate and we already loaded that. */
if (!fdata.data || vpninfo->sslkey != vpninfo->cert) {
May 31, 2012
May 31, 2012
910
gnutls_free(fdata.data);
Jun 9, 2012
Jun 9, 2012
911
fdata.data = NULL;
May 31, 2012
May 31, 2012
912
913
vpn_progress(vpninfo, PRG_TRACE,
Jun 9, 2012
Jun 9, 2012
914
_("Using private key file %s\n"), vpninfo->sslkey);
May 31, 2012
May 31, 2012
915
May 31, 2012
May 31, 2012
916
917
918
ret = load_datum(vpninfo, &fdata, vpninfo->sslkey);
if (ret)
goto out;
May 31, 2012
May 31, 2012
919
}
May 31, 2012
May 31, 2012
920
Jun 12, 2012
Jun 12, 2012
921
922
923
if (vpninfo->cert_type == CERT_TYPE_TPM ||
(vpninfo->cert_type == CERT_TYPE_UNKNOWN &&
strstr((char *)fdata.data, "-----BEGIN TSS KEY BLOB-----"))) {
Jun 13, 2012
Jun 13, 2012
924
#ifndef HAVE_TROUSERS
Jun 12, 2012
Jun 12, 2012
925
926
927
vpn_progress(vpninfo, PRG_ERR,
_("This version of OpenConnect was built without TPM support\n"));
return -EINVAL;
Jun 13, 2012
Jun 13, 2012
928
#else
Jun 13, 2012
Jun 13, 2012
929
ret = load_tpm_key(vpninfo, &fdata, &pkey, &pkey_sig);
Jun 13, 2012
Jun 13, 2012
930
931
932
if (ret)
goto out;
Jun 13, 2012
Jun 13, 2012
933
goto match_cert;
Jun 13, 2012
Jun 13, 2012
934
#endif
Jun 12, 2012
Jun 12, 2012
935
936
}
May 31, 2012
May 31, 2012
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
gnutls_x509_privkey_init(&key);
/* Try PKCS#1 (and PKCS#8 without password) first. GnuTLS doesn't
support OpenSSL's old PKCS#1-based encrypted format. We should
probably check for it and give a more coherent failure mode. */
err = gnutls_x509_privkey_import(key, &fdata, GNUTLS_X509_FMT_PEM);
if (err) {
/* If that fails, try PKCS#8 */
char *pass = vpninfo->cert_password;
/* Yay, just for fun this is *different* to PKCS#12. Where we could
try an empty password there, in this case the empty-password case
has already been *tried* by gnutls_x509_privkey_import(). If we
just call gnutls_x509_privkey_import_pkcs8() with a NULL password,
it'll SEGV. You have to set the GNUTLS_PKCS_PLAIN flag if you want
to try without a password. Passing NULL evidently isn't enough of
Jun 4, 2012
Jun 4, 2012
952
953
954
955
a hint. And in GnuTLS 3.1 where that crash has been fixed, passing
NULL will cause it to return GNUTLS_E_ENCRYPTED_STRUCTURE (a new
error code) rather than GNUTLS_E_DECRYPTION_FAILED. So just pass ""
instead of NULL, and don't worry about either case. */
May 31, 2012
May 31, 2012
956
957
958
959
960
961
962
while ((err = gnutls_x509_privkey_import_pkcs8(key, &fdata,
GNUTLS_X509_FMT_PEM,
pass?pass:"", 0))) {
if (err != GNUTLS_E_DECRYPTION_FAILED) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load private key as PKCS#8: %s\n"),
gnutls_strerror(err));
May 31, 2012
May 31, 2012
963
964
ret = -EINVAL;
goto out;
May 31, 2012
May 31, 2012
965
}
Jun 12, 2012
Jun 12, 2012
966
vpninfo->cert_password = NULL;
May 31, 2012
May 31, 2012
967
968
969
if (pass) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to decrypt PKCS#8 certificate file\n"));
Jun 12, 2012
Jun 12, 2012
970
free(pass);
May 31, 2012
May 31, 2012
971
}
Jun 13, 2012
Jun 13, 2012
972
973
err = request_passphrase(vpninfo, "openconnect_pem",
&pass, _("Enter PEM pass phrase:"));
May 31, 2012
May 31, 2012
974
if (err) {
May 31, 2012
May 31, 2012
975
976
ret = -EINVAL;
goto out;
May 31, 2012
May 31, 2012
977
978
}
}
Jun 12, 2012
Jun 12, 2012
979
980
free(pass);
vpninfo->cert_password = NULL;
May 31, 2012
May 31, 2012
981
}
Jun 10, 2012
Jun 10, 2012
982
983
/* Now attempt to make sure we use the *correct* certificate, to match the key */
May 31, 2012
May 31, 2012
984
985
986
987
988
err = gnutls_x509_privkey_get_key_id(key, 0, key_id, &key_id_size);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to get key ID: %s\n"),
gnutls_strerror(err));
Jun 13, 2012
Jun 13, 2012
989
ret = -EINVAL;
May 31, 2012
May 31, 2012
990
991
goto out;
}
Jun 10, 2012
Jun 10, 2012
992
for (i = 0; i < (extra_certs?nr_extra_certs:1); i++) {
May 31, 2012
May 31, 2012
993
994
995
unsigned char cert_id[20];
size_t cert_id_size = sizeof(cert_id);
Jun 10, 2012
Jun 10, 2012
996
err = gnutls_x509_crt_get_key_id(extra_certs?extra_certs[i]:cert, 0, cert_id, &cert_id_size);
May 31, 2012
May 31, 2012
997
998
999
1000
if (err)
continue;
if (cert_id_size == key_id_size && !memcmp(cert_id, key_id, key_id_size)) {