forked from patcharp/nginx-sticky-module-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_sticky_misc.c
279 lines (223 loc) · 6.56 KB
/
ngx_http_sticky_misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (C) 2010 Jerome Loyet (jerome at loyet dot net)
*/
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <ngx_sha1.h>
#include "ngx_http_sticky_misc.h"
#ifndef ngx_str_set
#define ngx_str_set(str, text) (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text
#endif
/* - fix for 1.11.2 removes include <openssl/md5.h> in ngx_md5.h */
#define MD5_CBLOCK 64
#define MD5_LBLOCK (MD5_CBLOCK/4)
#define MD5_DIGEST_LENGTH 16
#ifndef SHA_CBLOCK
#define SHA_CBLOCK 64
#endif
#ifndef SHA_DIGEST_LENGTH
#define SHA_DIGEST_LENGTH 20
#endif
// /* - bugfix for compiling on sles11 - needs gcc4.6 or later*/
// #pragma GCC diagnostic ignored "-Wuninitialized"
static ngx_int_t cookie_expires(char *str, size_t size, time_t t)
{
char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
struct tm e;
gmtime_r(&t, &e);
return snprintf(str, size, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
wdays[e.tm_wday], e.tm_mday, months[e.tm_mon], e.tm_year + 1900, e.tm_hour,e.tm_min,e.tm_sec);
}
ngx_int_t ngx_http_sticky_misc_set_cookie(ngx_http_request_t *r, ngx_str_t *name, ngx_str_t *value, ngx_str_t *domain, ngx_str_t *path, time_t expires, unsigned secure, unsigned httponly)
{
u_char *cookie, *p;
size_t len;
ngx_table_elt_t *set_cookie;
ngx_str_t remove;
char expires_str[80];
int expires_len = 0;
if (value == NULL) {
ngx_str_set(&remove, "_remove_");
value = &remove;
}
/* name = value */
len = name->len + 1 + value->len;
/*; Domain= */
if (domain->len > 0) {
len += sizeof("; Domain=") - 1 + domain->len;
}
/*; Expires= */
if (expires != NGX_CONF_UNSET) {
expires_len = cookie_expires(expires_str, sizeof(expires_str), time(NULL) + expires);
len += sizeof("; Expires=") - 1 + expires_len;
}
/* ; Path= */
if (path->len > 0) {
len += sizeof("; Path=") - 1 + path->len;
}
/* ; Secure */
if (secure) {
len += sizeof("; Secure") - 1;
}
/* ; HttpOnly */
if (httponly) {
len += sizeof("; HttpOnly") - 1;
}
cookie = ngx_pnalloc(r->pool, len);
if (cookie == NULL) {
return NGX_ERROR;
}
p = ngx_copy(cookie, name->data, name->len);
*p++ = '=';
p = ngx_copy(p, value->data, value->len);
if (domain->len > 0) {
p = ngx_copy(p, "; Domain=", sizeof("; Domain=") - 1);
p = ngx_copy(p, domain->data, domain->len);
}
if (expires != NGX_CONF_UNSET) {
p = ngx_copy(p, "; Expires=", sizeof("; Expires=") - 1);
p = ngx_copy(p, expires_str, expires_len);
}
if (path->len > 0) {
p = ngx_copy(p, "; Path=", sizeof("; Path=") - 1);
p = ngx_copy(p, path->data, path->len);
}
if (secure) {
p = ngx_copy(p, "; Secure", sizeof("; Secure") - 1);
}
if (httponly) {
p = ngx_copy(p, "; HttpOnly", sizeof("; HttpOnly") - 1);
}
set_cookie = ngx_list_push(&r->headers_out.headers);
if (set_cookie == NULL) {
return NGX_ERROR;
}
set_cookie->hash = 1;
ngx_str_set(&set_cookie->key, "Set-Cookie");
set_cookie->value.len = p - cookie;
set_cookie->value.data = cookie;
return NGX_OK;
}
ngx_int_t ngx_http_sticky_misc_md5(ngx_pool_t *pool, void *in, size_t len, ngx_str_t *digest)
{
ngx_md5_t md5;
u_char hash[MD5_DIGEST_LENGTH];
digest->data = ngx_pcalloc(pool, MD5_DIGEST_LENGTH * 2);
if (digest->data == NULL) {
return NGX_ERROR;
}
digest->len = MD5_DIGEST_LENGTH * 2;
ngx_md5_init(&md5);
ngx_md5_update(&md5, in, len);
ngx_md5_final(hash, &md5);
ngx_hex_dump(digest->data, hash, MD5_DIGEST_LENGTH);
return NGX_OK;
}
ngx_int_t ngx_http_sticky_misc_sha1(ngx_pool_t *pool, void *in, size_t len, ngx_str_t *digest)
{
ngx_sha1_t sha1;
u_char hash[SHA_DIGEST_LENGTH];
digest->data = ngx_pcalloc(pool, SHA_DIGEST_LENGTH * 2);
if (digest->data == NULL) {
return NGX_ERROR;
}
digest->len = SHA_DIGEST_LENGTH * 2;
ngx_sha1_init(&sha1);
ngx_sha1_update(&sha1, in, len);
ngx_sha1_final(hash, &sha1);
ngx_hex_dump(digest->data, hash, SHA_DIGEST_LENGTH);
return NGX_OK;
}
ngx_int_t ngx_http_sticky_misc_hmac_md5(ngx_pool_t *pool, void *in, size_t len, ngx_str_t *key, ngx_str_t *digest)
{
u_char hash[MD5_DIGEST_LENGTH];
u_char k[MD5_CBLOCK];
ngx_md5_t md5;
u_int i;
digest->data = ngx_pcalloc(pool, MD5_DIGEST_LENGTH * 2);
if (digest->data == NULL) {
return NGX_ERROR;
}
digest->len = MD5_DIGEST_LENGTH * 2;
ngx_memzero(k, sizeof(k));
if (key->len > MD5_CBLOCK) {
ngx_md5_init(&md5);
ngx_md5_update(&md5, key->data, key->len);
ngx_md5_final(k, &md5);
} else {
ngx_memcpy(k, key->data, key->len);
}
/* XOR ipad */
for (i=0; i < MD5_CBLOCK; i++) {
k[i] ^= 0x36;
}
ngx_md5_init(&md5);
ngx_md5_update(&md5, k, MD5_CBLOCK);
ngx_md5_update(&md5, in, len);
ngx_md5_final(hash, &md5);
/* Convert k to opad -- 0x6A = 0x36 ^ 0x5C */
for (i=0; i < MD5_CBLOCK; i++) {
k[i] ^= 0x6a;
}
ngx_md5_init(&md5);
ngx_md5_update(&md5, k, MD5_CBLOCK);
ngx_md5_update(&md5, hash, MD5_DIGEST_LENGTH);
ngx_md5_final(hash, &md5);
ngx_hex_dump(digest->data, hash, MD5_DIGEST_LENGTH);
return NGX_OK;
}
ngx_int_t ngx_http_sticky_misc_hmac_sha1(ngx_pool_t *pool, void *in, size_t len, ngx_str_t *key, ngx_str_t *digest)
{
u_char hash[SHA_DIGEST_LENGTH];
u_char k[SHA_CBLOCK];
ngx_sha1_t sha1;
u_int i;
digest->data = ngx_pcalloc(pool, SHA_DIGEST_LENGTH * 2);
if (digest->data == NULL) {
return NGX_ERROR;
}
digest->len = SHA_DIGEST_LENGTH * 2;
ngx_memzero(k, sizeof(k));
if (key->len > SHA_CBLOCK) {
ngx_sha1_init(&sha1);
ngx_sha1_update(&sha1, key->data, key->len);
ngx_sha1_final(k, &sha1);
} else {
ngx_memcpy(k, key->data, key->len);
}
/* XOR ipad */
for (i=0; i < SHA_CBLOCK; i++) {
k[i] ^= 0x36;
}
ngx_sha1_init(&sha1);
ngx_sha1_update(&sha1, k, SHA_CBLOCK);
ngx_sha1_update(&sha1, in, len);
ngx_sha1_final(hash, &sha1);
/* Convert k to opad -- 0x6A = 0x36 ^ 0x5C */
for (i=0; i < SHA_CBLOCK; i++) {
k[i] ^= 0x6a;
}
ngx_sha1_init(&sha1);
ngx_sha1_update(&sha1, k, SHA_CBLOCK);
ngx_sha1_update(&sha1, hash, SHA_DIGEST_LENGTH);
ngx_sha1_final(hash, &sha1);
ngx_hex_dump(digest->data, hash, SHA_DIGEST_LENGTH);
return NGX_OK;
}
ngx_int_t ngx_http_sticky_misc_text_raw(ngx_pool_t *pool, void *in, size_t len, ngx_str_t *digest)
{
if (!in) {
return NGX_ERROR;
}
digest->data = ngx_pnalloc(pool, len);
if (digest->data == NULL) {
return NGX_ERROR;
}
memcpy(digest->data, in, len);
digest->len = len;
return NGX_OK;
}