Skip to content

Commit

Permalink
various coding style fixes in Jeremy Wohl's patch for cryptographical…
Browse files Browse the repository at this point in the history
…ly strong random string functions in github pull request #10.
  • Loading branch information
agentzh committed May 10, 2012
1 parent c11f17f commit 5441a7e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ src/module.h
work/ work/
src/random.[ch] src/random.[ch]
build1[01] build1[01]
analyze
src/rotate.[ch]
13 changes: 13 additions & 0 deletions src/ngx_http_set_misc_module.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ static ndk_set_var_t ngx_http_set_misc_set_decode_hex_filter = {
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_set_encode_hex_filter = { static ndk_set_var_t ngx_http_set_misc_set_encode_hex_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_set_encode_hex, ngx_http_set_misc_set_encode_hex,
1, 1,
NULL NULL
}; };



#if NGX_OPENSSL #if NGX_OPENSSL
static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = { static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = {
NDK_SET_VAR_MULTI_VALUE, NDK_SET_VAR_MULTI_VALUE,
Expand All @@ -68,6 +70,7 @@ static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = {
}; };
#endif #endif



#ifndef NGX_HTTP_SET_HASH #ifndef NGX_HTTP_SET_HASH
static ndk_set_var_t ngx_http_set_misc_set_md5_filter = { static ndk_set_var_t ngx_http_set_misc_set_md5_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
Expand Down Expand Up @@ -95,41 +98,47 @@ static ndk_set_var_t ngx_http_set_misc_unescape_uri_filter = {
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = { static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_escape_uri, ngx_http_set_misc_escape_uri,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = { static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_decode_base32, ngx_http_set_misc_decode_base32,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = { static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_quote_sql_str, ngx_http_set_misc_quote_sql_str,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_quote_pgsql_str_filter = { static ndk_set_var_t ngx_http_set_misc_quote_pgsql_str_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_quote_pgsql_str, ngx_http_set_misc_quote_pgsql_str,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = { static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_quote_json_str, ngx_http_set_misc_quote_json_str,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = { static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_encode_base32, ngx_http_set_misc_encode_base32,
Expand All @@ -145,27 +154,31 @@ static ndk_set_var_t ngx_http_set_misc_local_today_filter = {
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_set_random_filter = { static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
NDK_SET_VAR_MULTI_VALUE, NDK_SET_VAR_MULTI_VALUE,
ngx_http_set_misc_set_random, ngx_http_set_misc_set_random,
2, 2,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_set_secure_random_alphanum_filter = { static ndk_set_var_t ngx_http_set_misc_set_secure_random_alphanum_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_set_secure_random_alphanum, ngx_http_set_misc_set_secure_random_alphanum,
1, 1,
NULL NULL
}; };



static ndk_set_var_t ngx_http_set_misc_set_secure_random_lcalpha_filter = { static ndk_set_var_t ngx_http_set_misc_set_secure_random_lcalpha_filter = {
NDK_SET_VAR_VALUE, NDK_SET_VAR_VALUE,
ngx_http_set_misc_set_secure_random_lcalpha, ngx_http_set_misc_set_secure_random_lcalpha,
1, 1,
NULL NULL
}; };



static ngx_command_t ngx_http_set_misc_commands[] = { static ngx_command_t ngx_http_set_misc_commands[] = {
{ ngx_string ("set_encode_base64"), { ngx_string ("set_encode_base64"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
Expand Down
48 changes: 30 additions & 18 deletions src/ngx_http_set_secure_random.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
#include "ngx_http_set_secure_random.h" #include "ngx_http_set_secure_random.h"
#include <stdlib.h> #include <stdlib.h>


const int MAX_RANDOM_STRING = 64;


const int ALPHANUM = 1; enum {
const int LCALPHA = 2; MAX_RANDOM_STRING = 64,
ALPHANUM = 1,
LCALPHA = 2
};



ngx_int_t ngx_int_t
ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r, ngx_http_set_misc_set_secure_random_common(int alphabet_type,
ngx_str_t *res, ngx_http_variable_value_t *v); ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v);



ngx_int_t ngx_int_t
ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r, ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r,
Expand All @@ -23,50 +27,57 @@ ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r,
return ngx_http_set_misc_set_secure_random_common(ALPHANUM, r, res, v); return ngx_http_set_misc_set_secure_random_common(ALPHANUM, r, res, v);
} }



ngx_int_t ngx_int_t
ngx_http_set_misc_set_secure_random_lcalpha(ngx_http_request_t *r, ngx_http_set_misc_set_secure_random_lcalpha(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v) ngx_str_t *res, ngx_http_variable_value_t *v)
{ {
return ngx_http_set_misc_set_secure_random_common(LCALPHA, r, res, v); return ngx_http_set_misc_set_secure_random_common(LCALPHA, r, res, v);
} }



ngx_int_t ngx_int_t
ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r, ngx_http_set_misc_set_secure_random_common(int alphabet_type,
ngx_str_t *res, ngx_http_variable_value_t *v) ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v)
{ {
static u_char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static u_char alphabet[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

u_char entropy[MAX_RANDOM_STRING]; u_char entropy[MAX_RANDOM_STRING];
u_char output[MAX_RANDOM_STRING]; u_char output[MAX_RANDOM_STRING];
ngx_int_t length, fd, i; ngx_int_t length, i;
ngx_fd_t fd;
ssize_t n; ssize_t n;



length = ngx_atoi(v->data, v->len); length = ngx_atoi(v->data, v->len);

if (length == NGX_ERROR || length < 1 || length > MAX_RANDOM_STRING) { if (length == NGX_ERROR || length < 1 || length > MAX_RANDOM_STRING) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"set_random: bad \"length\" argument: %v", v); "set_random: bad \"length\" argument: %v", v);
return NGX_ERROR; return NGX_ERROR;
} }


fd = ngx_open_file("/dev/urandom", NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); fd = ngx_open_file("/dev/urandom", NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
if (fd == -1) { if (fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"set_secure_random: could not open /dev/urandom"); "set_secure_random: could not open /dev/urandom");
return NGX_ERROR; return NGX_ERROR;
} }

n = ngx_read_fd(fd, entropy, length); n = ngx_read_fd(fd, entropy, length);
if (n != length) { if (n != length) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"set_secure_random: could not read all %d byte(s) from /dev/urandom", length); "set_secure_random: could not read all %i byte(s) from "
"/dev/urandom", length);
return NGX_ERROR; return NGX_ERROR;
} }

ngx_close_file(fd); ngx_close_file(fd);

for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
if (alphabet_type == LCALPHA) { if (alphabet_type == LCALPHA) {
output[i] = entropy[i] % 26 + 'a'; output[i] = entropy[i] % 26 + 'a';

} else { } else {
output[i] = alphabet[ entropy[i] % (sizeof alphabet - 1) ]; output[i] = alphabet[ entropy[i] % (sizeof alphabet - 1) ];
} }
Expand All @@ -78,13 +89,14 @@ ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t
} }


ngx_memcpy(res->data, output, length); ngx_memcpy(res->data, output, length);

res->len = length; res->len = length;


/* Set all required params */ /* set all required params */
v->valid = 1; v->valid = 1;
v->no_cacheable = 0; v->no_cacheable = 0;
v->not_found = 0; v->not_found = 0;


return NGX_OK; return NGX_OK;
} }

17 changes: 15 additions & 2 deletions t/secure-random.t
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
# vi:filetype=perl # vi:filetype=


use Test::Nginx::Socket; use Test::Nginx::Socket;


repeat_each(100); repeat_each(2);


plan tests => repeat_each() * 2 * blocks(); plan tests => repeat_each() * 2 * blocks();


Expand All @@ -25,6 +25,8 @@ __DATA__
GET /alphanum GET /alphanum
--- response_body_like: ^[a-zA-Z0-9]{32}$ --- response_body_like: ^[a-zA-Z0-9]{32}$




=== TEST 2: a 16-character alphanum === TEST 2: a 16-character alphanum
--- config --- config
location /alphanum { location /alphanum {
Expand All @@ -36,6 +38,8 @@ __DATA__
GET /alphanum GET /alphanum
--- response_body_like: ^[a-zA-Z0-9]{16}$ --- response_body_like: ^[a-zA-Z0-9]{16}$




=== TEST 3: a 1-character alphanum === TEST 3: a 1-character alphanum
--- config --- config
location /alphanum { location /alphanum {
Expand All @@ -47,6 +51,8 @@ __DATA__
GET /alphanum GET /alphanum
--- response_body_like: ^[a-zA-Z0-9]{1}$ --- response_body_like: ^[a-zA-Z0-9]{1}$




=== TEST 4: length less than <= 0 should fail === TEST 4: length less than <= 0 should fail
--- config --- config
location /alphanum { location /alphanum {
Expand All @@ -59,6 +65,8 @@ __DATA__
--- response_body_like: 500 Internal Server Error --- response_body_like: 500 Internal Server Error
--- error_code: 500 --- error_code: 500




=== TEST 5: length less than <= 0 should fail === TEST 5: length less than <= 0 should fail
--- config --- config
location /alphanum { location /alphanum {
Expand All @@ -71,6 +79,8 @@ __DATA__
--- response_body_like: 500 Internal Server Error --- response_body_like: 500 Internal Server Error
--- error_code: 500 --- error_code: 500




=== TEST 6: non-numeric length should fail === TEST 6: non-numeric length should fail
--- config --- config
location /alphanum { location /alphanum {
Expand All @@ -83,6 +93,8 @@ __DATA__
--- response_body_like: 500 Internal Server Error --- response_body_like: 500 Internal Server Error
--- error_code: 500 --- error_code: 500




=== TEST 7: a 16-character lcalpha === TEST 7: a 16-character lcalpha
--- config --- config
location /lcalpha { location /lcalpha {
Expand All @@ -93,3 +105,4 @@ __DATA__
--- request --- request
GET /lcalpha GET /lcalpha
--- response_body_like: ^[a-z]{16}$ --- response_body_like: ^[a-z]{16}$

0 comments on commit 5441a7e

Please sign in to comment.