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
#![allow(unused_variables)]
use std::ascii::AsciiExt;
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::str;
use std::io::prelude::*;
use openssl::sign::Signer;
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::hash::hash2;
use rustc_serialize::hex::ToHex;
use rustc_serialize::base64::{STANDARD, ToBase64};
use time::Tm;
use time::now_utc;
use url::percent_encoding::{DEFAULT_ENCODE_SET, QUERY_ENCODE_SET, utf8_percent_encode};
use aws::common::credentials::AwsCredentials;
use aws::common::params::Params;
use aws::common::region::Region;
use aws::s3::endpoint::Signature;
use aws::s3::endpoint::Endpoint;
static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Debug)]
pub struct SignedRequest<'a> {
pub method: String,
pub service: String,
pub region: Region,
pub path: String,
pub headers: BTreeMap<String, Vec<Vec<u8>>>,
pub params: Params,
pub bucket: String,
pub hostname: Option<String>,
pub payload: Option<&'a [u8]>,
pub content_type: Option<String>,
pub path_options: Option<String>,
pub canonical_query_string: String,
pub canonical_uri: String,
pub endpoint: &'a Endpoint,
}
impl<'a> SignedRequest<'a> {
pub fn new(method: &str, service: &str, region: Region, bucket: &str, path: &str, endpoint: &'a Endpoint)
-> SignedRequest<'a> {
SignedRequest {
method: method.to_string(),
service: service.to_string(),
region: region,
path: path.to_string(),
headers: BTreeMap::new(),
params: Params::new(),
bucket: bucket.to_string(),
hostname: None,
payload: None,
content_type: None,
path_options: None,
canonical_query_string: String::new(),
canonical_uri: String::new(),
endpoint: endpoint,
}
}
pub fn set_content_type(&mut self, content_type: String) {
self.content_type = Some(content_type);
}
pub fn set_bucket(&mut self, bucket: &str) {
self.bucket = bucket.to_string();
}
pub fn set_hostname(&mut self, hostname: Option<String>) {
self.hostname = hostname;
}
pub fn set_path_options(&mut self, path_options: Option<String>) {
self.path_options = path_options;
}
pub fn set_endpoint_prefix(&mut self, endpoint_prefix: String) {
self.hostname = Some(build_hostname(&endpoint_prefix, self.region));
}
pub fn set_payload(&mut self, payload: Option<&'a [u8]>) {
self.payload = payload;
}
pub fn set_params(&mut self, params: Params) {
self.params = params;
}
pub fn bucket(&self) -> &str {
&self.bucket
}
pub fn method(&self) -> &str {
&self.method
}
pub fn endpoint(&self) -> &Endpoint {
&self.endpoint
}
pub fn endpoint_scheme(&self) -> &str {
match self.endpoint.endpoint {
Some(ref url) => url.scheme(),
None => "https",
}
}
pub fn path(&self) -> &str {
&self.path
}
pub fn canonical_uri(&self) -> &str {
&self.canonical_uri
}
pub fn canonical_query_string(&self) -> &str {
&self.canonical_query_string
}
pub fn payload(&self) -> Option<&'a [u8]> {
self.payload
}
pub fn headers(&'a self) -> &'a BTreeMap<String, Vec<Vec<u8>>> {
&self.headers
}
pub fn hostname(&self) -> String {
match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, self.region),
}
}
pub fn path_options(&self) -> Option<String> {
self.path_options.clone()
}
pub fn remove_header(&mut self, key: &str) {
let key_lower = key.to_ascii_lowercase().to_string();
self.headers.remove(&key_lower);
}
pub fn add_header(&mut self, key: &str, value: &str) {
let key_lower = key.to_ascii_lowercase().to_string();
let value_vec = value.as_bytes().to_vec();
match self.headers.entry(key_lower) {
Entry::Vacant(entry) => {
let mut values = Vec::new();
values.push(value_vec);
entry.insert(values);
},
Entry::Occupied(entry) => {
entry.into_mut().push(value_vec);
},
}
}
pub fn update_header(&mut self, key: &str, value: &str) {
self.remove_header(key);
let key_lower = key.to_ascii_lowercase().to_string();
let value_vec = value.as_bytes().to_vec();
match self.headers.entry(key_lower) {
Entry::Vacant(entry) => {
let mut values = Vec::new();
values.push(value_vec);
entry.insert(values);
},
Entry::Occupied(entry) => {},
}
}
pub fn get_header(&mut self, key: &str) -> String {
let key_lower = key.to_ascii_lowercase().to_string();
match self.headers.entry(key_lower) {
Entry::Vacant(entry) => "".to_string(),
Entry::Occupied(entry) => canonical_values(entry.get()),
}
}
pub fn add_param<S>(&mut self, key: S, value: S)
where S: Into<String>,
{
self.params.insert(key.into(), value.into());
}
pub fn sign(&mut self, creds: &AwsCredentials) {
if self.endpoint.is_bucket_virtual {
if self.bucket.contains(".") && !self.path.contains(&format!("/{}/", self.bucket)) {
self.path = format!("/{}{}", self.bucket, self.path);
}
} else if !self.path.contains(&format!("/{}/", self.bucket)) {
self.path = format!("{}{}{}", if self.bucket.len() > 0 {"/"} else {""}, self.bucket, self.path);
}
match self.endpoint.signature {
Signature::V2 => self.sign_v2(&creds),
Signature::V4 => self.sign_v4(&creds),
}
}
fn sign_v2(&mut self, creds: &AwsCredentials) {
let hostname = match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, self.region),
};
self.update_header("Host", &hostname);
let ct = match self.content_type {
Some(ref h) => h.to_string(),
None => String::from("application/octet-stream"),
};
let ep = self.endpoint().clone().user_agent.unwrap_or(DEFAULT_USER_AGENT.to_string());
self.update_header("User-Agent", &ep);
self.update_header("Content-Type", &ct);
if let Some(ref token) = *creds.token() {
self.update_header("X-Amz-Security-Token", token);
}
let date = now_utc().rfc822().to_string();
self.update_header("Date", &date);
self.canonical_query_string = build_canonical_query_string(&self.params);
let md5 = self.get_header("Content-MD5");
let content_type = self.get_header("Content-Type");
let date_str = self.get_header("Date");
let string_to_sign = format!("{}\n{}\n{}\n{}\n{}{}",
&self.method,
md5,
content_type,
date_str,
canonical_headers_v2(&self.headers),
canonical_resources_v2(&self.bucket, &self.path, self.endpoint.is_bucket_virtual));
match self.payload {
None => {
self.update_header("Content-Length", &format!("{}", 0));
},
Some(payload) => {
self.update_header("Content-Length", &format!("{}", payload.len()));
},
}
let signature = {
let hmac_pkey = PKey::hmac(creds.aws_secret_access_key().as_bytes()).unwrap();
let mut hmac = Signer::new(MessageDigest::sha1(), &hmac_pkey).unwrap();
let _ = hmac.write_all(string_to_sign.as_bytes());
hmac.finish().unwrap().to_base64(STANDARD)
};
self.update_header("Authorization", &format!("AWS {}:{}", creds.aws_access_key_id(), signature));
}
fn sign_v4(&mut self, creds: &AwsCredentials) {
let hostname = match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, self.region),
};
self.remove_header("host");
self.add_header("host", &hostname);
let ep = self.endpoint().clone().user_agent.unwrap_or(DEFAULT_USER_AGENT.to_string());
self.update_header("User-Agent", &ep);
if let Some(ref token) = *creds.token() {
self.remove_header("X-Amz-Security-Token");
self.add_header("X-Amz-Security-Token", token);
}
self.canonical_query_string = build_canonical_query_string(&self.params);
let date = now_utc();
self.remove_header("x-amz-date");
self.add_header("x-amz-date", &date.strftime("%Y%m%dT%H%M%SZ").unwrap().to_string());
let signed_headers = signed_headers(&self.headers);
self.canonical_uri = canonical_uri(&self.path);
let canonical_headers = canonical_headers(&self.headers);
let canonical_request: String;
match self.payload {
None => {
canonical_request = format!("{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
self.canonical_uri,
self.canonical_query_string,
canonical_headers,
signed_headers,
&to_hexdigest_from_string(""));
self.remove_header("x-amz-content-sha256");
self.add_header("x-amz-content-sha256", &to_hexdigest_from_string(""));
},
Some(payload) => {
canonical_request = format!("{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
self.canonical_uri,
self.canonical_query_string,
canonical_headers,
signed_headers,
&to_hexdigest_from_bytes(payload));
self.remove_header("x-amz-content-sha256");
self.add_header("x-amz-content-sha256", &to_hexdigest_from_bytes(payload));
self.remove_header("content-length");
self.add_header("content-length", &format!("{}", payload.len()));
},
}
self.remove_header("content-type");
let ct = match self.content_type {
Some(ref h) => h.to_string(),
None => String::from("application/octet-stream"),
};
self.add_header("content-type", &ct);
let hashed_canonical_request = to_hexdigest_from_string(&canonical_request);
let scope = format!("{}/{}/{}/aws4_request", date.strftime("%Y%m%d").unwrap(), self.region, &self.service);
let string_to_sign = string_to_sign_v4(date, &hashed_canonical_request, &scope);
let signing_key = signing_key(creds.aws_secret_access_key(), date, &self.region.to_string(), &self.service);
let signature = signature(&string_to_sign, signing_key);
let auth_header = format!("AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
&creds.aws_access_key_id(),
scope,
signed_headers,
signature);
self.remove_header("authorization");
self.add_header("authorization", &auth_header);
}
}
fn signature(string_to_sign: &str, signing_key: Vec<u8>) -> String {
let hmac_pkey = PKey::hmac(&signing_key).unwrap();
let mut hmac = Signer::new(MessageDigest::sha256(), &hmac_pkey).unwrap();
hmac.write_all(string_to_sign.as_bytes()).unwrap();
hmac.finish().unwrap().to_hex().to_string()
}
fn signing_key(secret: &str, date: Tm, region: &str, service: &str) -> Vec<u8> {
let hmac_pkey = PKey::hmac(format!("AWS4{}", secret).as_bytes()).unwrap();
let mut hmac = Signer::new(MessageDigest::sha256(), &hmac_pkey).unwrap();
hmac.write_all(date.strftime("%Y%m%d").unwrap().to_string().as_bytes()).unwrap();
let k_date = hmac.finish().unwrap();
let hmac_pkey = PKey::hmac(&k_date).unwrap();
let mut hmac = Signer::new(MessageDigest::sha256(), &hmac_pkey).unwrap();
hmac.write_all(region.as_bytes()).unwrap();
let k_region = hmac.finish().unwrap();
let hmac_pkey = PKey::hmac(&k_region).unwrap();
let mut hmac = Signer::new(MessageDigest::sha256(), &hmac_pkey).unwrap();
hmac.write_all(service.as_bytes()).unwrap();
let k_service = hmac.finish().unwrap();
let hmac_pkey = PKey::hmac(&k_service).unwrap();
let mut hmac = Signer::new(MessageDigest::sha256(), &hmac_pkey).unwrap();
hmac.write_all(b"aws4_request").unwrap();
hmac.finish().unwrap()
}
pub fn string_to_sign_v4(date: Tm, hashed_canonical_request: &str, scope: &str) -> String {
format!("AWS4-HMAC-SHA256\n{}\n{}\n{}",
date.strftime("%Y%m%dT%H%M%SZ").unwrap(),
scope,
hashed_canonical_request)
}
fn canonical_headers(headers: &BTreeMap<String, Vec<Vec<u8>>>) -> String {
let mut canonical = String::new();
for item in headers.iter() {
if skipped_headers(item.0) {
continue;
}
canonical.push_str(format!("{}:{}\n", item.0.to_ascii_lowercase(), canonical_values(item.1)).as_ref());
}
canonical
}
fn canonical_values(values: &[Vec<u8>]) -> String {
let mut st = String::new();
for v in values {
let s = str::from_utf8(v).unwrap();
if !st.is_empty() {
st.push(',')
}
if s.starts_with('\"') {
st.push_str(s);
} else {
st.push_str(s.replace(" ", " ").trim());
}
}
st
}
fn canonical_uri(path: &str) -> String {
match path {
"" => "/".to_string(),
_ => encode_uri(path),
}
}
#[allow(dead_code)]
fn canonical_resources(path: &str) -> String {
match path {
"" => "/".to_string(),
_ => encode_uri(path),
}
}
fn signed_headers(headers: &BTreeMap<String, Vec<Vec<u8>>>) -> String {
let mut signed = String::new();
for (key, _) in headers.iter() {
if !signed.is_empty() {
signed.push(';')
}
if skipped_headers(key) {
continue;
}
signed.push_str(&key.to_ascii_lowercase());
}
signed
}
fn skipped_headers(header: &str) -> bool {
["authorization", "content-length", "content-type"].contains(&header)
}
fn build_canonical_query_string(params: &Params) -> String {
if params.is_empty() {
return String::new();
}
let mut output = String::new();
for item in params.iter() {
if !output.is_empty() {
output.push_str("&");
}
output.push_str(&byte_serialize(item.0));
output.push_str("=");
output.push_str(&byte_serialize(item.1));
}
output
}
#[inline]
fn encode_uri(uri: &str) -> String {
utf8_percent_encode(uri, QUERY_ENCODE_SET).collect::<String>()
}
#[inline]
fn byte_serialize(input: &str) -> String {
utf8_percent_encode(input, DEFAULT_ENCODE_SET).collect::<String>()
}
fn to_hexdigest_from_string(val: &str) -> String {
let h = hash2(MessageDigest::sha256(), val.as_bytes()).unwrap();
h.to_hex().to_string()
}
fn to_hexdigest_from_bytes(val: &[u8]) -> String {
let h = hash2(MessageDigest::sha256(), val).unwrap();
h.to_hex().to_string()
}
fn build_hostname(service: &str, region: Region) -> String {
match service {
"iam" => {
match region {
Region::CnNorth1 => format!("{}.{}.amazonaws.com.cn", service, region),
_ => format!("{}.amazonaws.com", service),
}
},
"s3" => {
match region {
Region::UsEast1 => "s3.amazonaws.com".to_string(),
Region::CnNorth1 => format!("s3.{}.amazonaws.com.cn", region),
_ => format!("s3-{}.amazonaws.com", region),
}
},
_ => {
match region {
Region::CnNorth1 => format!("{}.{}.amazonaws.com.cn", service, region),
_ => format!("{}.{}.amazonaws.com", service, region),
}
},
}
}
fn canonical_headers_v2(headers: &BTreeMap<String, Vec<Vec<u8>>>) -> String {
let mut canonical = String::new();
for item in headers.iter() {
if skipped_headers(item.0) {
continue;
} else {
match item.0.to_ascii_lowercase().find("x-amz-") {
None => {},
_ => canonical.push_str(format!("{}:{}\n",
item.0.to_ascii_lowercase(),
canonical_values(item.1))
.as_ref()),
};
}
}
canonical
}
fn canonical_resources_v2(bucket: &str, path: &str, is_bucket_virtual: bool) -> String {
if bucket.to_string().contains(".") || !is_bucket_virtual {
encode_uri(path)
} else {
match bucket {
"" => {
match path {
"" => "/".to_string(),
_ => encode_uri(path),
}
},
_ => {
match path {
"" => format!("/{}/", bucket),
_ => encode_uri(&format!("/{}{}", bucket, path)),
}
},
}
}
}
#[cfg(test)]
mod tests {
use region::Region;
use super::SignedRequest;
use super::super::ProfileProvider;
use super::super::credential::ProvideAwsCredentials;
#[test]
fn get_hostname_none_present() {
let request = SignedRequest::new("POST", "sqs", Region::UsEast1, "/");
assert_eq!("sqs.us-east-1.amazonaws.com", request.hostname());
}
#[test]
fn get_hostname_happy_path() {
let mut request = SignedRequest::new("POST", "sqs", Region::UsEast1, "/");
request.set_hostname(Some("test-hostname".to_string()));
assert_eq!("test-hostname", request.hostname());
}
#[test]
fn path_percent_encoded() {
let provider = ProfileProvider::with_configuration("tests/sample-data/multiple_profile_credentials", "foo");
let mut request = SignedRequest::new("GET", "s3", Region::UsEast1, "/path with spaces");
request.sign(provider.credentials().as_ref().unwrap());
assert_eq!("/path%20with%20spaces", request.canonical_uri());
}
}