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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
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
|
/*
* This program is is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
* @file rlm_yubikey.c
* @brief Authentication for yubikey OTP tokens.
*
* @author Arran Cudbard-Bell <a.cudbardb@networkradius.com>
* @copyright 2013 The FreeRADIUS server project
* @copyright 2013 Network RADIUS <info@networkradius.com>
*/
RCSID("$Id$")
#include "rlm_yubikey.h"
/*
* A mapping of configuration file names to internal variables.
*
* Note that the string is dynamically allocated, so it MUST
* be freed. When the configuration file parse re-reads the string,
* it free's the old one, and strdup's the new one, placing the pointer
* to the strdup'd string into 'config.string'. This gets around
* buffer over-flows.
*/
#ifdef HAVE_YKCLIENT
static const CONF_PARSER validation_config[] = {
{ "client_id", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_yubikey_t, client_id), 0 },
{ "api_key", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, rlm_yubikey_t, api_key), NULL },
CONF_PARSER_TERMINATOR
};
#endif
static const CONF_PARSER module_config[] = {
{ "id_length", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_yubikey_t, id_len), "12" },
{ "split", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_yubikey_t, split), "yes" },
{ "decrypt", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_yubikey_t, decrypt), "no" },
{ "validate", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_yubikey_t, validate), "no" },
#ifdef HAVE_YKCLIENT
{ "validation", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) validation_config },
#endif
CONF_PARSER_TERMINATOR
};
static char const modhextab[] = "cbdefghijklnrtuv";
static char const hextab[] = "0123456789abcdef";
#define is_modhex(x) (memchr(modhextab, tolower(x), 16))
/** Convert yubikey modhex to normal hex
*
* The same buffer may be passed as modhex and hex to convert the modhex in place.
*
* Modhex and hex must be the same size.
*
* @param[in] modhex data.
* @param[in] len of input and output buffers.
* @param[out] hex where to write the standard hexits.
* @return The number of bytes written to the output buffer, or -1 on error.
*/
static ssize_t modhex2hex(char const *modhex, uint8_t *hex, size_t len)
{
size_t i;
char *c1, *c2;
for (i = 0; i < len; i++) {
if (modhex[i << 1] == '\0') {
break;
}
/*
* We only deal with whole bytes
*/
if (modhex[(i << 1) + 1] == '\0')
return -1;
if (!(c1 = memchr(modhextab, tolower((uint8_t) modhex[i << 1]), 16)) ||
!(c2 = memchr(modhextab, tolower((uint8_t) modhex[(i << 1) + 1]), 16)))
return -1;
hex[i] = hextab[c1 - modhextab];
hex[i + 1] = hextab[c2 - modhextab];
}
return i;
}
/**
* @brief Convert Yubikey modhex to standard hex
*
* Example: "%{modhextohex:vvrbuctetdhc}" == "ffc1e0d3d260"
*/
static ssize_t modhex_to_hex_xlat(UNUSED void *instance, REQUEST *request, char const *fmt, char *out, size_t outlen)
{
ssize_t len;
if (outlen < strlen(fmt)) {
*out = '\0';
return 0;
}
/*
* mod2hex allows conversions in place
*/
len = modhex2hex(fmt, (uint8_t *) out, strlen(fmt));
if (len <= 0) {
*out = '\0';
REDEBUG("Modhex string invalid");
return -1;
}
return len;
}
static int mod_bootstrap(CONF_SECTION *conf, void *instance)
{
rlm_yubikey_t *inst = instance;
inst->name = cf_section_name2(conf);
if (!inst->name) inst->name = cf_section_name1(conf);
#ifndef HAVE_YUBIKEY
if (inst->decrypt) {
cf_log_err_cs(conf, "Requires libyubikey for OTP decryption");
return -1;
}
#endif
if (!cf_section_name2(conf)) return 0;
xlat_register("modhextohex", modhex_to_hex_xlat, NULL, inst);
return 0;
}
/*
* Do any per-module initialization that is separate to each
* configured instance of the module. e.g. set up connections
* to external databases, read configuration files, set up
* dictionary entries, etc.
*
* If configuration information is given in the config section
* that must be referenced in later calls, store a handle to it
* in *instance otherwise put a null pointer there.
*/
static int mod_instantiate(CONF_SECTION *conf, void *instance)
{
rlm_yubikey_t *inst = instance;
if (inst->validate) {
#ifdef HAVE_YKCLIENT
CONF_SECTION *cs;
cs = cf_section_sub_find(conf, "validation");
if (!cs) {
cf_log_err_cs(conf, "Missing validation section");
return -1;
}
if (rlm_yubikey_ykclient_init(cs, inst) < 0) {
return -1;
}
#else
cf_log_err_cs(conf, "Requires libykclient for OTP validation against Yubicloud servers");
return -1;
#endif
}
return 0;
}
/*
* Only free memory we allocated. The strings allocated via
* cf_section_parse() do not need to be freed.
*/
#ifdef HAVE_YKCLIENT
static int mod_detach(void *instance)
{
rlm_yubikey_ykclient_detach((rlm_yubikey_t *) instance);
return 0;
}
#endif
static int CC_HINT(nonnull) otp_string_valid(rlm_yubikey_t *inst, char const *otp, size_t len)
{
size_t i;
for (i = inst->id_len; i < len; i++) {
if (!is_modhex(otp[i])) return -i;
}
return 1;
}
/*
* Find the named user in this modules database. Create the set
* of attribute-value pairs to check and reply with for this user
* from the database. The authentication code only needs to check
* the password, the rest is done here.
*/
static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
{
rlm_yubikey_t *inst = instance;
DICT_VALUE *dval;
char const *passcode;
size_t len;
VALUE_PAIR *vp;
/*
* Can't do yubikey auth if there's no password.
*/
if (!request->password || (request->password->da->attr != PW_USER_PASSWORD)) {
/*
* Don't print out debugging messages if we know
* they're useless.
*/
if (request->packet->code == PW_CODE_ACCESS_CHALLENGE) {
return RLM_MODULE_NOOP;
}
RDEBUG2("No cleartext password in the request. Can't do Yubikey authentication");
return RLM_MODULE_NOOP;
}
passcode = request->password->vp_strvalue;
len = request->password->vp_length;
/*
* Now see if the passcode is the correct length (in its raw
* modhex encoded form).
*
* <public_id (6-16 bytes)> + <aes-block (32 bytes)>
*
*/
if (len > (inst->id_len + YUBIKEY_TOKEN_LEN)) {
/* May be a concatenation, check the last 32 bytes are modhex */
if (inst->split) {
char const *otp;
char *password;
size_t password_len;
int ret;
password_len = (len - (inst->id_len + YUBIKEY_TOKEN_LEN));
otp = passcode + password_len;
ret = otp_string_valid(inst, otp, (inst->id_len + YUBIKEY_TOKEN_LEN));
if (ret <= 0) {
if (RDEBUG_ENABLED3) {
RDMARKER(otp, -ret, "User-Password (aes-block) value contains non modhex chars");
} else {
RDEBUG("User-Password (aes-block) value contains non modhex chars");
}
return RLM_MODULE_NOOP;
}
/*
* Insert a new request attribute just containing the OTP
* portion.
*/
vp = pair_make_request("Yubikey-OTP", otp, T_OP_SET);
if (!vp) {
REDEBUG("Failed creating 'Yubikey-OTP' attribute");
return RLM_MODULE_FAIL;
}
/*
* Replace the existing string buffer for the password
* attribute with one just containing the password portion.
*/
MEM(password = talloc_array(request->password, char, password_len + 1));
strlcpy(password, passcode, password_len + 1);
fr_pair_value_strsteal(request->password, password);
RINDENT();
if (RDEBUG_ENABLED3) {
RDEBUG3("&request:Yubikey-OTP := '%s'", vp->vp_strvalue);
RDEBUG3("&request:User-Password := '%s'", request->password->vp_strvalue);
} else {
RDEBUG2("&request:Yubikey-OTP := <<< secret >>>");
RDEBUG2("&request:User-Password := <<< secret >>>");
}
REXDENT();
/*
* So the ID split code works on the non password portion.
*/
passcode = vp->vp_strvalue;
}
} else if (len < (inst->id_len + YUBIKEY_TOKEN_LEN)) {
RDEBUG2("User-Password value is not the correct length, expected at least %u bytes, got %zu bytes",
inst->id_len + YUBIKEY_TOKEN_LEN, len);
return RLM_MODULE_NOOP;
} else {
int ret;
ret = otp_string_valid(inst, passcode, (inst->id_len + YUBIKEY_TOKEN_LEN));
if (ret <= 0) {
if (RDEBUG_ENABLED3) {
RDMARKER(passcode, -ret, "User-Password (aes-block) value contains non modhex chars");
} else {
RDEBUG("User-Password (aes-block) value contains non modhex chars");
}
return RLM_MODULE_NOOP;
}
}
dval = dict_valbyname(PW_AUTH_TYPE, 0, inst->name);
if (dval) {
vp = radius_pair_create(request, &request->config, PW_AUTH_TYPE, 0);
vp->vp_integer = dval->value;
}
/*
* Split out the Public ID in case another module in authorize
* needs to verify it's associated with the user.
*
* It's left up to the user if they want to decode it or not.
*/
if (inst->id_len) {
vp = fr_pair_make(request->packet, &request->packet->vps, "Yubikey-Public-ID", NULL, T_OP_SET);
if (!vp) {
REDEBUG("Failed creating Yubikey-Public-ID");
return RLM_MODULE_FAIL;
}
fr_pair_value_bstrncpy(vp, passcode, inst->id_len);
}
return RLM_MODULE_OK;
}
/*
* Authenticate the user with the given password.
*/
static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
{
rlm_rcode_t rcode = RLM_MODULE_NOOP;
rlm_yubikey_t *inst = instance;
char const *passcode = NULL;
DICT_ATTR const *da;
VALUE_PAIR const *vp;
size_t len;
int ret;
da = dict_attrbyname("Yubikey-OTP");
if (!da) {
RDEBUG2("No Yubikey-OTP attribute defined, falling back to User-Password");
goto user_password;
}
vp = fr_pair_find_by_da(request->packet->vps, da, TAG_ANY);
if (vp) {
passcode = vp->vp_strvalue;
len = vp->vp_length;
} else {
RDEBUG2("No Yubikey-OTP attribute found, falling back to User-Password");
user_password:
/*
* Can't do yubikey auth if there's no password.
*/
if (!request->password || (request->password->da->attr != PW_USER_PASSWORD)) {
REDEBUG("No User-Password in the request. Can't do Yubikey authentication");
return RLM_MODULE_INVALID;
}
vp = request->password;
passcode = request->password->vp_strvalue;
len = request->password->vp_length;
}
/*
* Verify the passcode is the correct length (in its raw
* modhex encoded form).
*
* <public_id (6-16 bytes)> + <aes-block (32 bytes)>
*/
if (len != (inst->id_len + YUBIKEY_TOKEN_LEN)) {
REDEBUG("%s value is not the correct length, expected bytes %u, got bytes %zu",
vp->da->name, inst->id_len + YUBIKEY_TOKEN_LEN, len);
return RLM_MODULE_INVALID;
}
ret = otp_string_valid(inst, passcode, (inst->id_len + YUBIKEY_TOKEN_LEN));
if (ret <= 0) {
if (RDEBUG_ENABLED3) {
REMARKER(passcode, -ret, "Passcode (aes-block) value contains non modhex chars");
} else {
RERROR("Passcode (aes-block) value contains non modhex chars");
}
return RLM_MODULE_INVALID;
}
#ifdef HAVE_YUBIKEY
if (inst->decrypt) {
rcode = rlm_yubikey_decrypt(inst, request, passcode);
if (rcode != RLM_MODULE_OK) {
return rcode;
}
/* Fall-Through to doing ykclient auth in addition to local auth */
}
#endif
#ifdef HAVE_YKCLIENT
if (inst->validate) {
return rlm_yubikey_validate(inst, request, passcode);
}
#endif
return rcode;
}
/*
* The module name should be the only globally exported symbol.
* That is, everything else should be 'static'.
*
* If the module needs to temporarily modify it's instantiation
* data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
* The server will then take care of ensuring that the module
* is single-threaded.
*/
extern module_t rlm_yubikey;
module_t rlm_yubikey = {
.magic = RLM_MODULE_INIT,
.name = "yubikey",
.type = RLM_TYPE_THREAD_SAFE,
.inst_size = sizeof(rlm_yubikey_t),
.config = module_config,
.bootstrap = mod_bootstrap,
.instantiate = mod_instantiate,
#ifdef HAVE_YKCLIENT
.detach = mod_detach,
#endif
.methods = {
[MOD_AUTHENTICATE] = mod_authenticate,
[MOD_AUTHORIZE] = mod_authorize
},
};
|