Add support for the {SHA} encryption scheme in htpasswd files
Author: Louis Opter <kalessin@kalessin.fr
diff --git a/src/core/ngx_crypt.c b/src/core/ngx_crypt.c
|
a
|
b
|
|
| 24 | 24 | |
| 25 | 25 | static ngx_int_t ngx_crypt_ssha(ngx_pool_t *pool, u_char *key, u_char *salt, |
| 26 | 26 | u_char **encrypted); |
| | 27 | static ngx_int_t ngx_crypt_sha(ngx_pool_t *pool, u_char *key, u_char *salt, |
| | 28 | u_char **encrypted); |
| 27 | 29 | |
| 28 | 30 | #endif |
| 29 | 31 | |
| … |
… |
|
| 43 | 45 | #if (NGX_HAVE_SHA1) |
| 44 | 46 | } else if (ngx_strncmp(salt, "{SSHA}", sizeof("{SSHA}") - 1) == 0) { |
| 45 | 47 | return ngx_crypt_ssha(pool, key, salt, encrypted); |
| | 48 | } else if (ngx_strncmp(salt, "{SHA}", sizeof("{SHA}") - 1) == 0) { |
| | 49 | return ngx_crypt_sha(pool, key, salt, encrypted); |
| 46 | 50 | #endif |
| 47 | 51 | } |
| 48 | 52 | |
| … |
… |
|
| 241 | 245 | return NGX_OK; |
| 242 | 246 | } |
| 243 | 247 | |
| | 248 | static ngx_int_t |
| | 249 | ngx_crypt_sha(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted) |
| | 250 | { |
| | 251 | ngx_sha1_t sha1; |
| | 252 | u_char digest[20]; |
| | 253 | ngx_str_t encoded, decoded; |
| | 254 | |
| | 255 | /* "{SHA}" base64(SHA1(key)), generated with htpasswd -s from Apache */ |
| | 256 | |
| | 257 | decoded.len = sizeof(digest); |
| | 258 | decoded.data = digest; |
| | 259 | |
| | 260 | ngx_sha1_init(&sha1); |
| | 261 | ngx_sha1_update(&sha1, key, ngx_strlen(key)); |
| | 262 | ngx_sha1_final(digest, &sha1); |
| | 263 | |
| | 264 | encoded.len = sizeof("{SHA}") - 1 + ngx_base64_encoded_length(sizeof(digest)) + 1; |
| | 265 | *encrypted = ngx_pnalloc(pool, encoded.len); |
| | 266 | if (*encrypted == NULL) { |
| | 267 | return NGX_ERROR; |
| | 268 | } |
| | 269 | |
| | 270 | encoded.data = ngx_cpymem(*encrypted, "{SHA}", sizeof("{SHA}") - 1); |
| | 271 | ngx_encode_base64(&encoded, &decoded); |
| | 272 | encoded.data[encoded.len] = '\0'; |
| | 273 | |
| | 274 | return NGX_OK; |
| | 275 | } |
| | 276 | |
| 244 | 277 | #endif /* NGX_HAVE_SHA1 */ |
| 245 | 278 | |
| 246 | 279 | #endif /* NGX_CRYPT */ |