Skip to content

Latest commit

 

History

History
1620 lines (1423 loc) · 44 KB

gnutls.c

File metadata and controls

1620 lines (1423 loc) · 44 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;
May 31, 2012
May 31, 2012
332
333
334
335
336
337
338
err = request_passphrase(vpninfo, &pass,
_("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
450
451
452
453
454
455
456
457
458
459
460
461
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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#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,
_("Failed to create TPM hash object.\n"));
return GNUTLS_E_PK_SIGN_FAILED;
}
err = Tspi_Hash_SetHashValue(hash, data->size, data->data);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set value in TPM hash object.\n"));
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) {
vpn_progress(vpninfo, PRG_ERR,
_("TPM hash signature failed\n"));
return GNUTLS_E_PK_SIGN_FAILED;
}
return 0;
}
static int load_tpm_key(struct openconnect_info *vpninfo, gnutls_datum_t *fdata, gnutls_privkey_t *pkey)
{
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) {
if (!pass) {
err = request_passphrase(vpninfo, &pass, _("Enter TPM SRK PIN:"));
if (err)
goto out_srkpol;
}
/* We don't seem to get the error here... */
err = Tspi_Policy_SetSecret(vpninfo->srk_policy, TSS_SECRET_MODE_PLAIN,
strlen(pass), (void *)pass);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TPM PIN: %s\n"),
Trspi_Error_String(err));
goto out_srkpol;
}
free(pass);
pass = NULL;
/* ... 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;
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM key blob: %s\n"),
Trspi_Error_String(err));
if (err != TPM_E_AUTHFAIL)
goto out_srkpol;
}
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);
/* FIXME: Get key id using TSS_TSPATTRIB_KEYINFO_RSA_MODULUS etc. so
that we can ensure we have a matching cert. */
free (asn1.data);
return 0;
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
607
608
static int load_certificate(struct openconnect_info *vpninfo)
{
May 31, 2012
May 31, 2012
609
gnutls_datum_t fdata;
May 31, 2012
May 31, 2012
610
gnutls_x509_privkey_t key = NULL;
Jun 12, 2012
Jun 12, 2012
611
#ifdef HAVE_GNUTLS_CERTIFICATE_SET_KEY
Jun 10, 2012
Jun 10, 2012
612
gnutls_privkey_t pkey = NULL;
Jun 12, 2012
Jun 12, 2012
613
614
#endif
#ifdef HAVE_P11KIT
Jun 10, 2012
Jun 10, 2012
615
616
617
char *cert_url = (char *)vpninfo->cert;
char *key_url = (char *)vpninfo->sslkey;
#endif
May 31, 2012
May 31, 2012
618
gnutls_x509_crl_t crl = NULL;
May 31, 2012
May 31, 2012
619
620
gnutls_x509_crt_t last_cert, cert = NULL;
gnutls_x509_crt_t *extra_certs = NULL, *supporting_certs = NULL;
Jun 9, 2012
Jun 9, 2012
621
622
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
623
624
625
int err; /* GnuTLS error */
int ret = 0; /* our error (zero or -errno) */
int i;
Jun 10, 2012
Jun 10, 2012
626
int cert_is_p11 = 0, key_is_p11 = 0;
May 31, 2012
May 31, 2012
627
628
unsigned char key_id[20];
size_t key_id_size = sizeof(key_id);
Jun 11, 2012
Jun 11, 2012
629
char name[80];
May 29, 2012
May 29, 2012
630
Jun 9, 2012
Jun 9, 2012
631
632
fdata.data = NULL;
Jun 10, 2012
Jun 10, 2012
633
634
635
636
637
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
638
#ifdef HAVE_P11KIT
Jun 10, 2012
Jun 10, 2012
639
640
CK_OBJECT_CLASS class;
CK_ATTRIBUTE attr;
Jun 8, 2012
Jun 8, 2012
641
642
643
644
char pin_source[40];
P11KitUri *uri;
sprintf(pin_source, "openconnect:%p", vpninfo);
Jun 10, 2012
Jun 10, 2012
645
p11_kit_pin_register_callback(pin_source, pin_callback, vpninfo, NULL);
Jun 8, 2012
Jun 8, 2012
646
647
uri = p11_kit_uri_new();
Jun 10, 2012
Jun 10, 2012
648
Jun 10, 2012
Jun 10, 2012
649
650
651
652
653
654
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
655
if (cert_is_p11 &&
Jun 10, 2012
Jun 10, 2012
656
657
658
659
660
661
662
!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
663
664
665
p11_kit_uri_format(uri, P11_KIT_URI_FOR_OBJECT, &cert_url);
}
Jun 10, 2012
Jun 10, 2012
666
if (key_is_p11 &&
Jun 10, 2012
Jun 10, 2012
667
668
669
670
671
672
673
!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
674
675
p11_kit_uri_format(uri, P11_KIT_URI_FOR_OBJECT, &key_url);
}
Jun 10, 2012
Jun 10, 2012
676
Jun 8, 2012
Jun 8, 2012
677
p11_kit_uri_free(uri);
Jun 10, 2012
Jun 10, 2012
678
679
680
681
#else
vpn_progress(vpninfo, PRG_ERR,
_("This binary built without PKCS#11 support\n"));
return -EINVAL;
Jun 8, 2012
Jun 8, 2012
682
#endif
Jun 10, 2012
Jun 10, 2012
683
}
May 31, 2012
May 31, 2012
684
Jun 10, 2012
Jun 10, 2012
685
686
/* Load certificate(s) first... */
#ifdef HAVE_P11KIT
Jun 12, 2012
Jun 12, 2012
687
#ifndef HAVE_GNUTLS_CERTIFICATE_SET_KEY
Jun 10, 2012
Jun 10, 2012
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
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
704
705
706
707
#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
708
709
710
if (cert_is_p11) {
vpn_progress(vpninfo, PRG_TRACE,
_("Using PKCS#11 certificate %s\n"), cert_url);
Jun 8, 2012
Jun 8, 2012
711
Jun 10, 2012
Jun 10, 2012
712
713
714
715
716
717
718
719
720
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
721
722
if (err) {
vpn_progress(vpninfo, PRG_ERR,
Jun 10, 2012
Jun 10, 2012
723
_("Error loading certificate from PKCS#11: %s\n"),
May 31, 2012
May 31, 2012
724
gnutls_strerror(err));
Jun 10, 2012
Jun 10, 2012
725
726
ret = -EIO;
goto out;
May 31, 2012
May 31, 2012
727
}
Jun 10, 2012
Jun 10, 2012
728
goto got_certs;
May 31, 2012
May 31, 2012
729
}
Jun 12, 2012
Jun 12, 2012
730
#endif /* HAVE_P11KIT */
May 31, 2012
May 31, 2012
731
May 29, 2012
May 29, 2012
732
733
vpn_progress(vpninfo, PRG_TRACE,
_("Using certificate file %s\n"), vpninfo->cert);
May 31, 2012
May 31, 2012
734
735
736
737
ret = load_datum(vpninfo, &fdata, vpninfo->cert);
if (ret)
return ret;
May 31, 2012
May 31, 2012
738
Jun 10, 2012
Jun 10, 2012
739
740
741
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
742
743
744
745
ret = load_pkcs12_certificate(vpninfo, &fdata, &key,
&supporting_certs, &nr_supporting_certs,
&extra_certs, &nr_extra_certs,
&crl);
May 31, 2012
May 31, 2012
746
747
if (ret < 0)
goto out;
Jun 9, 2012
Jun 9, 2012
748
749
750
else if (!ret) {
if (nr_supporting_certs) {
cert = supporting_certs[0];
Jun 10, 2012
Jun 10, 2012
751
goto got_key;
Jun 9, 2012
Jun 9, 2012
752
753
754
755
756
757
}
vpn_progress(vpninfo, PRG_ERR,
_("PKCS#11 file contained no certificate\n"));
ret = -EINVAL;
goto out;
}
May 31, 2012
May 31, 2012
758
May 31, 2012
May 31, 2012
759
760
/* It returned NOT_PKCS12.
Fall through to try PEM formats. */
May 31, 2012
May 31, 2012
761
762
}
May 31, 2012
May 31, 2012
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* 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
784
vpn_progress(vpninfo, PRG_ERR,
May 31, 2012
May 31, 2012
785
_("Loading certificate failed: %s\n"),
May 31, 2012
May 31, 2012
786
787
788
reason);
ret = -EINVAL;
goto out;
May 29, 2012
May 29, 2012
789
}
May 31, 2012
May 31, 2012
790
791
nr_extra_certs = err;
err = 0;
May 31, 2012
May 31, 2012
792
Jun 10, 2012
Jun 10, 2012
793
goto got_certs;
Jun 10, 2012
Jun 10, 2012
794
got_certs:
Jun 10, 2012
Jun 10, 2012
795
/* Now we have the certificate(s) and we're looking for the private key... */
Jun 12, 2012
Jun 12, 2012
796
#if defined (HAVE_P11KIT) && defined (HAVE_GNUTLS_CERTIFICATE_SET_KEY)
Jun 10, 2012
Jun 10, 2012
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
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
842
goto match_cert;
Jun 10, 2012
Jun 10, 2012
843
844
845
846
847
848
}
#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
849
gnutls_free(fdata.data);
Jun 9, 2012
Jun 9, 2012
850
fdata.data = NULL;
May 31, 2012
May 31, 2012
851
852
vpn_progress(vpninfo, PRG_TRACE,
Jun 9, 2012
Jun 9, 2012
853
_("Using private key file %s\n"), vpninfo->sslkey);
May 31, 2012
May 31, 2012
854
May 31, 2012
May 31, 2012
855
856
857
ret = load_datum(vpninfo, &fdata, vpninfo->sslkey);
if (ret)
goto out;
May 31, 2012
May 31, 2012
858
}
May 31, 2012
May 31, 2012
859
Jun 12, 2012
Jun 12, 2012
860
861
862
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
863
#ifndef HAVE_TROUSERS
Jun 12, 2012
Jun 12, 2012
864
865
866
vpn_progress(vpninfo, PRG_ERR,
_("This version of OpenConnect was built without TPM support\n"));
return -EINVAL;
Jun 13, 2012
Jun 13, 2012
867
868
869
870
871
#else
ret = load_tpm_key(vpninfo, &fdata, &pkey);
if (ret)
goto out;
Jun 13, 2012
Jun 13, 2012
872
goto match_cert;
Jun 13, 2012
Jun 13, 2012
873
#endif
Jun 12, 2012
Jun 12, 2012
874
875
}
May 31, 2012
May 31, 2012
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
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
891
892
893
894
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
895
896
897
898
899
900
901
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
902
903
ret = -EINVAL;
goto out;
May 31, 2012
May 31, 2012
904
}
Jun 12, 2012
Jun 12, 2012
905
vpninfo->cert_password = NULL;
May 31, 2012
May 31, 2012
906
907
908
if (pass) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to decrypt PKCS#8 certificate file\n"));
Jun 12, 2012
Jun 12, 2012
909
free(pass);
May 31, 2012
May 31, 2012
910
911
912
913
}
err = request_passphrase(vpninfo, &pass,
_("Enter PEM pass phrase:"));
if (err) {
May 31, 2012
May 31, 2012
914
915
ret = -EINVAL;
goto out;
May 31, 2012
May 31, 2012
916
917
}
}
Jun 12, 2012
Jun 12, 2012
918
919
free(pass);
vpninfo->cert_password = NULL;
May 31, 2012
May 31, 2012
920
}
Jun 10, 2012
Jun 10, 2012
921
922
/* Now attempt to make sure we use the *correct* certificate, to match the key */
May 31, 2012
May 31, 2012
923
924
925
926
927
928
929
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));
goto out;
}
Jun 10, 2012
Jun 10, 2012
930
for (i = 0; i < (extra_certs?nr_extra_certs:1); i++) {
May 31, 2012
May 31, 2012
931
932
933
unsigned char cert_id[20];
size_t cert_id_size = sizeof(cert_id);
Jun 10, 2012
Jun 10, 2012
934
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
935
936
937
938
if (err)
continue;
if (cert_id_size == key_id_size && !memcmp(cert_id, key_id, key_id_size)) {
Jun 10, 2012
Jun 10, 2012
939
940
if (extra_certs) {
cert = extra_certs[i];
May 31, 2012
May 31, 2012
941
Jun 10, 2012
Jun 10, 2012
942
943
944
/* Move the rest of the array down */
for (; i < nr_extra_certs - 1; i++)
extra_certs[i] = extra_certs[i+1];
May 31, 2012
May 31, 2012
945
Jun 10, 2012
Jun 10, 2012
946
947
948
nr_extra_certs--;
}
goto got_key;
May 31, 2012
May 31, 2012
949
950
951
952
953
954
955
956
}
}
/* We shouldn't reach this. It means that we didn't find *any* matching cert */
vpn_progress(vpninfo, PRG_ERR,
_("No SSL certificate found to match private key\n"));
ret = -EINVAL;
goto out;
Jun 13, 2012
Jun 13, 2012
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
#ifdef HAVE_GNUTLS_CERTIFICATE_SET_KEY
match_cert:
if (!cert) {
/* FIXME: How do we check which cert matches the pkey?
For now we just assume that the first one in the list is the right one. */
cert = extra_certs[0];
/* Move the rest of the array down */
for (i = 0; i < nr_extra_certs - 1; i++)
extra_certs[i] = extra_certs[i+1];
nr_extra_certs--;
}
#endif
Jun 10, 2012
Jun 10, 2012
972
973
got_key:
/* Now we have both cert(s) and key, and we should be ready to go. */
May 31, 2012
May 31, 2012
974
check_certificate_expiry(vpninfo, cert);
Jun 11, 2012
Jun 11, 2012
975
976
977
get_cert_name(cert, name, sizeof(name));
vpn_progress(vpninfo, PRG_INFO, _("Using client certificate '%s'\n"),
name);
May 31, 2012
May 31, 2012
978
979
980
981
982
983
984
if (crl) {
err = gnutls_certificate_set_x509_crl(vpninfo->https_cred, &crl, 1);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Setting certificate recovation list failed: %s\n"),
gnutls_strerror(err));
May 31, 2012
May 31, 2012
985
goto out;
May 31, 2012
May 31, 2012
986
987
}
}
May 31, 2012
May 31, 2012
988
989
990
991
992
/* OpenSSL has problems with certificate chains — if there are
multiple certs with the same name, it doesn't necessarily
choose the _right_ one. (RT#1942)
Pick the right ones for ourselves and add them manually. */
Jun 9, 2012
Jun 9, 2012
993
994
995
996
997
998
999
1000
if (nr_supporting_certs) {
/* We already got a bunch of certs from PKCS#12 file.
Remember how many need to be freed when we're done,
since we'll expand the supporting_certs array with
more from the cafile if we can. */
last_cert = supporting_certs[nr_supporting_certs-1];
certs_to_free = nr_supporting_certs;