]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/sha2/slapd-sha2.c
Merge remote branch 'origin/mdb.master'
[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
22 #include <lber.h>
23 #include <lber_pvt.h>
24 #include <ac/string.h>
25 #include "lutil.h"
26 #include <stdint.h>
27 #include <string.h>
28 #include <assert.h>
29 #include "sha2.h"
30
31 #ifdef SLAPD_SHA2_DEBUG
32 #include <stdio.h>
33 #endif
34
35 char * sha256_hex_hash(const char * passwd) {
36
37         SHA256_CTX ct;
38         unsigned char hash[SHA256_DIGEST_LENGTH];
39         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA256_DIGEST_LENGTH)+1]; // extra char for \0
40
41         SHA256_Init(&ct);
42         SHA256_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
43         SHA256_Final(hash, &ct);
44
45         /* base64 encode it */
46         lutil_b64_ntop(
47                         hash,
48                         SHA256_DIGEST_LENGTH,
49                         real_hash,
50                         LUTIL_BASE64_ENCODE_LEN(SHA256_DIGEST_LENGTH)+1
51                         );
52
53         return real_hash;
54 }
55
56
57 char * sha384_hex_hash(const char * passwd) {
58
59         SHA384_CTX ct;
60         unsigned char hash[SHA384_DIGEST_LENGTH];
61         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA384_DIGEST_LENGTH)+1]; // extra char for \0
62
63         SHA384_Init(&ct);
64         SHA384_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
65         SHA384_Final(hash, &ct);
66
67         /* base64 encode it */
68         lutil_b64_ntop(
69                         hash,
70                         SHA384_DIGEST_LENGTH,
71                         real_hash,
72                         LUTIL_BASE64_ENCODE_LEN(SHA384_DIGEST_LENGTH)+1
73                         );
74
75         return real_hash;
76 }
77
78 char * sha512_hex_hash(const char * passwd) {
79
80         SHA512_CTX ct;
81         unsigned char hash[SHA512_DIGEST_LENGTH];
82         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1]; // extra char for \0
83
84         SHA512_Init(&ct);
85         SHA512_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
86         SHA512_Final(hash, &ct);
87
88         /* base64 encode it */
89         lutil_b64_ntop(
90                         hash,
91                         SHA512_DIGEST_LENGTH,
92                         real_hash,
93                         LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1
94                         );
95
96         return real_hash;
97 }
98
99 static int hash_sha256(
100         const struct berval *scheme,
101         const struct berval *passwd,
102         struct berval *hash,
103         const char **text )
104 {
105         SHA256_CTX ct;
106         unsigned char hash256[SHA256_DIGEST_LENGTH];
107
108         SHA256_Init(&ct);
109         SHA256_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
110         SHA256_Final(hash256, &ct);
111
112         struct berval digest;
113         digest.bv_val = (char *) hash256;
114         digest.bv_len = sizeof(hash256);
115
116         return lutil_passwd_string64(scheme, &digest, hash, NULL);
117 }
118
119 static int hash_sha384(
120         const struct berval *scheme,
121         const struct berval *passwd,
122         struct berval *hash,
123         const char **text )
124 {
125         SHA384_CTX ct;
126         unsigned char hash384[SHA384_DIGEST_LENGTH];
127
128 #ifdef SLAPD_SHA2_DEBUG
129         fprintf(stderr, "hashing password\n");
130 #endif
131         SHA384_Init(&ct);
132         SHA384_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
133         SHA384_Final(hash384, &ct);
134
135         struct berval digest;
136         digest.bv_val = (char *) hash384;
137         digest.bv_len = sizeof(hash384);
138
139         return lutil_passwd_string64(scheme, &digest, hash, NULL);
140 }
141
142 static int hash_sha512(
143         const struct berval *scheme,
144         const struct berval *passwd,
145         struct berval *hash,
146         const char **text )
147 {
148         SHA512_CTX ct;
149         unsigned char hash512[SHA512_DIGEST_LENGTH];
150
151         SHA512_Init(&ct);
152         SHA512_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
153         SHA512_Final(hash512, &ct);
154
155         struct berval digest;
156         digest.bv_val = (char *) hash512;
157         digest.bv_len = sizeof(hash512);
158
159         return lutil_passwd_string64(scheme, &digest, hash, NULL);
160 }
161
162 static int chk_sha256(
163         const struct berval *scheme, // Scheme of hashed reference password
164         const struct berval *passwd, // Hashed reference password to check against
165         const struct berval *cred, // user-supplied password to check
166         const char **text )
167 {
168 #ifdef SLAPD_SHA2_DEBUG
169         fprintf(stderr, "Validating password\n");
170         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
171         fprintf(stderr, "  Hashes to: %s\n", sha256_hex_hash(cred->bv_val));
172         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
173         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
174         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
175 #endif
176         return (strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val));
177 }
178
179 static int chk_sha384(
180         const struct berval *scheme, // Scheme of hashed reference password
181         const struct berval *passwd, // Hashed reference password to check against
182         const struct berval *cred, // user-supplied password to check
183         const char **text )
184 {
185 #ifdef SLAPD_SHA2_DEBUG
186         fprintf(stderr, "Validating password\n");
187         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
188         fprintf(stderr, "  Hashes to: %s\n", sha384_hex_hash(cred->bv_val));
189         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
190         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
191         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
192 #endif
193         return (strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val));
194 }
195
196 static int chk_sha512(
197         const struct berval *scheme, // Scheme of hashed reference password
198         const struct berval *passwd, // Hashed reference password to check against
199         const struct berval *cred, // user-supplied password to check
200         const char **text )
201 {
202 #ifdef SLAPD_SHA2_DEBUG
203         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
204         fprintf(stderr, "  Hashes to: %s\n", sha512_hex_hash(cred->bv_val));
205         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
206         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
207         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
208 #endif
209         return (strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val));
210 }
211
212 const struct berval sha256scheme = BER_BVC("{SHA256}");
213 const struct berval sha384scheme = BER_BVC("{SHA384}");
214 const struct berval sha512scheme = BER_BVC("{SHA512}");
215
216 int init_module(int argc, char *argv[]) {
217         int result = 0;
218         result = lutil_passwd_add( (struct berval *)&sha256scheme, chk_sha256, hash_sha256 );
219         if (result != 0) return result;
220         result = lutil_passwd_add( (struct berval *)&sha384scheme, chk_sha384, hash_sha384 );
221         if (result != 0) return result;
222         result = lutil_passwd_add( (struct berval *)&sha512scheme, chk_sha512, hash_sha512 );
223         return result;
224 }