626
|
1
|
|
2 /*
|
|
3 * Copyright (C) Maxim Dounin
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_md5.h>
|
|
10 #if (NGX_HAVE_SHA1)
|
|
11 #include <ngx_sha1.h>
|
|
12 #endif
|
|
13
|
|
14
|
628
|
15 #if (NGX_CRYPT)
|
|
16
|
626
|
17 static ngx_int_t ngx_crypt_apr1(ngx_pool_t *pool, u_char *key, u_char *salt,
|
|
18 u_char **encrypted);
|
|
19 static ngx_int_t ngx_crypt_plain(ngx_pool_t *pool, u_char *key, u_char *salt,
|
|
20 u_char **encrypted);
|
|
21
|
|
22 #if (NGX_HAVE_SHA1)
|
|
23
|
|
24 static ngx_int_t ngx_crypt_ssha(ngx_pool_t *pool, u_char *key, u_char *salt,
|
|
25 u_char **encrypted);
|
|
26
|
|
27 #endif
|
|
28
|
|
29
|
|
30 static u_char *ngx_crypt_to64(u_char *p, uint32_t v, size_t n);
|
|
31
|
|
32
|
|
33 ngx_int_t
|
|
34 ngx_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
|
|
35 {
|
|
36 if (ngx_strncmp(salt, "$apr1$", sizeof("$apr1$") - 1) == 0) {
|
|
37 return ngx_crypt_apr1(pool, key, salt, encrypted);
|
|
38
|
|
39 } else if (ngx_strncmp(salt, "{PLAIN}", sizeof("{PLAIN}") - 1) == 0) {
|
|
40 return ngx_crypt_plain(pool, key, salt, encrypted);
|
|
41
|
|
42 #if (NGX_HAVE_SHA1)
|
|
43 } else if (ngx_strncmp(salt, "{SSHA}", sizeof("{SSHA}") - 1) == 0) {
|
|
44 return ngx_crypt_ssha(pool, key, salt, encrypted);
|
|
45 #endif
|
|
46 }
|
|
47
|
|
48 /* fallback to libc crypt() */
|
|
49
|
|
50 return ngx_libc_crypt(pool, key, salt, encrypted);
|
|
51 }
|
|
52
|
|
53
|
|
54 static ngx_int_t
|
|
55 ngx_crypt_apr1(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
|
|
56 {
|
|
57 ngx_int_t n;
|
|
58 ngx_uint_t i;
|
|
59 u_char *p, *last, final[16];
|
|
60 size_t saltlen, keylen;
|
|
61 ngx_md5_t md5, ctx1;
|
|
62
|
|
63 /* Apache's apr1 crypt is Paul-Henning Kamp's md5 crypt with $apr1$ magic */
|
|
64
|
|
65 keylen = ngx_strlen(key);
|
|
66
|
|
67 /* true salt: no magic, max 8 chars, stop at first $ */
|
|
68
|
|
69 salt += sizeof("$apr1$") - 1;
|
|
70 last = salt + 8;
|
|
71 for (p = salt; *p && *p != '$' && p < last; p++) { /* void */ }
|
|
72 saltlen = p - salt;
|
|
73
|
|
74 /* hash key and salt */
|
|
75
|
|
76 ngx_md5_init(&md5);
|
|
77 ngx_md5_update(&md5, key, keylen);
|
628
|
78 ngx_md5_update(&md5, (u_char *) "$apr1$", sizeof("$apr1$") - 1);
|
626
|
79 ngx_md5_update(&md5, salt, saltlen);
|
|
80
|
|
81 ngx_md5_init(&ctx1);
|
|
82 ngx_md5_update(&ctx1, key, keylen);
|
|
83 ngx_md5_update(&ctx1, salt, saltlen);
|
|
84 ngx_md5_update(&ctx1, key, keylen);
|
|
85 ngx_md5_final(final, &ctx1);
|
|
86
|
|
87 for (n = keylen; n > 0; n -= 16) {
|
|
88 ngx_md5_update(&md5, final, n > 16 ? 16 : n);
|
|
89 }
|
|
90
|
|
91 ngx_memzero(final, sizeof(final));
|
|
92
|
|
93 for (i = keylen; i; i >>= 1) {
|
|
94 if (i & 1) {
|
|
95 ngx_md5_update(&md5, final, 1);
|
|
96
|
|
97 } else {
|
|
98 ngx_md5_update(&md5, key, 1);
|
|
99 }
|
|
100 }
|
|
101
|
|
102 ngx_md5_final(final, &md5);
|
|
103
|
|
104 for (i = 0; i < 1000; i++) {
|
|
105 ngx_md5_init(&ctx1);
|
|
106
|
|
107 if (i & 1) {
|
|
108 ngx_md5_update(&ctx1, key, keylen);
|
|
109
|
|
110 } else {
|
|
111 ngx_md5_update(&ctx1, final, 16);
|
|
112 }
|
|
113
|
|
114 if (i % 3) {
|
|
115 ngx_md5_update(&ctx1, salt, saltlen);
|
|
116 }
|
|
117
|
|
118 if (i % 7) {
|
|
119 ngx_md5_update(&ctx1, key, keylen);
|
|
120 }
|
|
121
|
|
122 if (i & 1) {
|
|
123 ngx_md5_update(&ctx1, final, 16);
|
|
124
|
|
125 } else {
|
|
126 ngx_md5_update(&ctx1, key, keylen);
|
|
127 }
|
|
128
|
|
129 ngx_md5_final(final, &ctx1);
|
|
130 }
|
|
131
|
|
132 /* output */
|
|
133
|
|
134 *encrypted = ngx_pnalloc(pool, sizeof("$apr1$") - 1 + saltlen + 16 + 1);
|
|
135 if (*encrypted == NULL) {
|
|
136 return NGX_ERROR;
|
|
137 }
|
|
138
|
|
139 p = ngx_cpymem(*encrypted, "$apr1$", sizeof("$apr1$") - 1);
|
|
140 p = ngx_copy(p, salt, saltlen);
|
|
141 *p++ = '$';
|
|
142
|
|
143 p = ngx_crypt_to64(p, (final[ 0]<<16) | (final[ 6]<<8) | final[12], 4);
|
|
144 p = ngx_crypt_to64(p, (final[ 1]<<16) | (final[ 7]<<8) | final[13], 4);
|
|
145 p = ngx_crypt_to64(p, (final[ 2]<<16) | (final[ 8]<<8) | final[14], 4);
|
|
146 p = ngx_crypt_to64(p, (final[ 3]<<16) | (final[ 9]<<8) | final[15], 4);
|
|
147 p = ngx_crypt_to64(p, (final[ 4]<<16) | (final[10]<<8) | final[ 5], 4);
|
|
148 p = ngx_crypt_to64(p, final[11], 2);
|
|
149 *p = '\0';
|
|
150
|
|
151 return NGX_OK;
|
|
152 }
|
|
153
|
|
154
|
|
155 static u_char *
|
|
156 ngx_crypt_to64(u_char *p, uint32_t v, size_t n)
|
|
157 {
|
|
158 static u_char itoa64[] =
|
|
159 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
160
|
|
161 while (n--) {
|
|
162 *p++ = itoa64[v & 0x3f];
|
|
163 v >>= 6;
|
|
164 }
|
|
165
|
|
166 return p;
|
|
167 }
|
|
168
|
|
169
|
|
170 static ngx_int_t
|
|
171 ngx_crypt_plain(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
|
|
172 {
|
|
173 size_t len;
|
|
174 u_char *p;
|
|
175
|
|
176 len = ngx_strlen(key);
|
|
177
|
|
178 *encrypted = ngx_pnalloc(pool, sizeof("{PLAIN}") - 1 + len + 1);
|
|
179 if (*encrypted == NULL) {
|
|
180 return NGX_ERROR;
|
|
181 }
|
|
182
|
|
183 p = ngx_cpymem(*encrypted, "{PLAIN}", sizeof("{PLAIN}") - 1);
|
|
184 ngx_memcpy(p, key, len + 1);
|
|
185
|
|
186 return NGX_OK;
|
|
187 }
|
|
188
|
|
189
|
|
190 #if (NGX_HAVE_SHA1)
|
|
191
|
|
192 static ngx_int_t
|
|
193 ngx_crypt_ssha(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
|
|
194 {
|
|
195 size_t len;
|
|
196 ngx_str_t encoded, decoded;
|
|
197 ngx_sha1_t sha1;
|
|
198
|
|
199 /* "{SSHA}" base64(SHA1(key salt) salt) */
|
|
200
|
|
201 /* decode base64 salt to find out true salt */
|
|
202
|
|
203 encoded.data = salt + sizeof("{SSHA}") - 1;
|
|
204 encoded.len = ngx_strlen(encoded.data);
|
|
205
|
|
206 decoded.data = ngx_pnalloc(pool, ngx_base64_decoded_length(encoded.len));
|
|
207 if (decoded.data == NULL) {
|
|
208 return NGX_ERROR;
|
|
209 }
|
|
210
|
|
211 ngx_decode_base64(&decoded, &encoded);
|
|
212
|
|
213 /* update SHA1 from key and salt */
|
|
214
|
|
215 ngx_sha1_init(&sha1);
|
|
216 ngx_sha1_update(&sha1, key, ngx_strlen(key));
|
|
217 ngx_sha1_update(&sha1, decoded.data + 20, decoded.len - 20);
|
|
218 ngx_sha1_final(decoded.data, &sha1);
|
|
219
|
|
220 /* encode it back to base64 */
|
|
221
|
|
222 len = sizeof("{SSHA}") - 1 + ngx_base64_encoded_length(decoded.len) + 1;
|
|
223
|
|
224 *encrypted = ngx_pnalloc(pool, len);
|
|
225 if (*encrypted == NULL) {
|
|
226 return NGX_ERROR;
|
|
227 }
|
|
228
|
|
229 encoded.data = ngx_cpymem(*encrypted, "{SSHA}", sizeof("{SSHA}") - 1);
|
|
230 ngx_encode_base64(&encoded, &decoded);
|
|
231 encoded.data[encoded.len] = '\0';
|
|
232
|
|
233 return NGX_OK;
|
|
234 }
|
|
235
|
|
236 #endif /* NGX_HAVE_SHA1 */
|
628
|
237
|
|
238 #endif /* NGX_CRYPT */
|