]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/sha2/slapd-sha2.c
SHA2: Make slapd-sha2 module portable
[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         struct berval digest;
108         digest.bv_val = (char *) hash256;
109         digest.bv_len = sizeof(hash256);
110
111         SHA256_Init(&ct);
112         SHA256_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
113         SHA256_Final(hash256, &ct);
114
115         return lutil_passwd_string64(scheme, &digest, hash, NULL);
116 }
117
118 static int hash_sha384(
119         const struct berval *scheme,
120         const struct berval *passwd,
121         struct berval *hash,
122         const char **text )
123 {
124         SHA384_CTX ct;
125         unsigned char hash384[SHA384_DIGEST_LENGTH];
126         struct berval digest;
127         digest.bv_val = (char *) hash384;
128         digest.bv_len = sizeof(hash384);
129
130 #ifdef SLAPD_SHA2_DEBUG
131         fprintf(stderr, "hashing password\n");
132 #endif
133         SHA384_Init(&ct);
134         SHA384_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
135         SHA384_Final(hash384, &ct);
136
137         return lutil_passwd_string64(scheme, &digest, hash, NULL);
138 }
139
140 static int hash_sha512(
141         const struct berval *scheme,
142         const struct berval *passwd,
143         struct berval *hash,
144         const char **text )
145 {
146         SHA512_CTX ct;
147         unsigned char hash512[SHA512_DIGEST_LENGTH];
148         struct berval digest;
149         digest.bv_val = (char *) hash512;
150         digest.bv_len = sizeof(hash512);
151
152         SHA512_Init(&ct);
153         SHA512_Update(&ct, (const uint8_t*)passwd->bv_val, passwd->bv_len);
154         SHA512_Final(hash512, &ct);
155
156         return lutil_passwd_string64(scheme, &digest, hash, NULL);
157 }
158
159 static int chk_sha256(
160         const struct berval *scheme, /* Scheme of hashed reference password */
161         const struct berval *passwd, /* Hashed reference password to check against */
162         const struct berval *cred, /* user-supplied password to check */
163         const char **text )
164 {
165 #ifdef SLAPD_SHA2_DEBUG
166         fprintf(stderr, "Validating password\n");
167         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
168         fprintf(stderr, "  Hashes to: %s\n", sha256_hex_hash(cred->bv_val));
169         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
170         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
171         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
172 #endif
173         return (strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val));
174 }
175
176 static int chk_sha384(
177         const struct berval *scheme, /* Scheme of hashed reference password */
178         const struct berval *passwd, /* Hashed reference password to check against */
179         const struct berval *cred, /* user-supplied password to check */
180         const char **text )
181 {
182 #ifdef SLAPD_SHA2_DEBUG
183         fprintf(stderr, "Validating password\n");
184         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
185         fprintf(stderr, "  Hashes to: %s\n", sha384_hex_hash(cred->bv_val));
186         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
187         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
188         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
189 #endif
190         return (strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val));
191 }
192
193 static int chk_sha512(
194         const struct berval *scheme, /* Scheme of hashed reference password */
195         const struct berval *passwd, /* Hashed reference password to check against */
196         const struct berval *cred, /* user-supplied password to check */
197         const char **text )
198 {
199 #ifdef SLAPD_SHA2_DEBUG
200         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
201         fprintf(stderr, "  Hashes to: %s\n", sha512_hex_hash(cred->bv_val));
202         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
203         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
204         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
205 #endif
206         return (strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val));
207 }
208
209 const struct berval sha256scheme = BER_BVC("{SHA256}");
210 const struct berval sha384scheme = BER_BVC("{SHA384}");
211 const struct berval sha512scheme = BER_BVC("{SHA512}");
212
213 int init_module(int argc, char *argv[]) {
214         int result = 0;
215         result = lutil_passwd_add( (struct berval *)&sha256scheme, chk_sha256, hash_sha256 );
216         if (result != 0) return result;
217         result = lutil_passwd_add( (struct berval *)&sha384scheme, chk_sha384, hash_sha384 );
218         if (result != 0) return result;
219         result = lutil_passwd_add( (struct berval *)&sha512scheme, chk_sha512, hash_sha512 );
220         return result;
221 }