]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/sha2/slapd-sha2.c
ITS#7269 SHA2: Avoid "static char real_hash[]" to support multithread
[openldap] / contrib / slapd-modules / passwd / sha2 / slapd-sha2.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2009-2012 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENT:
16  * This work was initially developed by Jeff Turner for inclusion
17  * in OpenLDAP Software.
18  *
19  * Hash methods for passwords generation added by Cédric Delfosse.
20  *
21  * chk_sha*() replaced with libraries/liblutil/passwd.c:chk_sha1()
22  * implementation to fix a race by SATOH Fumiyasu @ OSS Technology, Inc.
23  */
24
25 #include "portable.h"
26
27 #include <ac/string.h>
28
29 #include "lber_pvt.h"
30 #include "lutil.h"
31 #include "sha2.h"
32
33 #ifdef SLAPD_SHA2_DEBUG
34 #include <stdio.h>
35 #endif
36
37 static int hash_sha256(
38         const struct berval *scheme,
39         const struct berval *passwd,
40         struct berval *hash,
41         const char **text )
42 {
43         SHA256_CTX ct;
44         unsigned char hash256[SHA256_DIGEST_LENGTH];
45         struct berval digest;
46         digest.bv_val = (char *) hash256;
47         digest.bv_len = sizeof(hash256);
48
49         SHA256_Init(&ct);
50         SHA256_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
51         SHA256_Final(hash256, &ct);
52
53         return lutil_passwd_string64(scheme, &digest, hash, NULL);
54 }
55
56 static int hash_sha384(
57         const struct berval *scheme,
58         const struct berval *passwd,
59         struct berval *hash,
60         const char **text )
61 {
62         SHA384_CTX ct;
63         unsigned char hash384[SHA384_DIGEST_LENGTH];
64         struct berval digest;
65         digest.bv_val = (char *) hash384;
66         digest.bv_len = sizeof(hash384);
67
68 #ifdef SLAPD_SHA2_DEBUG
69         fprintf(stderr, "hashing password\n");
70 #endif
71         SHA384_Init(&ct);
72         SHA384_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
73         SHA384_Final(hash384, &ct);
74
75         return lutil_passwd_string64(scheme, &digest, hash, NULL);
76 }
77
78 static int hash_sha512(
79         const struct berval *scheme,
80         const struct berval *passwd,
81         struct berval *hash,
82         const char **text )
83 {
84         SHA512_CTX ct;
85         unsigned char hash512[SHA512_DIGEST_LENGTH];
86         struct berval digest;
87         digest.bv_val = (char *) hash512;
88         digest.bv_len = sizeof(hash512);
89
90         SHA512_Init(&ct);
91         SHA512_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
92         SHA512_Final(hash512, &ct);
93
94         return lutil_passwd_string64(scheme, &digest, hash, NULL);
95 }
96
97 #ifdef SLAPD_SHA2_DEBUG
98 static void chk_sha_debug(
99         const struct berval *scheme,
100         const struct berval *passwd,
101         const struct berval *cred,
102         const char *cred_hash,
103         size_t cred_len,
104         int cmp_rc)
105 {
106         int rc;
107         struct berval cred_b64;
108
109         cred_b64.bv_len = LUTIL_BASE64_ENCODE_LEN(cred_len) + 1;
110         cred_b64.bv_val = ber_memalloc(cred_b64.bv_len + 1);
111
112         if( cred_b64.bv_val == NULL ) {
113                 return;
114         }
115
116         rc = lutil_b64_ntop(
117                 (unsigned char *) cred_hash, cred_len,
118                 cred_b64.bv_val, cred_b64.bv_len );
119
120         if( rc < 0 ) {
121                 ber_memfree(cred_b64.bv_val);
122                 return;
123         }
124
125         fprintf(stderr, "Validating password\n");
126         fprintf(stderr, "  Hash scheme:\t\t%s\n", scheme->bv_val);
127         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
128         fprintf(stderr, "  Password hash:\t%s\n", cred_b64.bv_val);
129         fprintf(stderr, "  Stored password hash:\t%s\n", passwd->bv_val);
130         fprintf(stderr, "  Result:\t\t%s\n", cmp_rc ?  "do not match" : "match");
131
132         ber_memfree(cred_b64.bv_val);
133 }
134 #endif
135
136 static int chk_sha256(
137         const struct berval *scheme, /* Scheme of hashed reference password */
138         const struct berval *passwd, /* Hashed reference password to check against */
139         const struct berval *cred, /* user-supplied password to check */
140         const char **text )
141 {
142         SHA256_CTX SHAcontext;
143         unsigned char SHAdigest[SHA256_DIGEST_LENGTH];
144         int rc;
145         unsigned char *orig_pass = NULL;
146
147         /* safety check */
148         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHAdigest)) {
149                 return LUTIL_PASSWD_ERR;
150         }
151
152         /* base64 un-encode password */
153         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
154                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
155
156         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
157
158         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
159
160         if( rc != sizeof(SHAdigest) ) {
161                 ber_memfree(orig_pass);
162                 return LUTIL_PASSWD_ERR;
163         }
164
165         /* hash credentials with salt */
166         SHA256_Init(&SHAcontext);
167         SHA256_Update(&SHAcontext,
168                 (const unsigned char *) cred->bv_val, cred->bv_len);
169         SHA256_Final(SHAdigest, &SHAcontext);
170
171         /* compare */
172         rc = memcmp((char *)orig_pass, (char *)SHAdigest, sizeof(SHAdigest));
173 #ifdef SLAPD_SHA2_DEBUG
174         chk_sha_debug(scheme, passwd, cred, (char *)SHAdigest, sizeof(SHAdigest), rc);
175 #endif
176         ber_memfree(orig_pass);
177         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
178 }
179
180 static int chk_sha384(
181         const struct berval *scheme, /* Scheme of hashed reference password */
182         const struct berval *passwd, /* Hashed reference password to check against */
183         const struct berval *cred, /* user-supplied password to check */
184         const char **text )
185 {
186         SHA384_CTX SHAcontext;
187         unsigned char SHAdigest[SHA384_DIGEST_LENGTH];
188         int rc;
189         unsigned char *orig_pass = NULL;
190
191         /* safety check */
192         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHAdigest)) {
193                 return LUTIL_PASSWD_ERR;
194         }
195
196         /* base64 un-encode password */
197         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
198                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
199
200         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
201
202         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
203
204         if( rc != sizeof(SHAdigest) ) {
205                 ber_memfree(orig_pass);
206                 return LUTIL_PASSWD_ERR;
207         }
208
209         /* hash credentials with salt */
210         SHA384_Init(&SHAcontext);
211         SHA384_Update(&SHAcontext,
212                 (const unsigned char *) cred->bv_val, cred->bv_len);
213         SHA384_Final(SHAdigest, &SHAcontext);
214
215         /* compare */
216         rc = memcmp((char *)orig_pass, (char *)SHAdigest, sizeof(SHAdigest));
217 #ifdef SLAPD_SHA2_DEBUG
218         chk_sha_debug(scheme, passwd, cred, (char *)SHAdigest, sizeof(SHAdigest), rc);
219 #endif
220         ber_memfree(orig_pass);
221         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
222 }
223
224 static int chk_sha512(
225         const struct berval *scheme, /* Scheme of hashed reference password */
226         const struct berval *passwd, /* Hashed reference password to check against */
227         const struct berval *cred, /* user-supplied password to check */
228         const char **text )
229 {
230         SHA512_CTX SHAcontext;
231         unsigned char SHAdigest[SHA512_DIGEST_LENGTH];
232         int rc;
233         unsigned char *orig_pass = NULL;
234
235         /* safety check */
236         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHAdigest)) {
237                 return LUTIL_PASSWD_ERR;
238         }
239
240         /* base64 un-encode password */
241         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
242                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
243
244         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
245
246         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
247
248         if( rc != sizeof(SHAdigest) ) {
249                 ber_memfree(orig_pass);
250                 return LUTIL_PASSWD_ERR;
251         }
252
253         /* hash credentials with salt */
254         SHA512_Init(&SHAcontext);
255         SHA512_Update(&SHAcontext,
256                 (const unsigned char *) cred->bv_val, cred->bv_len);
257         SHA512_Final(SHAdigest, &SHAcontext);
258
259         /* compare */
260         rc = memcmp((char *)orig_pass, (char *)SHAdigest, sizeof(SHAdigest));
261 #ifdef SLAPD_SHA2_DEBUG
262         chk_sha_debug(scheme, passwd, cred, (char *)SHAdigest, sizeof(SHAdigest), rc);
263 #endif
264         ber_memfree(orig_pass);
265         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
266 }
267
268 const struct berval sha256scheme = BER_BVC("{SHA256}");
269 const struct berval sha384scheme = BER_BVC("{SHA384}");
270 const struct berval sha512scheme = BER_BVC("{SHA512}");
271
272 int init_module(int argc, char *argv[]) {
273         int result = 0;
274         result = lutil_passwd_add( (struct berval *)&sha256scheme, chk_sha256, hash_sha256 );
275         if (result != 0) return result;
276         result = lutil_passwd_add( (struct berval *)&sha384scheme, chk_sha384, hash_sha384 );
277         if (result != 0) return result;
278         result = lutil_passwd_add( (struct berval *)&sha512scheme, chk_sha512, hash_sha512 );
279         return result;
280 }