-
Notifications
You must be signed in to change notification settings - Fork 7
/
pw-pbkdf2.c
725 lines (674 loc) · 21.4 KB
/
pw-pbkdf2.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2009-2015 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENT:
* This work was initially developed by HAMANO Tsukasa <[email protected]>
*/
#define _GNU_SOURCE
#include "portable.h"
#include <slap.h>
#include <ac/string.h>
#include "lber_pvt.h"
#include "lutil.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_OPENSSL
#define PBKDF2_LIB "OpenSSL"
#include <openssl/evp.h>
#elif HAVE_GNUTLS
#define PBKDF2_LIB "Nettle"
#include <nettle/pbkdf2.h>
#include <nettle/hmac.h>
typedef void (*pbkdf2_hmac_update)(void *, unsigned, const uint8_t *);
typedef void (*pbkdf2_hmac_digest)(void *, unsigned, uint8_t *);
#elif HAVE_MOZNSS
#define PBKDF2_LIB "MOZNSS"
#include <nspr/plarena.h>
#include </usr/include/nspr4/prtypes.h>
#include <nss/pk11pub.h>
#else
#error Unsupported crypto backend.
#endif
static int pbkdf2_iteration = 10000;
#define PBKDF2_SALT_SIZE 16
#define PBKDF2_SHA1_DK_SIZE 20
#define PBKDF2_SHA256_DK_SIZE 32
#define PBKDF2_SHA512_DK_SIZE 64
#define PBKDF2_MAX_DK_SIZE 64
const struct berval pbkdf2_scheme = BER_BVC("{PBKDF2}");
const struct berval pbkdf2_sha1_scheme = BER_BVC("{PBKDF2-SHA1}");
const struct berval pbkdf2_sha256_scheme = BER_BVC("{PBKDF2-SHA256}");
const struct berval pbkdf2_sha512_scheme = BER_BVC("{PBKDF2-SHA512}");
/*
* Converting base64 string to adapted base64 string.
* Adapted base64 encode is identical to general base64 encode except
* that it uses '.' instead of '+', and omits trailing padding '=' and
* whitepsace.
* see http://pythonhosted.org/passlib/lib/passlib.utils.html
* This is destructive function.
*/
static int b64_to_ab64(char *str)
{
char *p = str;
do {
if(*p == '+'){
*p = '.';
}
if(*p == '='){
*p = '\0';
}
} while(*p++);
return 0;
}
/*
* Converting adapted base64 string to base64 string.
* dstsize will require src length + 2, due to output string have
* potential to append "=" or "==".
* return -1 if few output buffer.
*/
static int ab64_to_b64(char *src, char *dst, size_t dstsize){
int i;
char *p = src;
for(i=0; p[i] && p[i] != '$'; i++){
if(i >= dstsize){
dst[0] = '\0';
return -1;
}
if(p[i] == '.'){
dst[i] = '+';
}else{
dst[i] = p[i];
}
}
for(;i%4;i++){
if(i >= dstsize){
dst[0] = '\0';
return -1;
}
dst[i] = '=';
}
dst[i] = '\0';
return 0;
}
static int pbkdf2_format(
const struct berval *sc,
int iteration,
const struct berval *salt,
const struct berval *dk,
struct berval *msg)
{
int rc, msg_len;
char salt_b64[LUTIL_BASE64_ENCODE_LEN(PBKDF2_SALT_SIZE) + 1];
char dk_b64[LUTIL_BASE64_ENCODE_LEN(PBKDF2_MAX_DK_SIZE) + 1];
rc = lutil_b64_ntop((unsigned char *)salt->bv_val, salt->bv_len,
salt_b64, sizeof(salt_b64));
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
b64_to_ab64(salt_b64);
rc = lutil_b64_ntop((unsigned char *)dk->bv_val, dk->bv_len,
dk_b64, sizeof(dk_b64));
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
b64_to_ab64(dk_b64);
msg_len = asprintf(&msg->bv_val, "%s%d$%s$%s",
sc->bv_val, iteration,
salt_b64, dk_b64);
if(msg_len < 0){
msg->bv_len = 0;
return LUTIL_PASSWD_ERR;
}
msg->bv_len = msg_len;
return LUTIL_PASSWD_OK;
}
static int pbkdf2_encrypt(
const struct berval *scheme,
const struct berval *passwd,
struct berval *msg,
const char **text)
{
unsigned char salt_value[PBKDF2_SALT_SIZE];
struct berval salt;
unsigned char dk_value[PBKDF2_MAX_DK_SIZE];
struct berval dk;
int iteration = pbkdf2_iteration;
int rc;
#ifdef HAVE_OPENSSL
const EVP_MD *md;
#elif HAVE_GNUTLS
struct hmac_sha1_ctx sha1_ctx;
struct hmac_sha256_ctx sha256_ctx;
struct hmac_sha512_ctx sha512_ctx;
void * current_ctx = NULL;
pbkdf2_hmac_update current_hmac_update = NULL;
pbkdf2_hmac_digest current_hmac_digest = NULL;
#endif
salt.bv_val = (char *)salt_value;
salt.bv_len = sizeof(salt_value);
dk.bv_val = (char *)dk_value;
#ifdef HAVE_OPENSSL
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk.bv_len = PBKDF2_SHA1_DK_SIZE;
md = EVP_sha1();
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk.bv_len = PBKDF2_SHA256_DK_SIZE;
md = EVP_sha256();
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk.bv_len = PBKDF2_SHA512_DK_SIZE;
md = EVP_sha512();
}else{
return LUTIL_PASSWD_ERR;
}
#elif HAVE_GNUTLS
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk.bv_len = PBKDF2_SHA1_DK_SIZE;
current_ctx = &sha1_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha1_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha1_digest;
hmac_sha1_set_key(current_ctx, passwd->bv_len, (const uint8_t *) passwd->bv_val);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk.bv_len = PBKDF2_SHA256_DK_SIZE;
current_ctx = &sha256_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha256_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha256_digest;
hmac_sha256_set_key(current_ctx, passwd->bv_len, (const uint8_t *) passwd->bv_val);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk.bv_len = PBKDF2_SHA512_DK_SIZE;
current_ctx = &sha512_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha512_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha512_digest;
hmac_sha512_set_key(current_ctx, passwd->bv_len, (const uint8_t *) passwd->bv_val);
}else{
return LUTIL_PASSWD_ERR;
}
#elif HAVE_MOZNSS
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk.bv_len = PBKDF2_SHA1_DK_SIZE;
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk.bv_len = PBKDF2_SHA256_DK_SIZE;
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk.bv_len = PBKDF2_SHA512_DK_SIZE;
}else{
return LUTIL_PASSWD_ERR;
}
#endif
if(lutil_entropy((unsigned char *)salt.bv_val, salt.bv_len) < 0){
return LUTIL_PASSWD_ERR;
}
#ifdef HAVE_OPENSSL
if(!PKCS5_PBKDF2_HMAC(passwd->bv_val, passwd->bv_len,
(unsigned char *)salt.bv_val, salt.bv_len,
iteration, md, dk.bv_len, dk_value)){
return LUTIL_PASSWD_ERR;
}
#elif HAVE_GNUTLS
PBKDF2(current_ctx, current_hmac_update, current_hmac_digest,
dk.bv_len, iteration,
salt.bv_len, (const uint8_t *) salt.bv_val,
dk.bv_len, dk_value);
#elif HAVE_MOZNSS
PK11SlotInfo *slot = NULL;
SECAlgorithmID * algid = NULL;
PRArenaPool *salt_arena;
PRArenaPool *pw_arena;
PRArenaPool *symkey_arena;
salt_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (salt_arena == NULL)
{
fprintf(stderr,"error initializing salt_arena");
return LUTIL_PASSWD_ERR;
}
pw_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (pw_arena == NULL)
{
fprintf(stderr,"error initializing pw_arena");
return LUTIL_PASSWD_ERR;
}
symkey_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (symkey_arena == NULL)
{
fprintf(stderr,"error initializing symkey_arena");
return LUTIL_PASSWD_ERR;
}
SECItem salt_moz={ siBuffer, NULL, 0};
SECItem pwitem={ siBuffer, NULL, 0};
slot=PK11_GetBestSlot(SEC_OID_PKCS5_PBKDF2, NULL);
if (slot == NULL)
{
fprintf(stderr,"error initializing slot");
return LUTIL_PASSWD_ERR;
}
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)passwd->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, passwd->bv_val, passwd->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA1, SEC_OID_HMAC_SHA1, PBKDF2_SHA1_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA1_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
dk.bv_val=memcpy(dk.bv_val, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)passwd->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, passwd->bv_val, passwd->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA256, SEC_OID_HMAC_SHA256, PBKDF2_SHA256_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA256_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
dk.bv_val=memcpy(dk.bv_val, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)passwd->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, passwd->bv_val, passwd->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA512, SEC_OID_HMAC_SHA512, PBKDF2_SHA512_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA512_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
dk.bv_val=memcpy(dk.bv_val, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else{
return LUTIL_PASSWD_ERR;
}
PK11_FreeSlot(slot);
PORT_FreeArena(salt_arena, PR_FALSE);
PORT_FreeArena(pw_arena, PR_FALSE);
PORT_FreeArena(symkey_arena, PR_FALSE);
#endif
#ifdef SLAPD_PBKDF2_DEBUG
printf("Encrypt for %s\n", scheme->bv_val);
printf(" Library:\t%s\n", PBKDF2_LIB);
printf(" Password:\t%s\n", passwd->bv_val);
printf(" Salt:\t\t");
int i;
for(i=0; i<salt.bv_len; i++){
printf("%02x", salt_value[i]);
}
printf("\n");
printf(" Iteration:\t%d\n", iteration);
printf(" DK:\t\t");
for(i=0; i<dk.bv_len; i++){
printf("%02x", dk_value[i]);
}
printf("\n");
#endif
rc = pbkdf2_format(scheme, iteration, &salt, &dk, msg);
#ifdef SLAPD_PBKDF2_DEBUG
printf(" Output:\t%s\n", msg->bv_val);
#endif
return rc;
}
static int pbkdf2_check(
const struct berval *scheme,
const struct berval *passwd,
const struct berval *cred,
const char **text)
{
int rc;
int iteration;
/* salt_value require PBKDF2_SALT_SIZE + 1 in lutil_b64_pton. */
unsigned char salt_value[PBKDF2_SALT_SIZE + 1];
char salt_b64[LUTIL_BASE64_ENCODE_LEN(PBKDF2_SALT_SIZE) + 1];
/* dk_value require PBKDF2_MAX_DK_SIZE + 1 in lutil_b64_pton. */
unsigned char dk_value[PBKDF2_MAX_DK_SIZE + 1];
char dk_b64[LUTIL_BASE64_ENCODE_LEN(PBKDF2_MAX_DK_SIZE) + 1];
unsigned char input_dk_value[PBKDF2_MAX_DK_SIZE];
size_t dk_len;
#ifdef HAVE_OPENSSL
const EVP_MD *md;
#elif HAVE_GNUTLS
struct hmac_sha1_ctx sha1_ctx;
struct hmac_sha256_ctx sha256_ctx;
struct hmac_sha512_ctx sha512_ctx;
void * current_ctx = NULL;
pbkdf2_hmac_update current_hmac_update = NULL;
pbkdf2_hmac_digest current_hmac_digest = NULL;
#endif
#ifdef SLAPD_PBKDF2_DEBUG
printf("Checking for %s\n", scheme->bv_val);
printf(" Stored Value:\t%s\n", passwd->bv_val);
printf(" Input Cred:\t%s\n", cred->bv_val);
#endif
#ifdef HAVE_OPENSSL
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk_len = PBKDF2_SHA1_DK_SIZE;
md = EVP_sha1();
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk_len = PBKDF2_SHA256_DK_SIZE;
md = EVP_sha256();
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk_len = PBKDF2_SHA512_DK_SIZE;
md = EVP_sha512();
}else{
return LUTIL_PASSWD_ERR;
}
#elif HAVE_GNUTLS
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk_len = PBKDF2_SHA1_DK_SIZE;
current_ctx = &sha1_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha1_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha1_digest;
hmac_sha1_set_key(current_ctx, cred->bv_len, (const uint8_t *) cred->bv_val);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk_len = PBKDF2_SHA256_DK_SIZE;
current_ctx = &sha256_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha256_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha256_digest;
hmac_sha256_set_key(current_ctx, cred->bv_len, (const uint8_t *) cred->bv_val);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk_len = PBKDF2_SHA512_DK_SIZE;
current_ctx = &sha512_ctx;
current_hmac_update = (pbkdf2_hmac_update) &hmac_sha512_update;
current_hmac_digest = (pbkdf2_hmac_digest) &hmac_sha512_digest;
hmac_sha512_set_key(current_ctx, cred->bv_len, (const uint8_t *) cred->bv_val);
}else{
return LUTIL_PASSWD_ERR;
}
#elif HAVE_MOZNSS
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
dk_len = PBKDF2_SHA1_DK_SIZE;
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
dk_len = PBKDF2_SHA256_DK_SIZE;
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
dk_len = PBKDF2_SHA512_DK_SIZE;
}else{
return LUTIL_PASSWD_ERR;
}
#endif
iteration = atoi(passwd->bv_val);
if(iteration < 1){
return LUTIL_PASSWD_ERR;
}
char *ptr;
ptr = strchr(passwd->bv_val, '$');
if(!ptr){
return LUTIL_PASSWD_ERR;
}
ptr++; /* skip '$' */
rc = ab64_to_b64(ptr, salt_b64, sizeof(salt_b64));
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
ptr = strchr(ptr, '$');
if(!ptr){
return LUTIL_PASSWD_ERR;
}
ptr++; /* skip '$' */
rc = ab64_to_b64(ptr, dk_b64, sizeof(dk_b64));
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
/* The targetsize require PBKDF2_SALT_SIZE + 1 in lutil_b64_pton. */
rc = lutil_b64_pton(salt_b64, salt_value, PBKDF2_SALT_SIZE + 1);
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
/* consistency check */
if(rc != PBKDF2_SALT_SIZE){
return LUTIL_PASSWD_ERR;
}
/* The targetsize require PBKDF2_MAX_DK_SIZE + 1 in lutil_b64_pton. */
rc = lutil_b64_pton(dk_b64, dk_value, sizeof(dk_value));
if(rc < 0){
return LUTIL_PASSWD_ERR;
}
/* consistency check */
if(rc != dk_len){
return LUTIL_PASSWD_ERR;
}
#ifdef HAVE_OPENSSL
if(!PKCS5_PBKDF2_HMAC(cred->bv_val, cred->bv_len,
salt_value, PBKDF2_SALT_SIZE,
iteration, md, dk_len, input_dk_value)){
return LUTIL_PASSWD_ERR;
}
#elif HAVE_GNUTLS
PBKDF2(current_ctx, current_hmac_update, current_hmac_digest,
dk_len, iteration,
PBKDF2_SALT_SIZE, salt_value,
dk_len, input_dk_value);
#elif HAVE_MOZNSS
PK11SlotInfo *slot = NULL;
SECAlgorithmID * algid = NULL;
PRArenaPool *salt_arena;
PRArenaPool *pw_arena;
PRArenaPool *symkey_arena;
salt_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (salt_arena == NULL)
{
fprintf(stderr,"error initializing salt_arena");
return LUTIL_PASSWD_ERR;
}
pw_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (pw_arena == NULL)
{
fprintf(stderr,"error initializing pw_arena");
return LUTIL_PASSWD_ERR;
}
symkey_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (symkey_arena == NULL)
{
fprintf(stderr,"error initializing symkey_arena");
return LUTIL_PASSWD_ERR;
}
SECItem salt_moz={ siBuffer, NULL, 0};
SECItem pwitem={ siBuffer, NULL, 0};
slot=PK11_GetBestSlot(SEC_OID_PKCS5_PBKDF2, NULL);
if (slot == NULL)
{
fprintf(stderr,"error initializing slot");
return LUTIL_PASSWD_ERR;
}
if(!ber_bvcmp(scheme, &pbkdf2_scheme) || !ber_bvcmp(scheme, &pbkdf2_sha1_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)cred->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, cred->bv_val, cred->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA1, SEC_OID_HMAC_SHA1,PBKDF2_SHA1_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA1_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
memcpy(input_dk_value, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha256_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)cred->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, cred->bv_val, cred->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA256, SEC_OID_HMAC_SHA256, PBKDF2_SHA256_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA256_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
memcpy(input_dk_value, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else if(!ber_bvcmp(scheme, &pbkdf2_sha512_scheme)){
if (SECITEM_AllocItem(salt_arena, &salt_moz, (unsigned int) PBKDF2_SALT_SIZE) == NULL)
{
fprintf(stderr,"error initializing salt_moz");
return LUTIL_PASSWD_ERR;
}
salt_moz.data = memcpy(salt_moz.data , salt_value, PBKDF2_SALT_SIZE);
if (SECITEM_AllocItem(pw_arena, &pwitem, (unsigned int)cred->bv_len) == NULL)
{
fprintf(stderr,"error initializing pw_item");
return LUTIL_PASSWD_ERR;
}
pwitem.data= memcpy(pwitem.data, cred->bv_val, cred->bv_len );
algid=PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA512, SEC_OID_HMAC_SHA512, PBKDF2_SHA512_DK_SIZE, iteration, &salt_moz);
PK11SymKey *key = NULL;
key=PK11_PBEKeyGen(slot, algid, &pwitem, 0, NULL);
SECStatus rv2 = PK11_ExtractKeyValue(key);
SECItem *symkey;
if (SECITEM_AllocItem(symkey_arena, symkey, PBKDF2_SHA512_DK_SIZE) == NULL)
{
fprintf(stderr,"error initializing symkey");
return LUTIL_PASSWD_ERR;
}
symkey=PK11_GetKeyData(key);
memcpy(input_dk_value, symkey->data, symkey->len);
PK11_FreeSymKey(key);
}else{
return LUTIL_PASSWD_ERR;
}
PK11_FreeSlot(slot);
PORT_FreeArena(salt_arena, PR_FALSE);
PORT_FreeArena(pw_arena, PR_FALSE);
PORT_FreeArena(symkey_arena, PR_FALSE);
#endif
rc = memcmp(dk_value, input_dk_value, dk_len);
#ifdef SLAPD_PBKDF2_DEBUG
printf(" Iteration:\t%d\n", iteration);
printf(" Base64 Salt:\t%s\n", salt_b64);
printf(" Base64 DK:\t%s\n", dk_b64);
printf(" Stored Salt:\t");
int i;
for(i=0; i<PBKDF2_SALT_SIZE; i++){
printf("%02x", salt_value[i]);
}
printf("\n");
printf(" Stored DK:\t");
for(i=0; i<dk_len; i++){
printf("%02x", dk_value[i]);
}
printf("\n");
printf(" Input DK:\t");
for(i=0; i<dk_len; i++){
printf("%02x", input_dk_value[i]);
}
printf("\n");
printf(" Result:\t%d\n", rc);
#endif
return rc?LUTIL_PASSWD_ERR:LUTIL_PASSWD_OK;
}
int init_module(int argc, char *argv[]) {
int rc;
char *env = getenv("PBKDF2_ITERATION");
#if HAVE_MOZNSS
SECStatus rv;
rv = NSS_NoDB_Init(".");
if (rv != SECSuccess)
{
fprintf(stderr, "NSS initialization failed (err %d)\n",
PR_GetError());
return LUTIL_PASSWD_ERR;
}
#endif
if(env){
pbkdf2_iteration = atoi(env);
if(pbkdf2_iteration < 1){
Debug( LDAP_DEBUG_ANY, "pbkdf2_iteration must be greater than 1.",
NULL, NULL, NULL);
return -1;
}
}
rc = lutil_passwd_add((struct berval *)&pbkdf2_scheme,
pbkdf2_check, pbkdf2_encrypt);
if(rc) return rc;
rc = lutil_passwd_add((struct berval *)&pbkdf2_sha1_scheme,
pbkdf2_check, pbkdf2_encrypt);
if(rc) return rc;
rc = lutil_passwd_add((struct berval *)&pbkdf2_sha256_scheme,
pbkdf2_check, pbkdf2_encrypt);
if(rc) return rc;
rc = lutil_passwd_add((struct berval *)&pbkdf2_sha512_scheme,
pbkdf2_check, pbkdf2_encrypt);
return rc;
}
/*
* Local variables:
* indent-tabs-mode: t
* tab-width: 4
* c-basic-offset: 4
* End:
*/