aboutsummaryrefslogtreecommitdiff
path: root/lib/reqsign.c
blob: f3083c657b867221319b2b3394843b690aaa6594 (plain)
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
/* This file is part of Eclat.
   Copyright (C) 2012-2015 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <string.h>
#include <time.h>
#include "libeclat.h"
#include "sha256.h"
#include "grecs.h"

struct pname {
	size_t i;
	char **a;
};

static int
get_param_name(void *sym, void *data)
{
	struct grecs_syment *se = sym;
	struct pname *pn = data;
	pn->a[pn->i++] = se->name;
	return 0;
}

static int
compnames(const void *a, const void *b)
{
	char * const *ac = a;
	char * const *bc = b;
	return strcmp(*ac, *bc);
}

static void
requestsign2(struct ec2_request *req, char *secret)
{
	char **pnames;
	size_t i, n;
	struct grecs_txtacc *acc;
	struct pname pn;
	char *str;
	char digest[SHA256_DIGEST_SIZE];
	char *signature;
	size_t siglen;
	const char *verb;
	char tsbuf[22];
	time_t t;
	
	acc = grecs_txtacc_create();

	/* Add default parameters */
	eclat_request_add_param(req, "AWSAccessKeyId", req->access_key);
	eclat_request_add_param(req, "SignatureMethod", "HmacSHA256");
	eclat_request_add_param(req, "SignatureVersion", "2");
	if (req->token)
		eclat_request_add_param(req, "SecurityToken", req->token);

	time(&t);
	strftime(tsbuf, sizeof(tsbuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
	eclat_request_add_param(req, "Timestamp", tsbuf);

	eclat_request_encode(req);
	
	/* Collect and sort parameter names */
	n = grecs_symtab_count_entries(req->params);
	pnames = grecs_calloc(n, sizeof(pnames[0]));
	pn.i = 0;
	pn.a = pnames;
	grecs_symtab_enumerate(req->params, get_param_name, &pn);
	qsort(pnames, n, sizeof(pnames[0]), compnames);

	verb = (req->flags & EC2_RF_POST) ? "POST" : "GET";
	grecs_txtacc_grow_string(acc, verb);
	grecs_txtacc_grow_char(acc, '\n');
	grecs_txtacc_grow_string(acc, req->endpoint);
	grecs_txtacc_grow_char(acc, '\n');
	grecs_txtacc_grow_string(acc, req->uri);
	grecs_txtacc_grow_char(acc, '\n');

	/* Append a canonicalized request string */
	for (i = 0; i < n; i++) {
		struct ec2_param *p, key;

		key.name = pnames[i];
		p = grecs_symtab_lookup_or_install(req->params, &key, NULL);
		if (!p)
			abort();
		if (i != 0)
			grecs_txtacc_grow_char(acc, '&');
		grecs_txtacc_grow_string(acc, p->name);
		if (p->value) {
			grecs_txtacc_grow_char(acc, '=');
			grecs_txtacc_grow_string(acc, p->value);
		}
	}
	grecs_txtacc_grow_char(acc, 0);
	str = grecs_txtacc_finish(acc, 0);

	hmac_sha256(str, strlen(str), secret, strlen(secret), digest);

	eclat_base64_encode((unsigned char *)digest, sizeof(digest),
			    (unsigned char**) &signature, &siglen);
	eclat_request_add_param_encoded(req, "Signature", signature);
	free(signature);
	
	grecs_txtacc_free(acc);
	free(pnames);

/*FIXME
	t += req->ttl;
	strftime(tsbuf, sizeof(tsbuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
	eclat_request_add_param(req, "Expires", tsbuf);
*/
}

void
eclat_hex_encode(unsigned char *input, size_t inlen,
		 char **poutput, size_t *poutlen)
{
	size_t l = inlen * 2;
	char *p = grecs_malloc(l + 1);

	*poutput = p;
	*poutlen = l;

	while (inlen--) {
		static char xdig[] = "0123456789abcdef";
		unsigned c = *input++;

		*p++ = xdig[c >> 4];
		*p++ = xdig[c & 0xf];
	}
	*p = 0;
}
		
/* Ref. http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
 */
static void
requestsign4(struct ec2_request *req, char *secret)
{
	char **pnames;
	struct pname pn;
	size_t i, n;
	struct grecs_txtacc *acc;
	char digest[SHA256_DIGEST_SIZE];
	const char *verb;
	char tsbuf[22];
	time_t t;
	char *p;
	char const *payload;
	char *hashstr = NULL;
	size_t hashsize = 0;
	char *string_to_sign;
	static char algostr[] = "AWS4-HMAC-SHA256";
	static char termstr[] = "aws4_request";
	char *canonical_req;
	static char *signed_headers = "host;x-amz-date";
	char *credential;
	char *signature;
	struct sha256_ctx ctx;
	char *service;
	size_t service_len;

	service = req->endpoint;
	service_len = strcspn(service, ".");
	
	/* Create text accumulator */
	acc = grecs_txtacc_create();

	/* Timestamp */
	time(&t);
	strftime(tsbuf, sizeof(tsbuf), "%Y%m%dT%H%M%SZ", gmtime(&t));

	/* Build credential */	
	grecs_txtacc_grow_string(acc, req->access_key);
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow(acc, tsbuf, 8); /* %Y%m%d part only */
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow_string(acc, req->region);
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow(acc, service, service_len);
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow_string(acc, termstr);
	grecs_txtacc_grow_char(acc, 0);
	credential = grecs_txtacc_finish(acc, 0);
	
	eclat_request_add_header(req, "Host", req->endpoint);
	eclat_request_add_header(req, "X-Amz-Date", tsbuf);

	/* Encode the request */
	eclat_request_encode(req);

	/* Collect and sort parameter names */
	n = grecs_symtab_count_entries(req->params);
	pnames = grecs_calloc(n, sizeof(pnames[0]));
	pn.i = 0;
	pn.a = pnames;
	grecs_symtab_enumerate(req->params, get_param_name, &pn);
	qsort(pnames, n, sizeof(pnames[0]), compnames);

        /* Create a canonical request */
	verb = (req->flags & EC2_RF_POST) ? "POST" : "GET";
	grecs_txtacc_grow_string(acc, verb);
	grecs_txtacc_grow_char(acc, '\n');
	grecs_txtacc_grow_string(acc, req->uri);
	grecs_txtacc_grow_char(acc, '\n');
	/* Append a canonicalized request string */
	for (i = 0; i < n; i++) {
		struct ec2_param *p, key;

		key.name = pnames[i];
		p = grecs_symtab_lookup_or_install(req->params, &key, NULL);
		if (!p)
			abort();
		if (i != 0)
			grecs_txtacc_grow_char(acc, '&');
		grecs_txtacc_grow_string(acc, p->name);
		if (p->value) {
			grecs_txtacc_grow_char(acc, '=');
			grecs_txtacc_grow_string(acc, p->value);
		}
	}
	grecs_txtacc_grow_char(acc, '\n');
	
	/* CanonicalHeaders */
	grecs_txtacc_grow_string(acc, "host");
	grecs_txtacc_grow_char(acc, ':');
	grecs_txtacc_grow_string(acc, req->endpoint);
	grecs_txtacc_grow_char(acc, '\n');

	grecs_txtacc_grow_string(acc, "x-amz-date");
	grecs_txtacc_grow_char(acc, ':');
	grecs_txtacc_grow_string(acc, tsbuf);
	grecs_txtacc_grow_char(acc, '\n');
	
	/*   end of headers */
	grecs_txtacc_grow_char(acc, '\n');
	/* Signed Headers */
	grecs_txtacc_grow_string(acc, signed_headers);
	grecs_txtacc_grow_char(acc, '\n');
	/* Payload hash */
	if (req->flags & EC2_RF_POST) {
		/* FIXME: payload = req->request */
		err("%s:%d: POST is not yet implemented", __FILE__, __LINE__);
		abort();
	} else
		payload = "";

	sha256_init_ctx(&ctx);
	sha256_process_bytes(payload, strlen(payload), &ctx);
	sha256_finish_ctx(&ctx, digest);
	
	eclat_hex_encode((unsigned char *)digest, sizeof(digest),
			 &hashstr, &hashsize);
	grecs_txtacc_grow(acc, hashstr, hashsize);
	free(hashstr);

	grecs_txtacc_grow_char(acc, 0);
	canonical_req = grecs_txtacc_finish(acc, 0);

	sha256_init_ctx(&ctx);
	sha256_process_bytes(canonical_req, strlen(canonical_req), &ctx);
	sha256_finish_ctx(&ctx, digest);
	eclat_hex_encode((unsigned char *)digest, sizeof(digest),
			 &canonical_req, &hashsize);
	
        /* Create a string to sign */
	grecs_txtacc_grow_string(acc, algostr);
	grecs_txtacc_grow_char(acc, '\n');
	grecs_txtacc_grow_string(acc, tsbuf);
	grecs_txtacc_grow_char(acc, '\n');
	/*   credential scope: */
	grecs_txtacc_grow(acc, tsbuf, 8); /* %Y%m%d part only */
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow_string(acc, req->region);
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow(acc, service, service_len);
	grecs_txtacc_grow_char(acc, '/');
	grecs_txtacc_grow_string(acc, termstr);
	grecs_txtacc_grow_char(acc, '\n');

	/*   hashed request */
	grecs_txtacc_grow_string(acc, canonical_req);
	
	grecs_txtacc_grow_char(acc, 0);
	string_to_sign = grecs_txtacc_finish(acc, 0);
	
	/* Derive a signing key */
	grecs_txtacc_grow_string(acc, "AWS4");
	grecs_txtacc_grow_string(acc, secret);
	grecs_txtacc_grow_char(acc, 0);
	p = grecs_txtacc_finish(acc, 0);
	
	hmac_sha256(tsbuf, 8, p, strlen(p), digest);
	hmac_sha256(req->region, strlen(req->region), digest, sizeof(digest), 
		    digest);
	hmac_sha256(service, service_len, digest, sizeof(digest), 
		    digest);
	hmac_sha256(termstr, strlen(termstr), digest, sizeof(digest),
		    digest);

	/* Calculate the signature */
	hmac_sha256(string_to_sign, strlen(string_to_sign),
		    digest, sizeof(digest),
		    digest);
	eclat_hex_encode((unsigned char *)digest, sizeof(digest),
			 &signature, &hashsize);

	if (req->token)
		eclat_request_add_header(req, "X-Amz-Security-Token", req->token);
	
	/* Build authorization header */
	grecs_txtacc_grow_string(acc, algostr);
	grecs_txtacc_grow_string(acc, " Credential=");
	grecs_txtacc_grow_string(acc, credential);
	grecs_txtacc_grow_string(acc, ", SignedHeaders=");
	grecs_txtacc_grow_string(acc, signed_headers);
	grecs_txtacc_grow_string(acc, ", Signature=");
	grecs_txtacc_grow_string(acc, signature);
	grecs_txtacc_grow_char(acc, 0);
	p = grecs_txtacc_finish(acc, 0);
	eclat_request_add_header(req, "Authorization", p);
	
	free(signature);
	grecs_txtacc_free(acc);
	/* Encode the request */
	eclat_request_encode(req);
}


struct qsimpl {
	char *qs_version;
	void (*qs_fun)(struct ec2_request *, char *);
};

static struct qsimpl qstab[] = {
	{ "2", requestsign2 },
	{ "4", requestsign4 },
	{ NULL }
};

void
eclat_request_sign(struct ec2_request *req, char *secret, char *version)
{
	struct qsimpl *qs;

	for (qs = qstab; qs->qs_version && strcmp(qs->qs_version, version);
	     qs++)
		;

	if (qs->qs_version)
		qs->qs_fun(req, secret);
	else {
		err("INTERNAL ERROR: unsupported version %s", version);
		abort();
	}
}

Return to:

Send suggestions and report system problems to the System administrator.