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