]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/sha2/slapd-sha2.c
Cleanup
[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 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
20 #include <lber.h>
21 #include <lber_pvt.h>
22 #include <ac/string.h>
23 #include "lutil.h"
24 #include <stdint.h>
25 #include <string.h>
26 #include <assert.h>
27 #include "sha2.h"
28
29 #ifdef SLAPD_SHA2_DEBUG
30 #include <stdio.h>
31 #endif
32
33 char * sha256_hex_hash(const char * passwd) {
34
35         SHA256_CTX ct;
36         unsigned char hash[SHA256_DIGEST_LENGTH];
37         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA256_DIGEST_LENGTH)+1]; // extra char for \0
38
39         SHA256_Init(&ct);
40         SHA256_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
41         SHA256_Final(hash, &ct);
42
43         /* base64 encode it */
44         lutil_b64_ntop(
45                         hash,
46                         SHA256_DIGEST_LENGTH,
47                         real_hash,
48                         LUTIL_BASE64_ENCODE_LEN(SHA256_DIGEST_LENGTH)+1
49                         );
50
51         return real_hash;
52 }
53
54
55 char * sha384_hex_hash(const char * passwd) {
56
57         SHA384_CTX ct;
58         unsigned char hash[SHA384_DIGEST_LENGTH];
59         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA384_DIGEST_LENGTH)+1]; // extra char for \0
60
61         SHA384_Init(&ct);
62         SHA384_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
63         SHA384_Final(hash, &ct);
64
65         /* base64 encode it */
66         lutil_b64_ntop(
67                         hash,
68                         SHA384_DIGEST_LENGTH,
69                         real_hash,
70                         LUTIL_BASE64_ENCODE_LEN(SHA384_DIGEST_LENGTH)+1
71                         );
72
73         return real_hash;
74 }
75
76 char * sha512_hex_hash(const char * passwd) {
77
78         SHA512_CTX ct;
79         unsigned char hash[SHA512_DIGEST_LENGTH];
80         static char real_hash[LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1]; // extra char for \0
81
82         SHA512_Init(&ct);
83         SHA512_Update(&ct, (const uint8_t*)passwd, strlen(passwd));
84         SHA512_Final(hash, &ct);
85
86         /* base64 encode it */
87         lutil_b64_ntop(
88                         hash,
89                         SHA512_DIGEST_LENGTH,
90                         real_hash,
91                         LUTIL_BASE64_ENCODE_LEN(SHA512_DIGEST_LENGTH)+1
92                         );
93
94         return real_hash;
95 }
96
97 static int chk_sha256(
98         const struct berval *scheme, // Scheme of hashed reference password
99         const struct berval *passwd, // Hashed reference password to check against
100         const struct berval *cred, // user-supplied password to check
101         const char **text )
102 {
103 #ifdef SLAPD_SHA2_DEBUG
104         fprintf(stderr, "Validating password\n");
105         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
106         fprintf(stderr, "  Hashes to: %s\n", sha256_hex_hash(cred->bv_val));
107         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
108         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
109         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
110 #endif
111         return (strcmp(sha256_hex_hash(cred->bv_val), passwd->bv_val));
112 }
113
114 static int chk_sha384(
115         const struct berval *scheme, // Scheme of hashed reference password
116         const struct berval *passwd, // Hashed reference password to check against
117         const struct berval *cred, // user-supplied password to check
118         const char **text )
119 {
120 #ifdef SLAPD_SHA2_DEBUG
121         fprintf(stderr, "Validating password\n");
122         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
123         fprintf(stderr, "  Hashes to: %s\n", sha384_hex_hash(cred->bv_val));
124         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
125         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
126         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
127 #endif
128         return (strcmp(sha384_hex_hash(cred->bv_val), passwd->bv_val));
129 }
130
131 static int chk_sha512(
132         const struct berval *scheme, // Scheme of hashed reference password
133         const struct berval *passwd, // Hashed reference password to check against
134         const struct berval *cred, // user-supplied password to check
135         const char **text )
136 {
137 #ifdef SLAPD_SHA2_DEBUG
138         fprintf(stderr, "  Password to validate: %s\n", cred->bv_val);
139         fprintf(stderr, "  Hashes to: %s\n", sha512_hex_hash(cred->bv_val));
140         fprintf(stderr, "  Stored password scheme: %s\n", scheme->bv_val);
141         fprintf(stderr, "  Stored password value: %s\n", passwd->bv_val);
142         fprintf(stderr, "  -> Passwords %s\n", strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val) == 0 ? "match" : "do not match");
143 #endif
144         return (strcmp(sha512_hex_hash(cred->bv_val), passwd->bv_val));
145 }
146
147 const struct berval sha256scheme = BER_BVC("{SHA256}");
148 const struct berval sha384scheme = BER_BVC("{SHA384}");
149 const struct berval sha512scheme = BER_BVC("{SHA512}");
150
151 int init_module(int argc, char *argv[]) {
152         int result = 0;
153         result = lutil_passwd_add( (struct berval *)&sha256scheme, chk_sha256, NULL );
154         if (result != 0) return result;
155         result = lutil_passwd_add( (struct berval *)&sha384scheme, chk_sha384, NULL );
156         if (result != 0) return result;
157         result = lutil_passwd_add( (struct berval *)&sha512scheme, chk_sha512, NULL );
158         return result;
159 }