[fix-presigned] split presigned/normal signature verification
This commit is contained in:
parent
911a83ea7d
commit
a8cb8e8a8b
4 changed files with 417 additions and 268 deletions
|
@ -21,7 +21,7 @@ use crate::s3::cors::*;
|
||||||
use crate::s3::error::*;
|
use crate::s3::error::*;
|
||||||
use crate::s3::put::{get_headers, save_stream};
|
use crate::s3::put::{get_headers, save_stream};
|
||||||
use crate::s3::xml as s3_xml;
|
use crate::s3::xml as s3_xml;
|
||||||
use crate::signature::payload::{parse_date, verify_v4};
|
use crate::signature::payload::{verify_v4, Authorization};
|
||||||
|
|
||||||
pub async fn handle_post_object(
|
pub async fn handle_post_object(
|
||||||
garage: Arc<Garage>,
|
garage: Arc<Garage>,
|
||||||
|
@ -88,22 +88,11 @@ pub async fn handle_post_object(
|
||||||
.get("key")
|
.get("key")
|
||||||
.ok_or_bad_request("No key was provided")?
|
.ok_or_bad_request("No key was provided")?
|
||||||
.to_str()?;
|
.to_str()?;
|
||||||
let credential = params
|
|
||||||
.get("x-amz-credential")
|
|
||||||
.ok_or_else(|| Error::forbidden("Garage does not support anonymous access yet"))?
|
|
||||||
.to_str()?;
|
|
||||||
let policy = params
|
let policy = params
|
||||||
.get("policy")
|
.get("policy")
|
||||||
.ok_or_bad_request("No policy was provided")?
|
.ok_or_bad_request("No policy was provided")?
|
||||||
.to_str()?;
|
.to_str()?;
|
||||||
let signature = params
|
let authorization = Authorization::parse_form(¶ms)?;
|
||||||
.get("x-amz-signature")
|
|
||||||
.ok_or_bad_request("No signature was provided")?
|
|
||||||
.to_str()?;
|
|
||||||
let date = params
|
|
||||||
.get("x-amz-date")
|
|
||||||
.ok_or_bad_request("No date was provided")?
|
|
||||||
.to_str()?;
|
|
||||||
|
|
||||||
let key = if key.contains("${filename}") {
|
let key = if key.contains("${filename}") {
|
||||||
// if no filename is provided, don't replace. This matches the behavior of AWS.
|
// if no filename is provided, don't replace. This matches the behavior of AWS.
|
||||||
|
@ -116,16 +105,7 @@ pub async fn handle_post_object(
|
||||||
key.to_owned()
|
key.to_owned()
|
||||||
};
|
};
|
||||||
|
|
||||||
let date = parse_date(date)?;
|
let api_key = verify_v4(&garage, "s3", &authorization, policy.as_bytes()).await?;
|
||||||
let api_key = verify_v4(
|
|
||||||
&garage,
|
|
||||||
"s3",
|
|
||||||
credential,
|
|
||||||
&date,
|
|
||||||
signature,
|
|
||||||
policy.as_bytes(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let bucket_id = garage
|
let bucket_id = garage
|
||||||
.bucket_helper()
|
.bucket_helper()
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use chrono::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};
|
use chrono::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};
|
||||||
use hmac::Mac;
|
use hmac::Mac;
|
||||||
|
use hyper::header::{HeaderMap, HeaderName, AUTHORIZATION, CONTENT_TYPE, HOST};
|
||||||
use hyper::{body::Incoming as IncomingBody, Method, Request};
|
use hyper::{body::Incoming as IncomingBody, Method, Request};
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
@ -17,66 +19,87 @@ use super::{compute_scope, signing_hmac};
|
||||||
use crate::encoding::uri_encode;
|
use crate::encoding::uri_encode;
|
||||||
use crate::signature::error::*;
|
use crate::signature::error::*;
|
||||||
|
|
||||||
|
pub const X_AMZ_ALGORITHM: HeaderName = HeaderName::from_static("x-amz-algorithm");
|
||||||
|
pub const X_AMZ_CREDENTIAL: HeaderName = HeaderName::from_static("x-amz-credential");
|
||||||
|
pub const X_AMZ_DATE: HeaderName = HeaderName::from_static("x-amz-date");
|
||||||
|
pub const X_AMZ_EXPIRES: HeaderName = HeaderName::from_static("x-amz-expires");
|
||||||
|
pub const X_AMZ_SIGNEDHEADERS: HeaderName = HeaderName::from_static("x-amz-signedheaders");
|
||||||
|
pub const X_AMZ_SIGNATURE: HeaderName = HeaderName::from_static("x-amz-signature");
|
||||||
|
pub const X_AMZ_CONTENT_SH256: HeaderName = HeaderName::from_static("x-amz-content-sha256");
|
||||||
|
|
||||||
|
pub const AWS4_HMAC_SHA256: &str = "AWS4-HMAC-SHA256";
|
||||||
|
pub const UNSIGNED_PAYLOAD: &str = "UNSIGNED-PAYLOAD";
|
||||||
|
pub const STREAMING_AWS4_HMAC_SHA256_PAYLOAD: &str = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
|
||||||
|
|
||||||
|
pub type QueryMap = HashMap<String, String>;
|
||||||
|
|
||||||
pub async fn check_payload_signature(
|
pub async fn check_payload_signature(
|
||||||
garage: &Garage,
|
garage: &Garage,
|
||||||
service: &'static str,
|
service: &'static str,
|
||||||
request: &Request<IncomingBody>,
|
request: &Request<IncomingBody>,
|
||||||
) -> Result<(Option<Key>, Option<Hash>), Error> {
|
) -> Result<(Option<Key>, Option<Hash>), Error> {
|
||||||
let mut headers = HashMap::new();
|
let query = parse_query_map(request.uri())?;
|
||||||
for (key, val) in request.headers() {
|
|
||||||
headers.insert(key.to_string(), val.to_str()?.to_string());
|
let res = if query.contains_key(X_AMZ_ALGORITHM.as_str()) {
|
||||||
}
|
check_presigned_signature(garage, service, request, query).await
|
||||||
if let Some(query) = request.uri().query() {
|
} else {
|
||||||
let query_pairs = url::form_urlencoded::parse(query.as_bytes());
|
check_standard_signature(garage, service, request, query).await
|
||||||
for (key, val) in query_pairs {
|
};
|
||||||
headers.insert(key.to_lowercase(), val.to_string());
|
|
||||||
}
|
if let Err(e) = &res {
|
||||||
|
error!("ERROR IN SIGNATURE\n{:?}\n{}", request, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
let authorization = if let Some(authorization) = headers.get("authorization") {
|
res
|
||||||
parse_authorization(authorization, &headers)?
|
}
|
||||||
} else if let Some(algorithm) = headers.get("x-amz-algorithm") {
|
|
||||||
parse_query_authorization(algorithm, &headers)?
|
async fn check_standard_signature(
|
||||||
} else {
|
garage: &Garage,
|
||||||
let content_sha256 = headers.get("x-amz-content-sha256");
|
service: &'static str,
|
||||||
if let Some(content_sha256) = content_sha256.filter(|c| "UNSIGNED-PAYLOAD" != c.as_str()) {
|
request: &Request<IncomingBody>,
|
||||||
let sha256 = hex::decode(content_sha256)
|
query: QueryMap,
|
||||||
.ok()
|
) -> Result<(Option<Key>, Option<Hash>), Error> {
|
||||||
.and_then(|bytes| Hash::try_from(&bytes))
|
let authorization = Authorization::parse(request.headers())?;
|
||||||
.ok_or_bad_request("Invalid content sha256 hash")?;
|
|
||||||
return Ok((None, Some(sha256)));
|
// Verify that all necessary request headers are signed
|
||||||
} else {
|
let signed_headers = split_signed_headers(&authorization)?;
|
||||||
return Ok((None, None));
|
for (name, _) in request.headers().iter() {
|
||||||
|
if name.as_str().starts_with("x-amz-") || name == CONTENT_TYPE {
|
||||||
|
if !signed_headers.contains(name) {
|
||||||
|
return Err(Error::bad_request(format!(
|
||||||
|
"Header `{}` should be signed",
|
||||||
|
name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !signed_headers.contains(&HOST) {
|
||||||
|
return Err(Error::bad_request("Header `Host` should be signed"));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let canonical_request = canonical_request(
|
let canonical_request = canonical_request(
|
||||||
service,
|
service,
|
||||||
request.method(),
|
request.method(),
|
||||||
request.uri(),
|
request.uri().path(),
|
||||||
&headers,
|
&query,
|
||||||
&authorization.signed_headers,
|
request.headers(),
|
||||||
|
signed_headers,
|
||||||
&authorization.content_sha256,
|
&authorization.content_sha256,
|
||||||
|
)?;
|
||||||
|
let string_to_sign = string_to_sign(
|
||||||
|
&authorization.date,
|
||||||
|
&authorization.scope,
|
||||||
|
&canonical_request,
|
||||||
);
|
);
|
||||||
let (_, scope) = parse_credential(&authorization.credential)?;
|
|
||||||
let string_to_sign = string_to_sign(&authorization.date, &scope, &canonical_request);
|
|
||||||
|
|
||||||
trace!("canonical request:\n{}", canonical_request);
|
trace!("canonical request:\n{}", canonical_request);
|
||||||
trace!("string to sign:\n{}", string_to_sign);
|
trace!("string to sign:\n{}", string_to_sign);
|
||||||
|
|
||||||
let key = verify_v4(
|
let key = verify_v4(garage, service, &authorization, string_to_sign.as_bytes()).await?;
|
||||||
garage,
|
|
||||||
service,
|
|
||||||
&authorization.credential,
|
|
||||||
&authorization.date,
|
|
||||||
&authorization.signature,
|
|
||||||
string_to_sign.as_bytes(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let content_sha256 = if authorization.content_sha256 == "UNSIGNED-PAYLOAD" {
|
let content_sha256 = if authorization.content_sha256 == UNSIGNED_PAYLOAD {
|
||||||
None
|
None
|
||||||
} else if authorization.content_sha256 == "STREAMING-AWS4-HMAC-SHA256-PAYLOAD" {
|
} else if authorization.content_sha256 == STREAMING_AWS4_HMAC_SHA256_PAYLOAD {
|
||||||
let bytes = hex::decode(authorization.signature).ok_or_bad_request("Invalid signature")?;
|
let bytes = hex::decode(authorization.signature).ok_or_bad_request("Invalid signature")?;
|
||||||
Some(Hash::try_from(&bytes).ok_or_bad_request("Invalid signature")?)
|
Some(Hash::try_from(&bytes).ok_or_bad_request("Invalid signature")?)
|
||||||
} else {
|
} else {
|
||||||
|
@ -88,142 +111,86 @@ pub async fn check_payload_signature(
|
||||||
Ok((Some(key), content_sha256))
|
Ok((Some(key), content_sha256))
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Authorization {
|
async fn check_presigned_signature(
|
||||||
credential: String,
|
garage: &Garage,
|
||||||
signed_headers: String,
|
service: &'static str,
|
||||||
signature: String,
|
request: &Request<IncomingBody>,
|
||||||
content_sha256: String,
|
mut query: QueryMap,
|
||||||
date: DateTime<Utc>,
|
) -> Result<(Option<Key>, Option<Hash>), Error> {
|
||||||
|
let algorithm = query.get(X_AMZ_ALGORITHM.as_str()).unwrap();
|
||||||
|
let authorization = Authorization::parse_presigned(algorithm, &query)?;
|
||||||
|
|
||||||
|
// Check that all mandatory signed headers are included
|
||||||
|
let signed_headers = split_signed_headers(&authorization)?;
|
||||||
|
for (name, _) in request.headers().iter() {
|
||||||
|
if name.as_str().starts_with("x-amz-") {
|
||||||
|
if !signed_headers.contains(name) {
|
||||||
|
return Err(Error::bad_request(format!(
|
||||||
|
"Header `{}` should be signed",
|
||||||
|
name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !signed_headers.contains(&HOST) {
|
||||||
|
return Err(Error::bad_request("Header `Host` should be signed"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_authorization(
|
query.remove(X_AMZ_SIGNATURE.as_str());
|
||||||
authorization: &str,
|
let canonical_request = canonical_request(
|
||||||
headers: &HashMap<String, String>,
|
service,
|
||||||
) -> Result<Authorization, Error> {
|
request.method(),
|
||||||
let first_space = authorization
|
request.uri().path(),
|
||||||
.find(' ')
|
&query,
|
||||||
.ok_or_bad_request("Authorization field to short")?;
|
request.headers(),
|
||||||
let (auth_kind, rest) = authorization.split_at(first_space);
|
signed_headers,
|
||||||
|
&authorization.content_sha256,
|
||||||
|
)?;
|
||||||
|
let string_to_sign = string_to_sign(
|
||||||
|
&authorization.date,
|
||||||
|
&authorization.scope,
|
||||||
|
&canonical_request,
|
||||||
|
);
|
||||||
|
|
||||||
if auth_kind != "AWS4-HMAC-SHA256" {
|
trace!("canonical request:\n{}", canonical_request);
|
||||||
return Err(Error::bad_request("Unsupported authorization method"));
|
trace!("string to sign:\n{}", string_to_sign);
|
||||||
|
|
||||||
|
let key = verify_v4(garage, service, &authorization, string_to_sign.as_bytes()).await?;
|
||||||
|
|
||||||
|
Ok((Some(key), None))
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut auth_params = HashMap::new();
|
pub fn parse_query_map(uri: &http::uri::Uri) -> Result<QueryMap, Error> {
|
||||||
for auth_part in rest.split(',') {
|
let mut query = QueryMap::new();
|
||||||
let auth_part = auth_part.trim();
|
if let Some(query_str) = uri.query() {
|
||||||
let eq = auth_part
|
let query_pairs = url::form_urlencoded::parse(query_str.as_bytes());
|
||||||
.find('=')
|
for (key, val) in query_pairs {
|
||||||
.ok_or_bad_request("Field without value in authorization header")?;
|
if query.insert(key.to_string(), val.into_owned()).is_some() {
|
||||||
let (key, value) = auth_part.split_at(eq);
|
return Err(Error::bad_request(format!(
|
||||||
auth_params.insert(key.to_string(), value.trim_start_matches('=').to_string());
|
"duplicate query parameter: `{}`",
|
||||||
|
key
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
let cred = auth_params
|
fn split_signed_headers(authorization: &Authorization) -> Result<Vec<HeaderName>, Error> {
|
||||||
.get("Credential")
|
let signed_headers = authorization
|
||||||
.ok_or_bad_request("Could not find Credential in Authorization field")?;
|
.signed_headers
|
||||||
|
.split(';')
|
||||||
let content_sha256 = headers
|
.map(HeaderName::try_from)
|
||||||
.get("x-amz-content-sha256")
|
.collect::<Result<Vec<HeaderName>, _>>()
|
||||||
.ok_or_bad_request("Missing X-Amz-Content-Sha256 field")?;
|
.ok_or_bad_request("invalid header name")?;
|
||||||
|
Ok(signed_headers)
|
||||||
let date = headers
|
|
||||||
.get("x-amz-date")
|
|
||||||
.ok_or_bad_request("Missing X-Amz-Date field")
|
|
||||||
.map_err(Error::from)
|
|
||||||
.and_then(|d| parse_date(d))?;
|
|
||||||
|
|
||||||
if Utc::now() - date > Duration::hours(24) {
|
|
||||||
return Err(Error::bad_request("Date is too old".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let auth = Authorization {
|
|
||||||
credential: cred.to_string(),
|
|
||||||
signed_headers: auth_params
|
|
||||||
.get("SignedHeaders")
|
|
||||||
.ok_or_bad_request("Could not find SignedHeaders in Authorization field")?
|
|
||||||
.to_string(),
|
|
||||||
signature: auth_params
|
|
||||||
.get("Signature")
|
|
||||||
.ok_or_bad_request("Could not find Signature in Authorization field")?
|
|
||||||
.to_string(),
|
|
||||||
content_sha256: content_sha256.to_string(),
|
|
||||||
date,
|
|
||||||
};
|
|
||||||
Ok(auth)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_query_authorization(
|
|
||||||
algorithm: &str,
|
|
||||||
headers: &HashMap<String, String>,
|
|
||||||
) -> Result<Authorization, Error> {
|
|
||||||
if algorithm != "AWS4-HMAC-SHA256" {
|
|
||||||
return Err(Error::bad_request(
|
|
||||||
"Unsupported authorization method".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let cred = headers
|
|
||||||
.get("x-amz-credential")
|
|
||||||
.ok_or_bad_request("X-Amz-Credential not found in query parameters")?;
|
|
||||||
let signed_headers = headers
|
|
||||||
.get("x-amz-signedheaders")
|
|
||||||
.ok_or_bad_request("X-Amz-SignedHeaders not found in query parameters")?;
|
|
||||||
let signature = headers
|
|
||||||
.get("x-amz-signature")
|
|
||||||
.ok_or_bad_request("X-Amz-Signature not found in query parameters")?;
|
|
||||||
let content_sha256 = headers
|
|
||||||
.get("x-amz-content-sha256")
|
|
||||||
.map(|x| x.as_str())
|
|
||||||
.unwrap_or("UNSIGNED-PAYLOAD");
|
|
||||||
|
|
||||||
let duration = headers
|
|
||||||
.get("x-amz-expires")
|
|
||||||
.ok_or_bad_request("X-Amz-Expires not found in query parameters")?
|
|
||||||
.parse()
|
|
||||||
.map_err(|_| Error::bad_request("X-Amz-Expires is not a number".to_string()))?;
|
|
||||||
|
|
||||||
if duration > 7 * 24 * 3600 {
|
|
||||||
return Err(Error::bad_request(
|
|
||||||
"X-Amz-Expires may not exceed a week".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let date = headers
|
|
||||||
.get("x-amz-date")
|
|
||||||
.ok_or_bad_request("Missing X-Amz-Date field")
|
|
||||||
.map_err(Error::from)
|
|
||||||
.and_then(|d| parse_date(d))?;
|
|
||||||
|
|
||||||
if Utc::now() - date > Duration::seconds(duration) {
|
|
||||||
return Err(Error::bad_request("Date is too old".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Authorization {
|
|
||||||
credential: cred.to_string(),
|
|
||||||
signed_headers: signed_headers.to_string(),
|
|
||||||
signature: signature.to_string(),
|
|
||||||
content_sha256: content_sha256.to_string(),
|
|
||||||
date,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_credential(cred: &str) -> Result<(String, String), Error> {
|
|
||||||
let first_slash = cred
|
|
||||||
.find('/')
|
|
||||||
.ok_or_bad_request("Credentials does not contain '/' in authorization field")?;
|
|
||||||
let (key_id, scope) = cred.split_at(first_slash);
|
|
||||||
Ok((
|
|
||||||
key_id.to_string(),
|
|
||||||
scope.trim_start_matches('/').to_string(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn string_to_sign(datetime: &DateTime<Utc>, scope_string: &str, canonical_req: &str) -> String {
|
pub fn string_to_sign(datetime: &DateTime<Utc>, scope_string: &str, canonical_req: &str) -> String {
|
||||||
let mut hasher = Sha256::default();
|
let mut hasher = Sha256::default();
|
||||||
hasher.update(canonical_req.as_bytes());
|
hasher.update(canonical_req.as_bytes());
|
||||||
[
|
[
|
||||||
"AWS4-HMAC-SHA256",
|
AWS4_HMAC_SHA256,
|
||||||
&datetime.format(LONG_DATETIME).to_string(),
|
&datetime.format(LONG_DATETIME).to_string(),
|
||||||
scope_string,
|
scope_string,
|
||||||
&hex::encode(hasher.finalize().as_slice()),
|
&hex::encode(hasher.finalize().as_slice()),
|
||||||
|
@ -234,11 +201,12 @@ pub fn string_to_sign(datetime: &DateTime<Utc>, scope_string: &str, canonical_re
|
||||||
pub fn canonical_request(
|
pub fn canonical_request(
|
||||||
service: &'static str,
|
service: &'static str,
|
||||||
method: &Method,
|
method: &Method,
|
||||||
uri: &hyper::Uri,
|
canonical_uri: &str,
|
||||||
headers: &HashMap<String, String>,
|
query: &QueryMap,
|
||||||
signed_headers: &str,
|
headers: &HeaderMap,
|
||||||
|
mut signed_headers: Vec<HeaderName>,
|
||||||
content_sha256: &str,
|
content_sha256: &str,
|
||||||
) -> String {
|
) -> Result<String, Error> {
|
||||||
// There seems to be evidence that in AWSv4 signatures, the path component is url-encoded
|
// There seems to be evidence that in AWSv4 signatures, the path component is url-encoded
|
||||||
// a second time when building the canonical request, as specified in this documentation page:
|
// a second time when building the canonical request, as specified in this documentation page:
|
||||||
// -> https://docs.aws.amazon.com/rolesanywhere/latest/userguide/authentication-sign-process.html
|
// -> https://docs.aws.amazon.com/rolesanywhere/latest/userguide/authentication-sign-process.html
|
||||||
|
@ -268,49 +236,51 @@ pub fn canonical_request(
|
||||||
// it mentions it in the comments (same link to the souce code as above).
|
// it mentions it in the comments (same link to the souce code as above).
|
||||||
// We make the explicit choice of NOT normalizing paths in the K2V API because doing so
|
// We make the explicit choice of NOT normalizing paths in the K2V API because doing so
|
||||||
// would make non-normalized paths invalid K2V partition keys, and we don't want that.
|
// would make non-normalized paths invalid K2V partition keys, and we don't want that.
|
||||||
let path: std::borrow::Cow<str> = if service != "s3" {
|
let canonical_uri: std::borrow::Cow<str> = if service != "s3" {
|
||||||
uri_encode(uri.path(), false).into()
|
uri_encode(canonical_uri, false).into()
|
||||||
} else {
|
} else {
|
||||||
uri.path().into()
|
canonical_uri.into()
|
||||||
};
|
};
|
||||||
[
|
|
||||||
method.as_str(),
|
|
||||||
&path,
|
|
||||||
&canonical_query_string(uri),
|
|
||||||
&canonical_header_string(headers, signed_headers),
|
|
||||||
"",
|
|
||||||
signed_headers,
|
|
||||||
content_sha256,
|
|
||||||
]
|
|
||||||
.join("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn canonical_header_string(headers: &HashMap<String, String>, signed_headers: &str) -> String {
|
// Canonical query string from passed HeaderMap
|
||||||
let signed_headers_vec = signed_headers.split(';').collect::<Vec<_>>();
|
let canonical_query_string = {
|
||||||
let mut items = headers
|
let mut items = Vec::with_capacity(query.len());
|
||||||
.iter()
|
for (key, value) in query.iter() {
|
||||||
.filter(|(key, _)| signed_headers_vec.contains(&key.as_str()))
|
items.push(uri_encode(&key, true) + "=" + &uri_encode(&value, true));
|
||||||
.collect::<Vec<_>>();
|
|
||||||
items.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
|
|
||||||
items
|
|
||||||
.iter()
|
|
||||||
.map(|(key, value)| key.to_lowercase() + ":" + value.trim())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.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.sort();
|
||||||
items.join("&")
|
items.join("&")
|
||||||
} else {
|
};
|
||||||
"".to_string()
|
|
||||||
|
// Canonical header string calculated from signed headers
|
||||||
|
signed_headers.sort_by(|h1, h2| h1.as_str().cmp(h2.as_str()));
|
||||||
|
let canonical_header_string = {
|
||||||
|
let mut items = Vec::with_capacity(signed_headers.len());
|
||||||
|
for name in signed_headers.iter() {
|
||||||
|
let value = headers
|
||||||
|
.get(name)
|
||||||
|
.ok_or_bad_request(format!("signed header `{}` is not present", name))?
|
||||||
|
.to_str()?;
|
||||||
|
items.push((name, value));
|
||||||
}
|
}
|
||||||
|
items
|
||||||
|
.iter()
|
||||||
|
.map(|(key, value)| format!("{}:{}", key.as_str(), value.trim()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
|
};
|
||||||
|
let signed_headers = signed_headers.join(";");
|
||||||
|
|
||||||
|
let list = [
|
||||||
|
method.as_str(),
|
||||||
|
&canonical_uri,
|
||||||
|
&canonical_query_string,
|
||||||
|
&canonical_header_string,
|
||||||
|
"",
|
||||||
|
&signed_headers,
|
||||||
|
content_sha256,
|
||||||
|
];
|
||||||
|
Ok(list.join("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_date(date: &str) -> Result<DateTime<Utc>, Error> {
|
pub fn parse_date(date: &str) -> Result<DateTime<Utc>, Error> {
|
||||||
|
@ -322,28 +292,24 @@ pub fn parse_date(date: &str) -> Result<DateTime<Utc>, Error> {
|
||||||
pub async fn verify_v4(
|
pub async fn verify_v4(
|
||||||
garage: &Garage,
|
garage: &Garage,
|
||||||
service: &str,
|
service: &str,
|
||||||
credential: &str,
|
auth: &Authorization,
|
||||||
date: &DateTime<Utc>,
|
|
||||||
signature: &str,
|
|
||||||
payload: &[u8],
|
payload: &[u8],
|
||||||
) -> Result<Key, Error> {
|
) -> Result<Key, Error> {
|
||||||
let (key_id, scope) = parse_credential(credential)?;
|
let scope_expected = compute_scope(&auth.date, &garage.config.s3_api.s3_region, service);
|
||||||
|
if auth.scope != scope_expected {
|
||||||
let scope_expected = compute_scope(date, &garage.config.s3_api.s3_region, service);
|
return Err(Error::AuthorizationHeaderMalformed(auth.scope.to_string()));
|
||||||
if scope != scope_expected {
|
|
||||||
return Err(Error::AuthorizationHeaderMalformed(scope.to_string()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let key = garage
|
let key = garage
|
||||||
.key_table
|
.key_table
|
||||||
.get(&EmptyKey, &key_id)
|
.get(&EmptyKey, &auth.key_id)
|
||||||
.await?
|
.await?
|
||||||
.filter(|k| !k.state.is_deleted())
|
.filter(|k| !k.state.is_deleted())
|
||||||
.ok_or_else(|| Error::forbidden(format!("No such key: {}", &key_id)))?;
|
.ok_or_else(|| Error::forbidden(format!("No such key: {}", &auth.key_id)))?;
|
||||||
let key_p = key.params().unwrap();
|
let key_p = key.params().unwrap();
|
||||||
|
|
||||||
let mut hmac = signing_hmac(
|
let mut hmac = signing_hmac(
|
||||||
date,
|
&auth.date,
|
||||||
&key_p.secret_key,
|
&key_p.secret_key,
|
||||||
&garage.config.s3_api.s3_region,
|
&garage.config.s3_api.s3_region,
|
||||||
service,
|
service,
|
||||||
|
@ -351,9 +317,177 @@ pub async fn verify_v4(
|
||||||
.ok_or_internal_error("Unable to build signing HMAC")?;
|
.ok_or_internal_error("Unable to build signing HMAC")?;
|
||||||
hmac.update(payload);
|
hmac.update(payload);
|
||||||
let our_signature = hex::encode(hmac.finalize().into_bytes());
|
let our_signature = hex::encode(hmac.finalize().into_bytes());
|
||||||
if signature != our_signature {
|
if auth.signature != our_signature {
|
||||||
return Err(Error::forbidden("Invalid signature".to_string()));
|
return Err(Error::forbidden("Invalid signature".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============ Authorization header, or X-Amz-* query params =========
|
||||||
|
|
||||||
|
pub struct Authorization {
|
||||||
|
key_id: String,
|
||||||
|
scope: String,
|
||||||
|
signed_headers: String,
|
||||||
|
signature: String,
|
||||||
|
content_sha256: String,
|
||||||
|
date: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Authorization {
|
||||||
|
fn parse(headers: &HeaderMap) -> Result<Self, Error> {
|
||||||
|
let authorization = headers
|
||||||
|
.get(AUTHORIZATION)
|
||||||
|
.ok_or_bad_request("Missing authorization header")?
|
||||||
|
.to_str()?;
|
||||||
|
|
||||||
|
let (auth_kind, rest) = authorization
|
||||||
|
.split_once(' ')
|
||||||
|
.ok_or_bad_request("Authorization field to short")?;
|
||||||
|
|
||||||
|
if auth_kind != AWS4_HMAC_SHA256 {
|
||||||
|
return Err(Error::bad_request("Unsupported authorization method"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut auth_params = HashMap::new();
|
||||||
|
for auth_part in rest.split(',') {
|
||||||
|
let auth_part = auth_part.trim();
|
||||||
|
let eq = auth_part
|
||||||
|
.find('=')
|
||||||
|
.ok_or_bad_request("Field without value in authorization header")?;
|
||||||
|
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")
|
||||||
|
.ok_or_bad_request("Could not find Credential in Authorization field")?;
|
||||||
|
let signed_headers = auth_params
|
||||||
|
.get("SignedHeaders")
|
||||||
|
.ok_or_bad_request("Could not find SignedHeaders in Authorization field")?
|
||||||
|
.to_string();
|
||||||
|
let signature = auth_params
|
||||||
|
.get("Signature")
|
||||||
|
.ok_or_bad_request("Could not find Signature in Authorization field")?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let content_sha256 = headers
|
||||||
|
.get(X_AMZ_CONTENT_SH256)
|
||||||
|
.ok_or_bad_request("Missing X-Amz-Content-Sha256 field")?;
|
||||||
|
|
||||||
|
let date = headers
|
||||||
|
.get(X_AMZ_DATE)
|
||||||
|
.ok_or_bad_request("Missing X-Amz-Date field")
|
||||||
|
.map_err(Error::from)?
|
||||||
|
.to_str()?;
|
||||||
|
let date = parse_date(date)?;
|
||||||
|
|
||||||
|
if Utc::now() - date > Duration::hours(24) {
|
||||||
|
return Err(Error::bad_request("Date is too old".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (key_id, scope) = parse_credential(cred)?;
|
||||||
|
let auth = Authorization {
|
||||||
|
key_id,
|
||||||
|
scope,
|
||||||
|
signed_headers,
|
||||||
|
signature,
|
||||||
|
content_sha256: content_sha256.to_str()?.to_string(),
|
||||||
|
date,
|
||||||
|
};
|
||||||
|
Ok(auth)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_presigned(algorithm: &str, query: &QueryMap) -> Result<Self, Error> {
|
||||||
|
if algorithm != AWS4_HMAC_SHA256 {
|
||||||
|
return Err(Error::bad_request(
|
||||||
|
"Unsupported authorization method".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let cred = query
|
||||||
|
.get(X_AMZ_CREDENTIAL.as_str())
|
||||||
|
.ok_or_bad_request("X-Amz-Credential not found in query parameters")?;
|
||||||
|
let signed_headers = query
|
||||||
|
.get(X_AMZ_SIGNEDHEADERS.as_str())
|
||||||
|
.ok_or_bad_request("X-Amz-SignedHeaders not found in query parameters")?;
|
||||||
|
let signature = query
|
||||||
|
.get(X_AMZ_SIGNATURE.as_str())
|
||||||
|
.ok_or_bad_request("X-Amz-Signature not found in query parameters")?;
|
||||||
|
|
||||||
|
let duration = query
|
||||||
|
.get(X_AMZ_EXPIRES.as_str())
|
||||||
|
.ok_or_bad_request("X-Amz-Expires not found in query parameters")?
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| Error::bad_request("X-Amz-Expires is not a number".to_string()))?;
|
||||||
|
|
||||||
|
if duration > 7 * 24 * 3600 {
|
||||||
|
return Err(Error::bad_request(
|
||||||
|
"X-Amz-Expires may not exceed a week".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let date = query
|
||||||
|
.get(X_AMZ_DATE.as_str())
|
||||||
|
.ok_or_bad_request("Missing X-Amz-Date field")?;
|
||||||
|
let date = parse_date(date)?;
|
||||||
|
|
||||||
|
if Utc::now() - date > Duration::seconds(duration) {
|
||||||
|
return Err(Error::bad_request("Date is too old".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (key_id, scope) = parse_credential(cred)?;
|
||||||
|
Ok(Authorization {
|
||||||
|
key_id,
|
||||||
|
scope,
|
||||||
|
signed_headers: signed_headers.to_string(),
|
||||||
|
signature: signature.to_string(),
|
||||||
|
content_sha256: UNSIGNED_PAYLOAD.to_string(),
|
||||||
|
date,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn parse_form(params: &HeaderMap) -> Result<Self, Error> {
|
||||||
|
let credential = params
|
||||||
|
.get(X_AMZ_CREDENTIAL)
|
||||||
|
.ok_or_else(|| Error::forbidden("Garage does not support anonymous access yet"))?
|
||||||
|
.to_str()?;
|
||||||
|
let signature = params
|
||||||
|
.get(X_AMZ_SIGNATURE)
|
||||||
|
.ok_or_bad_request("No signature was provided")?
|
||||||
|
.to_str()?
|
||||||
|
.to_string();
|
||||||
|
let date = params
|
||||||
|
.get(X_AMZ_DATE)
|
||||||
|
.ok_or_bad_request("No date was provided")?
|
||||||
|
.to_str()?;
|
||||||
|
let date = parse_date(date)?;
|
||||||
|
|
||||||
|
if Utc::now() - date > Duration::hours(24) {
|
||||||
|
return Err(Error::bad_request("Date is too old".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (key_id, scope) = parse_credential(credential)?;
|
||||||
|
let auth = Authorization {
|
||||||
|
key_id,
|
||||||
|
scope,
|
||||||
|
signed_headers: "".to_string(),
|
||||||
|
signature,
|
||||||
|
content_sha256: UNSIGNED_PAYLOAD.to_string(),
|
||||||
|
date,
|
||||||
|
};
|
||||||
|
Ok(auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_credential(cred: &str) -> Result<(String, String), Error> {
|
||||||
|
let first_slash = cred
|
||||||
|
.find('/')
|
||||||
|
.ok_or_bad_request("Credentials does not contain '/' in authorization field")?;
|
||||||
|
let (key_id, scope) = cred.split_at(first_slash);
|
||||||
|
Ok((
|
||||||
|
key_id.to_string(),
|
||||||
|
scope.trim_start_matches('/').to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryFrom;
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use chrono::{offset::Utc, DateTime};
|
use chrono::{offset::Utc, DateTime};
|
||||||
use hmac::{Hmac, Mac};
|
use hmac::{Hmac, Mac};
|
||||||
use http_body_util::BodyExt;
|
use http_body_util::BodyExt;
|
||||||
use http_body_util::Full as FullBody;
|
use http_body_util::Full as FullBody;
|
||||||
|
use hyper::header::{
|
||||||
|
HeaderMap, HeaderName, HeaderValue, AUTHORIZATION, CONTENT_ENCODING, CONTENT_LENGTH, HOST,
|
||||||
|
};
|
||||||
use hyper::{Method, Request, Response, Uri};
|
use hyper::{Method, Request, Response, Uri};
|
||||||
use hyper_util::client::legacy::{connect::HttpConnector, Client};
|
use hyper_util::client::legacy::{connect::HttpConnector, Client};
|
||||||
use hyper_util::rt::TokioExecutor;
|
use hyper_util::rt::TokioExecutor;
|
||||||
|
@ -173,54 +176,85 @@ impl<'a> RequestBuilder<'a> {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let streaming_signer = signer.clone();
|
let streaming_signer = signer.clone();
|
||||||
|
|
||||||
let mut all_headers = self.signed_headers.clone();
|
let mut all_headers = self
|
||||||
|
.signed_headers
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| {
|
||||||
|
(
|
||||||
|
HeaderName::try_from(k).expect("invalid header name"),
|
||||||
|
HeaderValue::try_from(v).expect("invalid header value"),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<HeaderMap>();
|
||||||
|
|
||||||
let date = now.format(signature::LONG_DATETIME).to_string();
|
let date = now.format(signature::LONG_DATETIME).to_string();
|
||||||
all_headers.insert("x-amz-date".to_owned(), date);
|
all_headers.insert(
|
||||||
all_headers.insert("host".to_owned(), host);
|
signature::payload::X_AMZ_DATE,
|
||||||
|
HeaderValue::from_str(&date).unwrap(),
|
||||||
|
);
|
||||||
|
all_headers.insert(HOST, HeaderValue::from_str(&host).unwrap());
|
||||||
|
|
||||||
let body_sha = match self.body_signature {
|
let body_sha = match self.body_signature {
|
||||||
BodySignature::Unsigned => "UNSIGNED-PAYLOAD".to_owned(),
|
BodySignature::Unsigned => "UNSIGNED-PAYLOAD".to_owned(),
|
||||||
BodySignature::Classic => hex::encode(garage_util::data::sha256sum(&self.body)),
|
BodySignature::Classic => hex::encode(garage_util::data::sha256sum(&self.body)),
|
||||||
BodySignature::Streaming(size) => {
|
BodySignature::Streaming(size) => {
|
||||||
all_headers.insert("content-encoding".to_owned(), "aws-chunked".to_owned());
|
|
||||||
all_headers.insert(
|
all_headers.insert(
|
||||||
"x-amz-decoded-content-length".to_owned(),
|
CONTENT_ENCODING,
|
||||||
self.body.len().to_string(),
|
HeaderValue::from_str("aws-chunked").unwrap(),
|
||||||
|
);
|
||||||
|
all_headers.insert(
|
||||||
|
HeaderName::from_static("x-amz-decoded-content-length"),
|
||||||
|
HeaderValue::from_str(&self.body.len().to_string()).unwrap(),
|
||||||
);
|
);
|
||||||
// Get lenght of body by doing the conversion to a streaming body with an
|
// Get lenght of body by doing the conversion to a streaming body with an
|
||||||
// invalid signature (we don't know the seed) just to get its length. This
|
// invalid signature (we don't know the seed) just to get its length. This
|
||||||
// is a pretty lazy and inefficient way to do it, but it's enought for test
|
// is a pretty lazy and inefficient way to do it, but it's enought for test
|
||||||
// code.
|
// code.
|
||||||
all_headers.insert(
|
all_headers.insert(
|
||||||
"content-length".to_owned(),
|
CONTENT_LENGTH,
|
||||||
to_streaming_body(&self.body, size, String::new(), signer.clone(), now, "")
|
to_streaming_body(&self.body, size, String::new(), signer.clone(), now, "")
|
||||||
.len()
|
.len()
|
||||||
.to_string(),
|
.to_string()
|
||||||
|
.try_into()
|
||||||
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
"STREAMING-AWS4-HMAC-SHA256-PAYLOAD".to_owned()
|
"STREAMING-AWS4-HMAC-SHA256-PAYLOAD".to_owned()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
all_headers.insert("x-amz-content-sha256".to_owned(), body_sha.clone());
|
all_headers.insert(
|
||||||
|
signature::payload::X_AMZ_CONTENT_SH256,
|
||||||
|
HeaderValue::from_str(&body_sha).unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
let mut signed_headers = all_headers
|
let mut signed_headers = all_headers.keys().cloned().collect::<Vec<_>>();
|
||||||
.keys()
|
signed_headers.sort_by(|h1, h2| h1.as_str().cmp(h2.as_str()));
|
||||||
.map(|k| k.as_ref())
|
let signed_headers_str = signed_headers
|
||||||
.collect::<Vec<&str>>();
|
.iter()
|
||||||
signed_headers.sort();
|
.map(ToString::to_string)
|
||||||
let signed_headers = signed_headers.join(";");
|
.collect::<Vec<_>>()
|
||||||
|
.join(";");
|
||||||
|
|
||||||
all_headers.extend(self.unsigned_headers.clone());
|
all_headers.extend(self.unsigned_headers.iter().map(|(k, v)| {
|
||||||
|
(
|
||||||
|
HeaderName::try_from(k).expect("invalid header name"),
|
||||||
|
HeaderValue::try_from(v).expect("invalid header value"),
|
||||||
|
)
|
||||||
|
}));
|
||||||
|
|
||||||
|
let uri = Uri::try_from(&uri).unwrap();
|
||||||
|
let query = signature::payload::parse_query_map(&uri).unwrap();
|
||||||
|
|
||||||
let canonical_request = signature::payload::canonical_request(
|
let canonical_request = signature::payload::canonical_request(
|
||||||
self.service,
|
self.service,
|
||||||
&self.method,
|
&self.method,
|
||||||
&Uri::try_from(&uri).unwrap(),
|
uri.path(),
|
||||||
|
&query,
|
||||||
&all_headers,
|
&all_headers,
|
||||||
&signed_headers,
|
signed_headers,
|
||||||
&body_sha,
|
&body_sha,
|
||||||
);
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let string_to_sign = signature::payload::string_to_sign(&now, &scope, &canonical_request);
|
let string_to_sign = signature::payload::string_to_sign(&now, &scope, &canonical_request);
|
||||||
|
|
||||||
|
@ -228,14 +262,15 @@ impl<'a> RequestBuilder<'a> {
|
||||||
let signature = hex::encode(signer.finalize().into_bytes());
|
let signature = hex::encode(signer.finalize().into_bytes());
|
||||||
let authorization = format!(
|
let authorization = format!(
|
||||||
"AWS4-HMAC-SHA256 Credential={}/{},SignedHeaders={},Signature={}",
|
"AWS4-HMAC-SHA256 Credential={}/{},SignedHeaders={},Signature={}",
|
||||||
self.requester.key.id, scope, signed_headers, signature
|
self.requester.key.id, scope, signed_headers_str, signature
|
||||||
|
);
|
||||||
|
all_headers.insert(
|
||||||
|
AUTHORIZATION,
|
||||||
|
HeaderValue::from_str(&authorization).unwrap(),
|
||||||
);
|
);
|
||||||
all_headers.insert("authorization".to_owned(), authorization);
|
|
||||||
|
|
||||||
let mut request = Request::builder();
|
let mut request = Request::builder();
|
||||||
for (k, v) in all_headers {
|
*request.headers_mut().unwrap() = all_headers;
|
||||||
request = request.header(k, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
let body = if let BodySignature::Streaming(size) = self.body_signature {
|
let body = if let BodySignature::Streaming(size) = self.body_signature {
|
||||||
to_streaming_body(&self.body, size, signature, streaming_signer, now, &scope)
|
to_streaming_body(&self.body, size, signature, streaming_signer, now, &scope)
|
||||||
|
|
|
@ -26,7 +26,7 @@ async fn test_putobject_streaming() {
|
||||||
.builder(bucket.clone())
|
.builder(bucket.clone())
|
||||||
.method(Method::PUT)
|
.method(Method::PUT)
|
||||||
.path(STD_KEY.to_owned())
|
.path(STD_KEY.to_owned())
|
||||||
.unsigned_headers(headers)
|
.signed_headers(headers)
|
||||||
.vhost_style(true)
|
.vhost_style(true)
|
||||||
.body(vec![])
|
.body(vec![])
|
||||||
.body_signature(BodySignature::Streaming(10))
|
.body_signature(BodySignature::Streaming(10))
|
||||||
|
|
Loading…
Reference in a new issue