Skip to content
This repository
Browse code

Added SHA-256 and SHA-512 support

  • Loading branch information...
commit 468725b3dee3baef11e9a2aafe46e941ab50c236 1 parent 8d271b1
Valery Kholodkov authored

Showing 2 changed files with 167 additions and 0 deletions. Show diff stats Hide diff stats

  1. +3 0  Changelog
  2. +164 0 ngx_http_upload_module.c
3  Changelog
... ... @@ -1,4 +1,7 @@
1 1
  2 +Version 2.2.1
  3 + * Added feature: SHA-256 and SHA-512
  4 +
2 5 Version 2.2.0
3 6 * Added feature: resumable uploads
4 7 * Added feature: allow to use of $variables in "upload_pass" directive (Piotr Sikora)
164 ngx_http_upload_module.c
@@ -154,6 +154,8 @@ typedef struct {
154 154
155 155 unsigned int md5:1;
156 156 unsigned int sha1:1;
  157 + unsigned int sha256:1;
  158 + unsigned int sha512:1;
157 159 unsigned int crc32:1;
158 160 } ngx_http_upload_loc_conf_t;
159 161
@@ -167,6 +169,16 @@ typedef struct ngx_http_upload_sha1_ctx_s {
167 169 u_char sha1_digest[SHA_DIGEST_LENGTH * 2];
168 170 } ngx_http_upload_sha1_ctx_t;
169 171
  172 +typedef struct ngx_http_upload_sha256_ctx_s {
  173 + SHA256_CTX sha256;
  174 + u_char sha256_digest[SHA256_DIGEST_LENGTH * 2];
  175 +} ngx_http_upload_sha256_ctx_t;
  176 +
  177 +typedef struct ngx_http_upload_sha512_ctx_s {
  178 + SHA512_CTX sha512;
  179 + u_char sha512_digest[SHA512_DIGEST_LENGTH * 2];
  180 +} ngx_http_upload_sha512_ctx_t;
  181 +
