use crate::algorithm::{JweAlgorithm, JweEncryption, JwsAlgorithm};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Availability {
Supported,
RequiresFeature(&'static str),
RecognizedNotSupported,
RefusedByPolicy(&'static str),
}
impl Availability {
pub fn is_available(self) -> bool {
match self {
Availability::Supported => true,
Availability::RequiresFeature(feature) => feature_enabled(feature),
Availability::RecognizedNotSupported | Availability::RefusedByPolicy(_) => false,
}
}
pub fn label(self) -> &'static str {
match self {
Availability::Supported => "supported",
Availability::RequiresFeature(_) => "feature-gated",
Availability::RecognizedNotSupported => "recognized, unsupported",
Availability::RefusedByPolicy(_) => "refused by policy",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
Sign,
Verify,
Encrypt,
Decrypt,
DeriveKey,
Generate,
Import,
Export,
Thumbprint,
}
impl Operation {
pub fn as_str(self) -> &'static str {
match self {
Operation::Sign => "sign",
Operation::Verify => "verify",
Operation::Encrypt => "encrypt",
Operation::Decrypt => "decrypt",
Operation::DeriveKey => "deriveKey",
Operation::Generate => "generate",
Operation::Import => "import",
Operation::Export => "export",
Operation::Thumbprint => "thumbprint",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
JwsAlgorithm,
JweKeyManagement,
JweContentEncryption,
KeyFormat,
KeyType,
}
impl Kind {
pub fn as_str(self) -> &'static str {
match self {
Kind::JwsAlgorithm => "JWS algorithms",
Kind::JweKeyManagement => "JWE key management",
Kind::JweContentEncryption => "JWE content encryption",
Kind::KeyFormat => "Key formats",
Kind::KeyType => "Key types and curves",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Capability {
pub kind: Kind,
pub identifier: &'static str,
pub availability: Availability,
pub operations: &'static [Operation],
pub note: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Feature {
pub name: &'static str,
pub enabled: bool,
pub effect: &'static str,
}
pub fn feature_enabled(name: &str) -> bool {
#[allow(clippy::match_like_matches_macro)] match name {
"post-quantum" => cfg!(feature = "post-quantum"),
"rsa-oaep" => cfg!(feature = "rsa-oaep"),
"deprecated" => cfg!(feature = "deprecated"),
_ => false,
}
}
pub fn features() -> Vec<Feature> {
vec![
Feature {
name: "post-quantum",
enabled: feature_enabled("post-quantum"),
effect: "ML-DSA and composite signature algorithms, and their key generation",
},
Feature {
name: "rsa-oaep",
enabled: feature_enabled("rsa-oaep"),
effect: "RSA-OAEP key transport with SHA-1",
},
Feature {
name: "deprecated",
enabled: feature_enabled("deprecated"),
effect: "legacy identifiers this crate otherwise refuses: `RSA-OAEP` (SHA-1 label) \
and the `none` algorithm identifier",
},
]
}
pub fn jws_algorithms() -> Vec<Capability> {
const SIGN_VERIFY: &[Operation] = &[Operation::Sign, Operation::Verify];
let mut rows = vec![
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::HS256.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "symmetric; requires a key of at least the digest size",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::HS384.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "symmetric",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::HS512.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "symmetric",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::RS256.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSA keys must be at least 2048 bits",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::RS384.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSA keys must be at least 2048 bits",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::RS512.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSA keys must be at least 2048 bits",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::PS256.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSASSA-PSS; an imported RSA-PSS key (SPKI with id-RSASSA-PSS parameters) is \
refused, so use a plain RSA key",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::PS384.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSASSA-PSS",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::PS512.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "RSASSA-PSS",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::ES256.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "requires crv P-256",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::ES384.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "requires crv P-384",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::ES512.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "requires crv P-521",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::EdDSA.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note:
"curve-dependent Ed25519 / Ed448; application allowlists keep wire names distinct",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::Ed25519.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "fully specified identifier (RFC 9864); same primitive as `EdDSA`",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::Ed448.as_str(),
availability: Availability::Supported,
operations: SIGN_VERIFY,
note: "fully specified pure Ed448; empty context (RFC 8037 / RFC 9864)",
},
Capability {
kind: Kind::JwsAlgorithm,
identifier: JwsAlgorithm::ES256K.as_str(),
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "secp256k1 is not supported by the backend; the identifier parses and every \
operation refuses it",
},
];
#[cfg(feature = "post-quantum")]
let pq: &[(&str, &[Operation], &str)] = &[
("ML-DSA-44", SIGN_VERIFY, "FIPS 204"),
("ML-DSA-65", SIGN_VERIFY, "FIPS 204"),
("ML-DSA-87", SIGN_VERIFY, "FIPS 204"),
(
"ML-DSA-44-ES256",
SIGN_VERIFY,
"composite, draft-ietf-jose-pq-composite-sigs-07",
),
(
"ML-DSA-65-ES256",
SIGN_VERIFY,
"composite, draft-ietf-jose-pq-composite-sigs-07",
),
(
"ML-DSA-65-ES384",
SIGN_VERIFY,
"composite, draft-ietf-jose-pq-composite-sigs-07",
),
(
"ML-DSA-87-ES384",
SIGN_VERIFY,
"composite, draft-ietf-jose-pq-composite-sigs-07",
),
(
"ML-DSA-44-Ed25519",
SIGN_VERIFY,
"composite, draft-ietf-jose-pq-composite-sigs-07",
),
];
#[cfg(not(feature = "post-quantum"))]
let pq: &[(&str, &[Operation], &str)] = &[
("ML-DSA-44", &[], "FIPS 204"),
("ML-DSA-65", &[], "FIPS 204"),
("ML-DSA-87", &[], "FIPS 204"),
("ML-DSA-44-ES256", &[], "composite"),
("ML-DSA-65-ES256", &[], "composite"),
("ML-DSA-65-ES384", &[], "composite"),
("ML-DSA-87-ES384", &[], "composite"),
("ML-DSA-44-Ed25519", &[], "composite"),
];
for (identifier, operations, note) in pq {
rows.push(Capability {
kind: Kind::JwsAlgorithm,
identifier,
availability: pq_availability(identifier),
operations: pq_operations(identifier, operations),
note,
});
}
rows.push(Capability {
kind: Kind::JwsAlgorithm,
identifier: "none",
availability: Availability::RefusedByPolicy(
"unsecured JWS/JWT: the `deprecated` feature exposes the identifier, and verification \
of an unsecured token remains refused",
),
operations: &[],
note: "RFC 8725 §3.1 forbids accepting it; the identifier exists for inspection paths only",
});
rows
}
pub fn jwe_algorithms() -> Vec<Capability> {
const BOTH: &[Operation] = &[Operation::Encrypt, Operation::Decrypt];
let key_based = [
JweAlgorithm::A128KW,
JweAlgorithm::A192KW,
JweAlgorithm::A256KW,
];
let mut rows: Vec<Capability> = key_based
.iter()
.map(|alg| Capability {
kind: Kind::JweKeyManagement,
identifier: alg.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "AES key wrapping with an `oct` key of the matching size",
})
.collect();
rows.extend([
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::Dir.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "the `enc` key is the recipient key; size must match the content encryption",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::A128Gcmkw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "iv and tag are always library-generated, never read from a recipient header",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::A192Gcmkw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "iv and tag are always library-generated",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::A256Gcmkw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "iv and tag are always library-generated",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: rsa_oaep_identifier(),
availability: if feature_enabled("rsa-oaep") {
Availability::Supported
} else {
Availability::RequiresFeature("rsa-oaep")
},
operations: if feature_enabled("rsa-oaep") {
BOTH
} else {
&[]
},
note: "RSAES-OAEP with SHA-1 MGF1; compiled in only with `rsa-oaep`, and keys \
below 2048 bits are refused",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::RsaOaep256.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "RSA-OAEP with SHA-256",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::EcdhEs.as_str(),
availability: Availability::Supported,
operations: &[Operation::Encrypt, Operation::Decrypt, Operation::DeriveKey],
note: "direct ECDH-ES; the whole key is the derived KEK, so each `enc` needs the \
matching key size",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::EcdhEsA128kw.as_str(),
availability: Availability::Supported,
operations: &[Operation::Encrypt, Operation::Decrypt, Operation::DeriveKey],
note: "general serialization is refused for this family (single recipient per element)",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::EcdhEsA192kw.as_str(),
availability: Availability::Supported,
operations: &[Operation::Encrypt, Operation::Decrypt, Operation::DeriveKey],
note: "single recipient",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::EcdhEsA256kw.as_str(),
availability: Availability::Supported,
operations: &[Operation::Encrypt, Operation::Decrypt, Operation::DeriveKey],
note: "single recipient",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::Pbes2Hs256A128kw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "password-based: takes `Password`, and p2c is bounded (ADR 0021)",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::Pbes2Hs384A192kw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "password-based",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: JweAlgorithm::Pbes2Hs512A256kw.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "password-based",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: "RSA1_5",
availability: Availability::RefusedByPolicy(
"PKCS #1 v1.5 key encryption is a chosen-ciphertext oracle (BLEEP/BACA) and RFC \
8725 §4.1 forbids it",
),
operations: &[],
note: "never offered; the refusal names the algorithm so a misconfigured peer is \
visible",
},
Capability {
kind: Kind::JweKeyManagement,
identifier: "RSA-OAEP-384",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "not in the backend's key-management set; the identifier is unknown to the \
parser rather than silently accepted",
},
]);
rows
}
#[cfg(feature = "post-quantum")]
fn pq_availability(_identifier: &str) -> Availability {
Availability::Supported
}
#[cfg(not(feature = "post-quantum"))]
fn pq_availability(_identifier: &str) -> Availability {
Availability::RequiresFeature("post-quantum")
}
#[cfg(feature = "post-quantum")]
fn pq_operations<'a>(_identifier: &str, operations: &'a [Operation]) -> &'a [Operation] {
operations
}
#[cfg(not(feature = "post-quantum"))]
fn pq_operations<'a>(_identifier: &str, _operations: &'a [Operation]) -> &'a [Operation] {
&[]
}
#[cfg(feature = "rsa-oaep")]
fn rsa_oaep_identifier() -> &'static str {
JweAlgorithm::RsaOaep.as_str()
}
#[cfg(not(feature = "rsa-oaep"))]
fn rsa_oaep_identifier() -> &'static str {
"RSA-OAEP"
}
pub fn jwe_encryptions() -> Vec<Capability> {
const BOTH: &[Operation] = &[Operation::Encrypt, Operation::Decrypt];
[
JweEncryption::A128CbcHs256,
JweEncryption::A192CbcHs384,
JweEncryption::A256CbcHs512,
JweEncryption::A128GCM,
JweEncryption::A192GCM,
JweEncryption::A256GCM,
]
.iter()
.map(|enc| Capability {
kind: Kind::JweContentEncryption,
identifier: enc.as_str(),
availability: Availability::Supported,
operations: BOTH,
note: "iv and tag are library-generated; `enc` is always integrity-protected in the \
protected header",
})
.collect()
}
pub fn key_formats() -> Vec<Capability> {
const IMPORT: &[Operation] = &[Operation::Import];
const IMPORT_EXPORT: &[Operation] = &[Operation::Import, Operation::Export];
vec![
Capability {
kind: Kind::KeyFormat,
identifier: "JWK (JSON)",
availability: Availability::Supported,
operations: IMPORT_EXPORT,
note: "Jwk::from_json / Jwk::to_json",
},
Capability {
kind: Kind::KeyFormat,
identifier: "PKCS#8 DER / \"PRIVATE KEY\"",
availability: Availability::Supported,
operations: IMPORT_EXPORT,
note: "RSA, EC P-256/P-384/P-521, Ed25519, Ed448, X25519; export emits the narrower \
OneAsymmetricKey form (ADR 0022)",
},
Capability {
kind: Kind::KeyFormat,
identifier: "SPKI DER / \"PUBLIC KEY\"",
availability: Availability::Supported,
operations: IMPORT_EXPORT,
note: "OKP/X25519 has no SPKI form in this backend and is refused by name",
},
Capability {
kind: Kind::KeyFormat,
identifier: "PKCS#1 DER / \"RSA PRIVATE KEY\"",
availability: Availability::Supported,
operations: IMPORT,
note: "rebuilt from its parts and checked against itself (ADR 0023); multi-prime is \
refused",
},
Capability {
kind: Kind::KeyFormat,
identifier: "PKCS#1 \"RSA PUBLIC KEY\"",
availability: Availability::Supported,
operations: IMPORT,
note: "import only; export uses SPKI",
},
Capability {
kind: Kind::KeyFormat,
identifier: "SEC1 DER / \"EC PRIVATE KEY\"",
availability: Availability::Supported,
operations: IMPORT,
note: "must state its curve by name; explicit parameters are refused (ADR 0024)",
},
Capability {
kind: Kind::KeyFormat,
identifier: "Encrypted PKCS#8 / \"ENCRYPTED PRIVATE KEY\"",
availability: Availability::Supported,
operations: IMPORT_EXPORT,
note: "PBES2 + PBKDF2-HMAC-SHA2 + AES-CBC only, with a bounded work budget (ADR 0025)",
},
Capability {
kind: Kind::KeyFormat,
identifier: "X.509 certificate / \"CERTIFICATE\"",
availability: Availability::Supported,
operations: IMPORT,
note: "subject public key only; validates nothing about the certificate (ADR 0026)",
},
Capability {
kind: Kind::KeyFormat,
identifier: "PKCS#12 / JWK Set encryption",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "not implemented; a JWK Set is read as plain JSON only",
},
Capability {
kind: Kind::KeyFormat,
identifier: "password-protected legacy PEM (Proc-Type/DEK-Info)",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "recognized by the armor reader and refused: the encryption is its own key \
material, not a JOSE input",
},
]
}
pub fn key_types() -> Vec<Capability> {
const USE: &[Operation] = &[
Operation::Generate,
Operation::Import,
Operation::Export,
Operation::Thumbprint,
];
vec![
Capability {
kind: Kind::KeyType,
identifier: "RSA",
availability: Availability::Supported,
operations: USE,
note: "minimum 2048-bit modulus, enforced on import, generation and use",
},
Capability {
kind: Kind::KeyType,
identifier: "EC P-256",
availability: Availability::Supported,
operations: USE,
note: "ES256, ECDH-ES",
},
Capability {
kind: Kind::KeyType,
identifier: "EC P-384",
availability: Availability::Supported,
operations: USE,
note: "ES384, ECDH-ES",
},
Capability {
kind: Kind::KeyType,
identifier: "EC P-521",
availability: Availability::Supported,
operations: USE,
note: "ES512, ECDH-ES",
},
Capability {
kind: Kind::KeyType,
identifier: "EC secp256k1",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "no backend support; see ES256K",
},
Capability {
kind: Kind::KeyType,
identifier: "EC Brainpool curves",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "no backend support; a Brainpool certificate or SEC1 file is refused naming the \
curve",
},
Capability {
kind: Kind::KeyType,
identifier: "OKP Ed25519",
availability: Availability::Supported,
operations: USE,
note: "EdDSA / Ed25519; exports SPKI, unlike X25519",
},
Capability {
kind: Kind::KeyType,
identifier: "OKP X25519",
availability: Availability::Supported,
operations: USE,
note: "key agreement only; `deriveKey` on a signing key (and the reverse) is refused",
},
Capability {
kind: Kind::KeyType,
identifier: "OKP Ed448",
availability: Availability::Supported,
operations: USE,
note: "EdDSA / Ed448; pure Ed448, empty context; PKCS#8 and SPKI",
},
Capability {
kind: Kind::KeyType,
identifier: "OKP X448",
availability: Availability::RecognizedNotSupported,
operations: &[],
note: "backend primitive exists; JOSE agreement integration is not exposed",
},
Capability {
kind: Kind::KeyType,
identifier: "oct",
availability: Availability::Supported,
operations: USE,
note: "key size is checked against the algorithm that uses it",
},
Capability {
kind: Kind::KeyType,
identifier: "AKP (ML-DSA, composites)",
availability: Availability::RequiresFeature("post-quantum"),
operations: if feature_enabled("post-quantum") {
USE
} else {
&[]
},
note: "composite encodings have no PKCS#8/SPKI form, so PEM export is refused",
},
]
}
pub fn all() -> Vec<Capability> {
let mut rows = jws_algorithms();
rows.extend(jwe_algorithms());
rows.extend(jwe_encryptions());
rows.extend(key_formats());
rows.extend(key_types());
rows
}
pub fn find(kind: Kind, identifier: &str) -> Option<Capability> {
all()
.into_iter()
.find(|row| row.kind == kind && row.identifier == identifier)
}
pub fn available() -> Vec<Capability> {
all()
.into_iter()
.filter(|row| row.availability.is_available())
.collect()
}
pub fn markdown() -> String {
let mut out = String::new();
out.push_str("# Capability tables\n\nGenerated by `cargo run --example capabilities`.\n");
out.push_str(
"`tests/capability_report.rs` performs every advertised operation, so a row that ",
);
out.push_str("overstates itself fails the test suite.\n\n");
out.push_str("## Build features\n\n| feature | enabled here | effect |\n| --- | --- | --- |\n");
for feature in features() {
out.push_str(&format!(
"| `{}` | {} | {} |\n",
feature.name,
if feature.enabled { "yes" } else { "no" },
feature.effect
));
}
for kind in [
Kind::JwsAlgorithm,
Kind::JweKeyManagement,
Kind::JweContentEncryption,
Kind::KeyFormat,
Kind::KeyType,
] {
out.push_str(&format!("\n## {}\n\n", kind.as_str()));
out.push_str("| identifier | status | operations | note |\n| --- | --- | --- | --- |\n");
for row in all().into_iter().filter(|row| row.kind == kind) {
let operations = if row.operations.is_empty() {
"—".to_string()
} else {
row.operations
.iter()
.map(|op| op.as_str())
.collect::<Vec<_>>()
.join(", ")
};
let status = match row.availability {
Availability::RequiresFeature(feature) => {
format!("feature-gated (`{feature}`)")
}
Availability::RefusedByPolicy(reason) => {
format!("refused by policy — {reason}")
}
other => other.label().to_string(),
};
out.push_str(&format!(
"| `{}` | {status} | {operations} | {} |\n",
row.identifier, row.note
));
}
}
out
}