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