Skip to content

Commit

Permalink
Change oncecell to std::sync::LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
hidekatsu-izuno committed Sep 5, 2024
1 parent 2d2d8ef commit 68ef5d5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 50 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "josekit"
version = "0.8.7"
version = "0.9.0"
description = "JOSE (Javascript Object Signing and Encryption) library for Rust."
repository = "https://github.com/hidekatsu-izuno/josekit-rs"
readme = "README.md"
Expand All @@ -19,7 +19,6 @@ vendored = ["openssl/vendored"]
[dependencies]
thiserror = "1"
anyhow = "1"
once_cell = "1"
regex = "1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
Expand Down
4 changes: 2 additions & 2 deletions src/jwe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod jwe_header;
mod jwe_header_set;
pub mod zip;

use once_cell::sync::Lazy;
use std::sync::LazyLock;

use crate::JoseError;

Expand Down Expand Up @@ -54,7 +54,7 @@ pub use RsaesJweAlgorithm::RsaOaep256 as RSA_OAEP_256;
pub use RsaesJweAlgorithm::RsaOaep384 as RSA_OAEP_384;
pub use RsaesJweAlgorithm::RsaOaep512 as RSA_OAEP_512;

static DEFAULT_CONTEXT: Lazy<JweContext> = Lazy::new(|| JweContext::new());
static DEFAULT_CONTEXT: LazyLock<JweContext> = LazyLock::new(|| JweContext::new());

/// Return a representation of the data that is formatted by compact serialization.
///
Expand Down
9 changes: 7 additions & 2 deletions src/jwe/alg/pbes2_hmac_aeskw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ impl JweDecrypter for Pbes2HmacAeskwJweDecrypter {
};

if p2c > 1000000 {
bail!("The p2c value is too large. This is a possible DoS attack: {}", p2c);
bail!(
"The p2c value is too large. This is a possible DoS attack: {}",
p2c
);
}

let mut salt = Vec::with_capacity(self.algorithm().name().len() + 1 + p2s.len());
Expand Down Expand Up @@ -510,7 +513,9 @@ mod tests {

let decrypter = alg.decrypter_from_jwk(&jwk)?;

let err = decrypter.decrypt(encrypted_key.as_deref(), &enc, &out_header).unwrap_err();
let err = decrypter
.decrypt(encrypted_key.as_deref(), &enc, &out_header)
.unwrap_err();
assert_eq!(format!("{}", err), "Invalid JWE format: The p2c value is too large. This is a possible DoS attack: 1000001");
}

Expand Down
6 changes: 3 additions & 3 deletions src/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod jws_context;
mod jws_header;
mod jws_header_set;

use once_cell::sync::Lazy;
use std::sync::LazyLock;

use crate::JoseError;

Expand Down Expand Up @@ -41,7 +41,7 @@ pub use EcdsaJwsAlgorithm::Es512 as ES512;
use crate::jws::alg::eddsa::EddsaJwsAlgorithm;
pub use EddsaJwsAlgorithm::Eddsa as EdDSA;

static DEFAULT_CONTEXT: Lazy<JwsContext> = Lazy::new(|| JwsContext::new());
static DEFAULT_CONTEXT: LazyLock<JwsContext> = LazyLock::new(|| JwsContext::new());

/// Return a representation of the data that is formatted by compact serialization.
///
Expand Down Expand Up @@ -206,11 +206,11 @@ where

