Skip to content

Commit 69dd4cd

Browse files
Refactoring: update deps, improve local macros usage, etc (#83)
* feat: crypto: replaces `hmac_sha256` and `hmac_sha512` crates with `hmac` and `sha2` crates; * feat(crypto-helper): small refactoring; * refactor: fix clippy warnings; * refactor(crypto-helper): refactor modules structure; * chose: update deps;
1 parent 53fc841 commit 69dd4cd

File tree

21 files changed

+417
-389
lines changed

21 files changed

+417
-389
lines changed

Cargo.lock

+290-319
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tracing = "0.1"
4343
hex = "0.4"
4444
gloo-timers = "0.3"
4545
serde = { version = "1.0", features = ["derive"] }
46-
serde_qs = "0.12"
46+
serde_qs = "0.13"
4747
serde_json = "1.0"
4848
base64 = "0.22"
4949
time = { version = "0.3", features = ["local-offset", "wasm-bindgen"] }
@@ -52,11 +52,11 @@ time = { version = "0.3", features = ["local-offset", "wasm-bindgen"] }
5252
picky-krb = { git = "https://github.com/TheBestTravynka/picky-rs.git", rev = "604a246" }
5353
picky = { version = "7.0.0-rc.8", default-features = false }
5454
md5 = "0.7"
55-
sha1 = "0.11.0-pre.3"
56-
hmac-sha256 = "1.1"
57-
hmac-sha512 = { version = "1.1", features = ["sha384"] }
55+
sha1 = "0.10"
56+
sha2 = "0.10.8"
57+
hmac = "0.12.1"
5858
rsa = "0.9"
59-
bcrypt = "0.15"
59+
bcrypt = "0.16"
6060
flate2 = { version = "1.0", features = ["zlib"] }
6161
rand = { version = "0.9.0-alpha.0", features = ["small_rng"] }
6262
rand_chacha = "0.9.0-alpha.0"
@@ -69,4 +69,4 @@ oid = { version = "0.2", default-features = false }
6969
paste = "1.0"
7070

7171
# diff
72-
similar = { version = "2.4", features = ["serde"] }
72+
similar = { version = "2.6", features = ["serde"] }

crates/asn1-parser/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ std = []
1515
[dev-dependencies]
1616
tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "fmt", "ansi"] }
1717
prop-strategies = { path = "../prop-strategies" }
18-
proptest = "1.2.0"
18+
proptest = "1.2"
1919

2020
[dependencies]
2121
tracing = "0.1"
22-
num-bigint-dig = { version = "0.8.4", default-features = false }
23-
num-traits = { version = "0.2.17", default-features = false }
24-
oid = { version = "0.2.1", default-features = false }
25-
paste = "1.0.14"
26-
env_logger = "0.11.3"
22+
num-bigint-dig = { version = "0.8", default-features = false }
23+
num-traits = { version = "0.2", default-features = false }
24+
oid = { version = "0.2", default-features = false }
25+
paste = "1.0"
26+
env_logger = "0.11"

crates/asn1-parser/src/string/validators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn validate_general(_: &str) -> bool {
99
}
1010

