#![forbid(unsafe_code)]
#![warn(missing_docs)]
pub mod algorithm;
pub mod authenticated;
pub mod base64url;
pub mod capability;
pub mod error;
pub mod header;
pub mod jwe;
pub mod jwk;
pub mod jws;
pub mod jwt;
pub(crate) mod member_map;
pub mod password;
#[cfg(test)]
mod security_tests;
pub use algorithm::{JweAlgorithm, JweEncryption, JwsAlgorithm};
pub use authenticated::Authenticated;
pub use error::{ErrorCategory, JoseError, Result};
pub use header::JoseHeader;
pub use jwk::JwkOp;
pub use password::Password;
pub const MIN_RSA_BITS: usize = 2048;
pub const MAX_RSA_BITS: usize = 8192;
pub const MAX_KEY_DER_BYTES: usize = 64 * 1024;
pub const MAX_TOKEN_BYTES: usize = 4 * 1024 * 1024;
pub const SIGNATURE_VERIFICATION_FAILED: &str = "signature verification failed";
pub(crate) fn ensure_emit_size(len: usize) -> Result<()> {
if len > MAX_TOKEN_BYTES {
return Err(JoseError::InvalidToken(format!(
"emitted token size {len} exceeds MAX_TOKEN_BYTES ({MAX_TOKEN_BYTES}); \
this crate would refuse to decode it"
)));
}
Ok(())
}
pub(crate) fn ensure_rsa_bits(bits: usize) -> Result<()> {
if bits < MIN_RSA_BITS {
return Err(JoseError::Key(format!(
"RSA key size {bits} bits is below the required minimum of {MIN_RSA_BITS}"
)));
}
if bits > MAX_RSA_BITS {
return Err(JoseError::Key(format!(
"RSA key size {bits} bits exceeds the supported maximum of {MAX_RSA_BITS}"
)));
}
Ok(())
}
pub(crate) fn ensure_rsa_modulus_bytes(bytes: &[u8]) -> Result<()> {
let bits = match bytes.iter().enumerate().find(|(_, byte)| **byte != 0) {
Some((index, first)) => {
(bytes.len() - index - 1) * 8 + (u8::BITS - first.leading_zeros()) as usize
}
None => 0,
};
ensure_rsa_bits(bits)
}
pub(crate) fn ensure_key_der_size(len: usize) -> Result<()> {
if len > MAX_KEY_DER_BYTES {
return Err(JoseError::Key(format!(
"DER key size {len} bytes exceeds MAX_KEY_DER_BYTES ({MAX_KEY_DER_BYTES})"
)));
}
Ok(())
}