| 1 |
|
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
|
| 6 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 7 |
pub enum SignatureStatus { |
| 8 |
Unsigned, |
| 9 |
|
| 10 |
|
| 11 |
Valid { |
| 12 |
key: KeyId, |
| 13 |
name: String, |
| 14 |
email: String, |
| 15 |
sealed: bool, |
| 16 |
revoked: bool, |
| 17 |
}, |
| 18 |
Invalid(String), |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
#[derive(Clone, Debug)] |
| 23 |
pub struct Rotation { |
| 24 |
pub old: KeyId, |
| 25 |
pub new: KeyId, |
| 26 |
pub identity: Identity, |
| 27 |
|
| 28 |
|
| 29 |
pub policy: Option<PolicyUpdate>, |
| 30 |
} |
| 31 |
|
| 32 |
impl Repo { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub fn create_identity( |
| 38 |
&mut self, |
| 39 |
name: &str, |
| 40 |
email: &str, |
| 41 |
scope: IdentityScope, |
| 42 |
) -> Result<(KeyId, Identity)> { |
| 43 |
self.create_identity_with(name, email, scope, None) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub fn create_identity_with( |
| 48 |
&mut self, |
| 49 |
name: &str, |
| 50 |
email: &str, |
| 51 |
scope: IdentityScope, |
| 52 |
expires: Option<u64>, |
| 53 |
) -> Result<(KeyId, Identity)> { |
| 54 |
let sk = SecretKey::generate(); |
| 55 |
let mut id = sk.identity(name, email, Hlc::now().wall_ms); |
| 56 |
id.expires = expires; |
| 57 |
let key = self.install_key(&id, &sk)?; |
| 58 |
self.set_author(&id, &key, scope)?; |
| 59 |
Ok((key, id)) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
fn install_key(&self, id: &Identity, sk: &SecretKey) -> Result<KeyId> { |
| 65 |
let key = self.store.put_identity(id)?; |
| 66 |
fs::create_dir_all(&self.keys_dir)?; |
| 67 |
let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 68 |
write_atomic(&path, &sk.encode(&key))?; |
| 69 |
write_atomic( |
| 70 |
&self.keys_dir.join(format!("{}.identity", key.to_hex())), |
| 71 |
&id.encode(), |
| 72 |
)?; |
| 73 |
#[cfg(unix)] |
| 74 |
{ |
| 75 |
use std::os::unix::fs::PermissionsExt; |
| 76 |
fs::set_permissions(&path, fs::Permissions::from_mode(0o600))?; |
| 77 |
} |
| 78 |
Ok(key) |
| 79 |
} |
| 80 |
|
| 81 |
fn set_author(&mut self, id: &Identity, key: &KeyId, scope: IdentityScope) -> Result<()> { |
| 82 |
match scope { |
| 83 |
IdentityScope::Repo => { |
| 84 |
self.config.set("author.name", &id.name)?; |
| 85 |
self.config.set("author.email", &id.email)?; |
| 86 |
self.config.set("author.key", &key.to_hex())?; |
| 87 |
self.save_config()?; |
| 88 |
} |
| 89 |
IdentityScope::User => { |
| 90 |
Config::set_user(&self.user_config, "author.name", &id.name)?; |
| 91 |
Config::set_user(&self.user_config, "author.email", &id.email)?; |
| 92 |
Config::set_user(&self.user_config, "author.key", &key.to_hex())?; |
| 93 |
self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?; |
| 94 |
} |
| 95 |
} |
| 96 |
Ok(()) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
fn author_scope(&self) -> IdentityScope { |
| 101 |
if self.config.repo_layer().contains_key("author.key") { |
| 102 |
IdentityScope::Repo |
| 103 |
} else { |
| 104 |
IdentityScope::User |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
fn replace_identity( |
| 112 |
&mut self, |
| 113 |
new: Identity, |
| 114 |
sk: SecretKey, |
| 115 |
command: &str, |
| 116 |
) -> Result<Rotation> { |
| 117 |
let Some((old, old_sk)) = self.secret_key()? else { |
| 118 |
return fail("no secret key: run `oxy identity create` first"); |
| 119 |
}; |
| 120 |
if new.previous != Some(old) { |
| 121 |
return fail("the successor identity must name the current key as previous"); |
| 122 |
} |
| 123 |
let new_key = self.install_key(&new, &sk)?; |
| 124 |
let attest = Attest { |
| 125 |
repo: self.id()?, |
| 126 |
hlc: Hlc::now(), |
| 127 |
body: AttestBody::Rotate { new: new_key }, |
| 128 |
}; |
| 129 |
let r = self.store.put_attest(&attest)?; |
| 130 |
self.store |
| 131 |
.put_signature(&r, &old_sk.sign(Domain::Attest, &old, &r))?; |
| 132 |
let policy = match self.current_policy()? { |
| 133 |
Some((_, sp)) if sp.policy.mentions(&old) => { |
| 134 |
let mut p = self.next_policy()?; |
| 135 |
p.substitute_key(&old, new.clone()); |
| 136 |
Some(self.sign_policy_with(p, &[(old, &old_sk), (new_key, &sk)], command)?) |
| 137 |
} |
| 138 |
_ => None, |
| 139 |
}; |
| 140 |
self.set_author(&new, &new_key, self.author_scope())?; |
| 141 |
Ok(Rotation { |
| 142 |
old, |
| 143 |
new: new_key, |
| 144 |
identity: new, |
| 145 |
policy, |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub fn rotate_identity(&mut self) -> Result<Rotation> { |
| 152 |
let Some((old, _)) = self.secret_key()? else { |
| 153 |
return fail("no secret key: run `oxy identity create` first"); |
| 154 |
}; |
| 155 |
let cur = self.identity(&old)?; |
| 156 |
let sk = SecretKey::generate(); |
| 157 |
let mut id = sk.identity(&cur.name, &cur.email, Hlc::now().wall_ms); |
| 158 |
id.previous = Some(old); |
| 159 |
id.delegates = cur.delegates.clone(); |
| 160 |
id.expires = cur.expires; |
| 161 |
self.replace_identity(id, sk, "oxy identity rotate") |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
pub fn renew_identity(&mut self, expires: Option<u64>) -> Result<Rotation> { |
| 166 |
let Some((old, sk)) = self.secret_key()? else { |
| 167 |
return fail("no secret key: run `oxy identity create` first"); |
| 168 |
}; |
| 169 |
let cur = self.identity(&old)?; |
| 170 |
let id = cur.successor(cur.delegates.clone(), expires, Hlc::now().wall_ms); |
| 171 |
self.replace_identity(id, sk, "oxy identity renew") |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pub fn delegate_add(&mut self, delegate: Identity) -> Result<Rotation> { |
| 177 |
let Some((old, sk)) = self.secret_key()? else { |
| 178 |
return fail("no secret key: run `oxy identity create` first"); |
| 179 |
}; |
| 180 |
let dk = self.store.put_identity(&delegate)?; |
| 181 |
if dk == old { |
| 182 |
return fail("an identity cannot delegate to itself"); |
| 183 |
} |
| 184 |
let cur = self.identity(&old)?; |
| 185 |
if cur.delegates.contains(&dk) { |
| 186 |
return fail(format!("{} is already a delegate", dk.short(12))); |
| 187 |
} |
| 188 |
let mut delegates = cur.delegates.clone(); |
| 189 |
delegates.push(dk); |
| 190 |
let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 191 |
self.replace_identity( |
| 192 |
id, |
| 193 |
sk, |
| 194 |
&format!("oxy identity delegate add {}", dk.short(12)), |
| 195 |
) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
pub fn delegate_remove(&mut self, delegate: &KeyId) -> Result<Rotation> { |
| 200 |
let Some((old, sk)) = self.secret_key()? else { |
| 201 |
return fail("no secret key: run `oxy identity create` first"); |
| 202 |
}; |
| 203 |
let cur = self.identity(&old)?; |
| 204 |
if !cur.delegates.contains(delegate) { |
| 205 |
return fail(format!("{} is not a delegate", delegate.short(12))); |
| 206 |
} |
| 207 |
let delegates = cur |
| 208 |
.delegates |
| 209 |
.iter() |
| 210 |
.copied() |
| 211 |
.filter(|d| d != delegate) |
| 212 |
.collect(); |
| 213 |
let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 214 |
self.replace_identity( |
| 215 |
id, |
| 216 |
sk, |
| 217 |
&format!("oxy identity delegate remove {}", delegate.short(12)), |
| 218 |
) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
pub fn revoke_identity(&self, key: &KeyId, reason: &str) -> Result<Option<PolicyUpdate>> { |
| 225 |
let Some((own, sk)) = self.secret_key()? else { |
| 226 |
return fail("no secret key: run `oxy identity create` first"); |
| 227 |
}; |
| 228 |
let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 229 |
let is_root = policy.as_ref().is_some_and(|p| p.roots.contains(&own)); |
| 230 |
if *key != own && !is_root { |
| 231 |
return fail("only the key's holder or a policy root can revoke it"); |
| 232 |
} |
| 233 |
let attest = Attest { |
| 234 |
repo: self.id()?, |
| 235 |
hlc: Hlc::now(), |
| 236 |
body: AttestBody::Revoke { |
| 237 |
key: *key, |
| 238 |
reason: reason.into(), |
| 239 |
}, |
| 240 |
}; |
| 241 |
let r = self.store.put_attest(&attest)?; |
| 242 |
self.store |
| 243 |
.put_signature(&r, &sk.sign(Domain::Attest, &own, &r))?; |
| 244 |
let dir = self.oxy.join("revoked"); |
| 245 |
fs::create_dir_all(&dir)?; |
| 246 |
write_atomic(&dir.join(key.to_hex()), r.to_hex().as_bytes())?; |
| 247 |
match policy { |
| 248 |
Some(p) if p.mentions(key) => Ok(Some(self.policy_revoke(key)?)), |
| 249 |
_ => Ok(None), |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
pub fn is_revoked(&self, key: &KeyId) -> Result<bool> { |
| 255 |
if self.oxy.join("revoked").join(key.to_hex()).exists() { |
| 256 |
return Ok(true); |
| 257 |
} |
| 258 |
Ok(self |
| 259 |
.current_policy()? |
| 260 |
.is_some_and(|(_, sp)| sp.policy.is_revoked(key))) |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
pub fn secret_key(&self) -> Result<Option<(KeyId, SecretKey)>> { |
| 267 |
let Some(key) = self.config.author_key else { |
| 268 |
return Ok(None); |
| 269 |
}; |
| 270 |
let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 271 |
match fs::read(&path) { |
| 272 |
Ok(b) => { |
| 273 |
let (k, sk) = SecretKey::decode(&b)?; |
| 274 |
if k != key { |
| 275 |
return fail(format!( |
| 276 |
"{} holds a different key than its name", |
| 277 |
path.display() |
| 278 |
)); |
| 279 |
} |
| 280 |
if !self.store.has(&key) { |
| 281 |
let id_path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 282 |
if let Ok(bytes) = fs::read(&id_path) { |
| 283 |
let id = Identity::decode(&bytes)?; |
| 284 |
if id.hash() == key { |
| 285 |
self.store.put_identity(&id)?; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
Ok(Some((k, sk))) |
| 290 |
} |
| 291 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 292 |
Err(e) => Err(e.into()), |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
pub fn identity(&self, key: &KeyId) -> Result<Identity> { |
| 300 |
match self.store.get_identity(key) { |
| 301 |
Ok(id) => Ok(id), |
| 302 |
Err(e) => { |
| 303 |
let path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 304 |
match fs::read(&path) { |
| 305 |
Ok(bytes) => { |
| 306 |
let id = Identity::decode(&bytes)?; |
| 307 |
if id.hash() != *key { |
| 308 |
return fail(format!( |
| 309 |
"{} does not hold identity {}", |
| 310 |
path.display(), |
| 311 |
key.short(12) |
| 312 |
)); |
| 313 |
} |
| 314 |
Ok(id) |
| 315 |
} |
| 316 |
Err(_) => Err(e.into()), |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
pub fn sign_change(&self, hash: &ChangeHash) -> Result<Signature> { |
| 324 |
let Some((key, sk)) = self.secret_key()? else { |
| 325 |
return fail("no secret key: run `oxy identity create` first"); |
| 326 |
}; |
| 327 |
let sig = sk.sign(Domain::Change, &key, hash); |
| 328 |
self.store.put_signature(hash, &sig)?; |
| 329 |
Ok(sig) |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
pub fn verify_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 334 |
let change = self.store.get_change_meta(hash)?; |
| 335 |
let sigs = self.store.signatures(hash)?; |
| 336 |
if sigs.is_empty() { |
| 337 |
return Ok(SignatureStatus::Unsigned); |
| 338 |
} |
| 339 |
let mut last_err = String::new(); |
| 340 |
for sig in &sigs { |
| 341 |
match self.check_signer(&change, sig, Domain::Change, hash) { |
| 342 |
Ok(id) => { |
| 343 |
return Ok(SignatureStatus::Valid { |
| 344 |
key: sig.key, |
| 345 |
name: id.name, |
| 346 |
email: id.email, |
| 347 |
sealed: false, |
| 348 |
revoked: self.is_revoked(&sig.key)?, |
| 349 |
}) |
| 350 |
} |
| 351 |
Err(e) => last_err = e, |
| 352 |
} |
| 353 |
} |
| 354 |
Ok(SignatureStatus::Invalid(last_err)) |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
fn check_signer( |
| 359 |
&self, |
| 360 |
change: &crate::state::ChangeMeta, |
| 361 |
sig: &Signature, |
| 362 |
domain: Domain, |
| 363 |
target: &ObjectRef, |
| 364 |
) -> std::result::Result<Identity, String> { |
| 365 |
let id = self |
| 366 |
.store |
| 367 |
.get_identity(&sig.key) |
| 368 |
.map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
let authorised = change.author.is_zero() |
| 374 |
|| sig.key == change.author |
| 375 |
|| self |
| 376 |
.store |
| 377 |
.get_identity(&change.author) |
| 378 |
.map(|a| a.delegates.contains(&sig.key)) |
| 379 |
.unwrap_or(false); |
| 380 |
if !authorised { |
| 381 |
return Err(format!( |
| 382 |
"key {} is neither the author nor a delegate", |
| 383 |
sig.key.short(12) |
| 384 |
)); |
| 385 |
} |
| 386 |
if id.is_expired_at(change.hlc.wall_ms) { |
| 387 |
return Err(format!( |
| 388 |
"key {} had expired when the change was made", |
| 389 |
sig.key.short(12) |
| 390 |
)); |
| 391 |
} |
| 392 |
verify_signature(&id, sig, domain, target).map_err(|e| e.to_string())?; |
| 393 |
Ok(id) |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
pub fn authenticate_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 399 |
let direct = self.verify_change(hash)?; |
| 400 |
if matches!(direct, SignatureStatus::Valid { .. }) { |
| 401 |
return Ok(direct); |
| 402 |
} |
| 403 |
let change = self.store.get_change_meta(hash)?; |
| 404 |
let mut last_err = match &direct { |
| 405 |
SignatureStatus::Invalid(e) => e.clone(), |
| 406 |
_ => String::new(), |
| 407 |
}; |
| 408 |
for seal_ref in self.store.seals_for(hash)? { |
| 409 |
let attest = self.store.get_attest(&seal_ref)?; |
| 410 |
let AttestBody::Seal { hashes } = &attest.body else { |
| 411 |
continue; |
| 412 |
}; |
| 413 |
if !hashes.contains(hash) { |
| 414 |
continue; |
| 415 |
} |
| 416 |
for sig in self.store.signatures(&seal_ref)? { |
| 417 |
match self.check_signer(&change, &sig, Domain::Attest, &seal_ref) { |
| 418 |
Ok(id) => { |
| 419 |
return Ok(SignatureStatus::Valid { |
| 420 |
key: sig.key, |
| 421 |
name: id.name, |
| 422 |
email: id.email, |
| 423 |
sealed: true, |
| 424 |
revoked: self.is_revoked(&sig.key)?, |
| 425 |
}) |
| 426 |
} |
| 427 |
Err(e) => last_err = e, |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
if last_err.is_empty() { |
| 432 |
Ok(SignatureStatus::Unsigned) |
| 433 |
} else { |
| 434 |
Ok(SignatureStatus::Invalid(last_err)) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
pub fn seal(&self, hashes: &[ChangeHash]) -> Result<ObjectRef> { |
| 443 |
if hashes.is_empty() { |
| 444 |
return fail("nothing to seal"); |
| 445 |
} |
| 446 |
if hashes.len() > crate::attest::MAX_SEAL_HASHES { |
| 447 |
return fail(format!( |
| 448 |
"a seal covers at most {} changes", |
| 449 |
crate::attest::MAX_SEAL_HASHES |
| 450 |
)); |
| 451 |
} |
| 452 |
for h in hashes { |
| 453 |
if !self.store.has(h) { |
| 454 |
return fail(format!("unknown change {}", h.short(12))); |
| 455 |
} |
| 456 |
} |
| 457 |
let mut sorted = hashes.to_vec(); |
| 458 |
sorted.sort(); |
| 459 |
sorted.dedup(); |
| 460 |
let sa = self.sign_attest(&Attest::seal(self.id()?, sorted.clone()))?; |
| 461 |
let r = self.store.put_attest(&sa.attest)?; |
| 462 |
for sig in &sa.sigs { |
| 463 |
self.store.put_signature(&r, sig)?; |
| 464 |
} |
| 465 |
for h in &sorted { |
| 466 |
self.store.index_seal(h, &r)?; |
| 467 |
} |
| 468 |
Ok(r) |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
pub fn sealable(&self) -> Result<Vec<ChangeHash>> { |
| 474 |
let Some((key, _)) = self.secret_key()? else { |
| 475 |
return fail("no identity: run oxy identity create"); |
| 476 |
}; |
| 477 |
let heads = self.bookmark_heads()?; |
| 478 |
let state = self.load_state_history(&heads)?; |
| 479 |
let mut out = Vec::new(); |
| 480 |
for (h, meta) in state.changes() { |
| 481 |
if (meta.author == key || meta.author.is_zero()) |
| 482 |
&& !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 483 |
{ |
| 484 |
out.push(*h); |
| 485 |
} |
| 486 |
} |
| 487 |
out.sort(); |
| 488 |
Ok(out) |
| 489 |
} |
| 490 |
|
| 491 |
pub(crate) fn sign_attest(&self, attest: &Attest) -> Result<SignedAttest> { |
| 492 |
let Some((key, sk)) = self.secret_key()? else { |
| 493 |
return fail("no secret key: run `oxy identity create` first"); |
| 494 |
}; |
| 495 |
let r = self.store.put_attest(attest)?; |
| 496 |
let sig = sk.sign(Domain::Attest, &key, &r); |
| 497 |
self.store.put_signature(&r, &sig)?; |
| 498 |
if let AttestBody::Seal { hashes } = &attest.body { |
| 499 |
for h in hashes { |
| 500 |
self.store.index_seal(h, &r)?; |
| 501 |
} |
| 502 |
} |
| 503 |
Ok(SignedAttest { |
| 504 |
attest: attest.clone(), |
| 505 |
sigs: vec![sig], |
| 506 |
}) |
| 507 |
} |
| 508 |
} |