| 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 | 40× | pub fn create_identity( |
| 38 | 40× | &mut self, |
| 39 | 40× | name: &str, |
| 40 | 40× | email: &str, |
| 41 | 40× | scope: IdentityScope, |
| 42 | 40× | ) -> Result<(KeyId, Identity)> { |
| 43 | 40× | self.create_identity_with(name, email, scope, None) |
| 44 | 40× | } |
| 45 | |
|
| 46 | | |
| 47 | 42× | pub fn create_identity_with( |
| 48 | 42× | &mut self, |
| 49 | 42× | name: &str, |
| 50 | 42× | email: &str, |
| 51 | 42× | scope: IdentityScope, |
| 52 | 42× | expires: Option<u64>, |
| 53 | 42× | ) -> Result<(KeyId, Identity)> { |
| 54 | 42× | let sk = SecretKey::generate(); |
| 55 | 42× | let mut id = sk.identity(name, email, Hlc::now().wall_ms); |
| 56 | 42× | id.expires = expires; |
| 57 | 42× | let key = self.install_key(&id, &sk)?; |
| 58 | 42× | self.set_author(&id, &key, scope)?; |
| 59 | 42× | Ok((key, id)) |
| 60 | 42× | } |
| 61 | |
|
| 62 | | |
| 63 | | |
| 64 | 56× | fn install_key(&self, id: &Identity, sk: &SecretKey) -> Result<KeyId> { |
| 65 | 56× | let key = self.store.put_identity(id)?; |
| 66 | 56× | fs::create_dir_all(&self.keys_dir)?; |
| 67 | 56× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 68 | 56× | crate::durable::write_atomic_private(&path, &sk.encode(&key))?; |
| 69 | 56× | write_atomic( |
| 70 | 56× | &self.keys_dir.join(format!("{}.identity", key.to_hex())), |
| 71 | 56× | &id.encode(), |
| 72 | 0× | )?; |
| 73 | 56× | Ok(key) |
| 74 | 56× | } |
| 75 | |
|
| 76 | 57× | fn set_author(&mut self, id: &Identity, key: &KeyId, scope: IdentityScope) -> Result<()> { |
| 77 | 57× | match scope { |
| 78 | | IdentityScope::Repo => { |
| 79 | 31× | self.config.set("author.name", &id.name)?; |
| 80 | 31× | self.config.set("author.email", &id.email)?; |
| 81 | 31× | self.config.set("author.key", &key.to_hex())?; |
| 82 | 31× | self.save_config()?; |
| 83 | | } |
| 84 | | IdentityScope::User => { |
| 85 | 26× | Config::set_user(&self.user_config, "author.name", &id.name)?; |
| 86 | 26× | Config::set_user(&self.user_config, "author.email", &id.email)?; |
| 87 | 26× | Config::set_user(&self.user_config, "author.key", &key.to_hex())?; |
| 88 | 26× | self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?; |
| 89 | | } |
| 90 | | } |
| 91 | 57× | Ok(()) |
| 92 | 57× | } |
| 93 | |
|
| 94 | | |
| 95 | 0× | fn author_scope(&self) -> IdentityScope { |
| 96 | 0× | if self.config.repo_layer().contains_key("author.key") { |
| 97 | 0× | IdentityScope::Repo |
| 98 | | } else { |
| 99 | 0× | IdentityScope::User |
| 100 | | } |
| 101 | 0× | } |
| 102 | |
|
| 103 | | |
| 104 | | |
| 105 | | |
| 106 | | |
| 107 | | |
| 108 | 14× | fn record_successor(&mut self, id: &Identity, old: &KeyId, new_key: &KeyId) -> Result<()> { |
| 109 | 28× | let names = |v: Option<&String>| v.and_then(|k| KeyId::from_hex(k)) == Some(*old); |
| 110 | 14× | let repo_pins = names(self.config.repo_layer().get("author.key")); |
| 111 | 14× | let user_entries = super::config::read_config_file(&self.user_config).unwrap_or_default(); |
| 112 | 14× | let user_names = names(user_entries.get("author.key")); |
| 113 | 14× | if !repo_pins && !user_names { |
| 114 | 0× | return self.set_author(id, new_key, self.author_scope()); |
| 115 | 14× | } |
| 116 | 14× | if user_names { |
| 117 | 5× | self.set_author(id, new_key, IdentityScope::User)?; |
| 118 | 9× | } |
| 119 | 14× | if repo_pins { |
| 120 | 10× | self.set_author(id, new_key, IdentityScope::Repo)?; |
| 121 | 4× | } |
| 122 | 14× | Ok(()) |
| 123 | 14× | } |
| 124 | |
|
| 125 | | |
| 126 | | |
| 127 | | |
| 128 | 14× | fn replace_identity( |
| 129 | 14× | &mut self, |
| 130 | 14× | new: Identity, |
| 131 | 14× | sk: SecretKey, |
| 132 | 14× | command: &str, |
| 133 | 14× | ) -> Result<Rotation> { |
| 134 | 14× | let Some((old, old_sk)) = self.secret_key()? else { |
| 135 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 136 | | }; |
| 137 | 14× | if new.previous != Some(old) { |
| 138 | 0× | return fail("the successor identity must name the current key as previous"); |
| 139 | 14× | } |
| 140 | 14× | let new_key = self.install_key(&new, &sk)?; |
| 141 | 14× | let attest = Attest { |
| 142 | 14× | repo: self.id()?, |
| 143 | 14× | hlc: Hlc::now(), |
| 144 | 14× | body: AttestBody::Rotate { new: new_key }, |
| 145 | | }; |
| 146 | 14× | let r = self.store.put_attest(&attest)?; |
| 147 | 14× | self.store |
| 148 | 14× | .put_signature(&r, &old_sk.sign(Domain::Attest, &old, &r))?; |
| 149 | 14× | let policy = match self.current_policy()? { |
| 150 | 6× | Some((_, sp)) if sp.policy.mentions(&old) => { |
| 151 | 6× | let mut p = self.next_policy()?; |
| 152 | 6× | p.substitute_key(&old, new.clone()); |
| 153 | 6× | Some(self.sign_policy_with(p, &[(old, &old_sk), (new_key, &sk)], command)?) |
| 154 | | } |
| 155 | 8× | _ => None, |
| 156 | | }; |
| 157 | 14× | self.record_successor(&new, &old, &new_key)?; |
| 158 | 14× | Ok(Rotation { |
| 159 | 14× | old, |
| 160 | 14× | new: new_key, |
| 161 | 14× | identity: new, |
| 162 | 14× | policy, |
| 163 | 14× | }) |
| 164 | 14× | } |
| 165 | |
|
| 166 | | |
| 167 | | |
| 168 | 2× | pub fn rotate_identity(&mut self) -> Result<Rotation> { |
| 169 | 2× | let Some((old, _)) = self.secret_key()? else { |
| 170 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 171 | | }; |
| 172 | 2× | let cur = self.identity(&old)?; |
| 173 | 2× | let sk = SecretKey::generate(); |
| 174 | 2× | let mut id = sk.identity(&cur.name, &cur.email, Hlc::now().wall_ms); |
| 175 | 2× | id.previous = Some(old); |
| 176 | 2× | id.delegates = cur.delegates.clone(); |
| 177 | 2× | id.expires = cur.expires; |
| 178 | 2× | self.replace_identity(id, sk, "oxy identity rotate") |
| 179 | 2× | } |
| 180 | |
|
| 181 | | |
| 182 | 1× | pub fn renew_identity(&mut self, expires: Option<u64>) -> Result<Rotation> { |
| 183 | 1× | let Some((old, sk)) = self.secret_key()? else { |
| 184 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 185 | | }; |
| 186 | 1× | let cur = self.identity(&old)?; |
| 187 | 1× | let id = cur.successor(cur.delegates.clone(), expires, Hlc::now().wall_ms); |
| 188 | 1× | self.replace_identity(id, sk, "oxy identity renew") |
| 189 | 1× | } |
| 190 | |
|
| 191 | | |
| 192 | | |
| 193 | | |
| 194 | 10× | pub fn delegate_add( |
| 195 | 10× | &mut self, |
| 196 | 10× | delegate: Identity, |
| 197 | 10× | scope: Scope, |
| 198 | 10× | expires: Option<u64>, |
| 199 | 10× | ) -> Result<Rotation> { |
| 200 | 10× | self.delegate_add_many(vec![delegate], scope, expires) |
| 201 | 10× | } |
| 202 | |
|
| 203 | | |
| 204 | 11× | pub fn delegate_add_many( |
| 205 | 11× | &mut self, |
| 206 | 11× | new_delegates: Vec<Identity>, |
| 207 | 11× | scope: Scope, |
| 208 | 11× | expires: Option<u64>, |
| 209 | 11× | ) -> Result<Rotation> { |
| 210 | 11× | let Some((old, sk)) = self.secret_key()? else { |
| 211 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 212 | | }; |
| 213 | 11× | if scope.is_empty() { |
| 214 | 0× | return fail("a delegate needs at least one scope: changes, refs or seal"); |
| 215 | 11× | } |
| 216 | 11× | if new_delegates.is_empty() { |
| 217 | 0× | return fail("name at least one identity file"); |
| 218 | 11× | } |
| 219 | 11× | let cur = self.identity(&old)?; |
| 220 | 11× | let mut delegates = cur.delegates.clone(); |
| 221 | 11× | let mut added = Vec::new(); |
| 222 | 12× | for delegate in new_delegates { |
| 223 | 12× | let dk = self.store.put_identity(&delegate)?; |
| 224 | 12× | added.push(dk); |
| 225 | 12× | if dk == old { |
| 226 | 0× | return fail("an identity cannot delegate to itself"); |
| 227 | 12× | } |
| 228 | 12× | if cur.delegate(&dk).is_some() |
| 229 | 10× | || delegates |
| 230 | 10× | .iter() |
| 231 | 10× | .skip(cur.delegates.len()) |
| 232 | 10× | .any(|d| d.key == dk) |
| 233 | | { |
| 234 | 2× | return fail(format!( |
| 235 | | "{} is already a delegate; remove it first to change its scope or expiry", |
| 236 | 2× | dk.short(12) |
| 237 | | )); |
| 238 | 10× | } |
| 239 | 10× | delegates.push(Delegate { |
| 240 | 10× | key: dk, |
| 241 | 10× | scope, |
| 242 | 10× | expires, |
| 243 | 10× | }); |
| 244 | | } |
| 245 | 9× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 246 | 9× | self.replace_identity( |
| 247 | 9× | id, |
| 248 | 9× | sk, |
| 249 | 9× | &format!( |
| 250 | | "oxy identity delegate add {} --scope {scope}", |
| 251 | 9× | added |
| 252 | 9× | .iter() |
| 253 | 10× | .map(|k| k.short(12)) |
| 254 | 9× | .collect::<Vec<_>>() |
| 255 | 9× | .join(" ") |
| 256 | | ), |
| 257 | | ) |
| 258 | 11× | } |
| 259 | |
|
| 260 | | |
| 261 | 3× | pub fn delegate_remove(&mut self, delegate: &KeyId) -> Result<Rotation> { |
| 262 | 3× | let Some((old, sk)) = self.secret_key()? else { |
| 263 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 264 | | }; |
| 265 | 3× | let cur = self.identity(&old)?; |
| 266 | 3× | if cur.delegate(delegate).is_none() { |
| 267 | 1× | return fail(format!("{} is not a delegate", delegate.short(12))); |
| 268 | 2× | } |
| 269 | 2× | let delegates = cur |
| 270 | 2× | .delegates |
| 271 | 2× | .iter() |
| 272 | 2× | .copied() |
| 273 | 2× | .filter(|d| d.key != *delegate) |
| 274 | 2× | .collect(); |
| 275 | 2× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 276 | 2× | self.replace_identity( |
| 277 | 2× | id, |
| 278 | 2× | sk, |
| 279 | 2× | &format!("oxy identity delegate remove {}", delegate.short(12)), |
| 280 | | ) |
| 281 | 3× | } |
| 282 | |
|
| 283 | | |
| 284 | | |
| 285 | | |
| 286 | | |
| 287 | | |
| 288 | 5× | pub fn revoke_identity(&self, key: &KeyId, reason: &str) -> Result<Option<PolicyUpdate>> { |
| 289 | 5× | let Some((own, sk)) = self.secret_key()? else { |
| 290 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 291 | | }; |
| 292 | 5× | let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 293 | 5× | let is_root = policy.as_ref().is_some_and(|p| p.roots.contains(&own)); |
| 294 | 5× | let is_delegate = self.identity(&own)?.delegate(key).is_some(); |
| 295 | 5× | if *key != own && !is_root && !is_delegate { |
| 296 | 1× | return fail("only the key's holder, the identity that delegated to it, or a policy root can revoke it"); |
| 297 | 4× | } |
| 298 | 4× | let attest = Attest { |
| 299 | 4× | repo: self.id()?, |
| 300 | 4× | hlc: Hlc::now(), |
| 301 | 4× | body: AttestBody::Revoke { |
| 302 | 4× | key: *key, |
| 303 | 4× | reason: reason.into(), |
| 304 | 4× | }, |
| 305 | | }; |
| 306 | 4× | let r = self.store.put_attest(&attest)?; |
| 307 | 4× | self.store |
| 308 | 4× | .put_signature(&r, &sk.sign(Domain::Attest, &own, &r))?; |
| 309 | 4× | let dir = self.oxy.join("revoked"); |
| 310 | 4× | fs::create_dir_all(&dir)?; |
| 311 | 4× | write_atomic(&dir.join(key.to_hex()), r.to_hex().as_bytes())?; |
| 312 | 1× | match policy { |
| 313 | 1× | Some(p) if p.mentions(key) => Ok(Some(self.policy_revoke(key)?)), |
| 314 | 3× | _ => Ok(None), |
| 315 | | } |
| 316 | 5× | } |
| 317 | |
|
| 318 | | |
| 319 | 221× | pub fn is_revoked(&self, key: &KeyId) -> Result<bool> { |
| 320 | 221× | if self.oxy.join("revoked").join(key.to_hex()).exists() { |
| 321 | 5× | return Ok(true); |
| 322 | 216× | } |
| 323 | 216× | Ok(self |
| 324 | 216× | .current_policy()? |
| 325 | 216× | .is_some_and(|(_, sp)| sp.policy.is_revoked(key))) |
| 326 | 221× | } |
| 327 | |
|
| 328 | | |
| 329 | | |
| 330 | 566× | pub fn signing_key(&self) -> Option<KeyId> { |
| 331 | 566× | self.config.signer_key.or(self.config.author_key) |
| 332 | 566× | } |
| 333 | |
|
| 334 | | |
| 335 | | |
| 336 | | |
| 337 | | |
| 338 | 565× | pub fn secret_key(&self) -> Result<Option<(KeyId, SecretKey)>> { |
| 339 | 565× | let Some(key) = self.signing_key() else { |
| 340 | 194× | return Ok(None); |
| 341 | | }; |
| 342 | 371× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 343 | 371× | match fs::read(&path) { |
| 344 | 370× | Ok(b) => { |
| 345 | 370× | let (k, sk) = SecretKey::decode(&b)?; |
| 346 | 370× | if k != key { |
| 347 | 0× | return fail(format!( |
| 348 | | "{} holds a different key than its name", |
| 349 | 0× | path.display() |
| 350 | | )); |
| 351 | 370× | } |
| 352 | 370× | if !self.store.has(&key) { |
| 353 | 1× | let id_path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 354 | 1× | if let Ok(bytes) = fs::read(&id_path) { |
| 355 | 1× | let id = Identity::decode(&bytes)?; |
| 356 | 1× | if id.hash() == key { |
| 357 | 1× | self.store.put_identity(&id)?; |
| 358 | 0× | } |
| 359 | 0× | } |
| 360 | 369× | } |
| 361 | 370× | Ok(Some((k, sk))) |
| 362 | | } |
| 363 | 1× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 364 | 0× | Err(e) => Err(e.into()), |
| 365 | | } |
| 366 | 565× | } |
| 367 | |
|
| 368 | | |
| 369 | | |
| 370 | | |
| 371 | 58× | pub fn identity(&self, key: &KeyId) -> Result<Identity> { |
| 372 | 58× | match self.store.get_identity(key) { |
| 373 | 55× | Ok(id) => Ok(id), |
| 374 | 3× | Err(e) => { |
| 375 | 3× | let path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 376 | 3× | match fs::read(&path) { |
| 377 | 2× | Ok(bytes) => { |
| 378 | 2× | let id = Identity::decode(&bytes)?; |
| 379 | 2× | if id.hash() != *key { |
| 380 | 1× | return fail(format!( |
| 381 | | "{} does not hold identity {}", |
| 382 | 1× | path.display(), |
| 383 | 1× | key.short(12) |
| 384 | | )); |
| 385 | 1× | } |
| 386 | 1× | Ok(id) |
| 387 | | } |
| 388 | 1× | Err(_) => Err(e.into()), |
| 389 | | } |
| 390 | | } |
| 391 | | } |
| 392 | 58× | } |
| 393 | |
|
| 394 | | |
| 395 | 42× | pub fn sign_change(&self, hash: &ChangeHash) -> Result<Signature> { |
| 396 | 42× | let Some((key, sk)) = self.secret_key()? else { |
| 397 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 398 | | }; |
| 399 | 42× | let sig = sk.sign(Domain::Change, &key, hash); |
| 400 | 42× | self.store.put_signature(hash, &sig)?; |
| 401 | 42× | Ok(sig) |
| 402 | 42× | } |
| 403 | |
|
| 404 | | |
| 405 | 24× | pub fn verify_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 406 | 24× | self.verify_change_known(hash, None) |
| 407 | 24× | } |
| 408 | |
|
| 409 | | |
| 410 | | |
| 411 | | |
| 412 | 376× | fn verify_change_known( |
| 413 | 376× | &self, |
| 414 | 376× | hash: &ChangeHash, |
| 415 | 376× | known: Option<&crate::state::ChangeMeta>, |
| 416 | 376× | ) -> Result<SignatureStatus> { |
| 417 | 376× | let sigs = self.store.signatures(hash)?; |
| 418 | 376× | if sigs.is_empty() { |
| 419 | 295× | return Ok(SignatureStatus::Unsigned); |
| 420 | 81× | } |
| 421 | | let read; |
| 422 | 81× | let change = match known { |
| 423 | 15× | Some(m) => m, |
| 424 | | None => { |
| 425 | 66× | read = self.store.get_change_meta(hash)?; |
| 426 | 66× | &read |
| 427 | | } |
| 428 | | }; |
| 429 | 81× | let mut last_err = String::new(); |
| 430 | 81× | for sig in &sigs { |
| 431 | 81× | match self.check_signer(change, sig, Domain::Change, hash) { |
| 432 | 75× | Ok(id) => { |
| 433 | | return Ok(SignatureStatus::Valid { |
| 434 | 75× | key: sig.key, |
| 435 | 75× | name: id.name, |
| 436 | 75× | email: id.email, |
| 437 | | sealed: false, |
| 438 | 75× | revoked: self.is_revoked(&sig.key)?, |
| 439 | | }) |
| 440 | | } |
| 441 | 6× | Err(e) => last_err = e, |
| 442 | | } |
| 443 | | } |
| 444 | 6× | Ok(SignatureStatus::Invalid(last_err)) |
| 445 | 376× | } |
| 446 | |
|
| 447 | | |
| 448 | 219× | fn check_signer( |
| 449 | 219× | &self, |
| 450 | 219× | change: &crate::state::ChangeMeta, |
| 451 | 219× | sig: &Signature, |
| 452 | 219× | domain: Domain, |
| 453 | 219× | target: &ObjectRef, |
| 454 | 219× | ) -> std::result::Result<Identity, String> { |
| 455 | 219× | let id = self |
| 456 | 219× | .store |
| 457 | 219× | .get_identity(&sig.key) |
| 458 | 219× | .map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 459 | | |
| 460 | | |
| 461 | | |
| 462 | | |
| 463 | | |
| 464 | | |
| 465 | 219× | if !change.author.is_zero() && sig.key != change.author { |
| 466 | 23× | let needed = match domain { |
| 467 | 21× | Domain::Change => Scope::CHANGES, |
| 468 | 2× | _ => Scope::SEAL, |
| 469 | | }; |
| 470 | 23× | let delegation = self |
| 471 | 23× | .store |
| 472 | 23× | .get_identity(&change.author) |
| 473 | 23× | .ok() |
| 474 | 23× | .and_then(|a| a.delegate(&sig.key).copied()); |
| 475 | 18× | match delegation { |
| 476 | | None => { |
| 477 | 4× | return Err(format!( |
| 478 | 4× | "key {} is neither the author nor a delegate", |
| 479 | 4× | sig.key.short(12) |
| 480 | 4× | )) |
| 481 | | } |
| 482 | 19× | Some(d) if !d.scope.has(needed) => { |
| 483 | 1× | return Err(format!( |
| 484 | | "key {} is a delegate for {} only and may not {}", |
| 485 | 1× | sig.key.short(12), |
| 486 | | d.scope, |
| 487 | 1× | if needed == Scope::SEAL { |
| 488 | 1× | "seal" |
| 489 | | } else { |
| 490 | 0× | "sign changes" |
| 491 | | } |
| 492 | | )) |
| 493 | | } |
| 494 | 18× | Some(d) if !d.allows(needed, change.hlc.wall_ms) => { |
| 495 | 1× | return Err(format!( |
| 496 | 1× | "key {}'s delegation had expired when the change was made", |
| 497 | 1× | sig.key.short(12) |
| 498 | 1× | )) |
| 499 | | } |
| 500 | 17× | Some(_) => {} |
| 501 | | } |
| 502 | 196× | } |
| 503 | 213× | if id.is_expired_at(change.hlc.wall_ms) { |
| 504 | 1× | return Err(format!( |
| 505 | 1× | "key {} had expired when the change was made", |
| 506 | 1× | sig.key.short(12) |
| 507 | 1× | )); |
| 508 | 212× | } |
| 509 | 212× | verify_signature(&id, sig, domain, target).map_err(|e| e.to_string())?; |
| 510 | 211× | Ok(id) |
| 511 | 219× | } |
| 512 | |
|
| 513 | | |
| 514 | | |
| 515 | 281× | pub fn authenticate_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 516 | 281× | self.authenticate_change_known(hash, None) |
| 517 | 281× | } |
| 518 | |
|
| 519 | | |
| 520 | | |
| 521 | 352× | pub fn authenticate_change_known( |
| 522 | 352× | &self, |
| 523 | 352× | hash: &ChangeHash, |
| 524 | 352× | known: Option<&crate::state::ChangeMeta>, |
| 525 | 352× | ) -> Result<SignatureStatus> { |
| 526 | 352× | let direct = self.verify_change_known(hash, known)?; |
| 527 | 352× | if matches!(direct, SignatureStatus::Valid { .. }) { |
| 528 | 60× | return Ok(direct); |
| 529 | 292× | } |
| 530 | 292× | let mut last_err = match &direct { |
| 531 | 2× | SignatureStatus::Invalid(e) => e.clone(), |
| 532 | 290× | _ => String::new(), |
| 533 | | }; |
| 534 | 292× | let seals = self.store.seals_for(hash)?; |
| 535 | 292× | if seals.is_empty() { |
| 536 | 154× | return Ok(match last_err.is_empty() { |
| 537 | 152× | true => SignatureStatus::Unsigned, |
| 538 | 2× | false => SignatureStatus::Invalid(last_err), |
| 539 | | }); |
| 540 | 138× | } |
| 541 | | let read; |
| 542 | 138× | let change = match known { |
| 543 | 0× | Some(m) => m, |
| 544 | | None => { |
| 545 | 138× | read = self.store.get_change_meta(hash)?; |
| 546 | 138× | &read |
| 547 | | } |
| 548 | | }; |
| 549 | 138× | for seal_ref in seals { |
| 550 | 138× | let attest = self.store.get_attest(&seal_ref)?; |
| 551 | 138× | let AttestBody::Seal { hashes } = &attest.body else { |
| 552 | 0× | continue; |
| 553 | | }; |
| 554 | 138× | if !hashes.contains(hash) { |
| 555 | 0× | continue; |
| 556 | 138× | } |
| 557 | 138× | for sig in self.store.signatures(&seal_ref)? { |
| 558 | 138× | match self.check_signer(change, &sig, Domain::Attest, &seal_ref) { |
| 559 | 136× | Ok(id) => { |
| 560 | | return Ok(SignatureStatus::Valid { |
| 561 | 136× | key: sig.key, |
| 562 | 136× | name: id.name, |
| 563 | 136× | email: id.email, |
| 564 | | sealed: true, |
| 565 | 136× | revoked: self.is_revoked(&sig.key)?, |
| 566 | | }) |
| 567 | | } |
| 568 | 2× | Err(e) => last_err = e, |
| 569 | | } |
| 570 | | } |
| 571 | | } |
| 572 | 2× | if last_err.is_empty() { |
| 573 | 0× | Ok(SignatureStatus::Unsigned) |
| 574 | | } else { |
| 575 | 2× | Ok(SignatureStatus::Invalid(last_err)) |
| 576 | | } |
| 577 | 352× | } |
| 578 | |
|
| 579 | | |
| 580 | | |
| 581 | | |
| 582 | | |
| 583 | 1× | pub fn seal(&self, hashes: &[ChangeHash]) -> Result<ObjectRef> { |
| 584 | 1× | if hashes.is_empty() { |
| 585 | 0× | return fail("nothing to seal"); |
| 586 | 1× | } |
| 587 | 1× | if hashes.len() > crate::attest::MAX_SEAL_HASHES { |
| 588 | 0× | return fail(format!( |
| 589 | | "a seal covers at most {} changes", |
| 590 | | crate::attest::MAX_SEAL_HASHES |
| 591 | | )); |
| 592 | 1× | } |
| 593 | 1× | for h in hashes { |
| 594 | 1× | if !self.store.has(h) { |
| 595 | 0× | return fail(format!("unknown change {}", h.short(12))); |
| 596 | 1× | } |
| 597 | | } |
| 598 | 1× | let mut sorted = hashes.to_vec(); |
| 599 | 1× | sorted.sort(); |
| 600 | 1× | sorted.dedup(); |
| 601 | 1× | let sa = self.sign_attest(&Attest::seal(self.id()?, sorted.clone()))?; |
| 602 | 1× | let r = self.store.put_attest(&sa.attest)?; |
| 603 | 1× | for sig in &sa.sigs { |
| 604 | 1× | self.store.put_signature(&r, sig)?; |
| 605 | | } |
| 606 | 1× | for h in &sorted { |
| 607 | 1× | self.store.index_seal(h, &r)?; |
| 608 | | } |
| 609 | 1× | Ok(r) |
| 610 | 1× | } |
| 611 | |
|
| 612 | | |
| 613 | | |
| 614 | 0× | pub fn sealable(&self) -> Result<Vec<ChangeHash>> { |
| 615 | 0× | let Some((key, _)) = self.secret_key()? else { |
| 616 | 0× | return fail("no identity: run oxy identity create"); |
| 617 | | }; |
| 618 | 0× | let heads = self.bookmark_heads()?; |
| 619 | 0× | let state = self.load_state_history(&heads)?; |
| 620 | 0× | let mut out = Vec::new(); |
| 621 | 0× | for (h, meta) in state.changes() { |
| 622 | 0× | if (meta.author == key || meta.author.is_zero()) |
| 623 | 0× | && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 624 | 0× | { |
| 625 | 0× | out.push(*h); |
| 626 | 0× | } |
| 627 | | } |
| 628 | 0× | out.sort(); |
| 629 | 0× | Ok(out) |
| 630 | 0× | } |
| 631 | |
|
| 632 | 118× | pub(crate) fn sign_attest(&self, attest: &Attest) -> Result<SignedAttest> { |
| 633 | 118× | let Some((key, sk)) = self.secret_key()? else { |
| 634 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 635 | | }; |
| 636 | 118× | let r = self.store.put_attest(attest)?; |
| 637 | 116× | let sig = sk.sign(Domain::Attest, &key, &r); |
| 638 | 116× | self.store.put_signature(&r, &sig)?; |
| 639 | 116× | if let AttestBody::Seal { hashes } = &attest.body { |
| 640 | 38× | for h in hashes { |
| 641 | 38× | self.store.index_seal(h, &r)?; |
| 642 | | } |
| 643 | 81× | } |
| 644 | 116× | Ok(SignedAttest { |
| 645 | 116× | attest: attest.clone(), |
| 646 | 116× | sigs: vec![sig], |
| 647 | 116× | }) |
| 648 | 118× | } |
| 649 | | } |