Make rust-tls work (through ring)

This commit is contained in:
Igor Katson 2024-08-27 15:11:34 +01:00
parent 78ee947d44
commit 01a4f68e20
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
7 changed files with 77 additions and 27 deletions

1
Cargo.lock generated
View file

@ -1661,6 +1661,7 @@ name = "librqbit-sha1-wrapper"
version = "3.0.0"
dependencies = [
"crypto-hash",
"ring",
]
[[package]]

View file

@ -9,6 +9,10 @@ repository = "https://github.com/ikatson/rqbit"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["sha1-crypto-hash"]
sha1-crypto-hash = ["sha1w/sha1-crypto-hash"]
sha1-ring = ["sha1w/sha1-ring"]
[dependencies]
serde = { version = "1", features = ["derive"] }

View file

@ -17,8 +17,8 @@ http-api = ["axum", "tower-http"]
upnp-serve-adapter = ["upnp-serve"]
webui = []
timed_existence = []
default-tls = ["reqwest/default-tls"]
rust-tls = ["reqwest/rustls-tls"]
default-tls = ["reqwest/default-tls", "sha1w/sha1-crypto-hash"]
rust-tls = ["reqwest/rustls-tls", "sha1w/sha1-ring"]
storage_middleware = ["lru"]
storage_examples = []
tracing-subscriber-utils = ["tracing-subscriber"]
@ -35,7 +35,7 @@ tracker_comms = { path = "../tracker_comms", default-features = false, package =
buffers = { path = "../buffers", package = "librqbit-buffers", version = "4.1" }
librqbit-core = { path = "../librqbit_core", version = "4" }
clone_to_owned = { path = "../clone_to_owned", package = "librqbit-clone-to-owned", version = "3" }
peer_binary_protocol = { path = "../peer_binary_protocol", package = "librqbit-peer-protocol", version = "4.1" }
peer_binary_protocol = { path = "../peer_binary_protocol", default-features = false, package = "librqbit-peer-protocol", version = "4.1" }
sha1w = { path = "../sha1w", default-features = false, package = "librqbit-sha1-wrapper", version = "3.0.0" }
dht = { path = "../dht", package = "librqbit-dht", version = "5.1.0" }
librqbit-upnp = { path = "../upnp", version = "0.1.1" }

View file

@ -9,7 +9,6 @@ repository = "https://github.com/ikatson/rqbit"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1", features = ["derive"] }
bincode = "1"

View file

@ -8,7 +8,13 @@ documentation = "https://docs.rs/librqbit-sha1-wrapper"
repository = "https://github.com/ikatson/rqbit"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html#
[features]
default = ["sha1-crypto-hash"]
sha1-crypto-hash = ["crypto-hash"]
sha1-ring = ["ring"]
[dependencies]
crypto-hash = "0.3"
crypto-hash = { version = "0.3", optional = true }
ring = { version = "0.17", optional = true }

View file

@ -5,35 +5,75 @@
// openssl is 2-3x faster than rust's sha1.
// system library is the best choice probably (it's the default anyway).
pub type Sha1 = Sha1System;
pub trait ISha1 {
fn new() -> Self;
fn update(&mut self, buf: &[u8]);
fn finish(self) -> [u8; 20];
}
pub struct Sha1System {
inner: crypto_hash::Hasher,
}
#[cfg(feature = "sha1-crypto-hash")]
mod crypto_hash_impl {
use super::ISha1;
impl ISha1 for Sha1System {
fn new() -> Self {
Self {
inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1),
pub struct Sha1CryptoHash {
inner: crypto_hash::Hasher,
}
impl ISha1 for Sha1CryptoHash {
fn new() -> Self {
Self {
inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1),
}
}
fn update(&mut self, buf: &[u8]) {
use std::io::Write;
self.inner.write_all(buf).unwrap();
}
fn finish(mut self) -> [u8; 20] {
let result = self.inner.finish();
debug_assert_eq!(result.len(), 20);
let mut result_arr = [0u8; 20];
result_arr.copy_from_slice(&result);
result_arr
}
}
}
fn update(&mut self, buf: &[u8]) {
use std::io::Write;
self.inner.write_all(buf).unwrap();
#[cfg(feature = "sha1-ring")]
mod ring_impl {
use super::ISha1;
use ring::digest::{Context, SHA1_FOR_LEGACY_USE_ONLY as SHA1};
pub struct Sha1Ring {
ctx: Context,
}
fn finish(mut self) -> [u8; 20] {
let result = self.inner.finish();
debug_assert_eq!(result.len(), 20);
let mut result_arr = [0u8; 20];
result_arr.copy_from_slice(&result);
result_arr
impl ISha1 for Sha1Ring {
fn new() -> Self {
Self {
ctx: Context::new(&SHA1),
}
}
fn update(&mut self, buf: &[u8]) {
self.ctx.update(buf);
}
fn finish(self) -> [u8; 20] {
let result = self.ctx.finish();
debug_assert_eq!(result.as_ref().len(), 20);
let mut result_arr = [0u8; 20];
result_arr.copy_from_slice(result.as_ref());
result_arr
}
}
}
#[cfg(feature = "sha1-crypto-hash")]
pub type Sha1 = crypto_hash_impl::Sha1CryptoHash;
#[cfg(feature = "sha1-ring")]
pub type Sha1 = ring_impl::Sha1Ring;

View file

@ -13,10 +13,10 @@ quick-xml = "0.36.1"
http = "1.1.0"
httparse = "1.9.4"
uuid = { version = "1.10.0", features = ["v4"] }
librqbit-upnp = { path = "../upnp" }
librqbit-upnp = { path = "../upnp", default-features = false }
gethostname = "0.5.0"
librqbit-sha1-wrapper = { path = "../sha1w" }
librqbit-core = { path = "../librqbit_core" }
librqbit-sha1-wrapper = { path = "../sha1w", default-features = false }
librqbit-core = { path = "../librqbit_core", default-features = false }
mime_guess = "2.0.5"
url = "2.5.2"
parking_lot = "0.12.3"