2020-04-24 17:46:52 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
|
2021-03-16 14:58:40 +00:00
|
|
|
use hmac::{Hmac, Mac, NewMac};
|
2020-04-24 17:46:52 +00:00
|
|
|
use hyper::{Body, Method, Request};
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
|
|
|
use garage_table::*;
|
2021-02-21 14:24:30 +00:00
|
|
|
use garage_util::data::{sha256sum, Hash};
|
2020-04-24 17:46:52 +00:00
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
|
|
|
use garage_model::key_table::*;
|
2020-04-24 17:46:52 +00:00
|
|
|
|
2020-04-28 10:18:14 +00:00
|
|
|
use crate::encoding::uri_encode;
|
2020-11-08 14:04:30 +00:00
|
|
|
use crate::error::*;
|
2020-04-28 10:18:14 +00:00
|
|
|
|
2020-04-24 17:46:52 +00:00
|
|
|
const SHORT_DATE: &str = "%Y%m%d";
|
|
|
|
const LONG_DATETIME: &str = "%Y%m%dT%H%M%SZ";
|
|
|
|
|
|
|
|
type HmacSha256 = Hmac<Sha256>;
|
|
|
|
|
2020-07-15 13:31:13 +00:00
|
|
|
pub async fn check_signature(
|
|
|
|
garage: &Garage,
|
|
|
|
request: &Request<Body>,
|
|
|
|
) -> Result<(Key, Option<Hash>), Error> {
|
2020-04-24 17:46:52 +00:00
|
|
|
let mut headers = HashMap::new();
|
|
|
|
for (key, val) in request.headers() {
|
|
|
|
headers.insert(key.to_string(), val.to_str()?.to_string());
|
|
|
|
}
|
|
|
|
if let Some(query) = request.uri().query() {
|
|
|
|
let query_pairs = url::form_urlencoded::parse(query.as_bytes());
|
|
|
|
for (key, val) in query_pairs {
|
|
|
|
headers.insert(key.to_lowercase(), val.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let authorization = if let Some(authorization) = headers.get("authorization") {
|
|
|
|
parse_authorization(authorization, &headers)?
|
|
|
|
} else {
|
|
|
|
parse_query_authorization(&headers)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let date = headers
|
|
|
|
.get("x-amz-date")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Missing X-Amz-Date field")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let date: NaiveDateTime = NaiveDateTime::parse_from_str(date, LONG_DATETIME)
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Invalid date")?
|
2020-04-24 17:46:52 +00:00
|
|
|
.into();
|
|
|
|
let date: DateTime<Utc> = DateTime::from_utc(date, Utc);
|
|
|
|
|
|
|
|
if Utc::now() - date > Duration::hours(24) {
|
|
|
|
return Err(Error::BadRequest(format!("Date is too old")));
|
|
|
|
}
|
|
|
|
|
|
|
|
let scope = format!(
|
|
|
|
"{}/{}/s3/aws4_request",
|
|
|
|
date.format(SHORT_DATE),
|
|
|
|
garage.config.s3_api.s3_region
|
|
|
|
);
|
|
|
|
if authorization.scope != scope {
|
|
|
|
return Err(Error::BadRequest(format!(
|
|
|
|
"Invalid scope in authorization field, expected: {}",
|
|
|
|
scope
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = garage
|
|
|
|
.key_table
|
|
|
|
.get(&EmptyKey, &authorization.key_id)
|
|
|
|
.await?
|
2020-11-20 20:15:24 +00:00
|
|
|
.filter(|k| !k.deleted.get())
|
2020-04-24 17:46:52 +00:00
|
|
|
.ok_or(Error::Forbidden(format!(
|
|
|
|
"No such key: {}",
|
|
|
|
authorization.key_id
|
|
|
|
)))?;
|
|
|
|
|
|
|
|
let canonical_request = canonical_request(
|
|
|
|
request.method(),
|
|
|
|
&request.uri().path().to_string(),
|
|
|
|
&canonical_query_string(&request.uri()),
|
|
|
|
&headers,
|
|
|
|
&authorization.signed_headers,
|
|
|
|
&authorization.content_sha256,
|
|
|
|
);
|
|
|
|
let string_to_sign = string_to_sign(&date, &scope, &canonical_request);
|
|
|
|
|
|
|
|
let mut hmac = signing_hmac(
|
|
|
|
&date,
|
|
|
|
&key.secret_key,
|
|
|
|
&garage.config.s3_api.s3_region,
|
|
|
|
"s3",
|
|
|
|
)
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_internal_error("Unable to build signing HMAC")?;
|
2021-03-16 14:58:40 +00:00
|
|
|
hmac.update(string_to_sign.as_bytes());
|
|
|
|
let signature = hex::encode(hmac.finalize().into_bytes());
|
2020-04-24 17:46:52 +00:00
|
|
|
|
|
|
|
if authorization.signature != signature {
|
2020-07-08 11:33:02 +00:00
|
|
|
trace!("Canonical request: ``{}``", canonical_request);
|
|
|
|
trace!("String to sign: ``{}``", string_to_sign);
|
|
|
|
trace!("Expected: {}, got: {}", signature, authorization.signature);
|
2020-04-24 17:46:52 +00:00
|
|
|
return Err(Error::Forbidden(format!("Invalid signature")));
|
|
|
|
}
|
|
|
|
|
2020-07-15 13:31:13 +00:00
|
|
|
let content_sha256 = if authorization.content_sha256 == "UNSIGNED-PAYLOAD" {
|
|
|
|
None
|
|
|
|
} else {
|
2020-11-08 14:04:30 +00:00
|
|
|
let bytes = hex::decode(authorization.content_sha256)
|
|
|
|
.ok_or_bad_request("Invalid content sha256 hash")?;
|
2021-03-12 20:52:19 +00:00
|
|
|
Some(
|
|
|
|
Hash::try_from(&bytes[..])
|
|
|
|
.ok_or(Error::BadRequest(format!("Invalid content sha256 hash")))?,
|
|
|
|
)
|
2020-07-15 13:31:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((key, content_sha256))
|
2020-04-24 17:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Authorization {
|
|
|
|
key_id: String,
|
|
|
|
scope: String,
|
|
|
|
signed_headers: String,
|
|
|
|
signature: String,
|
|
|
|
content_sha256: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_authorization(
|
|
|
|
authorization: &str,
|
|
|
|
headers: &HashMap<String, String>,
|
|
|
|
) -> Result<Authorization, Error> {
|
|
|
|
let first_space = authorization
|
|
|
|
.find(' ')
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Authorization field to short")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let (auth_kind, rest) = authorization.split_at(first_space);
|
|
|
|
|
|
|
|
if auth_kind != "AWS4-HMAC-SHA256" {
|
|
|
|
return Err(Error::BadRequest("Unsupported authorization method".into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut auth_params = HashMap::new();
|
|
|
|
for auth_part in rest.split(',') {
|
|
|
|
let auth_part = auth_part.trim();
|
2020-11-08 14:04:30 +00:00
|
|
|
let eq = auth_part
|
|
|
|
.find('=')
|
|
|
|
.ok_or_bad_request("Field without value in authorization header")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let (key, value) = auth_part.split_at(eq);
|
|
|
|
auth_params.insert(key.to_string(), value.trim_start_matches('=').to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let cred = auth_params
|
|
|
|
.get("Credential")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Could not find Credential in Authorization field")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let (key_id, scope) = parse_credential(cred)?;
|
|
|
|
|
|
|
|
let content_sha256 = headers
|
|
|
|
.get("x-amz-content-sha256")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Missing X-Amz-Content-Sha256 field")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
|
|
|
|
let auth = Authorization {
|
|
|
|
key_id,
|
|
|
|
scope,
|
|
|
|
signed_headers: auth_params
|
|
|
|
.get("SignedHeaders")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Could not find SignedHeaders in Authorization field")?
|
2020-04-24 17:46:52 +00:00
|
|
|
.to_string(),
|
|
|
|
signature: auth_params
|
|
|
|
.get("Signature")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("Could not find Signature in Authorization field")?
|
2020-04-24 17:46:52 +00:00
|
|
|
.to_string(),
|
|
|
|
content_sha256: content_sha256.to_string(),
|
|
|
|
};
|
|
|
|
Ok(auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_query_authorization(headers: &HashMap<String, String>) -> Result<Authorization, Error> {
|
|
|
|
let algo = headers
|
|
|
|
.get("x-amz-algorithm")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("X-Amz-Algorithm not found in query parameters")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
if algo != "AWS4-HMAC-SHA256" {
|
|
|
|
return Err(Error::BadRequest(format!(
|
|
|
|
"Unsupported authorization method"
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let cred = headers
|
|
|
|
.get("x-amz-credential")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("X-Amz-Credential not found in query parameters")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let (key_id, scope) = parse_credential(cred)?;
|
|
|
|
let signed_headers = headers
|
|
|
|
.get("x-amz-signedheaders")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("X-Amz-SignedHeaders not found in query parameters")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let signature = headers
|
|
|
|
.get("x-amz-signature")
|
2020-11-08 14:04:30 +00:00
|
|
|
.ok_or_bad_request("X-Amz-Signature not found in query parameters")?;
|
2020-07-15 13:31:13 +00:00
|
|
|
let content_sha256 = headers
|
|
|
|
.get("x-amz-content-sha256")
|
|
|
|
.map(|x| x.as_str())
|
|
|
|
.unwrap_or("UNSIGNED-PAYLOAD");
|
2020-04-24 17:46:52 +00:00
|
|
|
|
|
|
|
Ok(Authorization {
|
|
|
|
key_id,
|
|
|
|
scope,
|
|
|
|
signed_headers: signed_headers.to_string(),
|
|
|
|
signature: signature.to_string(),
|
2020-07-15 13:31:13 +00:00
|
|
|
content_sha256: content_sha256.to_string(),
|
2020-04-24 17:46:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_credential(cred: &str) -> Result<(String, String), Error> {
|
2020-11-08 14:04:30 +00:00
|
|
|
let first_slash = cred
|
|
|
|
.find('/')
|
|
|
|
.ok_or_bad_request("Credentials does not contain / in authorization field")?;
|
2020-04-24 17:46:52 +00:00
|
|
|
let (key_id, scope) = cred.split_at(first_slash);
|
|
|
|
Ok((
|
|
|
|
key_id.to_string(),
|
|
|
|
scope.trim_start_matches('/').to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn string_to_sign(datetime: &DateTime<Utc>, scope_string: &str, canonical_req: &str) -> String {
|
|
|
|
let mut hasher = Sha256::default();
|
2021-03-16 14:58:40 +00:00
|
|
|
hasher.update(canonical_req.as_bytes());
|
2020-04-24 17:46:52 +00:00
|
|
|
[
|
|
|
|
"AWS4-HMAC-SHA256",
|
|
|
|
&datetime.format(LONG_DATETIME).to_string(),
|
|
|
|
scope_string,
|
2021-03-16 14:58:40 +00:00
|
|
|
&hex::encode(hasher.finalize().as_slice()),
|
2020-04-24 17:46:52 +00:00
|
|
|
]
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signing_hmac(
|
|
|
|
datetime: &DateTime<Utc>,
|
|
|
|
secret_key: &str,
|
|
|
|
region: &str,
|
|
|
|
service: &str,
|
|
|
|
) -> Result<HmacSha256, crypto_mac::InvalidKeyLength> {
|
|
|
|
let secret = String::from("AWS4") + secret_key;
|
|
|
|
let mut date_hmac = HmacSha256::new_varkey(secret.as_bytes())?;
|
2021-03-16 14:58:40 +00:00
|
|
|
date_hmac.update(datetime.format(SHORT_DATE).to_string().as_bytes());
|
|
|
|
let mut region_hmac = HmacSha256::new_varkey(&date_hmac.finalize().into_bytes())?;
|
|
|
|
region_hmac.update(region.as_bytes());
|
|
|
|
let mut service_hmac = HmacSha256::new_varkey(®ion_hmac.finalize().into_bytes())?;
|
|
|
|
service_hmac.update(service.as_bytes());
|
|
|
|
let mut signing_hmac = HmacSha256::new_varkey(&service_hmac.finalize().into_bytes())?;
|
|
|
|
signing_hmac.update(b"aws4_request");
|
|
|
|
let hmac = HmacSha256::new_varkey(&signing_hmac.finalize().into_bytes())?;
|
2020-04-24 17:46:52 +00:00
|
|
|
Ok(hmac)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn canonical_request(
|
|
|
|
method: &Method,
|
|
|
|
url_path: &str,
|
|
|
|
canonical_query_string: &str,
|
|
|
|
headers: &HashMap<String, String>,
|
|
|
|
signed_headers: &str,
|
|
|
|
content_sha256: &str,
|
|
|
|
) -> String {
|
|
|
|
[
|
|
|
|
method.as_str(),
|
|
|
|
url_path,
|
|
|
|
canonical_query_string,
|
|
|
|
&canonical_header_string(&headers, signed_headers),
|
|
|
|
"",
|
|
|
|
signed_headers,
|
|
|
|
content_sha256,
|
|
|
|
]
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn canonical_header_string(headers: &HashMap<String, String>, signed_headers: &str) -> String {
|
|
|
|
let signed_headers_vec = signed_headers.split(';').collect::<Vec<_>>();
|
|
|
|
let mut items = headers
|
|
|
|
.iter()
|
|
|
|
.filter(|(key, _)| signed_headers_vec.contains(&key.as_str()))
|
|
|
|
.map(|(key, value)| key.to_lowercase() + ":" + value.trim())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
items.sort();
|
|
|
|
items.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn canonical_query_string(uri: &hyper::Uri) -> String {
|
|
|
|
if let Some(query) = uri.query() {
|
|
|
|
let query_pairs = url::form_urlencoded::parse(query.as_bytes());
|
|
|
|
let mut items = query_pairs
|
|
|
|
.filter(|(key, _)| key != "X-Amz-Signature")
|
|
|
|
.map(|(key, value)| uri_encode(&key, true) + "=" + &uri_encode(&value, true))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
items.sort();
|
|
|
|
items.join("&")
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 23:13:07 +00:00
|
|
|
|
|
|
|
pub fn verify_signed_content(content_sha256: Option<Hash>, body: &[u8]) -> Result<(), Error> {
|
2021-02-23 17:46:25 +00:00
|
|
|
let expected_sha256 =
|
|
|
|
content_sha256.ok_or_bad_request("Request content hash not signed, aborting.")?;
|
2021-02-21 14:24:30 +00:00
|
|
|
if expected_sha256 != sha256sum(body) {
|
2021-02-23 17:46:25 +00:00
|
|
|
return Err(Error::BadRequest(format!(
|
|
|
|
"Request content hash does not match signed hash"
|
|
|
|
)));
|
2021-02-19 23:13:07 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|