Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 : #include <ndk.h>
7 :
8 : #include "ngx_http_set_hmac.h"
9 : #include <openssl/evp.h>
10 : #include <openssl/hmac.h>
11 :
12 :
13 : /* this function's implementation is partly borrowed from
14 : * https://github.com/anomalizer/ngx_aws_auth */
15 : ngx_int_t
16 0 : ngx_http_set_misc_set_hmac_sha1(ngx_http_request_t *r, ngx_str_t *res,
17 : ngx_http_variable_value_t *v)
18 : {
19 : ngx_http_variable_value_t *secret, *string_to_sign;
20 0 : unsigned int md_len = 0;
21 : unsigned char md[EVP_MAX_MD_SIZE];
22 : const EVP_MD *evp_md;
23 :
24 0 : evp_md = EVP_sha1();
25 :
26 0 : secret = v;
27 0 : string_to_sign = v + 1;
28 :
29 : dd("secret=%.*s, string_to_sign=%.*s", (int) secret->len, secret->data,
30 : (int) string_to_sign->len, string_to_sign->data);
31 :
32 0 : HMAC(evp_md, secret->data, secret->len, string_to_sign->data,
33 0 : string_to_sign->len, md, &md_len);
34 :
35 : /* defensive test if there is something wrong with openssl */
36 0 : if (md_len == 0 || md_len > EVP_MAX_MD_SIZE) {
37 0 : res->len = 0;
38 0 : return NGX_ERROR;
39 : }
40 :
41 0 : res->len = md_len;
42 0 : ndk_palloc_re(res->data, r->pool, md_len);
43 :
44 0 : ngx_memcpy(res->data,
45 : &md,
46 : md_len);
47 :
48 0 : return NGX_OK;
49 : }
50 :
|