Skip to content

Adding set_misc_base32_alphabet directive to allow the use of custom alphabets #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Table of Contents
* [set_hashed_upstream](#set_hashed_upstream)
* [set_encode_base32](#set_encode_base32)
* [set_misc_base32_padding](#set_misc_base32_padding)
* [set_misc_base32_alphabet](#set_misc_base32_alphabet)
* [set_decode_base32](#set_decode_base32)
* [set_encode_base64](#set_encode_base64)
* [set_decode_base64](#set_decode_base64)
Expand Down Expand Up @@ -505,6 +506,8 @@ Please note that we're using [echo-nginx-module](http://github.com/openresty/ech

RFC forces the `[A-Z2-7]` RFC-3548 compliant encoding, but we're using the "base32hex" encoding (`[0-9a-v]`).

The set_misc_base32_alphabet directive allows you to change the alphabet used for encoding/decoding so RFC-3548 compliant encoding is still possible.

By default, the `=` character is used to pad the left-over bytes due to alignment. But the padding behavior can be completely disabled by setting [set_misc_base32_padding](#set_misc_base32_padding) `off`.

When taking a single argument, this directive will do in-place modification of the argument variable. For example,
Expand Down Expand Up @@ -539,6 +542,22 @@ This directive can control whether to pad left-over bytes with the "=" character

[Back to TOC](#table-of-contents)

set_misc_base32_alphabet
-----------------------
**syntax:** *set_misc_base32_alphabet <alphabet>*

**default:** *"0123456789abcdefghijklmnopqrstuv"*

**context:** *http, server, server if, location, location if*

**phase:** *no*

This directive controls the alphabet used for encoding/decoding a base32 digest. It accepts a string containing the desired alphabet like "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" for standard alphabet.

Extended (base32hex) alphabet is used by default.

[Back to TOC](#table-of-contents)

set_decode_base32
-----------------
**syntax:** *set_decode_base32 $dst <src>*
Expand Down
15 changes: 15 additions & 0 deletions doc/HttpSetMiscModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ Please note that we're using [[HttpEchoModule]]'s [[HttpEchoModule#echo|echo dir

RFC forces the <code>[A-Z2-7]</code> RFC-3548 compliant encoding, but we're using the "base32hex" encoding (<code>[0-9a-v]</code>).

The [[#set_misc_base32_alphabet|set_misc_base32_alphabet]] directive allows you to change the alphabet used for encoding/decoding so RFC-3548 compliant encoding is still possible.

By default, the <code>=</code> character is used to pad the left-over bytes due to alignment. But the padding behavior can be completely disabled by setting [[#set_misc_base32_padding|set_misc_base32_padding]] <code>off</code>.

When taking a single argument, this directive will do in-place modification of the argument variable. For example,
Expand Down Expand Up @@ -441,6 +443,19 @@ This directive can be invoked by [[HttpLuaModule]]'s [[HttpLuaModule#ndk.set_var

This directive can control whether to pad left-over bytes with the "=" character when encoding a base32 digest by the [[#set_encode_base32|set_encode_base32]] directive.

== set_misc_base32_alphabet ==
'''syntax:''' ''set_misc_base32_alphabet <alphabet>''

'''default:''' ''"0123456789abcdefghijklmnopqrstuv"''

'''context:''' ''http, server, server if, location, location if''

'''phase:''' ''no''

This directive controls the alphabet used for encoding/decoding a base32 digest. It accepts a string containing the desired alphabet like "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" for standard alphabet.

Extended (base32hex) alphabet is used by default.

== set_decode_base32 ==
'''syntax:''' ''set_decode_base32 $dst <src>''

Expand Down
71 changes: 14 additions & 57 deletions src/ngx_http_set_base32.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@


static void encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
ngx_flag_t padding);
static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst);
ngx_flag_t padding, ngx_str_t *alphabet);
static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
u_char *basis32);


ngx_int_t
Expand All @@ -41,7 +42,8 @@ ngx_http_set_misc_encode_base32(ngx_http_request_t *r, ngx_str_t *res,

src = v->data; dst = p;

encode_base32(v->len, src, &len, dst, conf->base32_padding);
encode_base32(v->len, src, &len, dst, conf->base32_padding,
&conf->base32_alphabet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style, the 2nd line should align with the char right after ( according to nginx's coding style. I'll fix it myself during the merge.


res->data = p;
res->len = len;
Expand All @@ -61,6 +63,10 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,
u_char *src, *dst;
int ret;

ngx_http_set_misc_loc_conf_t *conf;

conf = ngx_http_get_module_loc_conf(r, ngx_http_set_misc_module);

len = base32_decoded_length(v->len);

dd("estimated dst len: %d", (int) len);
Expand All @@ -72,7 +78,7 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,

src = v->data; dst = p;

ret = decode_base32(v->len, src, &len, dst);
ret = decode_base32(v->len, src, &len, dst, conf->basis32);

if (ret == 0 /* OK */) {
res->data = p;
Expand Down Expand Up @@ -113,9 +119,9 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,
* */
static void
encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
ngx_flag_t padding)
ngx_flag_t padding, ngx_str_t *alphabet)
{
static unsigned char basis32[] = "0123456789abcdefghijklmnopqrstuv";
unsigned char *basis32 = alphabet->data;

size_t len;
u_char *s;
Expand Down Expand Up @@ -204,58 +210,9 @@ encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,


static int
decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst)
decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
u_char *basis32)
{
static unsigned char basis32[] = {
/* 0 - 15 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 16 - 31 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 32 - 47 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 48 - 63 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77, 77, 77, 77, 77, 77,

/* 64 - 79 */
77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

/* 80 - 95 */
25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 96 - 111 */
77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

/* 112 - 127 */
25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 128 - 143 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 144 - 159 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 160 - 175 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 176 - 191 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 192 - 207 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 208 - 223 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 224 - 239 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 240 - 255 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
};

size_t len, mod;
u_char *s = src;
u_char *d = dst;
Expand Down
18 changes: 18 additions & 0 deletions src/ngx_http_set_misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
offsetof(ngx_http_set_misc_loc_conf_t, base32_padding),
NULL
},
{
ngx_string("set_misc_base32_alphabet"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like the directive name to be simpler: set_base32_alphabet. Yes, I'm aware of the existing set_misc_base32_padding directive and I'll deprecate the old form and introduce the new form set_base32_padding, which sounds better :) I'll do this change myself during the merge :)

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_set_misc_loc_conf_t, base32_alphabet),
NULL
},
{
ngx_string("set_encode_base32"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
Expand Down Expand Up @@ -466,13 +475,22 @@ ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf)
char *
ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_uint_t i;

ngx_http_set_misc_loc_conf_t *prev = parent;
ngx_http_set_misc_loc_conf_t *conf = child;

ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1);

ngx_conf_merge_str_value(conf->base32_alphabet, prev->base32_alphabet,
"0123456789abcdefghijklmnopqrstuv");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Same style issue here. Need alignment.
  2. Also, the conf->base32_alphabet field is never properly initialized in ngx_http_set_misc_create_loc_conf. And this line actually triggers a valgrind error:
==9991== Conditional jump or move depends on uninitialised value(s)
==9991==    at 0x480A66: ngx_http_set_misc_merge_loc_conf (ngx_http_set_misc_module.c:485)
==9991==    by 0x430585: ngx_http_block (ngx_http.c:601)
==9991==    by 0x417010: ngx_conf_parse (ngx_conf_file.c:391)
==9991==    by 0x414BEC: ngx_init_cycle (ngx_cycle.c:264)
==9991==    by 0x4079DF: main (nginx.c:333)

Anyway, I'll fix both issues myself during the merge. Just FYI :)

(BTW, you can always run your tests with valgrind by setting the environment variable TEST_NGINX_USE_VALGRIND=1.


ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET);

for (i = 0; i < 32; i++) {
conf->basis32[conf->base32_alphabet.data[i]] = i;
}

return NGX_CONF_OK;
}

2 changes: 2 additions & 0 deletions src/ngx_http_set_misc_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

typedef struct {
ngx_flag_t base32_padding;
ngx_str_t base32_alphabet;
u_char basis32[256];
ngx_int_t current; /* for set_rotate */
} ngx_http_set_misc_loc_conf_t;

Expand Down
Loading