-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathykchalresp.c
361 lines (302 loc) · 11.9 KB
/
ykchalresp.c
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
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
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
260
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
308
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
/*
* Copyright (c) 2020-2024 LEVAI Daniel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Bits and pieces -- heck, whole functions taken from the
* yubikey-personalization repository, namely ykchalresp.c */
/*
* Copyright (c) 2011-2013 Yubico AB.
* All rights reserved.
*
* Author : Fredrik Thulin <[email protected]>
*
* Some basic code copied from ykpersonalize.c.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "ykchalresp.h"
#define SHA1_MAX_BLOCK_SIZE 64 /* Max size of input SHA1 block */
static int yk_check_firmware(YK_KEY *);
static void yk_report_error(yk_array *);
int
kc_ykchalresp(struct db_parameters *db_params)
{
YK_KEY *yk_key = NULL;
int yk_cmd = 0;
yk_array *yk = db_params->yk;
unsigned int yk_serial = 0;
bool may_block = true;
char *userpass = NULL;
unsigned char response[SHA1_MAX_BLOCK_SIZE];
unsigned char *challenge = NULL;
size_t challenge_len = 0, copied = 0, userpass_len = 0, yk_counter = 0;
yk_errno = 0;
if (!yk_init()) {
goto err;
}
/* If we have a user-password, then save it for later, because this is
* only available before we do any magic below, because we actually
* overwrite this with the YubiKey response.
*/
if (db_params->yk_password) {
userpass = malloc(db_params->pass_len); malloc_check(userpass);
userpass_len = db_params->pass_len;
memcpy(userpass, db_params->pass, userpass_len);
}
while (yk) {
/* If a YubiKey serial number was provided instead of a device
* index, try to lookup the device index from the serial
* number and store it in the yk list item's 'dev' member.
*/
if (yk->serial) {
if (getenv("KC_DEBUG"))
printf("%s(): searching for a device with serial: %d\n", __func__, yk->serial);
yk->dev = -1;
for (yk_counter = 0; 1; yk_counter++) {
if (getenv("KC_DEBUG"))
printf("%s(): trying to open device with index #%zu, slot = %d, dev = %d, serial = %d\n", __func__, yk_counter, yk->slot, yk->dev, yk->serial);
if (!(yk_key = yk_open_key(yk_counter)))
break;
if (yk_get_serial(yk_key, 1, 0, &yk_serial)) {
if (yk->serial == yk_serial)
yk->dev = yk_counter;
if (yk_key && !yk_close_key(yk_key)) {
goto err;
}
yk_key = NULL;
} else {
dprintf(STDERR_FILENO, "ERROR: Could not read serial number from YubiKey device #%zu!\n", yk_counter);
goto err;
}
}
if (yk->dev < 0) {
dprintf(STDERR_FILENO, "ERROR: Could not find YubiKey device with serial number %d!\n", yk->serial);
yk_errno = YK_ENOKEY;
goto err;
}
}
yk = yk->next;
}
yk_errno = 0;
yk_counter = 0;
yk = db_params->yk;
while (yk) {
yk_counter++;
printf("Using YubiKey slot #%d on device #%d\n", yk->slot, yk->dev);
if (getenv("KC_DEBUG"))
printf("%s(): yk_counter: %zu, yk array:\nyk->slot = %d\nyk->dev = %d\nyk->serial = %d\n", __func__, yk_counter, yk->slot, yk->dev, yk->serial);
if (getenv("KC_DEBUG"))
printf("%s(): trying to open device with index #%d\n", __func__, yk->dev);
if (!(yk_key = yk_open_key((int)yk->dev))) {
goto err;
}
if (getenv("KC_DEBUG"))
printf("%s(): checking firmware version\n", __func__);
if (!yk_check_firmware(yk_key)) {
goto err;
}
if (userpass && userpass_len > YUBIKEY_CHALLENGE_MAXLEN) {
dprintf(STDERR_FILENO, "ERROR: Password cannot be longer than %d bytes when using YubiKey challenge-response!\n", YUBIKEY_CHALLENGE_MAXLEN);
yk_errno = YK_EWRONGSIZ;
goto err;
}
switch(yk->slot) {
case 1:
yk_cmd = SLOT_CHAL_HMAC1;
break;
case 2:
yk_cmd = SLOT_CHAL_HMAC2;
break;
default:
dprintf(STDERR_FILENO, "ERROR: YubiKey slot #%d is invalid!\n", yk->slot);
yk_errno = YK_EINVALIDCMD;
goto err;
}
/* set up challenge */
if (!userpass && yk_counter == 1) {
/* Use the salt as the challenge when we *don't* want
* to use a user-password with the security key *and*
* this is the first key.
* The latter is important, because even if we don't
* use a user-password, we still re-use the response
* (that is saved in db->pass) from previous keys when
* using multiple security keys.
* Otherwise, when using multiple security keys, even
* if we don't want to use a user-password, if this is
* not the very first key, then we'll re-use the
* previous response (in the other branch of the
* conditional, below).
*/
if (getenv("KC_DEBUG"))
printf("%s(): automatic yubikey, without password\n", __func__);
/* this won't be the digest of the salt, but the actual
* decoded salt */
challenge_len = SALT_LEN < YUBIKEY_CHALLENGE_MAXLEN ? SALT_LEN : YUBIKEY_CHALLENGE_MAXLEN;
challenge = malloc(challenge_len); malloc_check(challenge);
yubikey_hex_decode((char *)challenge, (char *)db_params->salt, challenge_len);
if (getenv("KC_DEBUG"))
printf("%s(): the challenge is filled with %zu bytes from salt\n", __func__, challenge_len);
} else {
/* We're re-using the previous response (db->pass) when
* using multiple security keys to treat the password,
* even if there's no user-supplied password in
* db->pass. This makes for a kind of chained security
* key usage.
*/
if (getenv("KC_DEBUG"))
printf("%s(): using password with yubikey\n", __func__);
/* Here we not only copy the password to the challenge,
* but also append data from the salt if possible -- as
* much data as there is space left from
* YUBIKEY_CHALLENGE_MAXLEN after filling it with the
* password.
*/
challenge_len = db_params->pass_len + SALT_DIGEST_LEN < YUBIKEY_CHALLENGE_MAXLEN ? db_params->pass_len + SALT_DIGEST_LEN : YUBIKEY_CHALLENGE_MAXLEN;
challenge = malloc(challenge_len); malloc_check(challenge);
/* this is so we know how much we copied from db->pass
* to the challenge
*/
copied = challenge_len < db_params->pass_len ? challenge_len : db_params->pass_len;
/* copy the password to the challenge first ... */
memcpy(challenge, db_params->pass, copied);
/* ... then append from the salt if there's any space left in 'challenge' */
memcpy(challenge + copied, db_params->salt, challenge_len - copied);
if (getenv("KC_DEBUG"))
printf("%s(): the challenge(len:%zu) is filled with %zu bytes from salt after %zu bytes of password\n", __func__, challenge_len, challenge_len - db_params->pass_len, db_params->pass_len);
}
printf("Remember to touch your YubiKey if necessary\n");
memset(response, '\0', sizeof(response));
if (!yk_challenge_response(yk_key, yk_cmd, may_block,
challenge_len, challenge,
sizeof(response), response))
{
goto err;
}
memset(challenge, '\0', sizeof(challenge_len));
free(challenge); challenge = NULL;
if (yk_key && !yk_close_key(yk_key)) {
goto err;
}
yk_key = NULL;
/* realloc ..->pass and use the response */
/* copy the response as the constructed password */
memset(db_params->pass, '\0', db_params->pass_len);
db_params->pass_len = sizeof(response) > PASSWORD_MAXLEN ? PASSWORD_MAXLEN : sizeof(response);
db_params->pass = realloc(db_params->pass, db_params->pass_len); malloc_check(db_params->pass);
memcpy(db_params->pass, response, db_params->pass_len);
memset(response, '\0', sizeof(response));
yk = yk->next;
}
/* If there's a user-password, then append it to the response, but only
* if this is the last security key (we're out of the loop here, where
* we've iterated the security keys) in the chain, if there are
* multiple ones provided.
* This is because YubiKey's challenge-response mode can handle a
* challenge that's at most 64 bytes long, and with this (appending the
* password) we're creating something that's longer than that.
*/
if (userpass) {
if (getenv("KC_DEBUG"))
printf("%s(): constructing new password by appending user-password to yubikey response\n", __func__);
db_params->pass_len = db_params->pass_len + userpass_len > PASSWORD_MAXLEN ? PASSWORD_MAXLEN : db_params->pass_len + userpass_len;
db_params->pass = realloc(db_params->pass, db_params->pass_len); malloc_check(db_params->pass);
/* append the actual user-password as well to the end of the constructed password */
memcpy(db_params->pass + sizeof(response), userpass, db_params->pass_len - sizeof(response));
}
if (getenv("KC_DEBUG"))
printf("%s(): pass_len: %zu\n", __func__, db_params->pass_len);
err:
if (userpass)
memset(userpass, '\0', userpass_len);
free(userpass); userpass = NULL;
userpass_len = 0;
if (challenge)
memset(challenge, '\0', sizeof(challenge_len));
free(challenge); challenge = NULL;
challenge_len = 0;
memset(response, '\0', sizeof(response));
yk_report_error(yk);
if (yk_key && !yk_close_key(yk_key)) {
yk_report_error(yk);
}
if (yk_errno) {
return(0);
} else {
return(1);
}
} /* kc_ykchalresp() */
static int
yk_check_firmware(YK_KEY *yk_key)
{
YK_STATUS *st = ykds_alloc();
if (!yk_get_status(yk_key, st)) {
ykds_free(st);
return 0;
}
if (ykds_version_major(st) < 2 ||
(ykds_version_major(st) == 2
&& ykds_version_minor(st) < 2)) {
fprintf(stderr, "Challenge-response not supported before YubiKey 2.2.\n");
ykds_free(st);
return 0;
}
ykds_free(st);
return 1;
}/* yk_check_firmware() */
static void
yk_report_error(yk_array *yk)
{
if (yk_errno) {
if (yk_errno == YK_EUSBERR) {
fprintf(stderr, "USB error: %s\n",
yk_usb_strerror());
} else {
fprintf(stderr, "YubiKey core error (Device#%d, Slot#%d): %s\n",
yk->dev, yk->slot, yk_strerror(yk_errno));
}
}
}/* yk_report_error() */