Skip to content

Latest commit

 

History

History
669 lines (588 loc) · 17.3 KB

yubikey.c

File metadata and controls

669 lines (588 loc) · 17.3 KB
 
1
2
3
/*
* OpenConnect (SSL + DTLS) VPN client
*
Jan 26, 2015
Jan 26, 2015
4
* Copyright © 2008-2015 Intel Corporation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*
* 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.
*/
#include <config.h>
Jun 30, 2021
Jun 30, 2021
20
21
#include "openconnect-internal.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define NAME_TAG 0x71
#define NAME_LIST_TAG 0x72
#define KEY_TAG 0x73
#define CHALLENGE_TAG 0x74
#define RESPONSE_TAG 0x75
#define T_RESPONSE_TAG 0x76
#define NO_RESPONSE_TAG 0x77
#define PROPERTY_TAG 0x78
#define VERSION_TAG 0x79
#define IMF_TAG 0x7a
#define PUT_INS 0x01
#define DELETE_INS 0x02
#define SET_CODE_INS 0x03
#define RESET_INS 0x04
#define LIST_INS 0xa1
#define CALCULATE_INS 0xa2
#define VALIDATE_INS 0xa3
#define CALCULATE_ALL_INS 0xa4
#define SEND_REMAINING_INS 0xa5
static const unsigned char appselect[] = { 0x00, 0xa4, 0x04, 0x00, 0x07,
0xa0, 0x00, 0x00, 0x05, 0x27, 0x21, 0x01 };
static const unsigned char list_keys[] = { 0x00, LIST_INS, 0x00, 0x00 };
static const unsigned char send_remaining[] = { 0x00, SEND_REMAINING_INS, 0x00, 0x00 };
Nov 12, 2014
Nov 12, 2014
53
54
55
56
#ifdef _WIN32
#define scard_error(st) openconnect__win32_strerror(st)
#define free_scard_error(str) free(str)
#else
May 31, 2018
May 31, 2018
57
#define scard_error(st) ((char *)pcsc_stringify_error(st))
Jul 19, 2021
Jul 19, 2021
58
#define free_scard_error(str)
Nov 12, 2014
Nov 12, 2014
59
60
#endif
Oct 3, 2018
Oct 3, 2018
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifdef __APPLE__
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <winscard.h>
#endif
struct oc_pcsc_ctx {
SCARDHANDLE pcsc_ctx, pcsc_card;
char *yubikey_objname;
int yubikey_pw_set;
int yubikey_mode;
};
77
78
79
80
81
82
83
84
85
static int yubikey_cmd(struct openconnect_info *vpninfo, SCARDHANDLE card, int errlvl,
const char *desc,
const unsigned char *out, size_t outlen, struct oc_text_buf *buf)
{
DWORD status;
buf_truncate(buf);
do {
Nov 12, 2014
Nov 12, 2014
86
DWORD respsize = 258;
Oct 3, 2018
Oct 3, 2018
87
88
89
if (buf_ensure_space(buf, 258))
return -ENOMEM;
Oct 3, 2018
Oct 3, 2018
90
91
92
93
status = SCardTransmit (card, SCARD_PCI_T1, out, outlen,
NULL, (unsigned char *)&buf->data[buf->pos], &respsize);
if (status != SCARD_S_SUCCESS) {
Nov 12, 2014
Nov 12, 2014
94
char *pcsc_err = scard_error(status);
95
96
vpn_progress(vpninfo, errlvl,
_("Failed to send \"%s\" to ykneo-oath applet: %s\n"),
Nov 12, 2014
Nov 12, 2014
97
98
desc, pcsc_err);
free_scard_error(pcsc_err);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
return -EIO;
}
if (respsize < 2) {
vpn_progress(vpninfo, errlvl,
_("Invalid short response to \"%s\" from ykneo-oath applet\n"),
desc);
return -EIO;
}
buf->pos += respsize - 2;
/* Continuation */
out = send_remaining;
outlen = sizeof(send_remaining);
} while (buf->data[buf->pos] == 0x61);
Jan 26, 2015
Jan 26, 2015
115
116
status = load_be16(buf->data + buf->pos);
if (status == 0x9000)
117
118
119
return 0;
vpn_progress(vpninfo, errlvl,
Nov 14, 2014
Nov 14, 2014
120
121
122
123
124
125
126
127
_("Failure response to \"%s\": %04x\n"),
desc, (unsigned)status);
switch (status) {
case 0x6a80:
return -EINVAL;
default:
return -EIO;
}
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
}
static int buf_tlv(struct oc_text_buf *buf, int *loc, unsigned char *type)
{
int len;
int left = buf->pos - *loc;
if (left < 2)
return -EINVAL;
*type = (unsigned char)buf->data[(*loc)++];
len = (unsigned char)buf->data[(*loc)++];
left -= 2;
if (len > 0x82)
return -EINVAL;
else if (len == 0x81) {
if (left < 1)
return -EINVAL;
len = (unsigned char)buf->data[(*loc)++];
left--;
} else if (len == 0x82) {
if (left < 2)
return -EINVAL;
len = (unsigned char)buf->data[(*loc)++];
len <<= 8;
Nov 17, 2020
Nov 17, 2020
154
len |= (unsigned char)buf->data[(*loc)++];
155
156
157
158
159
160
161
162
163
left -= 2;
}
if (left < len)
return -EINVAL;
return len;
}
Nov 14, 2014
Nov 14, 2014
164
165
166
static int select_yubioath_applet(struct openconnect_info *vpninfo,
SCARDHANDLE pcsc_card, struct oc_text_buf *buf)
{
Nov 14, 2014
Nov 14, 2014
167
int ret, tlvlen, tlvpos, id_len, chall_len;
Nov 14, 2014
Nov 14, 2014
168
unsigned char type;
Nov 14, 2014
Nov 14, 2014
169
unsigned char applet_id[16], challenge[16];
Nov 14, 2014
Nov 14, 2014
170
unsigned char *applet_ver;
Feb 4, 2015
Feb 4, 2015
171
172
char *pin = NULL;
int pin_len = 0;
Nov 14, 2014
Nov 14, 2014
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
ret = yubikey_cmd(vpninfo, pcsc_card, PRG_DEBUG, _("select applet command"),
appselect, sizeof(appselect), buf);
if (ret)
return ret;
tlvpos = 0;
tlvlen = buf_tlv(buf, &tlvpos, &type);
if (tlvlen < 0 || type != VERSION_TAG || tlvlen != 3) {
bad_applet:
vpn_progress(vpninfo, PRG_ERR,
_("Unrecognised response from ykneo-oath applet\n"));
return -EIO;
}
applet_ver = (void *)&buf->data[tlvpos];
tlvpos += tlvlen;
tlvlen = buf_tlv(buf, &tlvpos, &type);
Nov 14, 2014
Nov 14, 2014
193
if (tlvlen < 0 || type != NAME_TAG || tlvlen > sizeof(applet_id))
Nov 14, 2014
Nov 14, 2014
194
goto bad_applet;
Nov 14, 2014
Nov 14, 2014
195
memcpy(applet_id, &buf->data[tlvpos], tlvlen);
Nov 14, 2014
Nov 14, 2014
196
197
198
199
id_len = tlvlen;
tlvpos += tlvlen;
/* Only print this during the first discovery loop */
Oct 3, 2018
Oct 3, 2018
200
if (!vpninfo->pcsc)
Nov 14, 2014
Nov 14, 2014
201
202
203
204
205
206
207
vpn_progress(vpninfo, PRG_INFO, _("Found ykneo-oath applet v%d.%d.%d.\n"),
applet_ver[0], applet_ver[1], applet_ver[2]);
if (tlvpos != buf->pos) {
unsigned char chalresp[7 + SHA1_SIZE + 10];
tlvlen = buf_tlv(buf, &tlvpos, &type);
Nov 14, 2014
Nov 14, 2014
208
if (tlvlen < 0 || type != CHALLENGE_TAG || tlvlen > sizeof(challenge))
Nov 14, 2014
Nov 14, 2014
209
210
goto bad_applet;
Nov 14, 2014
Nov 14, 2014
211
212
213
memcpy(challenge, &buf->data[tlvpos], tlvlen);
chall_len = tlvlen;
Oct 3, 2018
Oct 3, 2018
214
/* On later invocations, we know there must have been a
Feb 12, 2022
Feb 12, 2022
215
* successful authentication in the past. So try the same
Oct 3, 2018
Oct 3, 2018
216
217
* hash first, and only retry in the loop on failure. */
if (!vpninfo->pcsc) {
Nov 14, 2014
Nov 14, 2014
218
219
220
struct oc_auth_form f;
struct oc_form_opt o;
Oct 3, 2018
Oct 3, 2018
221
retry_pass:
Nov 14, 2014
Nov 14, 2014
222
223
224
225
226
227
228
229
230
231
232
233
234
memset(&f, 0, sizeof(f));
f.auth_id = (char *)"yubikey_oath_pin";
f.opts = &o;
f.message = (char *)_("PIN required for Yubikey OATH applet");
o.next = NULL;
o.type = OC_FORM_OPT_PASSWORD;
o.name = (char *)"yubikey_pin";
o.label = (char *)_("Yubikey PIN:");
o._value = NULL;
ret = process_auth_form(vpninfo, &f);
if (ret)
Feb 4, 2015
Feb 4, 2015
235
236
237
238
239
240
241
242
243
244
245
246
goto out;
if (!o._value) {
ret = -EPERM;
goto out;
}
if (pin) {
memset(pin, 0, pin_len);
free(pin);
}
pin = o._value;
pin_len = strlen(pin);
Feb 4, 2015
Feb 4, 2015
247
/* This *should* be UTF-8 but see the workaround below. */
Feb 4, 2015
Feb 4, 2015
248
ret = openconnect_hash_yubikey_password(vpninfo, o._value, pin_len,
Feb 4, 2015
Feb 4, 2015
249
applet_id, id_len);
Nov 14, 2014
Nov 14, 2014
250
if (ret)
Feb 4, 2015
Feb 4, 2015
251
goto out;
Nov 14, 2014
Nov 14, 2014
252
}
Oct 3, 2018
Oct 3, 2018
253
retry_hash:
Nov 14, 2014
Nov 14, 2014
254
if (openconnect_yubikey_chalresp(vpninfo, &challenge, chall_len,
Nov 14, 2014
Nov 14, 2014
255
256
257
chalresp + 7)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to calculate Yubikey unlock response\n"));
Feb 4, 2015
Feb 4, 2015
258
259
ret = -EIO;
goto out;
Nov 14, 2014
Nov 14, 2014
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
}
chalresp[0] = 0;
chalresp[1] = VALIDATE_INS;
chalresp[2] = 0;
chalresp[3] = 0;
chalresp[4] = sizeof(chalresp) - 5;
chalresp[5] = RESPONSE_TAG;
chalresp[6] = SHA1_SIZE;
/* Response is already filled in */
chalresp[7 + SHA1_SIZE] = CHALLENGE_TAG;
chalresp[8 + SHA1_SIZE] = 8;
memset(chalresp + 9 + SHA1_SIZE, 0xff, 8);
ret = yubikey_cmd(vpninfo, pcsc_card, PRG_ERR, _("unlock command"),
chalresp, sizeof(chalresp), buf);
Nov 14, 2014
Nov 14, 2014
276
if (ret == -EINVAL) {
Nov 14, 2014
Nov 14, 2014
277
memset(vpninfo->yubikey_pwhash, 0, sizeof(vpninfo->yubikey_pwhash));
Feb 4, 2015
Feb 4, 2015
278
279
if (pin) {
/* Try working around pre-KitKat PBKDF2 bug discussed at
Jun 28, 2021
Jun 28, 2021
280
* https://forum.yubico.com/viewtopica454-3.html?f=26&t=1601#p6807 and
Jun 27, 2021
Jun 27, 2021
281
* https://android-developers.googleblog.com/2013/12/changes-to-secretkeyfactory-api-in.html */
Feb 4, 2015
Feb 4, 2015
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
const char *in;
char *out;
/* Convert the UTF-8 PIN to byte-truncated form in-place */
in = out = pin;
while (*in) {
int c = get_utf8char(&in);
if (c < 0) {
/* Screw it. Break out of the loop in such a fashion
* that we don't try the 'converted' result. */
in = out;
break;
}
*(out++) = c;
}
/* If out == in then the string only contained ASCII so
* there was no conversion to be done (or was invalid
* UTF-8 and hit the error case above). So don't try. */
if (out != in &&
!openconnect_hash_yubikey_password(vpninfo, pin, out - pin,
applet_id, id_len)) {
/* We'll have printed with PRG_ERR when the proper
* encoding failed. So use PRG_ERR here too. */
vpn_progress(vpninfo, PRG_ERR,
_("Trying truncated-char PBKBF2 variant of Yubikey PIN\n"));
Oct 3, 2018
Oct 3, 2018
307
goto retry_hash;
Feb 4, 2015
Feb 4, 2015
308
309
}
}
Oct 3, 2018
Oct 3, 2018
310
goto retry_pass;
Nov 14, 2014
Nov 14, 2014
311
}
Nov 14, 2014
Nov 14, 2014
312
}
Feb 4, 2015
Feb 4, 2015
313
out:
Dec 21, 2018
Dec 21, 2018
314
free_pass(&pin);
Feb 4, 2015
Feb 4, 2015
315
return ret;
Nov 14, 2014
Nov 14, 2014
316
317
}
Nov 17, 2014
Nov 17, 2014
318
319
320
321
322
323
324
325
#ifdef _WIN32
#define reader_len wcslen
#else
#define SCardListReadersW SCardListReaders
#define SCardConnectW SCardConnect
#define reader_len strlen
#endif
326
327
328
329
int set_yubikey_mode(struct openconnect_info *vpninfo, const char *token_str)
{
SCARDHANDLE pcsc_ctx, pcsc_card;
LONG status;
Nov 17, 2014
Nov 17, 2014
330
331
332
#ifdef _WIN32
wchar_t *readers = NULL, *reader;
#else
333
char *readers = NULL, *reader;
Nov 17, 2014
Nov 17, 2014
334
#endif
335
336
337
338
339
340
DWORD readers_size, proto;
int ret, tlvlen, tlvpos;
struct oc_text_buf *buf = NULL;
status = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &pcsc_ctx);
if (status != SCARD_S_SUCCESS) {
Nov 12, 2014
Nov 12, 2014
341
char *pcsc_err = scard_error(status);
342
vpn_progress(vpninfo, PRG_ERR, _("Failed to establish PC/SC context: %s\n"),
Nov 12, 2014
Nov 12, 2014
343
344
pcsc_err);
free_scard_error(pcsc_err);
345
346
347
348
349
return -EIO;
}
vpn_progress(vpninfo, PRG_TRACE, _("Established PC/SC context\n"));
ret = -ENOENT;
Nov 17, 2014
Nov 17, 2014
350
status = SCardListReadersW(pcsc_ctx, NULL, NULL, &readers_size);
351
if (status != SCARD_S_SUCCESS) {
Nov 12, 2014
Nov 12, 2014
352
char *pcsc_err = scard_error(status);
353
vpn_progress(vpninfo, PRG_ERR, _("Failed to query reader list: %s\n"),
Nov 12, 2014
Nov 12, 2014
354
355
pcsc_err);
free_scard_error(pcsc_err);
356
357
goto out_ctx;
}
Nov 17, 2014
Nov 17, 2014
358
readers = calloc(readers_size, sizeof(readers[0]));
359
360
361
if (!readers)
goto out_ctx;
Nov 17, 2014
Nov 17, 2014
362
status = SCardListReadersW(pcsc_ctx, NULL, readers, &readers_size);
363
if (status != SCARD_S_SUCCESS) {
Nov 12, 2014
Nov 12, 2014
364
char *pcsc_err = scard_error(status);
365
vpn_progress(vpninfo, PRG_ERR, _("Failed to query reader list: %s\n"),
Nov 12, 2014
Nov 12, 2014
366
367
pcsc_err);
free_scard_error(pcsc_err);
368
369
370
371
372
goto out_ctx;
}
buf = buf_alloc();
Nov 17, 2014
Nov 17, 2014
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
reader = readers;
while (reader[0]) {
unsigned char type;
#ifdef _WIN32
char *reader_utf8;
int reader_len;
reader_len = WideCharToMultiByte(CP_UTF8, 0, reader, -1, NULL, 0, NULL, NULL);
reader_utf8 = malloc(reader_len);
if (!reader_utf8)
goto next_reader;
WideCharToMultiByte(CP_UTF8, 0, reader, -1, reader_utf8, reader_len, NULL, NULL);
#else
#define reader_utf8 reader
#endif
status = SCardConnectW(pcsc_ctx, reader, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T1,
&pcsc_card, &proto);
390
if (status != SCARD_S_SUCCESS) {
Nov 12, 2014
Nov 12, 2014
391
char *pcsc_err = scard_error(status);
392
vpn_progress(vpninfo, PRG_ERR, _("Failed to connect to PC/SC reader '%s': %s\n"),
Nov 17, 2014
Nov 17, 2014
393
reader_utf8, pcsc_err);
Nov 12, 2014
Nov 12, 2014
394
free_scard_error(pcsc_err);
Nov 17, 2014
Nov 17, 2014
395
goto free_reader_utf8;
Nov 17, 2014
Nov 17, 2014
397
vpn_progress(vpninfo, PRG_TRACE, _("Connected PC/SC reader '%s'\n"), reader_utf8);
398
399
400
status = SCardBeginTransaction(pcsc_card);
if (status != SCARD_S_SUCCESS) {
Nov 17, 2014
Nov 17, 2014
401
402
403
404
char *pcsc_err = scard_error(status);
vpn_progress(vpninfo, PRG_ERR, _("Failed to obtain exclusive access to reader '%s': %s\n"),
reader_utf8, pcsc_err);
free_scard_error(pcsc_err);
Nov 12, 2014
Nov 12, 2014
405
goto disconnect;
Oct 3, 2018
Oct 3, 2018
407
Nov 14, 2014
Nov 14, 2014
408
ret = select_yubioath_applet(vpninfo, pcsc_card, buf);
409
if (ret)
Nov 17, 2014
Nov 17, 2014
410
goto end_trans;
411
412
413
ret = yubikey_cmd(vpninfo, pcsc_card, PRG_ERR, _("list keys command"), list_keys, sizeof(list_keys), buf);
if (ret)
Nov 17, 2014
Nov 17, 2014
414
goto end_trans;
415
416
417
418
419
420
tlvpos = 0;
while (tlvpos < buf->pos) {
unsigned char mode, hash;
tlvlen = buf_tlv(buf, &tlvpos, &type);
Nov 14, 2014
Nov 14, 2014
421
422
423
424
if (type != NAME_LIST_TAG || tlvlen < 1) {
bad_applet:
vpn_progress(vpninfo, PRG_ERR,
_("Unrecognised response from ykneo-oath applet\n"));
Nov 17, 2014
Nov 17, 2014
425
goto end_trans;
Nov 14, 2014
Nov 14, 2014
426
}
427
428
429
430
431
432
433
434
435
mode = buf->data[tlvpos] & 0xf0;
hash = buf->data[tlvpos] & 0x0f;
if (mode != 0x10 && mode != 0x20)
goto bad_applet;
if (hash != 0x01 && hash != 0x02)
goto bad_applet;
if (!token_str ||
((tlvlen - 1 == strlen(token_str)) && !memcmp(token_str, &buf->data[tlvpos+1], tlvlen-1))) {
Oct 3, 2018
Oct 3, 2018
436
437
char *objname = strndup(&buf->data[tlvpos+1], tlvlen-1);
if (!objname) {
438
439
ret = -ENOMEM;
SCardEndTransaction(pcsc_card, SCARD_LEAVE_CARD);
Nov 12, 2014
Nov 12, 2014
440
SCardDisconnect(pcsc_card, SCARD_LEAVE_CARD);
441
442
443
444
445
446
447
goto out_ctx;
}
/* Translators: This is filled in with mode and hash type, and the key identifier.
e.g. "Found HOTP/SHA1 key: 'Work VPN key'\n" */
vpn_progress(vpninfo, PRG_INFO, _("Found %s/%s key '%s' on '%s'\n"),
(mode == 0x20) ? "TOTP" : "HOTP",
(hash == 0x2) ? "SHA256" : "SHA1",
Oct 3, 2018
Oct 3, 2018
448
objname, reader_utf8);
Oct 3, 2018
Oct 3, 2018
450
451
452
453
454
455
456
457
458
vpninfo->pcsc = calloc(1, sizeof(*vpninfo->pcsc));
if (!vpninfo->pcsc) {
free(objname);
goto out_ctx;
}
vpninfo->pcsc->yubikey_objname = objname;
vpninfo->pcsc->yubikey_mode = mode;
vpninfo->pcsc->pcsc_ctx = pcsc_ctx;
vpninfo->pcsc->pcsc_card = pcsc_card;
Nov 17, 2014
Nov 17, 2014
459
vpninfo->token_mode = OC_TOKEN_MODE_YUBIOATH;
460
461
462
463
464
465
466
467
468
SCardEndTransaction(pcsc_card, SCARD_LEAVE_CARD);
goto success;
}
tlvpos += tlvlen;
}
if (token_str) {
vpn_progress(vpninfo, PRG_ERR,
_("Token '%s' not found on Yubikey '%s'. Searching for another Yubikey...\n"),
Nov 17, 2014
Nov 17, 2014
469
token_str, reader_utf8);
Nov 17, 2014
Nov 17, 2014
472
end_trans:
473
SCardEndTransaction(pcsc_card, SCARD_LEAVE_CARD);
Nov 12, 2014
Nov 12, 2014
474
475
disconnect:
SCardDisconnect(pcsc_card, SCARD_LEAVE_CARD);
Nov 17, 2014
Nov 17, 2014
476
477
478
479
480
481
482
483
free_reader_utf8:
#ifdef _WIN32
free(reader_utf8);
next_reader:
#endif
while (*reader)
reader++;
reader++;
484
485
486
487
488
489
490
}
ret = -ENOENT;
out_ctx:
SCardReleaseContext(pcsc_ctx);
success:
free(readers);
buf_free(buf);
Oct 3, 2018
Oct 3, 2018
491
492
493
494
495
496
497
498
499
500
501
502
return ret;
}
/* Return value:
* < 0, if unable to generate a tokencode
* = 0, on success
*/
int can_gen_yubikey_code(struct openconnect_info *vpninfo,
struct oc_auth_form *form,
struct oc_form_opt *opt)
{
Oct 3, 2018
Oct 3, 2018
503
if (vpninfo->token_bypassed)
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
return -EINVAL;
if (vpninfo->token_tries == 0) {
vpn_progress(vpninfo, PRG_DEBUG,
_("OK to generate INITIAL tokencode\n"));
vpninfo->token_time = 0;
} else if (vpninfo->token_tries == 1) {
vpn_progress(vpninfo, PRG_DEBUG,
_("OK to generate NEXT tokencode\n"));
vpninfo->token_time += 30;
} else {
/* limit the number of retries, to avoid account lockouts */
vpn_progress(vpninfo, PRG_INFO,
_("Server is rejecting the Yubikey token; switching to manual entry\n"));
return -ENOENT;
}
return 0;
}
static int tlvlen_len(int tlvlen)
{
if (tlvlen < 0x80)
return 1;
else if (tlvlen < 0x100)
return 2;
else
return 3;
}
static int append_tlvlen(unsigned char *p, int tlvlen)
{
if (tlvlen < 0x80) {
if (p)
p[0] = tlvlen;
return 1;
} else if (tlvlen < 0x100) {
if (p) {
p[0] = 0x81;
p[1] = tlvlen;
}
return 2;
} else {
if (p) {
p[0] = 0x82;
Jan 26, 2015
Jan 26, 2015
547
store_be16(p + 1, tlvlen);
548
549
550
551
552
553
554
555
556
557
558
}
return 3;
}
}
int do_gen_yubikey_code(struct openconnect_info *vpninfo,
struct oc_auth_form *form,
struct oc_form_opt *opt)
{
struct oc_text_buf *respbuf = NULL;
DWORD status;
Oct 3, 2018
Oct 3, 2018
559
int name_len = strlen(vpninfo->pcsc->yubikey_objname);
560
561
562
563
564
565
int name_tlvlen;
int calc_tlvlen;
unsigned char *reqbuf = NULL;
int tokval;
int i = 0;
int ret;
Oct 3, 2018
Oct 3, 2018
566
567
568
569
570
571
if (!vpninfo->token_time)
vpninfo->token_time = time(NULL);
vpn_progress(vpninfo, PRG_INFO, _("Generating Yubikey token code\n"));
Oct 3, 2018
Oct 3, 2018
572
status = SCardBeginTransaction(vpninfo->pcsc->pcsc_card);
573
if (status != SCARD_S_SUCCESS) {
Nov 17, 2014
Nov 17, 2014
574
575
576
577
char *pcsc_err = scard_error(status);
vpn_progress(vpninfo, PRG_ERR, _("Failed to obtain exclusive access to Yubikey: %s\n"),
pcsc_err);
free_scard_error(pcsc_err);
578
579
580
581
return -EIO;
}
respbuf = buf_alloc();
Oct 3, 2018
Oct 3, 2018
582
ret = select_yubioath_applet(vpninfo, vpninfo->pcsc->pcsc_card, respbuf);
583
584
585
if (ret)
goto out;
Oct 3, 2018
Oct 3, 2018
586
name_tlvlen = tlvlen_len(strlen(vpninfo->pcsc->yubikey_objname));
587
588
calc_tlvlen = 1 /* NAME_TAG */ + name_tlvlen + name_len +
1 /* CHALLENGE_TAG */ + 1 /* Challenge TLV len */;
Oct 3, 2018
Oct 3, 2018
589
if (vpninfo->pcsc->yubikey_mode == 0x20)
590
591
592
593
594
calc_tlvlen += 8; /* TOTP needs the time as challenge */
reqbuf = malloc(4 + tlvlen_len(calc_tlvlen) + calc_tlvlen);
if (!reqbuf)
goto out;
Oct 3, 2018
Oct 3, 2018
595
596
597
598
599
600
601
602
reqbuf[i++] = 0;
reqbuf[i++] = CALCULATE_INS;
reqbuf[i++] = 0;
reqbuf[i++] = 1;
i += append_tlvlen(reqbuf + i, calc_tlvlen);
reqbuf[i++] = NAME_TAG;
i += append_tlvlen(reqbuf + i, name_len);
Oct 3, 2018
Oct 3, 2018
603
memcpy(reqbuf + i, vpninfo->pcsc->yubikey_objname, name_len);
604
605
i += name_len;
reqbuf[i++] = CHALLENGE_TAG;
Oct 3, 2018
Oct 3, 2018
606
if (vpninfo->pcsc->yubikey_mode == 0x20) {
607
608
609
610
611
612
long token_steps = vpninfo->token_time / 30;
reqbuf[i++] = 8;
reqbuf[i++] = 0;
reqbuf[i++] = 0;
reqbuf[i++] = 0;
reqbuf[i++] = 0;
Jan 26, 2015
Jan 26, 2015
613
614
store_be32(reqbuf + i, token_steps);
i += 4;
615
616
617
618
} else {
reqbuf[i++] = 0; /* HOTP mode, zero-length challenge */
}
Oct 3, 2018
Oct 3, 2018
619
ret = yubikey_cmd(vpninfo, vpninfo->pcsc->pcsc_card, PRG_ERR, _("calculate command"),
620
621
622
623
624
reqbuf, i, respbuf);
if (ret)
goto out;
if (respbuf->pos != 7 || (unsigned char)respbuf->data[0] != T_RESPONSE_TAG ||
Nov 14, 2014
Nov 14, 2014
625
626
627
628
629
630
respbuf->data[1] != 5 || respbuf->data[2] > 8 || respbuf->data[2] < 6) {
vpn_progress(vpninfo, PRG_ERR,
_("Unrecognised response from Yubikey when generating tokencode\n"));
ret = -EIO;
goto out;
}
Jan 26, 2015
Jan 26, 2015
632
tokval = load_be32(respbuf->data + 3);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
opt->_value = malloc(respbuf->data[2] + 1);
if (!opt->_value) {
ret = -ENOMEM;
goto out;
}
i = respbuf->data[2];
opt->_value[i] = 0;
while (i--) {
opt->_value[i] = '0' + tokval % 10;
tokval /= 10;
}
vpninfo->token_tries++;
out:
Oct 3, 2018
Oct 3, 2018
648
SCardEndTransaction(vpninfo->pcsc->pcsc_card, SCARD_LEAVE_CARD);
649
650
651
652
653
buf_free(respbuf);
free(reqbuf);
return ret;
}
Oct 3, 2018
Oct 3, 2018
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
void release_pcsc_ctx(struct openconnect_info *vpninfo)
{
if (!vpninfo->pcsc)
return;
if (vpninfo->token_mode == OC_TOKEN_MODE_YUBIOATH) {
SCardDisconnect(vpninfo->pcsc->pcsc_card, SCARD_LEAVE_CARD);
SCardReleaseContext(vpninfo->pcsc->pcsc_ctx);
}
memset(vpninfo->yubikey_pwhash, 0, sizeof(vpninfo->yubikey_pwhash));
free(vpninfo->pcsc->yubikey_objname);
free(vpninfo->pcsc);
vpninfo->pcsc = NULL;
}