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