use std::{fmt, io};
use zeroize::Zeroizing;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(any(target_os = "macos", test))]
mod requirement;
#[cfg(feature = "test-support")]
pub mod testing;
pub struct StoreKey(Zeroizing<[u8; 32]>);
impl StoreKey {
pub fn new(bytes: [u8; 32]) -> Self {
Self(Zeroizing::new(bytes))
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl fmt::Debug for StoreKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("StoreKey([REDACTED])")
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StoreAnchor {
pub revision: u64,
pub sha256: [u8; 32],
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AnchorState {
pub current: Option<StoreAnchor>,
pub high_water: u64,
}
impl AnchorState {
fn update(self, anchor: StoreAnchor) -> Result<Self, KeychainError> {
if self.current == Some(anchor) {
return Ok(self);
}
if anchor.revision <= self.high_water {
return Err(KeychainError::RevisionConflict);
}
Ok(Self {
current: Some(anchor),
high_water: anchor.revision,
})
}
fn clear(self, revision: u64) -> Result<Self, KeychainError> {
let repeat = self.current.is_none() && revision == self.high_water;
if revision == 0 || (revision <= self.high_water && !repeat) {
return Err(KeychainError::RevisionConflict);
}
Ok(Self {
current: None,
high_water: revision,
})
}
}
#[derive(Debug)]
pub struct KeychainState {
pub key: StoreKey,
pub anchor: AnchorState,
}
#[cfg(any(target_os = "macos", feature = "test-support"))]
const MAGIC: &[u8; 8] = b"SUBCKEY1";
#[cfg(any(target_os = "macos", feature = "test-support"))]
const ENCODED_LEN: usize = 89;
#[cfg(any(target_os = "macos", feature = "test-support"))]
impl KeychainState {
fn encode(&self) -> Zeroizing<Vec<u8>> {
let mut bytes = Zeroizing::new(Vec::with_capacity(ENCODED_LEN));
bytes.extend_from_slice(MAGIC);
bytes.extend_from_slice(self.key.as_bytes());
bytes.extend_from_slice(&self.anchor.high_water.to_be_bytes());
bytes.push(u8::from(self.anchor.current.is_some()));
let current = self.anchor.current.unwrap_or(StoreAnchor {
revision: 0,
sha256: [0; 32],
});
bytes.extend_from_slice(¤t.revision.to_be_bytes());
bytes.extend_from_slice(¤t.sha256);
bytes
}
fn decode(bytes: &[u8]) -> Result<Self, KeychainError> {
if bytes.len() != ENCODED_LEN || &bytes[..8] != MAGIC {
return Err(KeychainError::InvalidData);
}
let high_water = u64::from_be_bytes(bytes[40..48].try_into().unwrap());
let revision = u64::from_be_bytes(bytes[49..57].try_into().unwrap());
let sha256 = bytes[57..89].try_into().unwrap();
let current = match bytes[48] {
0 if revision == 0 && sha256 == [0; 32] => None,
1 if revision != 0 && revision == high_water => Some(StoreAnchor { revision, sha256 }),
_ => return Err(KeychainError::InvalidData),
};
Ok(Self {
key: StoreKey::new(bytes[8..40].try_into().unwrap()),
anchor: AnchorState {
current,
high_water,
},
})
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum KeychainError {
Unsupported,
Unavailable,
Denied,
Missing,
AlreadyExists,
InvalidData,
InvalidRequirement,
RevisionConflict,
OsStatus(i32),
Io(io::Error),
}
impl fmt::Display for KeychainError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Unsupported => "sealing requires a valid team-signed macOS image",
Self::Unavailable => "keychain or signing information unavailable",
Self::Denied => "key access denied; restore keychain access",
Self::Missing => "keychain item missing",
Self::AlreadyExists => "keychain item already exists",
Self::InvalidData => "invalid or unsupported keychain item format",
Self::InvalidRequirement => {
"signing requirement must bind the Apple anchor, identifier and team"
}
Self::RevisionConflict => "keychain revision must advance the high-water mark",
Self::OsStatus(_) => "Security framework operation failed",
Self::Io(_) => "test key-store I/O failed",
})
}
}
impl std::error::Error for KeychainError {}
pub struct KeychainStore {
#[cfg(target_os = "macos")]
inner: macos::Store,
}
impl KeychainStore {
pub fn open(service: &str) -> Result<Self, KeychainError> {
#[cfg(target_os = "macos")]
{
Ok(Self {
inner: macos::Store::open(service)?,
})
}
#[cfg(not(target_os = "macos"))]
{
let _ = service;
Err(KeychainError::Unsupported)
}
}
pub fn load(&self) -> Result<Option<KeychainState>, KeychainError> {
#[cfg(target_os = "macos")]
{
self.inner.load()
}
#[cfg(not(target_os = "macos"))]
{
Err(KeychainError::Unsupported)
}
}
pub fn create(&self, key: StoreKey) -> Result<(), KeychainError> {
#[cfg(target_os = "macos")]
{
self.inner.create(key)
}
#[cfg(not(target_os = "macos"))]
{
let _ = key;
Err(KeychainError::Unsupported)
}
}
pub fn update_anchor(&self, anchor: StoreAnchor) -> Result<(), KeychainError> {
self.change_anchor(|old| old.update(anchor))
}
pub fn clear_anchor(&self, unstamp_revision: u64) -> Result<(), KeychainError> {
self.change_anchor(|old| old.clear(unstamp_revision))
}
fn change_anchor(
&self,
change: impl FnOnce(AnchorState) -> Result<AnchorState, KeychainError>,
) -> Result<(), KeychainError> {
let mut state = self.load()?.ok_or(KeychainError::Missing)?;
state.anchor = change(state.anchor)?;
#[cfg(target_os = "macos")]
{
self.inner.replace(&state)
}
#[cfg(not(target_os = "macos"))]
{
Err(KeychainError::Unsupported)
}
}
pub fn delete(&self) -> Result<(), KeychainError> {
#[cfg(target_os = "macos")]
{
self.inner.delete()
}
#[cfg(not(target_os = "macos"))]
{
Err(KeychainError::Unsupported)
}
}
}
#[cfg(test)]
mod tests;