http_sig/signing.rs
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
use std::convert::TryInto;
use std::sync::Arc;
use std::time::SystemTime;
use chrono::Utc;
use http::header::{HeaderName, HeaderValue, AUTHORIZATION, DATE, HOST};
use itertools::Itertools;
use thiserror::Error;
use sha2::Digest;
use crate::algorithm::{HttpDigest, HttpSignatureSign};
use crate::canonicalize::{CanonicalizeConfig, CanonicalizeError, CanonicalizeExt, RequestLike};
use crate::header::{Header, PseudoHeader};
use crate::{DefaultDigestAlgorithm, DefaultSignatureAlgorithm, DATE_FORMAT};
/// This trait is to be implemented for types representing an outgoing
/// HTTP request. The HTTP signing extension methods are available on
/// any type implementing this trait.
pub trait ClientRequestLike: RequestLike {
/// Returns the host for the request (eg. "example.com") in case the Host header has
/// not been set explicitly.
/// When implementing this trait, do not just read the `Host` header from the request -
/// this method will only be called when the `Host` header is not set.
fn host(&self) -> Option<String> {
None
}
/// Add a header to the request. This function may be used to set the `Date` and `Digest`
/// headers if not already present depending on the configuration. The `Authorization`
/// header will always be set assuming the message was signed successfully.
fn set_header(&mut self, header: HeaderName, value: HeaderValue);
/// Compute the digest using the provided HTTP digest algorithm. If this is not possible,
/// then return `None`. This may require buffering the request data into memory.
fn compute_digest(&mut self, digest: &dyn HttpDigest) -> Option<String>;
}
/// The types of error which may occur whilst signing.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SigningError {
/// A header required to be part of the signature was not present
/// on the request, and the `skip_missing` configuration option
/// was disabled.
#[error("Failed to canonicalize request")]
Canonicalize(#[source] CanonicalizeError),
/// The signature creation date was in the future
#[error("Signature creation date was in the future")]
InvalidSignatureCreationDate,
/// The signature expires date was in the past
#[error("Signature expires date was in the past")]
InvalidSignatureExpiresDate,
}
impl From<CanonicalizeError> for SigningError {
fn from(other: CanonicalizeError) -> Self {
Self::Canonicalize(other)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
enum SignatureCreated {
Omit,
Automatic,
Absolute(i64),
}
impl SignatureCreated {
fn get(self, ts: i64) -> Option<i64> {
match self {
Self::Omit => None,
Self::Automatic => Some(ts),
Self::Absolute(ts) => Some(ts),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
enum SignatureExpires {
Omit,
Relative(i64),
Absolute(i64),
}
impl SignatureExpires {
fn get(self, ts: i64) -> Option<i64> {
match self {
Self::Omit => None,
Self::Relative(offset) => Some(ts.saturating_add(offset)),
Self::Absolute(ts) => Some(ts),
}
}
}
/// The configuration used for signing HTTP requests.
#[derive(Debug, Clone)]
pub struct SigningConfig {
signature: Arc<dyn HttpSignatureSign>,
digest: Arc<dyn HttpDigest>,
key_id: String,
headers: Vec<Header>,
compute_digest: bool,
add_date: bool,
add_host: bool,
skip_missing: bool,
signature_created: SignatureCreated,
signature_expires: SignatureExpires,
}
impl SigningConfig {
/// Creates a new signing configuration using the default signature algorithm, and the
/// specified key ID and key.
pub fn new_default(key_id: &str, key: &[u8]) -> Self {
Self::new(key_id, DefaultSignatureAlgorithm::new(key))
}
/// Creates a new signing configuration using a custom signature algorithm, and the specified
/// key ID.
pub fn new<SigAlg: HttpSignatureSign>(key_id: &str, signature: SigAlg) -> Self {
SigningConfig {
signature: Arc::new(signature),
digest: Arc::new(DefaultDigestAlgorithm::new()),
key_id: key_id.into(),
headers: [
Header::Pseudo(PseudoHeader::RequestTarget),
Header::Normal(HOST),
Header::Normal(DATE),
Header::Normal(HeaderName::from_static("digest")),
]
.to_vec(),
compute_digest: true,
add_date: true,
add_host: true,
skip_missing: true,
signature_created: SignatureCreated::Omit,
signature_expires: SignatureExpires::Omit,
}
}
/// Returns the key ID.
pub fn key_id(&self) -> &str {
self.key_id.as_ref()
}
/// Returns the HTTP digest algorithm.
pub fn digest(&self) -> &dyn HttpDigest {
&*self.digest
}
/// Sets the HTTP digest algorithm (in-place).
fn set_digest<DigestAlg: HttpDigest>(&mut self, digest: DigestAlg) -> &mut Self {
self.digest = Arc::new(digest);
self
}
/// Sets the HTTP digest algorithm.
pub fn with_digest<DigestAlg: HttpDigest>(mut self, digest: DigestAlg) -> Self {
self.set_digest(digest);
self
}
/// Returns whether the digest will be automatically computed
/// when not already present.
///
/// This is set to `true` by default.
pub fn compute_digest(&self) -> bool {
self.compute_digest
}
/// Controls whether the digest will be automatically computed
/// when not already present (in-place).
///
/// This is set to `true` by default.
pub fn set_compute_digest(&mut self, compute_digest: bool) -> &mut Self {
self.compute_digest = compute_digest;
self
}
/// Controls whether the digest will be automatically computed
/// when not already present.
///
/// This is set to `true` by default.
pub fn with_compute_digest(mut self, compute_digest: bool) -> Self {
self.set_compute_digest(compute_digest);
self
}
/// Returns whether the current date and time will be added to the request
/// when not already present.
///
/// This is set to `true` by default.
pub fn add_date(&self) -> bool {
self.add_date
}
/// Controls whether the current date and time will be added to the request
/// when not already present (in-place).
///
/// This is set to `true` by default.
pub fn set_add_date(&mut self, add_date: bool) -> &mut Self {
self.add_date = add_date;
self
}
/// Controls whether the current date and time will be added to the request
/// when not already present.
///
/// This is set to `true` by default.
pub fn with_add_date(mut self, add_date: bool) -> Self {
self.set_add_date(add_date);
self
}
/// Returns whether the host will be added to the request
/// when not already present.
///
/// This is set to `true` by default.
pub fn add_host(&self) -> bool {
self.add_host
}
/// Controls whether the host will be added to the request
/// when not already present (in-place).
///
/// This is set to `true` by default.
pub fn set_add_host(&mut self, add_host: bool) -> &mut Self {
self.add_host = add_host;
self
}
/// Controls whether the host will be added to the request
/// when not already present.
///
/// This is set to `true` by default.
pub fn with_add_host(mut self, add_host: bool) -> Self {
self.set_add_host(add_host);
self
}
/// Returns the list of headers to include in the signature. Headers in this list
/// which are not present in the request itself will be skipped when signing the request.
///
/// This list contains `(request-target)`, `host`, `date` and `digest` by default.
pub fn headers(&self) -> impl IntoIterator<Item = &Header> {
&self.headers
}
/// Controls the list of headers to include in the signature (in-place). Headers in this list
/// which are not present in the request itself will be skipped when signing the request.
///
/// This list contains `(request-target)`, `host`, `date` and `digest` by default.
pub fn set_headers(&mut self, headers: &[Header]) -> &mut Self {
self.headers = headers.to_vec();
self
}
/// Controls the list of headers to include in the signature. Headers in this list
/// which are not present in the request itself will be skipped when signing the request.
///
/// This list contains `(request-target)`, `host`, `date` and `digest` by default.
pub fn with_headers(mut self, headers: &[Header]) -> Self {
self.set_headers(headers);
self
}
/// Returns whether the missing headers will be skipped
/// when not present, or if signing will fail instead.
///
/// This is set to `true` by default.
pub fn skip_missing(&self) -> bool {
self.skip_missing
}
/// Controls whether the missing headers will be skipped
/// when not present, or if signing will fail instead.
///
/// This is set to `true` by default.
pub fn set_skip_missing(&mut self, skip_missing: bool) -> &mut Self {
self.skip_missing = skip_missing;
self
}
/// Controls whether the missing headers will be skipped
/// when not present, or if signing will fail instead.
///
/// This is set to `true` by default.
pub fn with_skip_missing(mut self, skip_missing: bool) -> Self {
self.set_skip_missing(skip_missing);
self
}
/// Ensures a signature created date will be added
/// automatically with the current time.
///
/// This is off by default.
pub fn set_signature_created_auto(&mut self) -> &mut Self {
self.signature_created = SignatureCreated::Automatic;
self
}
/// Ensures a signature created date will be added
/// automatically with the current time.
///
/// This is off by default.
pub fn with_signature_created_auto(mut self) -> Self {
self.signature_created = SignatureCreated::Automatic;
self
}
/// Determines if a signature created date will be added
/// automatically with the current time.
///
/// This is off by default.
pub fn signature_created_auto(&self) -> bool {
self.signature_created == SignatureCreated::Automatic
}
/// Ensures a signature created date will be added
/// with the specified unix timestamp.
///
/// This is off by default.
pub fn set_signature_created_at(&mut self, ts: i64) -> &mut Self {
self.signature_created = SignatureCreated::Absolute(ts);
self
}
/// Ensures a signature created date will be added
/// with the specified unix timestamp.
///
/// This is off by default.
pub fn with_signature_created_at(mut self, ts: i64) -> Self {
self.signature_created = SignatureCreated::Absolute(ts);
self
}
/// Determines if a signature created date will be added
/// with a specific unix timestamp.
///
/// This is off by default.
pub fn signature_created_at(&self) -> Option<i64> {
if let SignatureCreated::Absolute(ts) = self.signature_created {
Some(ts)
} else {
None
}
}
/// Ensures a signature expires date will be added
/// automatically relative to the current time.
///
/// This is off by default.
pub fn set_signature_expires_relative(&mut self, offset: i64) -> &mut Self {
self.signature_expires = SignatureExpires::Relative(offset);
self
}
/// Ensures a signature expires date will be added
/// automatically relative to the current time.
///
/// This is off by default.
pub fn with_signature_expires_auto(mut self, offset: i64) -> Self {
self.signature_expires = SignatureExpires::Relative(offset);
self
}
/// Determines if a signature expires date will be added
/// automatically relative to the current time.
///
/// This is off by default.
pub fn signature_expires_relative(&self) -> Option<i64> {
if let SignatureExpires::Relative(offset) = self.signature_expires {
Some(offset)
} else {
None
}
}
/// Ensures a signature expires date will be added
/// with the specified unix timestamp.
///
/// This is off by default.
pub fn set_signature_expires_at(&mut self, ts: i64) -> &mut Self {
self.signature_expires = SignatureExpires::Absolute(ts);
self
}
/// Ensures a signature expires date will be added
/// with the specified unix timestamp.
///
/// This is off by default.
pub fn with_signature_expires_at(mut self, ts: i64) -> Self {
self.signature_expires = SignatureExpires::Absolute(ts);
self
}
/// Determines if a signature expires date will be added
/// with a specific unix timestamp.
///
/// This is off by default.
pub fn signature_expires_at(&self) -> Option<i64> {
if let SignatureExpires::Absolute(ts) = self.signature_expires {
Some(ts)
} else {
None
}
}
}
/// Import this trait to get access to access the `signed` and `sign` methods on all types implementing
/// `ClientRequestLike`.
pub trait SigningExt: Sized {
/// Consumes the request and returns it signed according to the provided configuration.
fn signed(mut self, config: &SigningConfig) -> Result<Self, SigningError> {
self.sign(config)?;
Ok(self)
}
/// Signs the request in-place according to the provided configuration.
fn sign(&mut self, config: &SigningConfig) -> Result<(), SigningError>;
}
fn add_auto_headers<R: ClientRequestLike>(request: &mut R, config: &SigningConfig) -> Vec<Header> {
let digest_header = HeaderName::from_static("digest");
// Add missing date header
if config.add_date && !request.has_header(&DATE.into()) {
let date = Utc::now().format(DATE_FORMAT).to_string();
request.set_header(
DATE,
date.try_into()
.expect("Dates should always be valid header values"),
);
}
// Add missing host header
if config.add_host && !request.has_header(&HOST.into()) {
if let Some(host) = request.host() {
request.set_header(
HOST,
host.try_into()
.expect("Host should be valid in a HTTP header"),
);
}
}
// Add missing digest header
if config.compute_digest && !request.has_header(&digest_header.clone().into()) {
if let Some(digest_str) = request.compute_digest(&*config.digest) {
let digest = format!("{}={}", config.digest.name(), digest_str);
request.set_header(
digest_header,
digest
.try_into()
.expect("Digest should be valid in a HTTP header"),
);
}
}
// Build the content block
if config.skip_missing {
config
.headers
.iter()
.filter(|header| request.has_header(header))
.cloned()
.collect()
} else {
config.headers.clone()
}
}
fn unix_timestamp() -> i64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Unix time to be positive")
.as_secs() as i64
}
impl<R: ClientRequestLike> SigningExt for R {
fn sign(&mut self, config: &SigningConfig) -> Result<(), SigningError> {
// Add missing headers
let headers = add_auto_headers(self, config);
let joined_headers = headers.iter().map(|header| header.as_str()).join(" ");
// Determine config for canonicalization
let ts = unix_timestamp();
let mut canonicalize_config = CanonicalizeConfig::new().with_headers(headers);
if let Some(created) = config.signature_created.get(ts) {
if created > ts {
return Err(SigningError::InvalidSignatureCreationDate);
}
canonicalize_config.set_signature_created(created.into());
}
if let Some(expires) = config.signature_expires.get(ts) {
if expires < ts {
return Err(SigningError::InvalidSignatureExpiresDate);
}
canonicalize_config.set_signature_expires(expires.into());
}
// Compute canonical representation
let content = self.canonicalize(&canonicalize_config)?;
// Sign the content
let signature = config.signature.http_sign(content.as_bytes());
// Construct the authorization header
let auth_header = format!(
r#"Signature keyId="{}",algorithm="{}",signature="{}",headers="{}""#,
config.key_id, "hs2019", signature, joined_headers
);
// Attach the authorization header to the request
self.set_header(
AUTHORIZATION,
auth_header
.try_into()
.expect("Signature scheme should generate a valid header"),
);
Ok(())
}
}