Skip to content

Commit

Permalink
Maybe working.
Browse files Browse the repository at this point in the history
  • Loading branch information
EkardNT committed Apr 1, 2021
1 parent 6aa7488 commit 1ffb2d2
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 38 deletions.
88 changes: 66 additions & 22 deletions src/algorithm.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
use std::fmt::Display;

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Algorithm {
HS2019,
RSA_SHA1,
RSA_SHA256,
HMAC_SHA256,
ECDSA_SHA256
use std::{fmt::Debug, io::Write};

use ring::{rand::{SecureRandom}, signature::RsaKeyPair};

/// The signature algorithm used to generate the HTTP message signature. The signature
/// algorithm determines determines the hashing and signing algorithms used in computing
/// the signature. Technically, it also determines the canonicalization algorithm used to
/// build the string to sign, but as all signature algorithms share the same
/// canonicalization algorithm, this trait does not include that feature.
pub trait SignatureAlgorithm {
/// The name which will be used for the "algorithm" signature parameter.
fn name(&self) -> &str;

/// The id of the key, which will be used for the "keyId" signature parameter.
fn key_id(&self) -> &str;

/// Is the (created) signature element allowed?
fn allows_created(&self) -> bool;

/// Hash a block of data.
fn hash(&self, data: &[u8], output: &mut dyn Write) -> std::io::Result<()>;

/// Digitally sign a block of data.
fn sign(&self, data: &[u8], output: &mut dyn Write) -> std::io::Result<()>;
}

pub struct RsaSha256<Rand> {
key_id: String,
key: RsaKeyPair,
random: Rand,
}

impl Algorithm {
fn name(&self) -> &'static str {
match self {
Algorithm::HS2019 => "hs2019",
Algorithm::RSA_SHA1 => "rsa-sha1",
Algorithm::RSA_SHA256 => "rsa-sha256",
Algorithm::HMAC_SHA256 => "hmac-sha256",
Algorithm::ECDSA_SHA256 => "ecdsa-sha256",
impl<Rand> RsaSha256<Rand> {
pub fn new(key_id: impl Into<String>, key: RsaKeyPair, random: Rand) -> Self {
Self {
key_id: key_id.into(),
key,
random
}
}
}

impl Default for Algorithm {
fn default() -> Self {
Self::HS2019
impl<Rand: SecureRandom> SignatureAlgorithm for RsaSha256<Rand> {
fn name(&self) -> &str {
"rsa-sha256"
}

fn key_id(&self) -> &str {
&self.key_id
}

fn allows_created(&self) -> bool {
false
}

fn hash(&self, data: &[u8], output: &mut dyn Write) -> std::io::Result<()> {
let digest = ring::digest::digest(&ring::digest::SHA1_FOR_LEGACY_USE_ONLY, data);
output.write_all(digest.as_ref())
}

fn sign(&self, data: &[u8], output: &mut dyn Write) -> std::io::Result<()> {
// 1024 bytes is enough for RSA-8192 keys.
let mut signature = [0u8; 1024];
let signature = &mut signature[..self.key.public_modulus_len()];
self.key.sign(&ring::signature::RSA_PKCS1_SHA256, &self.random, data, signature)
.expect("Failed to compute RSA_PKCS1_SHA256");
output.write_all(signature)
}
}

impl Display for Algorithm {
impl<Rand> Debug for RsaSha256<Rand> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
f.debug_struct("RsaSha256")
.field("key_id", &self.key_id)
.field("key", &self.key)
.finish()
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

pub mod algorithm;
pub mod request;
pub mod sign;
pub mod sign;
pub mod signature;
16 changes: 16 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ pub enum Method {
Patch,
}

impl Method {
pub fn lowercase(&self) -> &'static [u8] {
match self {
Self::Get => b"get",
Self::Post => b"post",
Self::Put => b"put",
Self::Delete => b"delete",
Self::Head => b"head",
Self::Options => b"options",
Self::Connect => b"connect",
Self::Patch => b"patch",
Self::Trace => b"trace",
}
}
}

pub trait Headers {
type NameIter<'a> : Iterator<Item = &'a str>;
type ValueIter<'a> : Iterator<Item = &'a [u8]>;
Expand Down
15 changes: 0 additions & 15 deletions src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,12 @@ use ring::rand::SecureRandom;
use ring::signature::RsaKeyPair;

trait MethodExt {
fn lowercase(&self) -> &'static [u8];
fn is_body_mandatory(&self) -> bool;
fn required_object_storage_signing_elements(self) -> &'static [SignatureElement<'static>];
fn required_non_object_storage_signing_elements(&self) -> &'static [SignatureElement<'static>];
}

impl MethodExt for Method {
fn lowercase(&self) -> &'static [u8] {
match self {
Self::Get => b"get",
Self::Post => b"post",
Self::Put => b"put",
Self::Delete => b"delete",
Self::Head => b"head",
Self::Options => b"options",
Self::Connect => b"connect",
Self::Patch => b"patch",
Self::Trace => b"trace",
}
}

fn is_body_mandatory(&self) -> bool {
match self {
Self::Put | Self::Patch | Self::Post => true,
Expand Down
Loading

0 comments on commit 1ffb2d2

Please sign in to comment.