170 182 struct ngx_http_upload_ctx_s;
171 183
172 184 /*
@@ -229,6 +241,8 @@ typedef struct ngx_http_upload_ctx_s {
229 241
230 242 ngx_http_upload_md5_ctx_t *md5_ctx;
231 243 ngx_http_upload_sha1_ctx_t *sha1_ctx;
  244 + ngx_http_upload_sha256_ctx_t *sha256_ctx;
  245 + ngx_http_upload_sha512_ctx_t *sha512_ctx;
232 246 uint32_t crc32;
233 247
234 248 unsigned int first_part:1;
@@ -259,6 +273,10 @@ static ngx_int_t ngx_http_upload_md5_variable(ngx_http_request_t *r,
259 273 ngx_http_variable_value_t *v, uintptr_t data);
260 274 static ngx_int_t ngx_http_upload_sha1_variable(ngx_http_request_t *r,
261 275 ngx_http_variable_value_t *v, uintptr_t data);
  276 +static ngx_int_t ngx_http_upload_sha256_variable(ngx_http_request_t *r,
  277 + ngx_http_variable_value_t *v, uintptr_t data);
  278 +static ngx_int_t ngx_http_upload_sha512_variable(ngx_http_request_t *r,
  279 + ngx_http_variable_value_t *v, uintptr_t data);
262 280 static ngx_int_t ngx_http_upload_file_size_variable(ngx_http_request_t *r,
263 281 ngx_http_variable_value_t *v, uintptr_t data);
264 282 static void ngx_http_upload_content_range_variable_set(ngx_http_request_t *r,
@@ -693,6 +711,22 @@ static ngx_http_variable_t ngx_http_upload_aggregate_variables[] = { /* {{{ */
693 711 (uintptr_t) "0123456789ABCDEF",
694 712 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
695 713
  714 + { ngx_string("upload_file_sha256"), NULL, ngx_http_upload_sha256_variable,
  715 + (uintptr_t) "0123456789abcdef",
  716 + NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  717 +
  718 + { ngx_string("upload_file_sha256_uc"), NULL, ngx_http_upload_sha256_variable,
  719 + (uintptr_t) "0123456789ABCDEF",
  720 + NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  721 +
  722 + { ngx_string("upload_file_sha512"), NULL, ngx_http_upload_sha512_variable,
  723 + (uintptr_t) "0123456789abcdef",
  724 + NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  725 +
  726 + { ngx_string("upload_file_sha512_uc"), NULL, ngx_http_upload_sha512_variable,
  727 + (uintptr_t) "0123456789ABCDEF",
  728 + NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  729 +
696 730 { ngx_string("upload_file_crc32"), NULL, ngx_http_upload_crc32_variable,
697 731 (uintptr_t) offsetof(ngx_http_upload_ctx_t, crc32),
698 732 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
@@ -762,6 +796,26 @@ ngx_http_upload_handler(ngx_http_request_t *r)
762 796 }else
763 797 u->sha1_ctx = NULL;
764 798
  799 + if(ulcf->sha256) {
  800 + if(u->sha256_ctx == NULL) {
  801 + u->sha256_ctx = ngx_palloc(r->pool, sizeof(ngx_http_upload_sha256_ctx_t));
  802 + if (u->sha256_ctx == NULL) {
  803 + return NGX_HTTP_INTERNAL_SERVER_ERROR;
  804 + }
  805 + }
  806 + }else
  807 + u->sha256_ctx = NULL;
  808 +
  809 + if(ulcf->sha512) {
  810 + if(u->sha512_ctx == NULL) {
  811 + u->sha512_ctx = ngx_palloc(r->pool, sizeof(ngx_http_upload_sha512_ctx_t));
  812 + if (u->sha512_ctx == NULL) {
  813 + return NGX_HTTP_INTERNAL_SERVER_ERROR;
  814 + }
  815 + }
  816 + }else
  817 + u->sha512_ctx = NULL;
  818 +
765 819 u->calculate_crc32 = ulcf->crc32;
766 820
767 821 u->request = r;
@@ -1215,6 +1269,12 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
1215 1269 if(u->sha1_ctx != NULL)
1216 1270 SHA1_Init(&u->sha1_ctx->sha1);
1217 1271
  1272 + if(u->sha256_ctx != NULL)
  1273 + SHA256_Init(&u->sha256_ctx->sha256);
  1274 +
  1275 + if(u->sha512_ctx != NULL)
  1276 + SHA512_Init(&u->sha512_ctx->sha512);
  1277 +
1218 1278 if(u->calculate_crc32)
1219 1279 ngx_crc32_init(u->crc32);
1220 1280
@@ -1308,6 +1368,12 @@ static void ngx_http_upload_finish_handler(ngx_http_upload_ctx_t *u) { /* {{{ */
1308 1368 if(u->sha1_ctx)
1309 1369 SHA1_Final(u->sha1_ctx->sha1_digest, &u->sha1_ctx->sha1);
1310 1370
  1371 + if(u->sha256_ctx)
  1372 + SHA256_Final(u->sha256_ctx->sha256_digest, &u->sha256_ctx->sha256);
  1373 +
  1374 + if(u->sha512_ctx)
  1375 + SHA512_Final(u->sha512_ctx->sha512_digest, &u->sha512_ctx->sha512);
  1376 +
1311 1377 if(u->calculate_crc32)
1312 1378 ngx_crc32_final(u->crc32);
1313 1379
@@ -1470,6 +1536,12 @@ static ngx_int_t ngx_http_upload_flush_output_buffer(ngx_http_upload_ctx_t *u, u
1470 1536 if(u->sha1_ctx)
1471 1537 SHA1_Update(&u->sha1_ctx->sha1, buf, len);
1472 1538
  1539 + if(u->sha256_ctx)
  1540 + SHA256_Update(&u->sha256_ctx->sha256, buf, len);
  1541 +
  1542 + if(u->sha512_ctx)
  1543 + SHA512_Update(&u->sha512_ctx->sha512, buf, len);
  1544 +
1473 1545 if(u->calculate_crc32)
1474 1546 ngx_crc32_update(&u->crc32, buf, len);
1475 1547
@@ -2020,6 +2092,14 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
2020 2092 conf->sha1 = prev->sha1;
2021 2093 }
2022 2094
  2095 + if(prev->sha256) {
  2096 + conf->sha256 = prev->sha256;
  2097 + }
  2098 +
  2099 + if(prev->sha512) {
  2100 + conf->sha512 = prev->sha512;
  2101 + }
  2102 +
2023 2103 if(prev->crc32) {
2024 2104 conf->crc32 = prev->crc32;
2025 2105 }
@@ -2178,6 +2258,80 @@ ngx_http_upload_sha1_variable(ngx_http_request_t *r,
2178 2258 return NGX_OK;
2179 2259 } /* }}} */
2180 2260
  2261 +static ngx_int_t /* {{{ ngx_http_upload_sha256_variable */
  2262 +ngx_http_upload_sha256_variable(ngx_http_request_t *r,
  2263 + ngx_http_variable_value_t *v, uintptr_t data)
  2264 +{
  2265 + ngx_uint_t i;
  2266 + ngx_http_upload_ctx_t *u;
  2267 + u_char *c;
  2268 + u_char *hex_table;
  2269 +
  2270 + u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
  2271 +
  2272 + if(u->sha256_ctx == NULL || u->partial_content) {
  2273 + v->not_found = 1;
  2274 + return NGX_OK;
  2275 + }
  2276 +
  2277 + v->valid = 1;
  2278 + v->no_cacheable = 0;
  2279 + v->not_found = 0;
  2280 +
  2281 + hex_table = (u_char*)data;
  2282 + c = u->sha256_ctx->sha256_digest + SHA256_DIGEST_LENGTH * 2;
  2283 +
  2284 + i = SHA256_DIGEST_LENGTH;
  2285 +
  2286 + do{
  2287 + i--;
  2288 + *--c = hex_table[u->sha256_ctx->sha256_digest[i] & 0xf];
  2289 + *--c = hex_table[u->sha256_ctx->sha256_digest[i] >> 4];
  2290 + }while(i != 0);
  2291 +
  2292 + v->data = u->sha256_ctx->sha256_digest;
  2293 + v->len = SHA256_DIGEST_LENGTH * 2;
  2294 +
  2295 + return NGX_OK;
  2296 +} /* }}} */
  2297 +
  2298 +static ngx_int_t /* {{{ ngx_http_upload_sha512_variable */
  2299 +ngx_http_upload_sha512_variable(ngx_http_request_t *r,
  2300 + ngx_http_variable_value_t *v, uintptr_t data)
  2301 +{
  2302 + ngx_uint_t i;
  2303 + ngx_http_upload_ctx_t *u;
  2304 + u_char *c;
  2305 + u_char *hex_table;
  2306 +
  2307 + u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
  2308 +
  2309 + if(u->sha512_ctx == NULL || u->partial_content) {
  2310 + v->not_found = 1;
  2311 + return NGX_OK;
  2312 + }
  2313 +
  2314 + v->valid = 1;
  2315 + v->no_cacheable = 0;
  2316 + v->not_found = 0;
  2317 +
  2318 + hex_table = (u_char*)data;
  2319 + c = u->sha512_ctx->sha512_digest + SHA512_DIGEST_LENGTH * 2;
  2320 +
  2321 + i = SHA512_DIGEST_LENGTH;
  2322 +
  2323 + do{
  2324 + i--;
  2325 + *--c = hex_table[u->sha512_ctx->sha512_digest[i] & 0xf];
  2326 + *--c = hex_table[u->sha512_ctx->sha512_digest[i] >> 4];
  2327 + }while(i != 0);
  2328 +
  2329 + v->data = u->sha512_ctx->sha512_digest;
  2330 + v->len = SHA512_DIGEST_LENGTH * 2;
  2331 +
  2332 + return NGX_OK;
  2333 +} /* }}} */
  2334 +
2181 2335 static ngx_int_t /* {{{ ngx_http_upload_crc32_variable */
2182 2336 ngx_http_upload_crc32_variable(ngx_http_request_t *r,
2183 2337 ngx_http_variable_value_t *v, uintptr_t data)
@@ -2411,6 +2565,10 @@ ngx_http_upload_set_form_field(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2411 2565 ", upload_file_md5_uc"
2412 2566 ", upload_file_sha1"
2413 2567 ", upload_file_sha1_uc"
  2568 + ", upload_file_sha256"
  2569 + ", upload_file_sha256_uc"
  2570 + ", upload_file_sha512"
  2571 + ", upload_file_sha512_uc"
2414 2572 ", upload_file_crc32"
2415 2573 ", upload_content_range"
2416 2574 " and upload_file_size"
@@ -2424,6 +2582,12 @@ ngx_http_upload_set_form_field(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2424 2582 if(v->get_handler == ngx_http_upload_sha1_variable)
2425 2583 ulcf->sha1 = 1;
2426 2584
  2585 + if(v->get_handler == ngx_http_upload_sha256_variable)
  2586 + ulcf->sha256 = 1;
  2587 +
  2588 + if(v->get_handler == ngx_http_upload_sha512_variable)
  2589 + ulcf->sha512 = 1;
  2590 +
2427 2591 if(v->get_handler == ngx_http_upload_crc32_variable)
2428 2592 ulcf->crc32 = 1;
2429 2593 }

0 comments on commit 468725b

Please sign in to comment.
Something went wrong with that request. Please try again.