#[derive(Debug, Clone, Default)]
pub struct SigningKeySet {
entries: Vec<(String, [u8; 32])>,
}
const ED25519_ALGORITHM: &str = "ed25519";
impl SigningKeySet {
pub fn from_public_keys<I, S>(public_keys: I) -> Result<Self, crate::error::CryptoError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut entries = Vec::new();
for key in public_keys {
let b64 = key.as_ref();
let bytes = crate::crypto::ed25519::public_key_from_base64(b64)?;
entries.push((crate::crypto::ed25519::key_id(b64), bytes));
}
Ok(SigningKeySet { entries })
}
pub fn from_resources(resources: &[crate::models::signing_key::SigningKeyResource]) -> Self {
let entries = resources
.iter()
.filter(|r| {
r.attributes
.algorithm
.eq_ignore_ascii_case(ED25519_ALGORITHM)
})
.filter_map(|r| {
crate::crypto::ed25519::public_key_from_base64(&r.attributes.public_key)
.ok()
.map(|bytes| (r.id.clone(), bytes))
})
.collect();
SigningKeySet { entries }
}
pub fn find(&self, kid: &str) -> Option<&[u8; 32]> {
self.entries
.iter()
.find(|(candidate, _)| candidate == kid)
.map(|(_, key)| key)
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn kids(&self) -> impl Iterator<Item = &str> {
self.entries.iter().map(|(kid, _)| kid.as_str())
}
pub(crate) fn keys(&self) -> impl Iterator<Item = &[u8; 32]> {
self.entries.iter().map(|(_, key)| key)
}
pub(crate) fn find_verifying_key(
&self,
message: &[u8],
signature: &[u8],
) -> Result<Option<&[u8; 32]>, crate::error::CryptoError> {
for key in self.keys() {
match crate::crypto::ed25519::verify(key, message, signature) {
Ok(()) => return Ok(Some(key)),
Err(crate::error::CryptoError::InvalidSignature) => {
return Err(crate::error::CryptoError::InvalidSignature)
}
Err(_) => {}
}
}
Ok(None)
}
pub(crate) fn label_failure(
&self,
probe: Result<Vec<u8>, crate::error::CheckoutError>,
) -> crate::error::CheckoutError {
use crate::error::{CheckoutError, CryptoError};
let plaintext = match probe {
Ok(plaintext) => plaintext,
Err(
missing @ (CheckoutError::LicenseKeyMissing | CheckoutError::FingerprintMissing),
) => return missing,
Err(_) => return CryptoError::VerificationFailed.into(),
};
match crate::checkout::license_file::probe_kid(&plaintext) {
Ok(kid) if self.find(&kid).is_some() => CryptoError::VerificationFailed.into(),
Ok(kid) => CheckoutError::UnknownSigningKey { kid },
Err(_) => CryptoError::VerificationFailed.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ZERO_KEY_B64: &str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
const ZERO_KEY_KID: &str = "51643eac9777b63a";
fn resource(
id: &str,
algorithm: &str,
public_key: &str,
) -> crate::models::signing_key::SigningKeyResource {
serde_json::from_value(serde_json::json!({
"type": "signing-keys",
"id": id,
"attributes": {
"algorithm": algorithm,
"publicKey": public_key,
"status": "retired",
"created": "2026-01-01T00:00:00Z",
}
}))
.unwrap()
}
#[test]
fn a_key_embedded_in_the_binary_indexes_itself_by_its_computed_kid() {
let set = SigningKeySet::from_public_keys([ZERO_KEY_B64]).unwrap();
assert_eq!(set.len(), 1);
assert!(!set.is_empty());
assert!(set.find(ZERO_KEY_KID).is_some());
assert_eq!(set.kids().collect::<Vec<_>>(), vec![ZERO_KEY_KID]);
}
#[test]
fn a_mistyped_embedded_key_fails_loudly_rather_than_silently() {
assert!(SigningKeySet::from_public_keys(["not base64 at all"]).is_err());
assert!(
SigningKeySet::from_public_keys(["QUJD"]).is_err(),
"3 bytes is not a key"
);
}
#[test]
fn a_fetched_key_set_takes_the_kid_from_the_resource_id() {
let set =
SigningKeySet::from_resources(&[resource("deadbeefdeadbeef", "ed25519", ZERO_KEY_B64)]);
assert!(set.find("deadbeefdeadbeef").is_some());
assert!(set.find(ZERO_KEY_KID).is_none());
}
#[test]
fn one_unusable_published_key_does_not_strand_the_others() {
let set = SigningKeySet::from_resources(&[
resource("0000000000000000", "ml-dsa-44", ZERO_KEY_B64),
resource("1111111111111111", "ed25519", "!!!not base64!!!"),
resource("2222222222222222", "ed25519", ZERO_KEY_B64),
]);
assert_eq!(set.len(), 1);
assert!(set.find("2222222222222222").is_some());
assert!(set.find("0000000000000000").is_none());
assert!(set.find("1111111111111111").is_none());
}
#[test]
fn kid_matching_is_exact() {
let set = SigningKeySet::from_public_keys([ZERO_KEY_B64]).unwrap();
assert!(set.find(&ZERO_KEY_KID.to_uppercase()).is_none());
assert!(set.find("51643eac9777b63").is_none());
assert!(set.find("").is_none());
}
#[test]
fn an_empty_set_is_buildable_and_finds_nothing() {
let set = SigningKeySet::default();
assert!(set.is_empty());
assert_eq!(set.len(), 0);
assert!(set.find(ZERO_KEY_KID).is_none());
assert_eq!(SigningKeySet::from_resources(&[]).len(), 0);
assert_eq!(
SigningKeySet::from_public_keys(Vec::<String>::new())
.unwrap()
.len(),
0
);
}
}