Debian Codegraph @

servers/slapd/overlays/otp.c

raw
1 /* otp.c - OATH 2-factor authentication module */
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 * Portions Copyright 2016-2017 by Michael Ströder <[email protected]>
8 * Portions Copyright 2018 by Ondřej Kuzník, Symas Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19 /* ACKNOWLEDGEMENTS:
20 * This work includes code from the lastbind overlay.
21 */
22
23 #include <portable.h>
24
25 #ifdef SLAPD_OVER_OTP
26
27 #if HAVE_STDINT_H
28 #include <stdint.h>
29 #endif
30
31 #include <lber.h>
32 #include <lber_pvt.h>
33 #include "lutil.h"
34 #include <ac/stdlib.h>
35 #include <ac/ctype.h>
36 #include <ac/string.h>
37 /* include socket.h to get sys/types.h and/or winsock2.h */
38 #include <ac/socket.h>
39
40 #if HAVE_OPENSSL
41 #include <openssl/sha.h>
42 #include <openssl/hmac.h>
43
44 #define TOTP_SHA512_DIGEST_LENGTH SHA512_DIGEST_LENGTH
45
46 #if OPENSSL_VERSION_MAJOR >= 3
47 #define TOTP_SHA1 SN_sha1
48 #define TOTP_SHA224 SN_sha224
49 #define TOTP_SHA256 SN_sha256
50 #define TOTP_SHA384 SN_sha384
51 #define TOTP_SHA512 SN_sha512
52 #define TOTP_HMAC_CTX EVP_MAC_CTX *
53 #else
54 #define TOTP_SHA1 EVP_sha1()
55 #define TOTP_SHA224 EVP_sha224()
56 #define TOTP_SHA256 EVP_sha256()
57 #define TOTP_SHA384 EVP_sha384()
58 #define TOTP_SHA512 EVP_sha512()
59 #define TOTP_HMAC_CTX HMAC_CTX *
60 #endif
61
62 #if OPENSSL_VERSION_NUMBER < 0x10100000L
63 static HMAC_CTX *
64 HMAC_CTX_new( void )
65 {
66 HMAC_CTX *ctx = OPENSSL_malloc( sizeof(*ctx) );
67 if ( ctx != NULL ) {
68 HMAC_CTX_init( ctx );
69 }
70 return ctx;
71 }
72
73 static void
74 HMAC_CTX_free( HMAC_CTX *ctx )
75 {
76 if ( ctx != NULL ) {
77 HMAC_CTX_cleanup( ctx );
78 OPENSSL_free( ctx );
79 }
80 }
81 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
82
83 #if OPENSSL_VERSION_MAJOR >= 3
84 static EVP_MAC *evp_mac;
85 #define HMAC_setup( ctx, key, len, hash ) \
86 { OSSL_PARAM params[2]; \
87 ctx = EVP_MAC_CTX_new( evp_mac ); \
88 params[0] = OSSL_PARAM_construct_utf8_string( "digest", (char *)hash, 0 ); \
89 params[1] = OSSL_PARAM_construct_end(); \
90 EVP_MAC_init( ctx, key, len, params ); }
91 #define HMAC_crunch( ctx, buf, len ) EVP_MAC_update( ctx, buf, len )
92 #define HMAC_finish( ctx, dig, dlen ) \
93 { size_t outlen; \
94 EVP_MAC_final( ctx, dig, &outlen, TOTP_SHA512_DIGEST_LENGTH ); \
95 dlen = outlen; } \
96 EVP_MAC_CTX_free( ctx )
97
98 #else
99 #define HMAC_setup( ctx, key, len, hash ) \
100 ctx = HMAC_CTX_new(); \
101 HMAC_Init_ex( ctx, key, len, hash, 0 )
102 #define HMAC_crunch( ctx, buf, len ) HMAC_Update( ctx, buf, len )
103 #define HMAC_finish( ctx, dig, dlen ) \
104 HMAC_Final( ctx, dig, &dlen ); \
105 HMAC_CTX_free( ctx )
106 #endif
107
108 #elif HAVE_GNUTLS
109 #include <nettle/hmac.h>
110
111 #define TOTP_SHA512_DIGEST_LENGTH SHA512_DIGEST_SIZE
112 #define TOTP_SHA1 &nettle_sha1
113 #define TOTP_SHA224 &nettle_sha224
114 #define TOTP_SHA256 &nettle_sha256
115 #define TOTP_SHA384 &nettle_sha384
116 #define TOTP_SHA512 &nettle_sha512
117 #define TOTP_HMAC_CTX struct hmac_sha512_ctx
118
119 #define HMAC_setup( ctx, key, len, hash ) \
120 const struct nettle_hash *h = hash; \
121 hmac_set_key( &ctx.outer, &ctx.inner, &ctx.state, h, len, key )
122 #define HMAC_crunch( ctx, buf, len ) hmac_update( &ctx.state, h, len, buf )
123 #define HMAC_finish( ctx, dig, dlen ) \
124 hmac_digest( &ctx.outer, &ctx.inner, &ctx.state, h, h->digest_size, dig ); \
125 dlen = h->digest_size
126
127 #else
128 #error Unsupported crypto backend.
129 #endif
130
131 #include "slap.h"
132 #include "slap-config.h"
133
134 /* Schema from OATH-LDAP project by Michael Ströder */
135
136 static struct {
137 char *name, *oid;
138 } otp_oid[] = {
139 { "oath-ldap", "1.3.6.1.4.1.5427.1.389.4226" },
140 { "oath-ldap-at", "oath-ldap:4" },
141 { "oath-ldap-oc", "oath-ldap:6" },
142 { NULL }
143 };
144
145 AttributeDescription *ad_oathOTPToken;
146 AttributeDescription *ad_oathSecret;
147 AttributeDescription *ad_oathOTPLength;
148 AttributeDescription *ad_oathHMACAlgorithm;
149
150 AttributeDescription *ad_oathHOTPParams;
151 AttributeDescription *ad_oathHOTPToken;
152 AttributeDescription *ad_oathHOTPCounter;
153 AttributeDescription *ad_oathHOTPLookahead;
154
155 AttributeDescription *ad_oathTOTPTimeStepPeriod;
156 AttributeDescription *ad_oathTOTPParams;
157 AttributeDescription *ad_oathTOTPToken;
158 AttributeDescription *ad_oathTOTPLastTimeStep;
159 AttributeDescription *ad_oathTOTPTimeStepWindow;
160 AttributeDescription *ad_oathTOTPTimeStepDrift;
161
162 static struct otp_at {
163 char *schema;
164 AttributeDescription **adp;
165 } otp_at[] = {
166 { "( oath-ldap-at:1 "
167 "NAME 'oathSecret' "
168 "DESC 'OATH-LDAP: Shared Secret (possibly encrypted with public key in oathEncKey)' "
169 "X-ORIGIN 'OATH-LDAP' "
170 "SINGLE-VALUE "
171 "EQUALITY octetStringMatch "
172 "SUBSTR octetStringSubstringsMatch "
173 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
174 &ad_oathSecret },
175
176 { "( oath-ldap-at:2 "
177 "NAME 'oathTokenSerialNumber' "
178 "DESC 'OATH-LDAP: Proprietary hardware token serial number assigned by vendor' "
179 "X-ORIGIN 'OATH-LDAP' "
180 "SINGLE-VALUE "
181 "EQUALITY caseIgnoreMatch "
182 "SUBSTR caseIgnoreSubstringsMatch "
183 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.44{64})" },
184
185 { "( oath-ldap-at:3 "
186 "NAME 'oathTokenIdentifier' "
187 "DESC 'OATH-LDAP: Globally unique OATH token identifier' "
188 "X-ORIGIN 'OATH-LDAP' "
189 "SINGLE-VALUE "
190 "EQUALITY caseIgnoreMatch "
191 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{256} )" },
192
193 { "( oath-ldap-at:4 "
194 "NAME 'oathParamsEntry' "
195 "DESC 'OATH-LDAP: DN pointing to OATH parameter/policy object' "
196 "X-ORIGIN 'OATH-LDAP' "
197 "SINGLE-VALUE "
198 "SUP distinguishedName )" },
199 { "( oath-ldap-at:4.1 "
200 "NAME 'oathTOTPTimeStepPeriod' "
201 "DESC 'OATH-LDAP: Time window for TOTP (seconds)' "
202 "X-ORIGIN 'OATH-LDAP' "
203 "SINGLE-VALUE "
204 "EQUALITY integerMatch "
205 "ORDERING integerOrderingMatch "
206 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
207 &ad_oathTOTPTimeStepPeriod },
208
209 { "( oath-ldap-at:5 "
210 "NAME 'oathOTPLength' "
211 "DESC 'OATH-LDAP: Length of OTP (number of digits)' "
212 "X-ORIGIN 'OATH-LDAP' "
213 "SINGLE-VALUE "
214 "EQUALITY integerMatch "
215 "ORDERING integerOrderingMatch "
216 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
217 &ad_oathOTPLength },
218 { "( oath-ldap-at:5.1 "
219 "NAME 'oathHOTPParams' "
220 "DESC 'OATH-LDAP: DN pointing to HOTP parameter object' "
221 "X-ORIGIN 'OATH-LDAP' "
222 "SINGLE-VALUE "
223 "SUP oathParamsEntry )",
224 &ad_oathHOTPParams },
225 { "( oath-ldap-at:5.2 "
226 "NAME 'oathTOTPParams' "
227 "DESC 'OATH-LDAP: DN pointing to TOTP parameter object' "
228 "X-ORIGIN 'OATH-LDAP' "
229 "SINGLE-VALUE "
230 "SUP oathParamsEntry )",
231 &ad_oathTOTPParams },
232
233 { "( oath-ldap-at:6 "
234 "NAME 'oathHMACAlgorithm' "
235 "DESC 'OATH-LDAP: HMAC algorithm used for generating OTP values' "
236 "X-ORIGIN 'OATH-LDAP' "
237 "SINGLE-VALUE "
238 "EQUALITY objectIdentifierMatch "
239 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
240 &ad_oathHMACAlgorithm },
241
242 { "( oath-ldap-at:7 "
243 "NAME 'oathTimestamp' "
244 "DESC 'OATH-LDAP: Timestamp (not directly used).' "
245 "X-ORIGIN 'OATH-LDAP' "
246 "SINGLE-VALUE "
247 "EQUALITY generalizedTimeMatch "
248 "ORDERING generalizedTimeOrderingMatch "
249 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )" },
250 { "( oath-ldap-at:7.1 "
251 "NAME 'oathLastFailure' "
252 "DESC 'OATH-LDAP: Timestamp of last failed OATH validation' "
253 "X-ORIGIN 'OATH-LDAP' "
254 "SINGLE-VALUE "
255 "SUP oathTimestamp )" },
256 { "( oath-ldap-at:7.2 "
257 "NAME 'oathLastLogin' "
258 "DESC 'OATH-LDAP: Timestamp of last successful OATH validation' "
259 "X-ORIGIN 'OATH-LDAP' "
260 "SINGLE-VALUE "
261 "SUP oathTimestamp )" },
262 { "( oath-ldap-at:7.3 "
263 "NAME 'oathSecretTime' "
264 "DESC 'OATH-LDAP: Timestamp of generation of oathSecret attribute.' "
265 "X-ORIGIN 'OATH-LDAP' "
266 "SINGLE-VALUE "
267 "SUP oathTimestamp )" },
268
269 { "( oath-ldap-at:8 "
270 "NAME 'oathSecretMaxAge' "
271 "DESC 'OATH-LDAP: Time in seconds for which the shared secret (oathSecret) will be valid from oathSecretTime value.' "
272 "X-ORIGIN 'OATH-LDAP' "
273 "SINGLE-VALUE "
274 "EQUALITY integerMatch "
275 "ORDERING integerOrderingMatch "
276 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )" },
277
278 { "( oath-ldap-at:9 "
279 "NAME 'oathToken' "
280 "DESC 'OATH-LDAP: DN pointing to OATH token object' "
281 "X-ORIGIN 'OATH-LDAP' "
282 "SINGLE-VALUE "
283 "SUP distinguishedName )" },
284 { "( oath-ldap-at:9.1 "
285 "NAME 'oathHOTPToken' "
286 "DESC 'OATH-LDAP: DN pointing to OATH/HOTP token object' "
287 "X-ORIGIN 'OATH-LDAP' "
288 "SINGLE-VALUE "
289 "SUP oathToken )",
290 &ad_oathHOTPToken },
291 { "( oath-ldap-at:9.2 "
292 "NAME 'oathTOTPToken' "
293 "DESC 'OATH-LDAP: DN pointing to OATH/TOTP token object' "
294 "X-ORIGIN 'OATH-LDAP' "
295 "SINGLE-VALUE "
296 "SUP oathToken )",
297 &ad_oathTOTPToken },
298
299 { "( oath-ldap-at:10 "
300 "NAME 'oathCounter' "
301 "DESC 'OATH-LDAP: Counter for OATH data (not directly used)' "
302 "X-ORIGIN 'OATH-LDAP' "
303 "SINGLE-VALUE "
304 "EQUALITY integerMatch "
305 "ORDERING integerOrderingMatch "
306 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )" },
307 { "( oath-ldap-at:10.1 "
308 "NAME 'oathFailureCount' "
309 "DESC 'OATH-LDAP: OATH failure counter' "
310 "X-ORIGIN 'OATH-LDAP' "
311 "SINGLE-VALUE "
312 "SUP oathCounter )" },
313 { "( oath-ldap-at:10.2 "
314 "NAME 'oathHOTPCounter' "
315 "DESC 'OATH-LDAP: Counter for HOTP' "
316 "X-ORIGIN 'OATH-LDAP' "
317 "SINGLE-VALUE "
318 "SUP oathCounter )",
319 &ad_oathHOTPCounter },
320 { "( oath-ldap-at:10.3 "
321 "NAME 'oathHOTPLookAhead' "
322 "DESC 'OATH-LDAP: Look-ahead window for HOTP' "
323 "X-ORIGIN 'OATH-LDAP' "
324 "SINGLE-VALUE "
325 "SUP oathCounter )",
326 &ad_oathHOTPLookahead },
327 { "( oath-ldap-at:10.5 "
328 "NAME 'oathThrottleLimit' "
329 "DESC 'OATH-LDAP: Failure throttle limit' "
330 "X-ORIGIN 'OATH-LDAP' "
331 "SINGLE-VALUE "
332 "SUP oathCounter )" },
333 { "( oath-ldap-at:10.6 "
334 "NAME 'oathTOTPLastTimeStep' "
335 "DESC 'OATH-LDAP: Last time step seen for TOTP (time/period)' "
336 "X-ORIGIN 'OATH-LDAP' "
337 "SINGLE-VALUE "
338 "SUP oathCounter )",
339 &ad_oathTOTPLastTimeStep },
340 { "( oath-ldap-at:10.7 "
341 "NAME 'oathMaxUsageCount' "
342 "DESC 'OATH-LDAP: Maximum number of times a token can be used' "
343 "X-ORIGIN 'OATH-LDAP' "
344 "SINGLE-VALUE "
345 "SUP oathCounter )" },
346 { "( oath-ldap-at:10.8 "
347 "NAME 'oathTOTPTimeStepWindow' "
348 "DESC 'OATH-LDAP: Size of time step +/- tolerance window used for TOTP validation' "
349 "X-ORIGIN 'OATH-LDAP' "
350 "SINGLE-VALUE "
351 "SUP oathCounter )",
352 &ad_oathTOTPTimeStepWindow },
353 { "( oath-ldap-at:10.9 "
354 "NAME 'oathTOTPTimeStepDrift' "
355 "DESC 'OATH-LDAP: Last observed time step shift seen for TOTP' "
356 "X-ORIGIN 'OATH-LDAP' "
357 "SINGLE-VALUE "
358 "SUP oathCounter )",
359 &ad_oathTOTPTimeStepDrift },
360
361 { "( oath-ldap-at:11 "
362 "NAME 'oathSecretLength' "
363 "DESC 'OATH-LDAP: Length of plain-text shared secret (number of bytes)' "
364 "X-ORIGIN 'OATH-LDAP' "
365 "SINGLE-VALUE "
366 "EQUALITY integerMatch "
367 "ORDERING integerOrderingMatch "
368 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )" },
369
370 { "( oath-ldap-at:12 "
371 "NAME 'oathEncKey' "
372 "DESC 'OATH-LDAP: public key to be used for encrypting new shared secrets' "
373 "X-ORIGIN 'OATH-LDAP' "
374 "SINGLE-VALUE "
375 "EQUALITY caseIgnoreMatch "
376 "SUBSTR caseIgnoreSubstringsMatch "
377 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )" },
378
379 { "( oath-ldap-at:13 "
380 "NAME 'oathResultCode' "
381 "DESC 'OATH-LDAP: LDAP resultCode to use in response' "
382 "X-ORIGIN 'OATH-LDAP' "
383 "SINGLE-VALUE "
384 "EQUALITY integerMatch "
385 "ORDERING integerOrderingMatch "
386 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )" },
387 { "( oath-ldap-at:13.1 "
388 "NAME 'oathSuccessResultCode' "
389 "DESC 'OATH-LDAP: success resultCode to use in bind/compare response' "
390 "X-ORIGIN 'OATH-LDAP' "
391 "SUP oathResultCode )" },
392 { "( oath-ldap-at:13.2 "
393 "NAME 'oathFailureResultCode' "
394 "DESC 'OATH-LDAP: failure resultCode to use in bind/compare response' "
395 "X-ORIGIN 'OATH-LDAP' "
396 "SUP oathResultCode )" },
397
398 { "( oath-ldap-at:14 "
399 "NAME 'oathTokenPIN' "
400 "DESC 'OATH-LDAP: Configuration PIN (possibly encrypted with oathEncKey)' "
401 "X-ORIGIN 'OATH-LDAP' "
402 "SINGLE-VALUE "
403 "EQUALITY caseIgnoreMatch "
404 "SUBSTR caseIgnoreSubstringsMatch "
405 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )" },
406
407 { "( oath-ldap-at:15 "
408 "NAME 'oathMessage' "
409 "DESC 'OATH-LDAP: success diagnosticMessage to use in bind/compare response' "
410 "X-ORIGIN 'OATH-LDAP' "
411 "SINGLE-VALUE "
412 "EQUALITY caseIgnoreMatch "
413 "SUBSTR caseIgnoreSubstringsMatch "
414 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024} )" },
415 { "( oath-ldap-at:15.1 "
416 "NAME 'oathSuccessMessage' "
417 "DESC 'OATH-LDAP: success diagnosticMessage to use in bind/compare response' "
418 "X-ORIGIN 'OATH-LDAP' "
419 "SUP oathMessage )" },
420 { "( oath-ldap-at:15.2 "
421 "NAME 'oathFailureMessage' "
422 "DESC 'OATH-LDAP: failure diagnosticMessage to use in bind/compare response' "
423 "X-ORIGIN 'OATH-LDAP' "
424 "SUP oathMessage )" },
425
426 { NULL }
427 };
428
429 ObjectClass *oc_oathOTPUser;
430 ObjectClass *oc_oathHOTPToken;
431 ObjectClass *oc_oathTOTPToken;
432 ObjectClass *oc_oathHOTPParams;
433 ObjectClass *oc_oathTOTPParams;
434
435 static struct otp_oc {
436 char *schema;
437 ObjectClass **ocp;
438 } otp_oc[] = {
439 { "( oath-ldap-oc:1 "
440 "NAME 'oathUser' "
441 "DESC 'OATH-LDAP: User Object' "
442 "X-ORIGIN 'OATH-LDAP' "
443 "ABSTRACT )",
444 &oc_oathOTPUser },
445 { "( oath-ldap-oc:1.1 "
446 "NAME 'oathHOTPUser' "
447 "DESC 'OATH-LDAP: HOTP user object' "
448 "X-ORIGIN 'OATH-LDAP' "
449 "AUXILIARY "
450 "SUP oathUser "
451 "MAY ( oathHOTPToken ) )" },
452 { "( oath-ldap-oc:1.2 "
453 "NAME 'oathTOTPUser' "
454 "DESC 'OATH-LDAP: TOTP user object' "
455 "X-ORIGIN 'OATH-LDAP' "
456 "AUXILIARY "
457 "SUP oathUser "
458 "MUST ( oathTOTPToken ) )" },
459 { "( oath-ldap-oc:2 "
460 "NAME 'oathParams' "
461 "DESC 'OATH-LDAP: Parameter object' "
462 "X-ORIGIN 'OATH-LDAP' "
463 "ABSTRACT "
464 "MUST ( oathOTPLength $ oathHMACAlgorithm ) "
465 "MAY ( oathSecretMaxAge $ oathSecretLength $ "
466 "oathMaxUsageCount $ oathThrottleLimit $ oathEncKey $ "
467 "oathSuccessResultCode $ oathSuccessMessage $ "
468 "oathFailureResultCode $ oathFailureMessage ) )" },
469 { "( oath-ldap-oc:2.1 "
470 "NAME 'oathHOTPParams' "
471 "DESC 'OATH-LDAP: HOTP parameter object' "
472 "X-ORIGIN 'OATH-LDAP' "
473 "AUXILIARY "
474 "SUP oathParams "
475 "MUST ( oathHOTPLookAhead ) )",
476 &oc_oathHOTPParams },
477 { "( oath-ldap-oc:2.2 "
478 "NAME 'oathTOTPParams' "
479 "DESC 'OATH-LDAP: TOTP parameter object' "
480 "X-ORIGIN 'OATH-LDAP' "
481 "AUXILIARY "
482 "SUP oathParams "
483 "MUST ( oathTOTPTimeStepPeriod ) "
484 "MAY ( oathTOTPTimeStepWindow ) )",
485 &oc_oathTOTPParams },
486 { "( oath-ldap-oc:3 "
487 "NAME 'oathToken' "
488 "DESC 'OATH-LDAP: User Object' "
489 "X-ORIGIN 'OATH-LDAP' "
490 "ABSTRACT "
491 "MAY ( oathSecret $ oathSecretTime $ "
492 "oathLastLogin $ oathFailureCount $ oathLastFailure $ "
493 "oathTokenSerialNumber $ oathTokenIdentifier $ oathTokenPIN ) )" },
494 { "( oath-ldap-oc:3.1 "
495 "NAME 'oathHOTPToken' "
496 "DESC 'OATH-LDAP: HOTP token object' "
497 "X-ORIGIN 'OATH-LDAP' "
498 "AUXILIARY "
499 "SUP oathToken "
500 "MAY ( oathHOTPParams $ oathHOTPCounter ) )",
501 &oc_oathHOTPToken },
502 { "( oath-ldap-oc:3.2 "
503 "NAME 'oathTOTPToken' "
504 "DESC 'OATH-LDAP: TOTP token' "
505 "X-ORIGIN 'OATH-LDAP' "
506 "AUXILIARY "
507 "SUP oathToken "
508 "MAY ( oathTOTPParams $ oathTOTPLastTimeStep $ oathTOTPTimeStepDrift ) )",
509 &oc_oathTOTPToken },
510 { NULL }
511 };
512
513 typedef struct myval {
514 ber_len_t mv_len;
515 void *mv_val;
516 } myval;
517
518 static void
519 do_hmac( const void *hash, myval *key, myval *data, myval *out )
520 {
521 TOTP_HMAC_CTX ctx;
522 unsigned int digestLen;
523
524 HMAC_setup( ctx, key->mv_val, key->mv_len, hash );
525 HMAC_crunch( ctx, data->mv_val, data->mv_len );
526 HMAC_finish( ctx, out->mv_val, digestLen );
527 out->mv_len = digestLen;
528 }
529
530 #define MAX_DIGITS 8
531 static const int DIGITS_POWER[] = {
532 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
533 };
534
535 static const void *
536 otp_choose_mech( struct berval *oid )
537 {
538 /* RFC 8018 OIDs */
539 const struct berval oid_hmacwithSHA1 = BER_BVC("1.2.840.113549.2.7");
540 const struct berval oid_hmacwithSHA224 = BER_BVC("1.2.840.113549.2.8");
541 const struct berval oid_hmacwithSHA256 = BER_BVC("1.2.840.113549.2.9");
542 const struct berval oid_hmacwithSHA384 = BER_BVC("1.2.840.113549.2.10");
543 const struct berval oid_hmacwithSHA512 = BER_BVC("1.2.840.113549.2.11");
544
545 if ( !ber_bvcmp( &oid_hmacwithSHA1, oid ) ) {
546 return TOTP_SHA1;
547 } else if ( !ber_bvcmp( &oid_hmacwithSHA224, oid ) ) {
548 return TOTP_SHA224;
549 } else if ( !ber_bvcmp( &oid_hmacwithSHA256, oid ) ) {
550 return TOTP_SHA256;
551 } else if ( !ber_bvcmp( &oid_hmacwithSHA384, oid ) ) {
552 return TOTP_SHA384;
553 } else if ( !ber_bvcmp( &oid_hmacwithSHA512, oid ) ) {
554 return TOTP_SHA512;
555 }
556
557 Debug( LDAP_DEBUG_TRACE, "otp_choose_mech: "
558 "hmac OID %s unsupported\n",
559 oid->bv_val );
560 return NULL;
561 }
562
563 static void
564 generate(
565 struct berval *bv,
566 uint64_t tval,
567 int digits,
568 struct berval *out,
569 const void *mech )
570 {
571 unsigned char digest[TOTP_SHA512_DIGEST_LENGTH];
572 myval digval;
573 myval key, data;
574 unsigned char msg[8];
575 int i, offset, res, otp;
576
577 #if WORDS_BIGENDIAN
578 *(uint64_t *)msg = tval;
579 #else
580 for ( i = 7; i >= 0; i-- ) {
581 msg[i] = tval & 0xff;
582 tval >>= 8;
583 }
584 #endif
585
586 key.mv_len = bv->bv_len;
587 key.mv_val = bv->bv_val;
588
589 data.mv_val = msg;
590 data.mv_len = sizeof(msg);
591
592 digval.mv_val = digest;
593 digval.mv_len = sizeof(digest);
594 do_hmac( mech, &key, &data, &digval );
595
596 offset = digest[digval.mv_len - 1] & 0xf;
597 res = ( (digest[offset] & 0x7f) << 24 ) |
598 ( ( digest[offset + 1] & 0xff ) << 16 ) |
599 ( ( digest[offset + 2] & 0xff ) << 8 ) |
600 ( digest[offset + 3] & 0xff );
601
602 otp = res % DIGITS_POWER[digits];
603 out->bv_len = snprintf( out->bv_val, out->bv_len, "%0*d", digits, otp );
604 }
605
606 static int
607 otp_bind_response( Operation *op, SlapReply *rs )
608 {
609 if ( rs->sr_err == LDAP_SUCCESS ) {
610 /* If the bind succeeded, return our result */
611 rs->sr_err = LDAP_INVALID_CREDENTIALS;
612 }
613 return SLAP_CB_CONTINUE;
614 }
615
616 static long
617 otp_hotp( Operation *op, Entry *token )
618 {
619 char outbuf[MAX_DIGITS + 1];
620 Entry *params = NULL;
621 Attribute *a;
622 BerValue *secret, client_otp;
623 const void *mech;
624 long last_step = -1, found = -1;
625 int i, otp_len, window;
626
627 a = attr_find( token->e_attrs, ad_oathSecret );
628 secret = &a->a_vals[0];
629
630 a = attr_find( token->e_attrs, ad_oathHOTPCounter );
631 if ( a && lutil_atol( &last_step, a->a_vals[0].bv_val ) != 0 ) {
632 Debug( LDAP_DEBUG_ANY, "otp_hotp: "
633 "could not parse oathHOTPCounter value %s\n",
634 a->a_vals[0].bv_val );
635 goto done;
636 }
637
638 a = attr_find( token->e_attrs, ad_oathHOTPParams );
639 if ( !a ||
640 be_entry_get_rw( op, &a->a_nvals[0], oc_oathHOTPParams, NULL, 0,
641 &params ) ) {
642 goto done;
643 }
644
645 a = attr_find( params->e_attrs, ad_oathOTPLength );
646 if ( lutil_atoi( &otp_len, a->a_vals[0].bv_val ) != 0 ) {
647 Debug( LDAP_DEBUG_ANY, "otp_hotp: "
648 "could not parse oathOTPLength value %s\n",
649 a->a_vals[0].bv_val );
650 goto done;
651 }
652 if ( otp_len > MAX_DIGITS || op->orb_cred.bv_len < otp_len ) {
653 /* Client didn't even send the token, fail immediately */
654 goto done;
655 }
656
657 a = attr_find( params->e_attrs, ad_oathHOTPLookahead );
658 if ( lutil_atoi( &window, a->a_vals[0].bv_val ) != 0 ) {
659 Debug( LDAP_DEBUG_ANY, "otp_hotp: "
660 "could not parse oathHOTPLookAhead value %s\n",
661 a->a_vals[0].bv_val );
662 goto done;
663 }
664 window++;
665
666 a = attr_find( params->e_attrs, ad_oathHMACAlgorithm );
667 if ( !(mech = otp_choose_mech( &a->a_vals[0] )) ) {
668 goto done;
669 }
670 be_entry_release_r( op, params );
671 params = NULL;
672
673 /* We are provided "password" + "OTP", split accordingly */
674 client_otp.bv_len = otp_len;
675 client_otp.bv_val = op->orb_cred.bv_val + op->orb_cred.bv_len - otp_len;
676
677 /* If check succeeds, advance the step counter accordingly */
678 for ( i = 1; i <= window; i++ ) {
679 BerValue out = { .bv_val = outbuf, .bv_len = sizeof(outbuf) };
680
681 generate( secret, last_step + i, otp_len, &out, mech );
682 if ( !ber_bvcmp( &out, &client_otp ) ) {
683 found = last_step + i;
684 /* Would we leak information if we stopped right now? */
685 }
686 }
687
688 if ( found >= 0 ) {
689 /* OTP check passed, trim the password */
690 op->orb_cred.bv_len -= otp_len;
691 Debug( LDAP_DEBUG_STATS, "%s HOTP token %s no. %ld redeemed\n",
692 op->o_log_prefix, token->e_name.bv_val, found );
693 }
694
695 done:
696 memset( outbuf, 0, sizeof(outbuf) );
697 if ( params ) {
698 be_entry_release_r( op, params );
699 }
700 return found;
701 }
702
703 static long
704 otp_totp( Operation *op, Entry *token, long *drift )
705 {
706 char outbuf[MAX_DIGITS + 1];
707 Entry *params = NULL;
708 Attribute *a;
709 BerValue *secret, client_otp;
710 const void *mech;
711 long t, last_step = -1, found = -1, window = 0, old_drift;
712 int i, otp_len, time_step;
713
714 a = attr_find( token->e_attrs, ad_oathSecret );
715 secret = &a->a_vals[0];
716
717 a = attr_find( token->e_attrs, ad_oathTOTPLastTimeStep );
718 if ( a && lutil_atol( &last_step, a->a_vals[0].bv_val ) != 0 ) {
719 Debug( LDAP_DEBUG_ANY, "otp_totp: "
720 "could not parse oathTOTPLastTimeStep value %s\n",
721 a->a_vals[0].bv_val );
722 goto done;
723 }
724
725 a = attr_find( token->e_attrs, ad_oathTOTPParams );
726 if ( !a ||
727 be_entry_get_rw( op, &a->a_nvals[0], oc_oathTOTPParams, NULL, 0,
728 &params ) ) {
729 goto done;
730 }
731
732 a = attr_find( params->e_attrs, ad_oathTOTPTimeStepPeriod );
733 if ( lutil_atoi( &time_step, a->a_vals[0].bv_val ) != 0 ) {
734 Debug( LDAP_DEBUG_ANY, "otp_totp: "
735 "could not parse oathTOTPTimeStepPeriod value %s\n",
736 a->a_vals[0].bv_val );
737 goto done;
738 }
739
740 a = attr_find( params->e_attrs, ad_oathTOTPTimeStepWindow );
741 if ( a && lutil_atol( &window, a->a_vals[0].bv_val ) != 0 ) {
742 Debug( LDAP_DEBUG_ANY, "otp_totp: "
743 "could not parse oathTOTPTimeStepWindow value %s\n",
744 a->a_vals[0].bv_val );
745 goto done;
746 }
747
748 a = attr_find( params->e_attrs, ad_oathTOTPTimeStepDrift );
749 if ( a && lutil_atol( drift, a->a_vals[0].bv_val ) != 0 ) {
750 Debug( LDAP_DEBUG_ANY, "otp_totp: "
751 "could not parse oathTOTPTimeStepDrift value %s\n",
752 a->a_vals[0].bv_val );
753 goto done;
754 }
755 old_drift = *drift;
756 t = op->o_time / time_step + *drift;
757
758 a = attr_find( params->e_attrs, ad_oathOTPLength );
759 if ( lutil_atoi( &otp_len, a->a_vals[0].bv_val ) != 0 ) {
760 Debug( LDAP_DEBUG_ANY, "otp_totp: "
761 "could not parse oathOTPLength value %s\n",
762 a->a_vals[0].bv_val );
763 goto done;
764 }
765 if ( otp_len > MAX_DIGITS || op->orb_cred.bv_len < otp_len ) {
766 /* Client didn't even send the token, fail immediately */
767 goto done;
768 }
769
770 a = attr_find( params->e_attrs, ad_oathHMACAlgorithm );
771 if ( !(mech = otp_choose_mech( &a->a_vals[0] )) ) {
772 goto done;
773 }
774 be_entry_release_r( op, params );
775 params = NULL;
776
777 /* We are provided "password" + "OTP", split accordingly */
778 client_otp.bv_len = otp_len;
779 client_otp.bv_val = op->orb_cred.bv_val + op->orb_cred.bv_len - otp_len;
780
781 /* If check succeeds, advance the step counter accordingly */
782 /* Negation of A001057 series that enumerates all integers:
783 * (0, -1, 1, -2, 2, ...) */
784 for ( i = 0; i >= -window; i = ( i < 0 ) ? -i : ~i ) {
785 BerValue out = { .bv_val = outbuf, .bv_len = sizeof(outbuf) };
786
787 if ( t + i <= last_step ) continue;
788
789 generate( secret, t + i, otp_len, &out, mech );
790 if ( !ber_bvcmp( &out, &client_otp ) ) {
791 found = t + i;
792 *drift = old_drift + i;
793 /* Would we leak information if we stopped right now? */
794 }
795 }
796
797 /* OTP check passed, trim the password */
798 if ( found >= 0 ) {
799 assert( found > last_step );
800
801 op->orb_cred.bv_len -= otp_len;
802 Debug( LDAP_DEBUG_TRACE, "%s TOTP token %s redeemed with new drift of %ld\n",
803 op->o_log_prefix, token->e_name.bv_val, *drift );
804 }
805
806 done:
807 memset( outbuf, 0, sizeof(outbuf) );
808 if ( params ) {
809 be_entry_release_r( op, params );
810 }
811 return found;
812 }
813
814 static int
815 otp_op_bind( Operation *op, SlapReply *rs )
816 {
817 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
818 BerValue totpdn = BER_BVNULL, hotpdn = BER_BVNULL, ndn;
819 Entry *user = NULL, *token = NULL;
820 AttributeDescription *ad = NULL, *drift_ad = NULL;
821 Attribute *a;
822 long t = -1, drift = 0;
823 int rc = SLAP_CB_CONTINUE;
824
825 if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
826 return rc;
827 }
828
829 op->o_bd->bd_info = (BackendInfo *)on->on_info;
830
831 if ( be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &user ) ) {
832 goto done;
833 }
834
835 if ( !is_entry_objectclass_or_sub( user, oc_oathOTPUser ) ) {
836 be_entry_release_r( op, user );
837 goto done;
838 }
839
840 if ( (a = attr_find( user->e_attrs, ad_oathTOTPToken )) ) {
841 ber_dupbv_x( &totpdn, &a->a_nvals[0], op->o_tmpmemctx );
842 }
843
844 if ( (a = attr_find( user->e_attrs, ad_oathHOTPToken )) ) {
845 ber_dupbv_x( &hotpdn, &a->a_nvals[0], op->o_tmpmemctx );
846 }
847 be_entry_release_r( op, user );
848
849 if ( !BER_BVISNULL( &totpdn ) &&
850 be_entry_get_rw( op, &totpdn, oc_oathTOTPToken, ad_oathSecret, 0,
851 &token ) == LDAP_SUCCESS ) {
852 ndn = totpdn;
853 ad = ad_oathTOTPLastTimeStep;
854 drift_ad = ad_oathTOTPTimeStepDrift;
855 t = otp_totp( op, token, &drift );
856 be_entry_release_r( op, token );
857 token = NULL;
858 }
859 if ( t < 0 && !BER_BVISNULL( &hotpdn ) &&
860 be_entry_get_rw( op, &hotpdn, oc_oathHOTPToken, ad_oathSecret, 0,
861 &token ) == LDAP_SUCCESS ) {
862 ndn = hotpdn;
863 ad = ad_oathHOTPCounter;
864 t = otp_hotp( op, token );
865 be_entry_release_r( op, token );
866 token = NULL;
867 }
868
869 /* If check succeeds, advance the step counter and drift accordingly */
870 if ( t >= 0 ) {
871 char outbuf[32], drift_buf[32];
872 Operation op2;
873 Opheader oh;
874 Modifications mod[2], *m = mod;
875 SlapReply rs2 = { REP_RESULT };
876 slap_callback cb = { .sc_response = &slap_null_cb };
877 BerValue bv[2], bv_drift[2];
878
879 bv[0].bv_val = outbuf;
880 bv[0].bv_len = snprintf( bv[0].bv_val, sizeof(outbuf), "%ld", t );
881 BER_BVZERO( &bv[1] );
882
883 m->sml_numvals = 1;
884 m->sml_values = bv;
885 m->sml_nvalues = NULL;
886 m->sml_desc = ad;
887 m->sml_op = LDAP_MOD_REPLACE;
888 m->sml_flags = SLAP_MOD_INTERNAL;
889
890 if ( drift_ad ) {
891 m->sml_next = &mod[1];
892
893 bv_drift[0].bv_val = drift_buf;
894 bv_drift[0].bv_len = snprintf(
895 bv_drift[0].bv_val, sizeof(drift_buf), "%ld", drift );
896 BER_BVZERO( &bv_drift[1] );
897
898 m++;
899 m->sml_numvals = 1;
900 m->sml_values = bv_drift;
901 m->sml_nvalues = NULL;
902 m->sml_desc = drift_ad;
903 m->sml_op = LDAP_MOD_REPLACE;
904 m->sml_flags = SLAP_MOD_INTERNAL;
905 }
906 m->sml_next = NULL;
907
908 op2 = *op;
909 oh = *op->o_hdr;
910 op2.o_hdr = &oh;
911
912 op2.o_callback = &cb;
913
914 op2.o_tag = LDAP_REQ_MODIFY;
915 op2.orm_modlist = mod;
916 op2.o_dn = op->o_bd->be_rootdn;
917 op2.o_ndn = op->o_bd->be_rootndn;
918 op2.o_req_dn = ndn;
919 op2.o_req_ndn = ndn;
920 op2.o_opid = -1;
921
922 op2.o_bd->be_modify( &op2, &rs2 );
923 if ( rs2.sr_err != LDAP_SUCCESS ) {
924 rc = LDAP_OTHER;
925 goto done;
926 }
927 } else {
928 /* Client failed the bind, but we still have to pass it over to the
929 * backend and fail the Bind later */
930 slap_callback *cb;
931 cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
932 cb->sc_response = otp_bind_response;
933 cb->sc_next = op->o_callback;
934 op->o_callback = cb;
935 }
936
937 done:
938 if ( !BER_BVISNULL( &hotpdn ) ) {
939 ber_memfree_x( hotpdn.bv_val, op->o_tmpmemctx );
940 }
941 if ( !BER_BVISNULL( &totpdn ) ) {
942 ber_memfree_x( totpdn.bv_val, op->o_tmpmemctx );
943 }
944 op->o_bd->bd_info = (BackendInfo *)on;
945 return rc;
946 }
947
948 static slap_overinst otp;
949
950 int
951 otp_initialize( void )
952 {
953 ConfigArgs ca;
954 char *argv[4];
955 int i;
956
957 otp.on_bi.bi_type = "otp";
958 otp.on_bi.bi_op_bind = otp_op_bind;
959
960 ca.argv = argv;
961 argv[0] = "otp";
962 ca.argv = argv;
963 ca.argc = 3;
964 ca.fname = argv[0];
965
966 argv[3] = NULL;
967 for ( i = 0; otp_oid[i].name; i++ ) {
968 argv[1] = otp_oid[i].name;
969 argv[2] = otp_oid[i].oid;
970 parse_oidm( &ca, 0, NULL );
971 }
972
973 /* schema integration */
974 for ( i = 0; otp_at[i].schema; i++ ) {
975 if ( register_at( otp_at[i].schema, otp_at[i].adp, 0 ) ) {
976 Debug( LDAP_DEBUG_ANY, "otp_initialize: "
977 "register_at failed\n" );
978 return -1;
979 }
980 }
981
982 for ( i = 0; otp_oc[i].schema; i++ ) {
983 if ( register_oc( otp_oc[i].schema, otp_oc[i].ocp, 0 ) ) {
984 Debug( LDAP_DEBUG_ANY, "otp_initialize: "
985 "register_oc failed\n" );
986 return -1;
987 }
988 }
989
990 #if OPENSSL_VERSION_MAJOR >= 3
991 evp_mac = EVP_MAC_fetch( NULL, "HMAC", "provider=default" );
992 #endif
993 return overlay_register( &otp );
994 }
995
996 #if SLAPD_OVER_OTP == SLAPD_MOD_DYNAMIC
997 int
998 init_module( int argc, char *argv[] )
999 {
1000 return otp_initialize();
1001 }
1002 #endif /* SLAPD_OVER_OTP == SLAPD_MOD_DYNAMIC */
1003
1004 #endif /* defined(SLAPD_OVER_OTP) */