1111
pub fn validate_printable(data: &str) -> bool {
12-
const ALLOWED_SPECIAL: &[u8] = &[b' ', b'\'', b'(', b')', b'+', b',', b'-', b'.', b'/', b':', b'=', b'?'];
12+
const ALLOWED_SPECIAL: &[u8] = b" '()+,-./:=?";
1313

1414
for c in data.as_bytes() {
1515
if !(c.is_ascii_lowercase() || c.is_ascii_uppercase() || c.is_ascii_digit() || ALLOWED_SPECIAL.contains(c)) {

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "1.80.1"
2+
channel = "1.83"
33
components = [ "rustfmt", "clippy" ]

src/asn1.rs src/asn1/mod.rs

File renamed without changes.
File renamed without changes.

src/crypto_helper/algorithm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,15 @@ pub enum Argon2Error<'a> {
412412
InvalidVariant(&'a str),
413413
}
414414

415-
impl<'a> std::fmt::Display for Argon2Error<'a> {
415+
impl std::fmt::Display for Argon2Error<'_> {
416416
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
417417
match self {
418418
Self::InvalidVersion(version) => write!(f, "InvalidVersion({version})"),
419419
Self::InvalidVariant(variant) => write!(f, "InvalidVariant({variant})"),
420420
}
421421
}
422422
}
423-
impl<'a> std::error::Error for Argon2Error<'a> {}
423+
impl std::error::Error for Argon2Error<'_> {}
424424

425425
impl Argon2Input {
426426
pub fn with_variant(&self, variant: Argon2Variant) -> Self {

src/crypto_helper/info.rs src/crypto_helper/info/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use yew::{function_component, html, use_state, Callback, Html, Properties, Targe
77
use super::algorithm::Algorithm;
88
use crate::crypto_helper::algorithm::{COMPRESSION_ALGOS, ENCRYPTION_ALGOS, HASHING_ALGOS, HMAC_ALGOS};
99
use crate::crypto_helper::info::algo_search::AlgoSearch;
10-
use crate::generate_algo_list_for_yew;
1110

1211
#[derive(PartialEq, Properties)]
1312
pub struct InfoProps {
File renamed without changes.

src/crypto_helper/macros.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[macro_export]
21
macro_rules! generate_algo_list_for_yew {
32
(algo_list: $algo_list:expr, props: $props:expr) => {{
43
let mut sorted_algo_list = $algo_list.to_vec();

src/crypto_helper.rs src/crypto_helper/mod.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#[macro_use]
2+
pub mod macros;
3+
14
mod algorithm;
25
mod computations;
36
mod info;
47
mod input;
5-
mod macros;
68
mod output;
79

810
pub use algorithm::Algorithm;
@@ -30,9 +32,27 @@ fn convert(algrithm: &Algorithm) -> Result<Vec<u8>, String> {
3032
sha1.update(input);
3133
Ok(sha1.finalize().to_vec())
3234
}
33-
Algorithm::Sha256(input) => Ok(hmac_sha256::Hash::hash(input).to_vec()),
34-
Algorithm::Sha384(input) => Ok(hmac_sha512::sha384::Hash::hash(input).to_vec()),
35-
Algorithm::Sha512(input) => Ok(hmac_sha512::Hash::hash(input).to_vec()),
35+
Algorithm::Sha256(input) => Ok({
36+
use sha2::Digest;
37+
38+
let mut hasher = sha2::Sha256::new();
39+
hasher.update(input);
40+
hasher.finalize().to_vec()
41+
}),
42+
Algorithm::Sha384(input) => Ok({
43+
use sha2::Digest;
44+
45+
let mut hasher = sha2::Sha384::new();
46+
hasher.update(input);
47+
hasher.finalize().to_vec()
48+
}),
49+
Algorithm::Sha512(input) => Ok({
50+
use sha2::Digest;
51+
52+
let mut hasher = sha2::Sha512::new();
53+
hasher.update(input);
54+
hasher.finalize().to_vec()
55+
}),
3656
Algorithm::Aes128CtsHmacSha196(input) => process_krb_cipher(CipherSuite::Aes128CtsHmacSha196.cipher(), input),
3757
Algorithm::Aes256CtsHmacSha196(input) => process_krb_cipher(CipherSuite::Aes256CtsHmacSha196.cipher(), input),
3858
Algorithm::HmacSha196Aes128(input) => process_krb_hmac(ChecksumSuite::HmacSha196Aes128.hasher(), input),
File renamed without changes.

src/diff.rs src/diff/mod.rs

File renamed without changes.

src/jwt/jwt.rs src/jwt/jwt/mod.rs

File renamed without changes.

src/jwt/jwt_utils.rs

+48-36
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use super::jwt::Jwt;
1313
use super::signature::JwtSignatureAlgorithm;
1414
use crate::common::{build_byte_input, build_simple_output, BytesFormat};
1515
use crate::url_query_params::generate_jwt_link;
16-
use crate::{check_asymmetric_key, check_symmetric_key, generate_placeholder, sign, verify};
1716

1817
const DEFAULT_TEXT_FOR_RSA_PLACEHOLDER: &str = "RSA private/public key in PEM (-----BEGIN RSA PRIVATE/PUBLIC KEY-----)";
1918
const DEFAULT_TEXT_FOR_EC_PLACEHOLDER: &str = "EC private/public key in PEM (-----BEGIN EC PRIVATE/PUBLIC KEY-----)";
@@ -129,7 +128,11 @@ fn calculate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
129128
notificator: spawn_notification
130129
);
131130

132-
Some(hmac_sha256::HMAC::mac(data_to_sign.as_bytes(), key).to_vec())
131+
Some(sign_hmac!(
132+
hash_alg: sha2::Sha256,
133+
key: key,
134+
msg: data_to_sign.as_bytes(),
135+
))
133136
}
134137
JwtSignatureAlgorithm::Hs384(key) => {
135138
check_symmetric_key!(
@@ -139,7 +142,11 @@ fn calculate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
139142
notificator: spawn_notification
140143
);
141144

142-
Some(hmac_sha512::sha384::HMAC::mac(data_to_sign.as_bytes(), key).to_vec())
145+
Some(sign_hmac!(
146+
hash_alg: sha2::Sha384,
147+
key: key,
148+
msg: data_to_sign.as_bytes(),
149+
))
143150
}
144151
JwtSignatureAlgorithm::Hs512(key) => {
145152
check_symmetric_key!(
@@ -149,7 +156,11 @@ fn calculate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
149156
notificator: spawn_notification
150157
);
151158

152-
Some(hmac_sha512::HMAC::mac(data_to_sign.as_bytes(), key).to_vec())
159+
Some(sign_hmac!(
160+
hash_alg: sha2::Sha512,
161+
key: key,
162+
msg: data_to_sign.as_bytes(),
163+
))
153164
}
154165
JwtSignatureAlgorithm::Rs256(key) => {
155166
let private_key = check_asymmetric_key!(
@@ -271,8 +282,8 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
271282
STANDARD.encode(jwt.parsed_payload.as_bytes())
272283
);
273284

274-
let calculated_signature = match &jwt.signature_algorithm {
275-
JwtSignatureAlgorithm::None => Vec::new(),
285+
Some(match &jwt.signature_algorithm {
286+
JwtSignatureAlgorithm::None => Vec::<u8>::new() == jwt.signature,
276287
JwtSignatureAlgorithm::Hs256(key) => {
277288
check_symmetric_key!(
278289
key: key,
@@ -281,7 +292,12 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
281292
notificator: spawn_notification
282293
);
283294

284-
hmac_sha256::HMAC::mac(data_to_sign.as_bytes(), key).to_vec()
295+
verify_hmac!(
296+
hash_alg: sha2::Sha384,
297+
key: key,
298+
msg: data_to_sign.as_bytes(),
299+
digest: jwt.signature.as_slice(),
300+
)
285301
}
286302
JwtSignatureAlgorithm::Hs384(key) => {
287303
check_symmetric_key!(
@@ -291,7 +307,12 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
291307
notificator: spawn_notification
292308
);
293309

294-
hmac_sha512::sha384::HMAC::mac(data_to_sign.as_bytes(), key).to_vec()
310+
verify_hmac!(
311+
hash_alg: sha2::Sha384,
312+
key: key,
313+
msg: data_to_sign.as_bytes(),
314+
digest: jwt.signature.as_slice(),
315+
)
295316
}
296317
JwtSignatureAlgorithm::Hs512(key) => {
297318
check_symmetric_key!(
@@ -301,7 +322,12 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
301322
notificator: spawn_notification
302323
);
303324

304-
hmac_sha512::HMAC::mac(data_to_sign.as_bytes(), key).to_vec()
325+
verify_hmac!(
326+
hash_alg: sha2::Sha512,
327+
key: key,
328+
msg: data_to_sign.as_bytes(),
329+
digest: jwt.signature.as_slice(),
330+
)
305331
}
306332
JwtSignatureAlgorithm::Rs256(key) => {
307333
let public_key = check_asymmetric_key!(
@@ -311,16 +337,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
311337
key_kind: PublicKey,
312338
);
313339

314-
let is_ok = verify!(
340+
verify!(
315341
signature_algo: SignatureAlgorithm::RsaPkcs1v15,
316342
hash_algo: HashAlgorithm::SHA2_256,
317343
public_key: &public_key,
318344
data_to_sign: data_to_sign.as_bytes(),
319345
jwt_signature: &jwt.signature,
320346
notificator: spawn_notification
321-
);
322-
323-
return Some(is_ok);
347+
)
324348
}
325349
JwtSignatureAlgorithm::Rs384(key) => {
326350
let public_key = check_asymmetric_key!(
@@ -330,16 +354,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
330354
key_kind: PublicKey,
331355
);
332356

333-
let is_ok = verify!(
357+
verify!(
334358
signature_algo: SignatureAlgorithm::RsaPkcs1v15,
335359
hash_algo: HashAlgorithm::SHA2_384,
336360
public_key: &public_key,
337361
data_to_sign: data_to_sign.as_bytes(),
338362
jwt_signature: &jwt.signature,
339363
notificator: spawn_notification
340-
);
341-
342-
return Some(is_ok);
364+
)
343365
}
344366
JwtSignatureAlgorithm::Rs512(key) => {
345367
let public_key = check_asymmetric_key!(
@@ -349,16 +371,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
349371
key_kind: PublicKey,
350372
);
351373

352-
let is_ok = verify!(
374+
verify!(
353375
signature_algo: SignatureAlgorithm::RsaPkcs1v15,
354376
hash_algo: HashAlgorithm::SHA2_512,
355377
public_key: &public_key,
356378
data_to_sign: data_to_sign.as_bytes(),
357379
jwt_signature: &jwt.signature,
358380
notificator: spawn_notification
359-
);
360-
361-
return Some(is_ok);
381+
)
362382
}
363383
JwtSignatureAlgorithm::Es256(key) => {
364384
let public_key = check_asymmetric_key!(
@@ -368,16 +388,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
368388
key_kind: PublicKey,
369389
);
370390

371-
let is_ok = verify!(
391+
verify!(
372392
signature_algo: SignatureAlgorithm::Ecdsa,
373393
hash_algo: HashAlgorithm::SHA2_256,
374394
public_key: &public_key,
375395
data_to_sign: data_to_sign.as_bytes(),
376396
jwt_signature: &jwt.signature,
377397
notificator: spawn_notification
378-
);
379-
380-
return Some(is_ok);
398+
)
381399
}
382400
JwtSignatureAlgorithm::Es384(key) => {
383401
let public_key = check_asymmetric_key!(
@@ -387,16 +405,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
387405
key_kind: PublicKey,
388406
);
389407

390-
let is_ok = verify!(
408+
verify!(
391409
signature_algo: SignatureAlgorithm::Ecdsa,
392410
hash_algo: HashAlgorithm::SHA2_384,
393411
public_key: &public_key,
394412
data_to_sign: data_to_sign.as_bytes(),
395413
jwt_signature: &jwt.signature,
396414
notificator: spawn_notification
397-
);
398-
399-
return Some(is_ok);
415+
)
400416
}
401417
JwtSignatureAlgorithm::Es512(key) => {
402418
let public_key = check_asymmetric_key!(
@@ -406,16 +422,14 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
406422
key_kind: PublicKey,
407423
);
408424

409-
let is_ok = verify!(
425+
verify!(
410426
signature_algo: SignatureAlgorithm::Ecdsa,
411427
hash_algo: HashAlgorithm::SHA2_512,
412428
public_key: &public_key,
413429
data_to_sign: data_to_sign.as_bytes(),
414430
jwt_signature: &jwt.signature,
415431
notificator: spawn_notification
416-
);
417-
418-
return Some(is_ok);
432+
)
419433
}
420434
JwtSignatureAlgorithm::Unsupported(algo_name) => {
421435
spawn_notification.emit(Notification::from_description_and_type(
@@ -425,9 +439,7 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
425439

426440
return None;
427441
}
428-
};
429-
430-
Some(jwt.signature == calculated_signature)
442+
})
431443
}
432444

433445
pub fn generate_jwt(jwt: &Jwt, spawn_notification: Callback<Notification>) -> Option<Vec<u8>> {

0 commit comments

Comments
 (0)