forked from claesjac/nginx-aws-auth-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_aws_auth_module.c
1045 lines (812 loc) · 27.6 KB
/
ngx_http_aws_auth_module.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#define SHA256_DIGEST_HEX_LENGTH (SHA256_DIGEST_LENGTH * 2)
#define HMAC_DIGEST_MAX_HEX_LENGTH (EVP_MAX_MD_SIZE * 2)
#define AMZ_DATE_MAX_LEN (sizeof("YYYYmmdd"))
#define AMZ_DATE_TIME_MAX_LEN (sizeof("YYYYmmddTHHMMSSZ"))
static char *ngx_http_aws_auth_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_http_aws_auth_preconfiguration(ngx_conf_t *cf);
typedef struct {
ngx_str_t access_key;
ngx_str_t secret_key;
ngx_str_t service;
ngx_str_t region;
ngx_str_t secret_key_prefix;
ngx_str_t signing_key_date;
ngx_str_t signing_key;
ngx_str_t key_scope;
ngx_str_t key_scope_suffix;
unsigned ignore:1;
} ngx_http_aws_auth_ctx_t;
typedef struct {
ngx_conf_t *cf;
ngx_command_t *cmds;
} ngx_http_aws_auth_conf_ctx_t;
static ngx_str_t ngx_http_aws_auth_date_var_name =
ngx_string("aws_auth_date");
static ngx_str_t ngx_http_aws_auth_host =
ngx_string("host");
static ngx_str_t ngx_http_aws_auth_amz_prefix =
ngx_string("x-amz-");
static ngx_str_t ngx_http_aws_auth_content_sha_header =
ngx_string("x-amz-content-sha256");
static ngx_str_t ngx_http_aws_auth_date_header =
ngx_string("x-amz-date");
static ngx_str_t ngx_http_aws_auth_aws4_request =
ngx_string("aws4_request");
static ngx_str_t ngx_http_aws_auth_aws4 =
ngx_string("AWS4");
static ngx_command_t ngx_http_aws_auth_commands[] = {
{ ngx_string("aws_auth"),
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
ngx_http_aws_auth_block,
0,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_aws_auth_module_ctx = {
ngx_http_aws_auth_preconfiguration, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_aws_auth_module = {
NGX_MODULE_V1,
&ngx_http_aws_auth_module_ctx, /* module context */
ngx_http_aws_auth_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_command_t ngx_http_aws_auth_block_commands[] = {
{ ngx_string("access_key"),
NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
0,
offsetof(ngx_http_aws_auth_ctx_t, access_key),
NULL },
{ ngx_string("secret_key"),
NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
0,
offsetof(ngx_http_aws_auth_ctx_t, secret_key),
NULL },
{ ngx_string("service"),
NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
0,
offsetof(ngx_http_aws_auth_ctx_t, service),
NULL },
{ ngx_string("region"),
NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
0,
offsetof(ngx_http_aws_auth_ctx_t, region),
NULL },
ngx_null_command
};
static ngx_uint_t argument_number[] = {
NGX_CONF_NOARGS,
NGX_CONF_TAKE1,
NGX_CONF_TAKE2,
NGX_CONF_TAKE3,
NGX_CONF_TAKE4,
NGX_CONF_TAKE5,
NGX_CONF_TAKE6,
NGX_CONF_TAKE7
};
static void
ngx_http_aws_auth_sha256_hex(ngx_str_t *message, u_char *digest)
{
u_char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, message->data, message->len);
SHA256_Final(hash, &sha256);
ngx_hex_dump(digest, hash, sizeof(hash));
}
static ngx_int_t
ngx_http_aws_auth_hmac_sha256(ngx_http_request_t *r, ngx_str_t *key,
ngx_str_t *message, ngx_str_t *dest)
{
unsigned hash_len;
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
HMAC_CTX hmac_buf;
#endif
HMAC_CTX *hmac;
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
hmac = HMAC_CTX_new();
if (hmac == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_hmac_sha256_hex: HMAC_CTX_new failed");
return NGX_ERROR;
}
#else
hmac = &hmac_buf;
HMAC_CTX_init(hmac);
#endif
HMAC_Init_ex(hmac, key->data, key->len, EVP_sha256(), NULL);
HMAC_Update(hmac, message->data, message->len);
HMAC_Final(hmac, dest->data, &hash_len);
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
HMAC_CTX_free(hmac);
#else
HMAC_CTX_cleanup(hmac);
#endif
dest->len = hash_len;
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_hmac_sha256_hex(ngx_http_request_t *r, ngx_str_t *key,
ngx_str_t *message, ngx_str_t *dest)
{
u_char hash_buf[EVP_MAX_MD_SIZE];
ngx_str_t hash;
hash.data = hash_buf;
if (ngx_http_aws_auth_hmac_sha256(r, key, message, &hash) != NGX_OK) {
return NGX_ERROR;
}
dest->len = ngx_hex_dump(dest->data, hash.data, hash.len) - dest->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_date_time(
ngx_http_request_t *r,
ngx_http_variable_value_t *v,
uintptr_t data)
{
struct tm tm;
v->data = ngx_pnalloc(r->pool, AMZ_DATE_TIME_MAX_LEN);
if (v->data == NULL) {
return NGX_ERROR;
}
ngx_libc_gmtime(ngx_time(), &tm);
v->len = strftime((char*)v->data, AMZ_DATE_TIME_MAX_LEN, "%Y%m%dT%H%M%SZ",
&tm);
if (v->len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_date_time: strftime failed");
return NGX_ERROR;
}
v->valid = 1;
return NGX_OK;
}
static int ngx_libc_cdecl
ngx_http_aws_auth_compare_keyvals(const void *one, const void *two)
{
const ngx_keyval_t *h1 = one;
const ngx_keyval_t *h2 = two;
size_t len = ngx_min(h1->key.len, h2->key.len);
ngx_int_t rc;
rc = ngx_memcmp(h1->key.data, h2->key.data, len);
if (rc != 0) {
return rc;
}
if (h1->key.len < h2->key.len) {
return -1;
}
if (h1->key.len > h2->key.len) {
return 1;
}
return 0;
}
static ngx_int_t
ngx_http_aws_auth_get_signed_headers(ngx_http_request_t *r, ngx_buf_t *request,
ngx_array_t *headers, ngx_str_t *date, ngx_str_t *content_sha)
{
ngx_int_t rc;
ngx_str_t key;
ngx_flag_t required;
ngx_keyval_t *h;
date->len = 0;
content_sha->len = 0;
ngx_array_init(headers, r->pool, 5, sizeof(*h));
for (;;) {
rc = ngx_http_parse_header_line(r, request, 1);
if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
/* a whole header has been parsed successfully */
break;
}
if (rc != NGX_OK) {
/* there was error while a header line parsing */
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_get_signed_headers: "
"failed to parse request header %i", rc);
return NGX_ERROR;
}
/* a header line has been parsed successfully */
key.data = r->header_name_start;
key.len = r->header_name_end - r->header_name_start;
required = 0;
if (key.len == ngx_http_aws_auth_host.len &&
ngx_strncasecmp(key.data, ngx_http_aws_auth_host.data,
ngx_http_aws_auth_host.len) == 0)
{
required = 1;
}
else if (key.len > ngx_http_aws_auth_amz_prefix.len &&
ngx_strncasecmp(key.data, ngx_http_aws_auth_amz_prefix.data,
ngx_http_aws_auth_amz_prefix.len) == 0)
{
required = 1;
}
if (!required) {
continue;
}
h = ngx_array_push(headers);
h->key.data = ngx_pnalloc(r->pool, key.len);
h->key.len = key.len;
if (key.len == r->lowcase_index) {
ngx_memcpy(h->key.data, r->lowcase_header, key.len);
} else {
ngx_strlow(h->key.data, key.data, key.len);
}
h->value.data = r->header_start;
h->value.len = r->header_end - r->header_start;
if (key.len == ngx_http_aws_auth_content_sha_header.len &&
ngx_strncmp(h->key.data, ngx_http_aws_auth_content_sha_header.data,
ngx_http_aws_auth_content_sha_header.len) == 0)
{
*content_sha = h->value;
}
if (key.len == ngx_http_aws_auth_date_header.len &&
ngx_strncmp(h->key.data, ngx_http_aws_auth_date_header.data,
ngx_http_aws_auth_date_header.len) == 0)
{
*date = h->value;
}
}
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_get_canonical_querystring(ngx_http_request_t *r, u_char *qs, ngx_int_t qs_len, ngx_str_t *querystring) {
ngx_keyval_t *h;
ngx_keyval_t *last;
u_char *e, *ke;
ngx_array_t *elems;
ngx_int_t s = 0;
elems = ngx_array_create(r->pool, 5, sizeof(*h));
for (;;) {
/* Abort and skip invalid requests */
if (qs_len <= 0) {
break;
}
/* Skip "reserved" characters in the beginning of the string */
if (*qs == '&' || *qs == '=' || *qs == '?') {
qs++;
qs_len--;
continue;
}
/* Find next & to get end of key/value */
e = ngx_strlchr(qs, qs + qs_len, '&');
/* If there are no more parts then point e to last character of query */
if (e == NULL) {
e = qs + qs_len;
}
/* Construct element */
h = ngx_array_push(elems);
/* Find = to get end of key */
ke = ngx_strlchr(qs, e, '=');
h->key.len = ke == NULL ? e - qs : ke - qs;
h->key.data = ngx_palloc(r->pool, h->key.len);
ngx_memcpy(h->key.data, qs, h->key.len);
s += h->key.len; /* key length + length of = */
if (ke != NULL) {
h->value.len = e - ke - 1;
h->value.data = ngx_palloc(r->pool, h->value.len);
ngx_memcpy(h->value.data, ke + 1, h->value.len);
s += h->value.len;
} else {
h->value.len = 0;
}
/* Move start of qs */
qs += h->key.len + h->value.len + 1;
qs_len -= h->key.len + h->value.len + 1;
/* Add one for = */
s++;
/* Add one for & if we're passed first elem */
if (elems->nelts > 1) {
s++;
}
}
ngx_qsort(elems->elts, elems->nelts, sizeof(ngx_keyval_t),
ngx_http_aws_auth_compare_keyvals);
querystring->data = (u_char *) ngx_palloc(r->pool, s);
querystring->len = s;
last = (ngx_keyval_t*)elems->elts + elems->nelts;
e = querystring->data;
for (h = elems->elts; h < last; h++) {
if (h != elems->elts) {
*e++ = '&';
}
ngx_memcpy(e, h->key.data, h->key.len);
e += h->key.len;
*e++ = '=';
if (h->value.len > 0) {
ngx_memcpy(e, h->value.data, h->value.len);
e += h->value.len;
}
}
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_canonical_request(ngx_http_request_t *r,
ngx_http_aws_auth_ctx_t *ctx, ngx_str_t *signed_headers, ngx_str_t *date,
ngx_str_t *result)
{
u_char *p;
u_char *pos;
u_char *qs;
size_t alloc_size;
ngx_int_t canon_uri_len;
ngx_int_t qs_len;
ngx_int_t rc;
ngx_buf_t *request;
ngx_str_t method;
ngx_str_t content_sha;
ngx_array_t headers;
ngx_str_t querystring;
ngx_keyval_t *h;
ngx_keyval_t *last;
ngx_http_upstream_t *u;
/* make ngx_http_proxy generate the request for us */
u = r->upstream;
if (u == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no upstream");
return NGX_ERROR;
}
ctx->ignore = 1;
rc = r->upstream->create_request(r);
ctx->ignore = 0;
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: create request failed %i",
rc);
return NGX_ERROR;
}
if (u->request_bufs == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no request bufs");
return NGX_ERROR;
}
if (u->request_bufs->next != NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: more than one request buf");
return NGX_ERROR;
}
request = u->request_bufs->buf;
u->request_bufs = NULL;
/* get the method */
pos = ngx_strlchr(request->pos, request->last, ' ');
if (pos == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no space in request buf");
return NGX_ERROR;
}
method.data = request->pos;
method.len = pos - method.data;
/* get the uri */
qs = ngx_strlchr(u->uri.data, u->uri.data + u->uri.len, '?');
if (qs == NULL) {
canon_uri_len = u->uri.len;
qs_len = 0;
querystring.data = NULL;
querystring.len = 0;
} else {
canon_uri_len = qs - u->uri.data;
qs_len = u->uri.len - canon_uri_len - 1;
/* Get query string */
ngx_http_aws_auth_get_canonical_querystring(r, qs + 1, qs_len, &querystring);
}
/* skip the request line */
request->pos = ngx_strlchr(request->pos, request->last, LF);
if (request->pos == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no LF in request buf");
return NGX_ERROR;
}
request->pos++;
/* get headers */
if (ngx_http_aws_auth_get_signed_headers(r, request,
&headers, date, &content_sha) != NGX_OK) {
return NGX_ERROR;
}
if (!content_sha.len) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no %V header",
&ngx_http_aws_auth_content_sha_header);
return NGX_ERROR;
}
if (!date->len) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_canonical_request: no %V header",
&ngx_http_aws_auth_date_header);
return NGX_ERROR;
}
ngx_qsort(headers.elts, headers.nelts, sizeof(ngx_keyval_t),
ngx_http_aws_auth_compare_keyvals);
last = (ngx_keyval_t*)headers.elts + headers.nelts;
signed_headers->len = 0;
alloc_size = 0;
for (h = headers.elts; h < last; h++) {
signed_headers->len += h->key.len + 1;
alloc_size += h->key.len + h->value.len + 2; /* 2 = : + LF */
}
/* build signed headers string */
if (signed_headers->len > 0) {
signed_headers->data = ngx_pnalloc(r->pool, signed_headers->len);
if (signed_headers->data == NULL) {
return NGX_ERROR;
}
p = signed_headers->data;
for (h = headers.elts; h < last; h++) {
p = ngx_copy(p, h->key.data, h->key.len);
*p++ = ';';
}
signed_headers->len--; /* remove the last ; */
}
/* canonical request */
alloc_size += method.len + u->uri.len + signed_headers->len + querystring.len +
content_sha.len + 5; /* 5 = LFs */
result->data = ngx_pnalloc(r->pool, alloc_size);
if (result->data == NULL) {
return NGX_ERROR;
}
p = result->data;
p = ngx_copy(p, method.data, method.len);
*p++ = LF;
p = ngx_copy(p, u->uri.data, canon_uri_len);
*p++ = LF;
p = ngx_copy(p, querystring.data, querystring.len);
*p++ = LF; /* no query params */
for (h = headers.elts; h < last; h++) {
p = ngx_copy(p, h->key.data, h->key.len);
*p++ = ':';
p = ngx_copy(p, h->value.data, h->value.len);
*p++ = LF;
}
*p++ = LF;
p = ngx_copy(p, signed_headers->data, signed_headers->len);
*p++ = LF;
p = ngx_copy(p, content_sha.data, content_sha.len);
result->len = p - result->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_generate_signing_key(ngx_http_request_t *r,
ngx_http_aws_auth_ctx_t *ctx)
{
u_char *p;
u_char date_buf[AMZ_DATE_MAX_LEN];
struct tm tm;
ngx_str_t date;
ngx_str_t *signing_key;
/* get the GMT date */
ngx_libc_gmtime(ngx_time(), &tm);
date.len = strftime((char*)date_buf, sizeof(date_buf), "%Y%m%d", &tm);
if (date.len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_aws_auth_generate_signing_key: strftime failed");
return NGX_ERROR;
}
date.data = date_buf;
/* check whether date changed since last time */
if (ctx->signing_key_date.len == date.len &&
ngx_memcmp(date.data, ctx->signing_key_date.data, date.len) == 0)
{
return NGX_OK;
}
/* generate a key */
ctx->signing_key_date.len = 0;
signing_key = &ctx->signing_key;
if (ngx_http_aws_auth_hmac_sha256(r, &ctx->secret_key_prefix, &date,
signing_key) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_http_aws_auth_hmac_sha256(r, signing_key, &ctx->region,
signing_key) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_http_aws_auth_hmac_sha256(r, signing_key, &ctx->service,
signing_key) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_http_aws_auth_hmac_sha256(r, signing_key,
&ngx_http_aws_auth_aws4_request, signing_key) != NGX_OK)
{
return NGX_ERROR;
}
/* save the date and key scope */
ctx->signing_key_date.len = date.len;
ngx_memcpy(ctx->signing_key_date.data, date.data, date.len);
p = ngx_copy(ctx->key_scope.data, ctx->signing_key_date.data,
ctx->signing_key_date.len);
p = ngx_copy(p, ctx->key_scope_suffix.data, ctx->key_scope_suffix.len);
ctx->key_scope.len = p - ctx->key_scope.data;
return NGX_OK;
}
static ngx_int_t
ngx_http_aws_auth_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
u_char *p;
size_t alloc_size;
ngx_str_t date;
ngx_str_t result;
ngx_str_t signature;
ngx_str_t string_to_sign;
ngx_str_t signed_headers;
ngx_str_t canonical_sha256;
ngx_str_t canonical_request;
ngx_http_aws_auth_ctx_t *ctx;
u_char signature_buf[HMAC_DIGEST_MAX_HEX_LENGTH];
u_char canonical_sha256_buf[SHA256_DIGEST_HEX_LENGTH];
static const char string_to_sign_template[] =
"AWS4-HMAC-SHA256\n"
"%V\n"
"%V\n"
"%V";
static const char authorization_template[] =
"AWS4-HMAC-SHA256 Credential=%V/%V, "
"SignedHeaders=%V, "
"Signature=%V";
ctx = (void*)data;
if (ctx->ignore) {
v->not_found = 1;
return NGX_OK;
}
/* canonical request */
if (ngx_http_aws_auth_canonical_request(r, ctx, &signed_headers, &date,
&canonical_request) != NGX_OK) {
return NGX_ERROR;
}
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_aws_auth_variable: "
"signed_headers=%V, date=%V, canonical_request=%V",
&signed_headers, &date, &canonical_request);
ngx_http_aws_auth_sha256_hex(&canonical_request, canonical_sha256_buf);
canonical_sha256.data = canonical_sha256_buf;
canonical_sha256.len = sizeof(canonical_sha256_buf);
/* generate signing key */
if (ngx_http_aws_auth_generate_signing_key(r, ctx) != NGX_OK) {
return NGX_ERROR;
}
/* string to sign */
string_to_sign.data = ngx_pnalloc(r->pool,
sizeof(string_to_sign_template) + date.len + ctx->key_scope.len +
canonical_sha256.len);
if (string_to_sign.data == NULL) {
return NGX_ERROR;
}
p = ngx_sprintf(string_to_sign.data, string_to_sign_template,
&date, &ctx->key_scope, &canonical_sha256);
string_to_sign.len = p - string_to_sign.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_aws_auth_variable: string_to_sign=%V", &string_to_sign);
/* signature */
signature.data = signature_buf;
if (ngx_http_aws_auth_hmac_sha256_hex(r, &ctx->signing_key,
&string_to_sign, &signature) != NGX_OK) {
return NGX_ERROR;
}
/* result */
alloc_size = sizeof(authorization_template) + ctx->access_key.len +
ctx->key_scope.len + signed_headers.len + signature.len;
result.data = ngx_pnalloc(r->pool, alloc_size);
if (result.data == NULL) {
return NGX_ERROR;
}
p = ngx_sprintf(result.data, authorization_template, &ctx->access_key,
&ctx->key_scope, &signed_headers, &signature);
result.len = p - result.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_aws_auth_variable: result=%V", &result);
v->data = result.data;
v->len = result.len;
v->valid = 1;
v->not_found = 0;
return NGX_OK;
}
/* copied from ngx_conf_handler, removed support for modules */
static char *
ngx_http_secure_token_command_handler(ngx_conf_t *cf, ngx_command_t *dummy,
void *conf)
{
ngx_http_aws_auth_conf_ctx_t *ctx;
ngx_command_t *cmd;
ngx_str_t *name;
char *rv;
ctx = cf->ctx;
cmd = ctx->cmds;
name = cf->args->elts;
for ( /* void */; cmd->name.len; cmd++) {
if (name->len != cmd->name.len) {
continue;
}
if (ngx_strcmp(name->data, cmd->name.data) != 0) {
continue;
}
/* is the directive's argument count right ? */
if (!(cmd->type & NGX_CONF_ANY)) {
if (cmd->type & NGX_CONF_FLAG) {
if (cf->args->nelts != 2) {
goto invalid;
}
}
else if (cmd->type & NGX_CONF_1MORE) {
if (cf->args->nelts < 2) {
goto invalid;
}
}
else if (cmd->type & NGX_CONF_2MORE) {
if (cf->args->nelts < 3) {
goto invalid;
}
}
else if (cf->args->nelts > NGX_CONF_MAX_ARGS) {
goto invalid;
}
else if (!(cmd->type & argument_number[cf->args->nelts - 1]))
{
goto invalid;
}
}
rv = cmd->set(ctx->cf, cmd, conf);
if (rv == NGX_CONF_OK) {
return NGX_CONF_OK;
}
if (rv == NGX_CONF_ERROR) {
return NGX_CONF_ERROR;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"%s\" directive %s", name->data, rv);
return NGX_CONF_ERROR;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown directive \"%s\"", name->data);
return NGX_CONF_ERROR;
invalid:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid number of arguments in \"%s\" directive",
name->data);
return NGX_CONF_ERROR;
}
static char *
ngx_http_aws_auth_init_ctx(ngx_conf_t *cf, ngx_http_aws_auth_ctx_t *ctx)
{
u_char *p;
/* add prefix to secret key */
ctx->secret_key_prefix.data = ngx_pnalloc(cf->pool,
ngx_http_aws_auth_aws4.len + ctx->secret_key.len);
if (ctx->secret_key_prefix.data == NULL) {
return NGX_CONF_ERROR;
}
p = ctx->secret_key_prefix.data;
p = ngx_copy(p, ngx_http_aws_auth_aws4.data, ngx_http_aws_auth_aws4.len);
p = ngx_copy(p, ctx->secret_key.data, ctx->secret_key.len);
ctx->secret_key_prefix.len = p - ctx->secret_key_prefix.data;
/* init key scope suffix */
ctx->key_scope_suffix.data = ngx_pnalloc(cf->pool, ctx->region.len +
ctx->service.len + ngx_http_aws_auth_aws4_request.len + 3);
if (ctx->key_scope_suffix.data == NULL) {
return NGX_CONF_ERROR;
}
p = ctx->key_scope_suffix.data;
*p++ = '/';
p = ngx_copy(p, ctx->region.data, ctx->region.len);
*p++ = '/';
p = ngx_copy(p, ctx->service.data, ctx->service.len);
*p++ = '/';
p = ngx_copy(p, ngx_http_aws_auth_aws4_request.data,
ngx_http_aws_auth_aws4_request.len);
ctx->key_scope_suffix.len = p - ctx->key_scope_suffix.data;
/* alloc additional buffers */
p = ngx_pnalloc(cf->pool, AMZ_DATE_MAX_LEN + EVP_MAX_MD_SIZE +
AMZ_DATE_MAX_LEN + ctx->key_scope_suffix.len);
if (p == NULL) {
return NGX_CONF_ERROR;
}
ctx->signing_key_date.data = p;
p += AMZ_DATE_MAX_LEN;
ctx->signing_key.data = p;
p += EVP_MAX_MD_SIZE;
ctx->key_scope.data = p;
return NGX_CONF_OK;
}
static char *
ngx_http_aws_auth_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_aws_auth_conf_ctx_t conf_ctx;
ngx_http_aws_auth_ctx_t *ctx;
ngx_http_variable_t *var;
ngx_conf_t save;
ngx_str_t *value;
ngx_str_t name;
char *rv;
value = cf->args->elts;
/* get the variable name */
name = value[1];
if (name.data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &name);
return NGX_CONF_ERROR;
}
name.len--;
name.data++;
/* initialize the context */
ctx = ngx_pcalloc(cf->pool, sizeof(*ctx));
if (ctx == NULL) {
return NGX_CONF_ERROR;
}
/* add the variable */
var = ngx_http_add_variable(cf, &name, 0);
if (var == NULL) {
return NGX_CONF_ERROR;
}
/* parse the block */
var->get_handler = ngx_http_aws_auth_variable;
var->data = (uintptr_t)ctx;
conf_ctx.cmds = ngx_http_aws_auth_block_commands;
conf_ctx.cf = &save;
save = *cf;
cf->ctx = &conf_ctx;
cf->handler = ngx_http_secure_token_command_handler;
cf->handler_conf = (void*)ctx;
rv = ngx_conf_parse(cf, NULL);
*cf = save;
if (rv != NGX_CONF_OK) {
return rv;