| 1 |
/* slapd-totp.c - Password module and overlay for TOTP */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2015-2024 The OpenLDAP Foundation. |
| 6 |
* Portions Copyright 2015 by Howard Chu, Symas Corp. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 13 |
* A copy of this license is available in the file LICENSE in the |
| 14 |
* top-level directory of the distribution or, alternatively, at |
| 15 |
* <http://www.OpenLDAP.org/license.html>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work includes code from the lastbind overlay. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <portable.h> |
| 22 |
|
| 23 |
#if HAVE_STDINT_H |
| 24 |
#include <stdint.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <lber.h> |
| 28 |
#include <lber_pvt.h> |
| 29 |
#include "lutil.h" |
| 30 |
#include <ac/stdlib.h> |
| 31 |
#include <ac/ctype.h> |
| 32 |
#include <ac/string.h> |
| 33 |
/* include socket.h to get sys/types.h and/or winsock2.h */ |
| 34 |
#include <ac/socket.h> |
| 35 |
|
| 36 |
#if HAVE_OPENSSL |
| 37 |
#include <openssl/sha.h> |
| 38 |
#include <openssl/hmac.h> |
| 39 |
|
| 40 |
#define TOTP_SHA512_DIGEST_LENGTH SHA512_DIGEST_LENGTH |
| 41 |
#define TOTP_SHA1 EVP_sha1() |
| 42 |
#define TOTP_SHA256 EVP_sha256() |
| 43 |
#define TOTP_SHA512 EVP_sha512() |
| 44 |
#define TOTP_HMAC_CTX HMAC_CTX * |
| 45 |
|
| 46 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 47 |
static HMAC_CTX *HMAC_CTX_new(void) |
| 48 |
{ |
| 49 |
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx)); |
| 50 |
if (ctx != NULL) { |
| 51 |
HMAC_CTX_init(ctx); |
| 52 |
} |
| 53 |
return ctx; |
| 54 |
} |
| 55 |
|
| 56 |
static void HMAC_CTX_free(HMAC_CTX *ctx) |
| 57 |
{ |
| 58 |
if (ctx != NULL) { |
| 59 |
HMAC_CTX_cleanup(ctx); |
| 60 |
OPENSSL_free(ctx); |
| 61 |
} |
| 62 |
} |
| 63 |
#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */ |
| 64 |
|
| 65 |
#define HMAC_setup(ctx, key, len, hash) \ |
| 66 |
ctx = HMAC_CTX_new(); \ |
| 67 |
HMAC_Init_ex(ctx, key, len, hash, 0) |
| 68 |
#define HMAC_crunch(ctx, buf, len) HMAC_Update(ctx, buf, len) |
| 69 |
#define HMAC_finish(ctx, dig, dlen) \ |
| 70 |
HMAC_Final(ctx, dig, &dlen); \ |
| 71 |
HMAC_CTX_free(ctx) |
| 72 |
|
| 73 |
#elif HAVE_GNUTLS |
| 74 |
#include <nettle/hmac.h> |
| 75 |
|
| 76 |
#define TOTP_SHA512_DIGEST_LENGTH SHA512_DIGEST_SIZE |
| 77 |
#define TOTP_SHA1 &nettle_sha1 |
| 78 |
#define TOTP_SHA256 &nettle_sha256 |
| 79 |
#define TOTP_SHA512 &nettle_sha512 |
| 80 |
#define TOTP_HMAC_CTX struct hmac_sha512_ctx |
| 81 |
|
| 82 |
#define HMAC_setup(ctx, key, len, hash) \ |
| 83 |
const struct nettle_hash *h=hash;\ |
| 84 |
hmac_set_key(&ctx.outer, &ctx.inner, &ctx.state, h, len, key) |
| 85 |
#define HMAC_crunch(ctx, buf, len) hmac_update(&ctx.state, h, len, buf) |
| 86 |
#define HMAC_finish(ctx, dig, dlen) \ |
| 87 |
hmac_digest(&ctx.outer, &ctx.inner, &ctx.state, h, h->digest_size, dig);\ |
| 88 |
dlen = h->digest_size |
| 89 |
|
| 90 |
#else |
| 91 |
# error Unsupported crypto backend. |
| 92 |
#endif |
| 93 |
|
| 94 |
#include "slap.h" |
| 95 |
#include "slap-config.h" |
| 96 |
|
| 97 |
static LUTIL_PASSWD_CHK_FUNC chk_totp1, chk_totp256, chk_totp512, |
| 98 |
chk_totp1andpw, chk_totp256andpw, chk_totp512andpw; |
| 99 |
static LUTIL_PASSWD_HASH_FUNC hash_totp1, hash_totp256, hash_totp512, |
| 100 |
hash_totp1andpw, hash_totp256andpw, hash_totp512andpw; |
| 101 |
static const struct berval scheme_totp1 = BER_BVC("{TOTP1}"); |
| 102 |
static const struct berval scheme_totp256 = BER_BVC("{TOTP256}"); |
| 103 |
static const struct berval scheme_totp512 = BER_BVC("{TOTP512}"); |
| 104 |
static const struct berval scheme_totp1andpw = BER_BVC("{TOTP1ANDPW}"); |
| 105 |
static const struct berval scheme_totp256andpw = BER_BVC("{TOTP256ANDPW}"); |
| 106 |
static const struct berval scheme_totp512andpw = BER_BVC("{TOTP512ANDPW}"); |
| 107 |
|
| 108 |
static AttributeDescription *ad_authTimestamp; |
| 109 |
|
| 110 |
/* This is the definition used by ISODE, as supplied to us in |
| 111 |
* ITS#6238 Followup #9 |
| 112 |
*/ |
| 113 |
static struct schema_info { |
| 114 |
char *def; |
| 115 |
AttributeDescription **ad; |
| 116 |
} totp_OpSchema[] = { |
| 117 |
{ "( 1.3.6.1.4.1.453.16.2.188 " |
| 118 |
"NAME 'authTimestamp' " |
| 119 |
"DESC 'last successful authentication using any method/mech' " |
| 120 |
"EQUALITY generalizedTimeMatch " |
| 121 |
"ORDERING generalizedTimeOrderingMatch " |
| 122 |
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 " |
| 123 |
"SINGLE-VALUE NO-USER-MODIFICATION USAGE dsaOperation )", |
| 124 |
&ad_authTimestamp}, |
| 125 |
{ NULL, NULL } |
| 126 |
}; |
| 127 |
|
| 128 |
/* RFC3548 base32 encoding/decoding */ |
| 129 |
|
| 130 |
static const char Base32[] = |
| 131 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
| 132 |
static const char Pad32 = '='; |
| 133 |
|
| 134 |
static int |
| 135 |
totp_b32_ntop( |
| 136 |
unsigned char const *src, |
| 137 |
size_t srclength, |
| 138 |
char *target, |
| 139 |
size_t targsize) |
| 140 |
{ |
| 141 |
size_t datalength = 0; |
| 142 |
unsigned char input0; |
| 143 |
unsigned int input1; /* assumed to be at least 32 bits */ |
| 144 |
unsigned char output[8]; |
| 145 |
int i; |
| 146 |
|
| 147 |
while (4 < srclength) { |
| 148 |
if (datalength + 8 > targsize) |
| 149 |
return (-1); |
| 150 |
input0 = *src++; |
| 151 |
input1 = *src++; |
| 152 |
input1 <<= 8; |
| 153 |
input1 |= *src++; |
| 154 |
input1 <<= 8; |
| 155 |
input1 |= *src++; |
| 156 |
input1 <<= 8; |
| 157 |
input1 |= *src++; |
| 158 |
srclength -= 5; |
| 159 |
|
| 160 |
for (i=7; i>1; i--) { |
| 161 |
output[i] = input1 & 0x1f; |
| 162 |
input1 >>= 5; |
| 163 |
} |
| 164 |
output[0] = input0 >> 3; |
| 165 |
output[1] = (input0 & 0x07) << 2 | input1; |
| 166 |
|
| 167 |
for (i=0; i<8; i++) |
| 168 |
target[datalength++] = Base32[output[i]]; |
| 169 |
} |
| 170 |
|
| 171 |
/* Now we worry about padding. */ |
| 172 |
if (0 != srclength) { |
| 173 |
static const int outlen[] = { 2,4,5,7 }; |
| 174 |
int n; |
| 175 |
if (datalength + 8 > targsize) |
| 176 |
return (-1); |
| 177 |
|
| 178 |
/* Get what's left. */ |
| 179 |
input1 = *src++; |
| 180 |
for (i = 1; i < srclength; i++) { |
| 181 |
input1 <<= 8; |
| 182 |
input1 |= *src++; |
| 183 |
} |
| 184 |
input1 <<= 8 * (4-srclength); |
| 185 |
n = outlen[srclength-1]; |
| 186 |
for (i=0; i<n; i++) { |
| 187 |
target[datalength++] = Base32[(input1 & 0xf8000000) >> 27]; |
| 188 |
input1 <<= 5; |
| 189 |
} |
| 190 |
for (; i<8; i++) |
| 191 |
target[datalength++] = Pad32; |
| 192 |
} |
| 193 |
if (datalength >= targsize) |
| 194 |
return (-1); |
| 195 |
target[datalength] = '\0'; /* Returned value doesn't count \0. */ |
| 196 |
return (datalength); |
| 197 |
} |
| 198 |
|
| 199 |
/* converts characters, eight at a time, starting at src |
| 200 |
from base - 32 numbers into five 8 bit bytes in the target area. |
| 201 |
it returns the number of data bytes stored at the target, or -1 on error. |
| 202 |
*/ |
| 203 |
|
| 204 |
static int |
| 205 |
totp_b32_pton( |
| 206 |
char const *src, |
| 207 |
unsigned char *target, |
| 208 |
size_t targsize) |
| 209 |
{ |
| 210 |
int tarindex, state, ch; |
| 211 |
char *pos; |
| 212 |
|
| 213 |
state = 0; |
| 214 |
tarindex = 0; |
| 215 |
|
| 216 |
while ((ch = *src++) != '\0') { |
| 217 |
if (ch == Pad32) |
| 218 |
break; |
| 219 |
|
| 220 |
pos = strchr(Base32, ch); |
| 221 |
if (pos == 0) /* A non-base32 character. */ |
| 222 |
return (-1); |
| 223 |
|
| 224 |
switch (state) { |
| 225 |
case 0: |
| 226 |
if (target) { |
| 227 |
if ((size_t)tarindex >= targsize) |
| 228 |
return (-1); |
| 229 |
target[tarindex] = (pos - Base32) << 3; |
| 230 |
} |
| 231 |
state = 1; |
| 232 |
break; |
| 233 |
case 1: |
| 234 |
if (target) { |
| 235 |
if ((size_t)tarindex + 1 >= targsize) |
| 236 |
return (-1); |
| 237 |
target[tarindex] |= (pos - Base32) >> 2; |
| 238 |
target[tarindex+1] = ((pos - Base32) & 0x3) |
| 239 |
<< 6 ; |
| 240 |
} |
| 241 |
tarindex++; |
| 242 |
state = 2; |
| 243 |
break; |
| 244 |
case 2: |
| 245 |
if (target) { |
| 246 |
target[tarindex] |= (pos - Base32) << 1; |
| 247 |
} |
| 248 |
state = 3; |
| 249 |
break; |
| 250 |
case 3: |
| 251 |
if (target) { |
| 252 |
if ((size_t)tarindex + 1 >= targsize) |
| 253 |
return (-1); |
| 254 |
target[tarindex] |= (pos - Base32) >> 4; |
| 255 |
target[tarindex+1] = ((pos - Base32) & 0xf) |
| 256 |
<< 4 ; |
| 257 |
} |
| 258 |
tarindex++; |
| 259 |
state = 4; |
| 260 |
break; |
| 261 |
case 4: |
| 262 |
if (target) { |
| 263 |
if ((size_t)tarindex + 1 >= targsize) |
| 264 |
return (-1); |
| 265 |
target[tarindex] |= (pos - Base32) >> 1; |
| 266 |
target[tarindex+1] = ((pos - Base32) & 0x1) |
| 267 |
<< 7 ; |
| 268 |
} |
| 269 |
tarindex++; |
| 270 |
state = 5; |
| 271 |
break; |
| 272 |
case 5: |
| 273 |
if (target) { |
| 274 |
target[tarindex] |= (pos - Base32) << 2; |
| 275 |
} |
| 276 |
state = 6; |
| 277 |
break; |
| 278 |
case 6: |
| 279 |
if (target) { |
| 280 |
if ((size_t)tarindex + 1 >= targsize) |
| 281 |
return (-1); |
| 282 |
target[tarindex] |= (pos - Base32) >> 3; |
| 283 |
target[tarindex+1] = ((pos - Base32) & 0x7) |
| 284 |
<< 5 ; |
| 285 |
} |
| 286 |
tarindex++; |
| 287 |
state = 7; |
| 288 |
break; |
| 289 |
case 7: |
| 290 |
if (target) { |
| 291 |
target[tarindex] |= (pos - Base32); |
| 292 |
} |
| 293 |
state = 0; |
| 294 |
tarindex++; |
| 295 |
break; |
| 296 |
|
| 297 |
default: |
| 298 |
abort(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* We are done decoding Base-32 chars. Let's see if we ended |
| 304 |
* on a byte boundary, and/or with erroneous trailing characters. |
| 305 |
*/ |
| 306 |
|
| 307 |
if (ch == Pad32) { /* We got a pad char. */ |
| 308 |
int i = 0; |
| 309 |
|
| 310 |
/* count pad chars */ |
| 311 |
for (; ch; ch = *src++) { |
| 312 |
if (ch != Pad32) |
| 313 |
return (-1); |
| 314 |
i++; |
| 315 |
} |
| 316 |
/* there are only 4 valid ending states with a |
| 317 |
* pad character, make sure the number of pads is valid. |
| 318 |
*/ |
| 319 |
switch(state) { |
| 320 |
case 2: if (i != 6) return -1; |
| 321 |
break; |
| 322 |
case 4: if (i != 4) return -1; |
| 323 |
break; |
| 324 |
case 5: if (i != 3) return -1; |
| 325 |
break; |
| 326 |
case 7: if (i != 1) return -1; |
| 327 |
break; |
| 328 |
default: |
| 329 |
return -1; |
| 330 |
} |
| 331 |
/* |
| 332 |
* Now make sure that the "extra" bits that slopped past |
| 333 |
* the last full byte were zeros. If we don't check them, |
| 334 |
* they become a subliminal channel. |
| 335 |
*/ |
| 336 |
if (target && target[tarindex] != 0) |
| 337 |
return (-1); |
| 338 |
} else { |
| 339 |
/* |
| 340 |
* We ended by seeing the end of the string. Make sure we |
| 341 |
* have no partial bytes lying around. |
| 342 |
*/ |
| 343 |
if (state != 0) |
| 344 |
return (-1); |
| 345 |
} |
| 346 |
|
| 347 |
return (tarindex); |
| 348 |
} |
| 349 |
|
| 350 |
/* RFC6238 TOTP */ |
| 351 |
|
| 352 |
|
| 353 |
typedef struct myval { |
| 354 |
ber_len_t mv_len; |
| 355 |
void *mv_val; |
| 356 |
} myval; |
| 357 |
|
| 358 |
static void do_hmac( |
| 359 |
const void *hash, |
| 360 |
myval *key, |
| 361 |
myval *data, |
| 362 |
myval *out) |
| 363 |
{ |
| 364 |
TOTP_HMAC_CTX ctx; |
| 365 |
unsigned int digestLen; |
| 366 |
|
| 367 |
HMAC_setup(ctx, key->mv_val, key->mv_len, hash); |
| 368 |
HMAC_crunch(ctx, data->mv_val, data->mv_len); |
| 369 |
HMAC_finish(ctx, out->mv_val, digestLen); |
| 370 |
out->mv_len = digestLen; |
| 371 |
} |
| 372 |
|
| 373 |
static const int DIGITS_POWER[] = { |
| 374 |
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; |
| 375 |
|
| 376 |
static void generate( |
| 377 |
myval *key, |
| 378 |
uint64_t tval, |
| 379 |
int digits, |
| 380 |
myval *out, |
| 381 |
const void *mech) |
| 382 |
{ |
| 383 |
unsigned char digest[TOTP_SHA512_DIGEST_LENGTH]; |
| 384 |
myval digval; |
| 385 |
myval data; |
| 386 |
unsigned char msg[8]; |
| 387 |
int i, offset, res, otp; |
| 388 |
|
| 389 |
#if WORDS_BIGENDIAN |
| 390 |
*(uint64_t *)msg = tval; |
| 391 |
#else |
| 392 |
for (i=7; i>=0; i--) { |
| 393 |
msg[i] = tval & 0xff; |
| 394 |
tval >>= 8; |
| 395 |
} |
| 396 |
#endif |
| 397 |
|
| 398 |
data.mv_val = msg; |
| 399 |
data.mv_len = sizeof(msg); |
| 400 |
|
| 401 |
digval.mv_val = digest; |
| 402 |
digval.mv_len = sizeof(digest); |
| 403 |
do_hmac(mech, key, &data, &digval); |
| 404 |
|
| 405 |
offset = digest[digval.mv_len-1] & 0xf; |
| 406 |
res = ((digest[offset] & 0x7f) << 24) | |
| 407 |
((digest[offset+1] & 0xff) << 16) | |
| 408 |
((digest[offset+2] & 0xff) << 8) | |
| 409 |
(digest[offset+3] & 0xff); |
| 410 |
|
| 411 |
otp = res % DIGITS_POWER[digits]; |
| 412 |
out->mv_len = snprintf(out->mv_val, out->mv_len, "%0*d", digits, otp); |
| 413 |
} |
| 414 |
|
| 415 |
static int totp_op_cleanup( Operation *op, SlapReply *rs ); |
| 416 |
static int totp_bind_response( Operation *op, SlapReply *rs ); |
| 417 |
|
| 418 |
#define TIME_STEP 30 |
| 419 |
#define DIGITS 6 |
| 420 |
#define DELIM '|' /* a single character */ |
| 421 |
#define TOTP_AND_PW_HASH_SCHEME "{SSHA}" |
| 422 |
|
| 423 |
static int chk_totp( |
| 424 |
const struct berval *passwd, |
| 425 |
const struct berval *cred, |
| 426 |
const void *mech, |
| 427 |
const char **text) |
| 428 |
{ |
| 429 |
void *ctx, *op_tmp; |
| 430 |
Operation *op; |
| 431 |
Entry *e; |
| 432 |
Attribute *a; |
| 433 |
long t, told = 0; |
| 434 |
int rc; |
| 435 |
myval out, key; |
| 436 |
char outbuf[32]; |
| 437 |
|
| 438 |
/* Find our thread context, find our Operation */ |
| 439 |
ctx = ldap_pvt_thread_pool_context(); |
| 440 |
if (ldap_pvt_thread_pool_getkey(ctx, totp_op_cleanup, &op_tmp, NULL) || |
| 441 |
!op_tmp) |
| 442 |
return LUTIL_PASSWD_ERR; |
| 443 |
op = op_tmp; |
| 444 |
|
| 445 |
rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e); |
| 446 |
if (rc != LDAP_SUCCESS) return LUTIL_PASSWD_ERR; |
| 447 |
|
| 448 |
/* Make sure previous login is older than current time */ |
| 449 |
t = op->o_time / TIME_STEP; |
| 450 |
a = attr_find(e->e_attrs, ad_authTimestamp); |
| 451 |
if (a) { |
| 452 |
struct lutil_tm tm; |
| 453 |
struct lutil_timet tt; |
| 454 |
if (lutil_parsetime(a->a_vals[0].bv_val, &tm) == 0 && |
| 455 |
lutil_tm2time(&tm, &tt) == 0) { |
| 456 |
told = tt.tt_sec / TIME_STEP; |
| 457 |
if (told >= t) |
| 458 |
rc = LUTIL_PASSWD_ERR; |
| 459 |
} |
| 460 |
if (!rc) { /* seems OK, remember old stamp */ |
| 461 |
slap_callback *sc; |
| 462 |
for (sc = op->o_callback; sc; sc = sc->sc_next) { |
| 463 |
if (sc->sc_response == totp_bind_response) { |
| 464 |
sc->sc_private = ber_dupbv_x(NULL, &a->a_vals[0], op->o_tmpmemctx); |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} /* else no previous login, 1st use is OK */ |
| 470 |
|
| 471 |
be_entry_release_r(op, e); |
| 472 |
if (rc) return rc; |
| 473 |
|
| 474 |
/* Key is stored in base32 */ |
| 475 |
key.mv_len = passwd->bv_len * 5 / 8; |
| 476 |
key.mv_val = ber_memalloc(key.mv_len+1); |
| 477 |
|
| 478 |
if (!key.mv_val) |
| 479 |
return LUTIL_PASSWD_ERR; |
| 480 |
|
| 481 |
rc = totp_b32_pton(passwd->bv_val, key.mv_val, key.mv_len); |
| 482 |
if (rc < 1) { |
| 483 |
rc = LUTIL_PASSWD_ERR; |
| 484 |
goto out; |
| 485 |
} |
| 486 |
|
| 487 |
out.mv_val = outbuf; |
| 488 |
out.mv_len = sizeof(outbuf); |
| 489 |
generate(&key, t, DIGITS, &out, mech); |
| 490 |
|
| 491 |
/* compare */ |
| 492 |
if (out.mv_len != cred->bv_len) { |
| 493 |
rc = LUTIL_PASSWD_ERR; |
| 494 |
goto out; |
| 495 |
} |
| 496 |
|
| 497 |
rc = memcmp(out.mv_val, cred->bv_val, out.mv_len) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK; |
| 498 |
|
| 499 |
/* If current value doesn't match, try again with previous value |
| 500 |
* but only if the most recent login is older than the previous |
| 501 |
* time step but still set */ |
| 502 |
if (rc == LUTIL_PASSWD_ERR && told < t - 1 && told > 0) { |
| 503 |
out.mv_val = outbuf; |
| 504 |
out.mv_len = sizeof(outbuf); |
| 505 |
generate(&key, t - 1, DIGITS, &out, mech); |
| 506 |
/* compare */ |
| 507 |
if (out.mv_len != cred->bv_len) |
| 508 |
goto out; |
| 509 |
rc = memcmp(out.mv_val, cred->bv_val, out.mv_len) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK; |
| 510 |
} |
| 511 |
|
| 512 |
out: |
| 513 |
memset(key.mv_val, 0, key.mv_len); |
| 514 |
ber_memfree(key.mv_val); |
| 515 |
return rc; |
| 516 |
} |
| 517 |
|
| 518 |
static int chk_totp_and_pw( |
| 519 |
const struct berval *scheme, |
| 520 |
const struct berval *passwd, |
| 521 |
const struct berval *cred, |
| 522 |
const char **text, |
| 523 |
const void *mech) |
| 524 |
{ |
| 525 |
char *s; |
| 526 |
int rc = LUTIL_PASSWD_ERR, rc_pass, rc_otp; |
| 527 |
ber_len_t len; |
| 528 |
struct berval cred_pass, cred_otp, passwd_pass, passwd_otp; |
| 529 |
|
| 530 |
/* Check credential length, no point to continue if too short */ |
| 531 |
if (cred->bv_len <= DIGITS) |
| 532 |
return rc; |
| 533 |
|
| 534 |
/* The OTP seed of the stored password */ |
| 535 |
s = strchr(passwd->bv_val, DELIM); |
| 536 |
if (s) { |
| 537 |
len = s - passwd->bv_val; |
| 538 |
} else { |
| 539 |
return rc; |
| 540 |
} |
| 541 |
if (!ber_str2bv(passwd->bv_val, len, 1, &passwd_otp)) |
| 542 |
return rc; |
| 543 |
|
| 544 |
/* The password part of the stored password */ |
| 545 |
s++; |
| 546 |
ber_str2bv(s, 0, 0, &passwd_pass); |
| 547 |
|
| 548 |
/* The OTP part of the entered credential */ |
| 549 |
ber_str2bv(&cred->bv_val[cred->bv_len - DIGITS], DIGITS, 0, &cred_otp); |
| 550 |
|
| 551 |
/* The password part of the entered credential */ |
| 552 |
if (!ber_str2bv(cred->bv_val, cred->bv_len - DIGITS, 0, &cred_pass)) { |
| 553 |
/* Cleanup */ |
| 554 |
memset(passwd_otp.bv_val, 0, passwd_otp.bv_len); |
| 555 |
ber_memfree(passwd_otp.bv_val); |
| 556 |
return rc; |
| 557 |
} |
| 558 |
|
| 559 |
rc_otp = chk_totp(&passwd_otp, &cred_otp, mech, text); |
| 560 |
rc_pass = lutil_passwd(&passwd_pass, &cred_pass, NULL, text); |
| 561 |
if (rc_otp == LUTIL_PASSWD_OK && rc_pass == LUTIL_PASSWD_OK) |
| 562 |
rc = LUTIL_PASSWD_OK; |
| 563 |
|
| 564 |
/* Cleanup and return */ |
| 565 |
memset(passwd_otp.bv_val, 0, passwd_otp.bv_len); |
| 566 |
ber_memfree(passwd_otp.bv_val); |
| 567 |
|
| 568 |
return rc; |
| 569 |
} |
| 570 |
|
| 571 |
static int chk_totp1( |
| 572 |
const struct berval *scheme, |
| 573 |
const struct berval *passwd, |
| 574 |
const struct berval *cred, |
| 575 |
const char **text) |
| 576 |
{ |
| 577 |
return chk_totp(passwd, cred, TOTP_SHA1, text); |
| 578 |
} |
| 579 |
|
| 580 |
static int chk_totp256( |
| 581 |
const struct berval *scheme, |
| 582 |
const struct berval *passwd, |
| 583 |
const struct berval *cred, |
| 584 |
const char **text) |
| 585 |
{ |
| 586 |
return chk_totp(passwd, cred, TOTP_SHA256, text); |
| 587 |
} |
| 588 |
|
| 589 |
static int chk_totp512( |
| 590 |
const struct berval *scheme, |
| 591 |
const struct berval *passwd, |
| 592 |
const struct berval *cred, |
| 593 |
const char **text) |
| 594 |
{ |
| 595 |
return chk_totp(passwd, cred, TOTP_SHA512, text); |
| 596 |
} |
| 597 |
|
| 598 |
static int chk_totp1andpw( |
| 599 |
const struct berval *scheme, |
| 600 |
const struct berval *passwd, |
| 601 |
const struct berval *cred, |
| 602 |
const char **text) |
| 603 |
{ |
| 604 |
return chk_totp_and_pw(scheme, passwd, cred, text, TOTP_SHA1); |
| 605 |
} |
| 606 |
|
| 607 |
static int chk_totp256andpw( |
| 608 |
const struct berval *scheme, |
| 609 |
const struct berval *passwd, |
| 610 |
const struct berval *cred, |
| 611 |
const char **text) |
| 612 |
{ |
| 613 |
return chk_totp_and_pw(scheme, passwd, cred, text, TOTP_SHA256); |
| 614 |
} |
| 615 |
|
| 616 |
static int chk_totp512andpw( |
| 617 |
const struct berval *scheme, |
| 618 |
const struct berval *passwd, |
| 619 |
const struct berval *cred, |
| 620 |
const char **text) |
| 621 |
{ |
| 622 |
return chk_totp_and_pw(scheme, passwd, cred, text, TOTP_SHA512); |
| 623 |
} |
| 624 |
|
| 625 |
static int passwd_string32( |
| 626 |
const struct berval *scheme, |
| 627 |
const struct berval *passwd, |
| 628 |
struct berval *hash) |
| 629 |
{ |
| 630 |
int b32len = (passwd->bv_len + 4)/5 * 8; |
| 631 |
int rc; |
| 632 |
hash->bv_len = scheme->bv_len + b32len; |
| 633 |
hash->bv_val = ber_memalloc(hash->bv_len + 1); |
| 634 |
AC_MEMCPY(hash->bv_val, scheme->bv_val, scheme->bv_len); |
| 635 |
rc = totp_b32_ntop((unsigned char *)passwd->bv_val, passwd->bv_len, |
| 636 |
hash->bv_val + scheme->bv_len, b32len+1); |
| 637 |
if (rc < 0) { |
| 638 |
ber_memfree(hash->bv_val); |
| 639 |
hash->bv_val = NULL; |
| 640 |
return LUTIL_PASSWD_ERR; |
| 641 |
} |
| 642 |
return LUTIL_PASSWD_OK; |
| 643 |
} |
| 644 |
|
| 645 |
static int hash_totp_and_pw( |
| 646 |
const struct berval *scheme, |
| 647 |
const struct berval *passwd, |
| 648 |
struct berval *hash, |
| 649 |
const char **text) |
| 650 |
{ |
| 651 |
struct berval otp, pass, hash_otp, hash_pass; |
| 652 |
ber_len_t len; |
| 653 |
char *s; |
| 654 |
int rc = LUTIL_PASSWD_ERR; |
| 655 |
|
| 656 |
/* The OTP seed part */ |
| 657 |
s = strchr(passwd->bv_val, DELIM); |
| 658 |
if (s) { |
| 659 |
len = s - passwd->bv_val; |
| 660 |
} else { |
| 661 |
return rc; |
| 662 |
} |
| 663 |
if (!ber_str2bv(passwd->bv_val, len, 0, &otp)) |
| 664 |
return rc; |
| 665 |
|
| 666 |
/* The static password part */ |
| 667 |
s++; |
| 668 |
ber_str2bv(s, 0, 0, &pass); |
| 669 |
|
| 670 |
/* Hash the OTP seed */ |
| 671 |
rc = passwd_string32(scheme, &otp, &hash_otp); |
| 672 |
|
| 673 |
/* If successful, hash the static password, else cleanup and return */ |
| 674 |
if (rc == LUTIL_PASSWD_OK) { |
| 675 |
rc = lutil_passwd_hash(&pass, TOTP_AND_PW_HASH_SCHEME, |
| 676 |
&hash_pass, text); |
| 677 |
} else { |
| 678 |
return LUTIL_PASSWD_ERR; |
| 679 |
} |
| 680 |
|
| 681 |
/* If successful, allocate memory to combine them, else cleanup |
| 682 |
* and return */ |
| 683 |
if (rc == LUTIL_PASSWD_OK) { |
| 684 |
/* Add 1 character to bv_len to hold DELIM */ |
| 685 |
hash->bv_len = hash_pass.bv_len + hash_otp.bv_len + 1; |
| 686 |
hash->bv_val = ber_memalloc(hash->bv_len + 1); |
| 687 |
if (!hash->bv_val) |
| 688 |
rc = LUTIL_PASSWD_ERR; |
| 689 |
} else { |
| 690 |
memset(hash_otp.bv_val, 0, hash_otp.bv_len); |
| 691 |
ber_memfree(hash_otp.bv_val); |
| 692 |
return LUTIL_PASSWD_ERR; |
| 693 |
} |
| 694 |
|
| 695 |
/* If successful, combine the two hashes with the delimiter */ |
| 696 |
if (rc == LUTIL_PASSWD_OK) { |
| 697 |
AC_MEMCPY(hash->bv_val, hash_otp.bv_val, hash_otp.bv_len); |
| 698 |
hash->bv_val[hash_otp.bv_len] = DELIM; |
| 699 |
AC_MEMCPY(hash->bv_val + hash_otp.bv_len + 1, |
| 700 |
hash_pass.bv_val, hash_pass.bv_len); |
| 701 |
hash->bv_val[hash->bv_len] = '\0'; |
| 702 |
} |
| 703 |
|
| 704 |
/* Cleanup and return */ |
| 705 |
memset(hash_otp.bv_val, 0, hash_otp.bv_len); |
| 706 |
memset(hash_pass.bv_val, 0, hash_pass.bv_len); |
| 707 |
ber_memfree(hash_otp.bv_val); |
| 708 |
ber_memfree(hash_pass.bv_val); |
| 709 |
|
| 710 |
return rc; |
| 711 |
} |
| 712 |
|
| 713 |
static int hash_totp1( |
| 714 |
const struct berval *scheme, |
| 715 |
const struct berval *passwd, |
| 716 |
struct berval *hash, |
| 717 |
const char **text) |
| 718 |
{ |
| 719 |
#if 0 |
| 720 |
if (passwd->bv_len != SHA_DIGEST_LENGTH) { |
| 721 |
*text = "invalid key length"; |
| 722 |
return LUTIL_PASSWD_ERR; |
| 723 |
} |
| 724 |
#endif |
| 725 |
return passwd_string32(scheme, passwd, hash); |
| 726 |
} |
| 727 |
|
| 728 |
static int hash_totp256( |
| 729 |
const struct berval *scheme, |
| 730 |
const struct berval *passwd, |
| 731 |
struct berval *hash, |
| 732 |
const char **text) |
| 733 |
{ |
| 734 |
#if 0 |
| 735 |
if (passwd->bv_len != SHA256_DIGEST_LENGTH) { |
| 736 |
*text = "invalid key length"; |
| 737 |
return LUTIL_PASSWD_ERR; |
| 738 |
} |
| 739 |
#endif |
| 740 |
return passwd_string32(scheme, passwd, hash); |
| 741 |
} |
| 742 |
|
| 743 |
static int hash_totp512( |
| 744 |
const struct berval *scheme, |
| 745 |
const struct berval *passwd, |
| 746 |
struct berval *hash, |
| 747 |
const char **text) |
| 748 |
{ |
| 749 |
#if 0 |
| 750 |
if (passwd->bv_len != SHA512_DIGEST_LENGTH) { |
| 751 |
*text = "invalid key length"; |
| 752 |
return LUTIL_PASSWD_ERR; |
| 753 |
} |
| 754 |
#endif |
| 755 |
return passwd_string32(scheme, passwd, hash); |
| 756 |
} |
| 757 |
|
| 758 |
static int hash_totp1andpw( |
| 759 |
const struct berval *scheme, |
| 760 |
const struct berval *passwd, |
| 761 |
struct berval *hash, |
| 762 |
const char **text) |
| 763 |
{ |
| 764 |
#if 0 |
| 765 |
if (passwd->bv_len != SHA_DIGEST_LENGTH) { |
| 766 |
*text = "invalid key length"; |
| 767 |
return LUTIL_PASSWD_ERR; |
| 768 |
} |
| 769 |
#endif |
| 770 |
return hash_totp_and_pw(scheme, passwd, hash, text); |
| 771 |
} |
| 772 |
|
| 773 |
static int hash_totp256andpw( |
| 774 |
const struct berval *scheme, |
| 775 |
const struct berval *passwd, |
| 776 |
struct berval *hash, |
| 777 |
const char **text) |
| 778 |
{ |
| 779 |
#if 0 |
| 780 |
if (passwd->bv_len != SHA256_DIGEST_LENGTH) { |
| 781 |
*text = "invalid key length"; |
| 782 |
return LUTIL_PASSWD_ERR; |
| 783 |
} |
| 784 |
#endif |
| 785 |
return hash_totp_and_pw(scheme, passwd, hash, text); |
| 786 |
} |
| 787 |
|
| 788 |
static int hash_totp512andpw( |
| 789 |
const struct berval *scheme, |
| 790 |
const struct berval *passwd, |
| 791 |
struct berval *hash, |
| 792 |
const char **text) |
| 793 |
{ |
| 794 |
#if 0 |
| 795 |
if (passwd->bv_len != SHA512_DIGEST_LENGTH) { |
| 796 |
*text = "invalid key length"; |
| 797 |
return LUTIL_PASSWD_ERR; |
| 798 |
} |
| 799 |
#endif |
| 800 |
return hash_totp_and_pw(scheme, passwd, hash, text); |
| 801 |
} |
| 802 |
|
| 803 |
static int totp_op_cleanup( |
| 804 |
Operation *op, |
| 805 |
SlapReply *rs ) |
| 806 |
{ |
| 807 |
slap_callback *cb; |
| 808 |
|
| 809 |
/* clear out the current key */ |
| 810 |
ldap_pvt_thread_pool_setkey( op->o_threadctx, totp_op_cleanup, |
| 811 |
NULL, 0, NULL, NULL ); |
| 812 |
|
| 813 |
/* free the callback */ |
| 814 |
cb = op->o_callback; |
| 815 |
op->o_callback = cb->sc_next; |
| 816 |
if (cb->sc_private) |
| 817 |
ber_bvfree_x(cb->sc_private, op->o_tmpmemctx); |
| 818 |
op->o_tmpfree( cb, op->o_tmpmemctx ); |
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
static int |
| 823 |
totp_bind_response( Operation *op, SlapReply *rs ) |
| 824 |
{ |
| 825 |
Modifications *mod = NULL; |
| 826 |
BackendInfo *bi = op->o_bd->bd_info; |
| 827 |
Entry *e; |
| 828 |
int rc; |
| 829 |
|
| 830 |
/* we're only interested if the bind was successful */ |
| 831 |
if ( rs->sr_err != LDAP_SUCCESS ) |
| 832 |
return SLAP_CB_CONTINUE; |
| 833 |
|
| 834 |
rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e ); |
| 835 |
op->o_bd->bd_info = bi; |
| 836 |
|
| 837 |
if ( rc != LDAP_SUCCESS ) { |
| 838 |
return SLAP_CB_CONTINUE; |
| 839 |
} |
| 840 |
|
| 841 |
{ |
| 842 |
time_t now; |
| 843 |
Attribute *a; |
| 844 |
Modifications *m; |
| 845 |
char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ]; |
| 846 |
struct berval timestamp; |
| 847 |
|
| 848 |
/* get the current time */ |
| 849 |
now = op->o_time; |
| 850 |
|
| 851 |
/* update the authTimestamp in the user's entry with the current time */ |
| 852 |
timestamp.bv_val = nowstr; |
| 853 |
timestamp.bv_len = sizeof(nowstr); |
| 854 |
slap_timestamp( &now, ×tamp ); |
| 855 |
|
| 856 |
m = ch_calloc( sizeof(Modifications), 1 ); |
| 857 |
m->sml_op = LDAP_MOD_REPLACE; |
| 858 |
m->sml_flags = 0; |
| 859 |
m->sml_type = ad_authTimestamp->ad_cname; |
| 860 |
m->sml_desc = ad_authTimestamp; |
| 861 |
m->sml_numvals = 1; |
| 862 |
m->sml_values = ch_calloc( sizeof(struct berval), 2 ); |
| 863 |
m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 ); |
| 864 |
|
| 865 |
ber_dupbv( &m->sml_values[0], ×tamp ); |
| 866 |
ber_dupbv( &m->sml_nvalues[0], ×tamp ); |
| 867 |
m->sml_next = mod; |
| 868 |
mod = m; |
| 869 |
|
| 870 |
/* get authTimestamp attribute, if it exists */ |
| 871 |
if ((a = attr_find( e->e_attrs, ad_authTimestamp)) != NULL && op->o_callback->sc_private) { |
| 872 |
struct berval *bv = op->o_callback->sc_private; |
| 873 |
m = ch_calloc( sizeof(Modifications), 1 ); |
| 874 |
m->sml_op = LDAP_MOD_DELETE; |
| 875 |
m->sml_flags = 0; |
| 876 |
m->sml_type = ad_authTimestamp->ad_cname; |
| 877 |
m->sml_desc = ad_authTimestamp; |
| 878 |
m->sml_numvals = 1; |
| 879 |
m->sml_values = ch_calloc( sizeof(struct berval), 2 ); |
| 880 |
m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 ); |
| 881 |
|
| 882 |
ber_dupbv( &m->sml_values[0], bv ); |
| 883 |
ber_dupbv( &m->sml_nvalues[0], bv ); |
| 884 |
m->sml_next = mod; |
| 885 |
mod = m; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
be_entry_release_r( op, e ); |
| 890 |
|
| 891 |
/* perform the update */ |
| 892 |
if ( mod ) { |
| 893 |
Operation op2 = *op; |
| 894 |
SlapReply r2 = { REP_RESULT }; |
| 895 |
slap_callback cb = { NULL, slap_null_cb, NULL, NULL }; |
| 896 |
|
| 897 |
/* This is a DSA-specific opattr, it never gets replicated. */ |
| 898 |
op2.o_tag = LDAP_REQ_MODIFY; |
| 899 |
op2.o_callback = &cb; |
| 900 |
op2.orm_modlist = mod; |
| 901 |
op2.o_dn = op->o_bd->be_rootdn; |
| 902 |
op2.o_ndn = op->o_bd->be_rootndn; |
| 903 |
op2.o_dont_replicate = 1; |
| 904 |
rc = op->o_bd->be_modify( &op2, &r2 ); |
| 905 |
slap_mods_free( mod, 1 ); |
| 906 |
if (rc != LDAP_SUCCESS) { |
| 907 |
/* slapd has logged this as a success already, but we |
| 908 |
* need to fail it because the authTimestamp changed |
| 909 |
* out from under us. |
| 910 |
*/ |
| 911 |
rs->sr_err = LDAP_INVALID_CREDENTIALS; |
| 912 |
connection2anonymous(op->o_conn); |
| 913 |
op2 = *op; |
| 914 |
op2.o_callback = NULL; |
| 915 |
send_ldap_result(&op2, rs); |
| 916 |
op->o_bd->bd_info = bi; |
| 917 |
return rs->sr_err; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
op->o_bd->bd_info = bi; |
| 922 |
return SLAP_CB_CONTINUE; |
| 923 |
} |
| 924 |
|
| 925 |
static int totp_op_bind( |
| 926 |
Operation *op, |
| 927 |
SlapReply *rs ) |
| 928 |
{ |
| 929 |
/* If this is a simple Bind, stash the Op pointer so our chk |
| 930 |
* function can find it. Set a cleanup callback to clear it |
| 931 |
* out when the Bind completes. |
| 932 |
*/ |
| 933 |
if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE ) { |
| 934 |
slap_callback *cb; |
| 935 |
ldap_pvt_thread_pool_setkey( op->o_threadctx, |
| 936 |
totp_op_cleanup, op, 0, NULL, NULL ); |
| 937 |
cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx ); |
| 938 |
cb->sc_response = totp_bind_response; |
| 939 |
cb->sc_cleanup = totp_op_cleanup; |
| 940 |
cb->sc_next = op->o_callback; |
| 941 |
op->o_callback = cb; |
| 942 |
} |
| 943 |
return SLAP_CB_CONTINUE; |
| 944 |
} |
| 945 |
|
| 946 |
static int totp_db_open( |
| 947 |
BackendDB *be, |
| 948 |
ConfigReply *cr |
| 949 |
) |
| 950 |
{ |
| 951 |
int rc = 0; |
| 952 |
|
| 953 |
if (!ad_authTimestamp) { |
| 954 |
const char *text = NULL; |
| 955 |
rc = slap_str2ad("authTimestamp", &ad_authTimestamp, &text); |
| 956 |
if (rc) { |
| 957 |
rc = register_at(totp_OpSchema[0].def, totp_OpSchema[0].ad, 0 ); |
| 958 |
if (rc) { |
| 959 |
snprintf(cr->msg, sizeof(cr->msg), "unable to find or register authTimestamp attribute: %s (%d)", |
| 960 |
text, rc); |
| 961 |
Debug(LDAP_DEBUG_ANY, "totp: %s.\n", cr->msg ); |
| 962 |
} |
| 963 |
ad_authTimestamp->ad_type->sat_flags |= SLAP_AT_MANAGEABLE; |
| 964 |
} |
| 965 |
} |
| 966 |
return rc; |
| 967 |
} |
| 968 |
|
| 969 |
static slap_overinst totp; |
| 970 |
|
| 971 |
int |
| 972 |
totp_initialize(void) |
| 973 |
{ |
| 974 |
int rc; |
| 975 |
|
| 976 |
totp.on_bi.bi_type = "totp"; |
| 977 |
|
| 978 |
totp.on_bi.bi_db_open = totp_db_open; |
| 979 |
totp.on_bi.bi_op_bind = totp_op_bind; |
| 980 |
|
| 981 |
rc = lutil_passwd_add((struct berval *) &scheme_totp1, chk_totp1, hash_totp1); |
| 982 |
if (!rc) |
| 983 |
rc = lutil_passwd_add((struct berval *) &scheme_totp256, chk_totp256, hash_totp256); |
| 984 |
if (!rc) |
| 985 |
rc = lutil_passwd_add((struct berval *) &scheme_totp512, chk_totp512, hash_totp512); |
| 986 |
if (!rc) |
| 987 |
rc = lutil_passwd_add((struct berval *) &scheme_totp1andpw, chk_totp1andpw, hash_totp1andpw); |
| 988 |
if (!rc) |
| 989 |
rc = lutil_passwd_add((struct berval *) &scheme_totp256andpw, chk_totp256andpw, hash_totp256andpw); |
| 990 |
if (!rc) |
| 991 |
rc = lutil_passwd_add((struct berval *) &scheme_totp512andpw, chk_totp512andpw, hash_totp512andpw); |
| 992 |
if (rc) |
| 993 |
return rc; |
| 994 |
|
| 995 |
return overlay_register(&totp); |
| 996 |
} |
| 997 |
|
| 998 |
int init_module(int argc, char *argv[]) { |
| 999 |
return totp_initialize(); |
| 1000 |
} |