#[cfg(test)]
mod tests {
use std::cell::OnceCell;
use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use once_cell::sync::OnceCell;

use crate::jws::{self, EdDSA, JwsHeader, JwsHeaderSet, JwsVerifier, ES256, RS256};
use crate::Value;
Expand Down
4 changes: 2 additions & 2 deletions src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub use crate::jwt::jwt_payload_validator::JwtPayloadValidator;

pub use crate::jwt::alg::unsecured::UnsecuredJwsAlgorithm::None;

use once_cell::sync::Lazy;
use std::sync::LazyLock;

use crate::jwe::{JweDecrypter, JweEncrypter, JweHeader};
use crate::jwk::{Jwk, JwkSet};
use crate::jws::{JwsHeader, JwsSigner, JwsVerifier};
use crate::{JoseError, JoseHeader};

static DEFAULT_CONTEXT: Lazy<JwtContext> = Lazy::new(|| JwtContext::new());
static DEFAULT_CONTEXT: LazyLock<JwtContext> = LazyLock::new(|| JwtContext::new());

/// Return the string repsentation of the JWT with a "none" algorithm.
///
Expand Down
13 changes: 7 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ pub mod der;
pub mod hash_algorithm;
pub mod oid;

use std::sync::LazyLock;

use anyhow::bail;
use base64::DecodeError;
use base64::Engine as _;
use once_cell::sync::Lazy;
use openssl::bn::BigNumRef;
use openssl::rand;
use regex;
Expand All @@ -28,7 +29,7 @@ pub(crate) fn ceiling(len: usize, div: usize) -> usize {
}

pub(crate) fn is_base64_standard(input: &str) -> bool {
static RE_BASE64_STANDARD: Lazy<regex::Regex> = Lazy::new(|| {
static RE_BASE64_STANDARD: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(
r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$",
)
Expand All @@ -39,7 +40,7 @@ pub(crate) fn is_base64_standard(input: &str) -> bool {
}

pub(crate) fn is_base64_urlsafe_nopad(input: &str) -> bool {
static RE_BASE64_URL_SAFE_NOPAD: Lazy<regex::Regex> = Lazy::new(|| {
static RE_BASE64_URL_SAFE_NOPAD: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(
r"^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-][AQgw]|[A-Za-z0-9_-]{2}[AEIMQUYcgkosw048])?$",
)
Expand Down Expand Up @@ -72,7 +73,7 @@ pub(crate) fn decode_base64_urlsafe_no_pad(
}

pub(crate) fn parse_pem(input: &[u8]) -> anyhow::Result<(String, Vec<u8>)> {
static RE_PEM: Lazy<regex::bytes::Regex> = Lazy::new(|| {
static RE_PEM: LazyLock<regex::bytes::Regex> = LazyLock::new(|| {
regex::bytes::Regex::new(concat!(
r"^",
r"-----BEGIN ([A-Z0-9 -]+)-----[\t ]*(?:\r\n|[\r\n])",
Expand All @@ -83,8 +84,8 @@ pub(crate) fn parse_pem(input: &[u8]) -> anyhow::Result<(String, Vec<u8>)> {
.unwrap()
});

static RE_FILTER: Lazy<regex::bytes::Regex> =
Lazy::new(|| regex::bytes::Regex::new("[\t\r\n ]").unwrap());
static RE_FILTER: LazyLock<regex::bytes::Regex> =
LazyLock::new(|| regex::bytes::Regex::new("[\t\r\n ]").unwrap());

let result = if let Some(caps) = RE_PEM.captures(input) {
match (caps.get(1), caps.get(2), caps.get(3)) {
Expand Down
66 changes: 33 additions & 33 deletions src/util/oid.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
use once_cell::sync::Lazy;
use std::sync::LazyLock;

pub static OID_RSA_ENCRYPTION: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 1]));
pub static OID_RSA_ENCRYPTION: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 1]));

pub static OID_RSASSA_PSS: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 10]));
pub static OID_RSASSA_PSS: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 10]));

pub static OID_SHA1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 14, 3, 2, 26]));
pub static OID_SHA1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 14, 3, 2, 26]));

pub static OID_SHA256: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 1]));
pub static OID_SHA256: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 1]));

pub static OID_SHA384: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 2]));
pub static OID_SHA384: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 2]));

pub static OID_SHA512: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 3]));
pub static OID_SHA512: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[2, 16, 840, 1, 101, 3, 4, 2, 3]));

pub static OID_MGF1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 8]));
pub static OID_MGF1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 113549, 1, 1, 8]));

pub static OID_ID_EC_PUBLIC_KEY: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 2, 1]));
pub static OID_ID_EC_PUBLIC_KEY: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 2, 1]));

pub static OID_PRIME256V1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 3, 1, 7]));
pub static OID_PRIME256V1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 3, 1, 7]));

pub static OID_SECP384R1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 34]));
pub static OID_SECP384R1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 34]));

pub static OID_SECP521R1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 35]));
pub static OID_SECP521R1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 35]));

pub static OID_SECP256K1: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 10]));
pub static OID_SECP256K1: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 132, 0, 10]));

pub static OID_ED25519: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 112]));
pub static OID_ED25519: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 112]));

pub static OID_ED448: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 113]));
pub static OID_ED448: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 113]));

pub static OID_X25519: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 110]));
pub static OID_X25519: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 110]));

pub static OID_X448: Lazy<ObjectIdentifier> =
Lazy::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 111]));
pub static OID_X448: LazyLock<ObjectIdentifier> =
LazyLock::new(|| ObjectIdentifier::from_slice(&[1, 3, 101, 111]));

#[derive(Debug, Eq, PartialEq)]
pub struct ObjectIdentifier {
Expand Down

0 comments on commit 68ef5d5

Please sign in to comment.