| 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 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 2× | pub fn create_user_identity( |
| 28 | 2× | name: Option<&str>, |
| 29 | 2× | email: Option<&str>, |
| 30 | 2× | expires: Option<u64>, |
| 31 | 2× | ) -> Result<(KeyId, Identity)> { |
| 32 | 2× | let user = default_user_config(); |
| 33 | 2× | let defaults = Config::load(Some(&user), Path::new(""))?; |
| 34 | 2× | let name = name.map(str::to_string).unwrap_or(defaults.author_name); |
| 35 | 2× | let email = email.map(str::to_string).unwrap_or(defaults.author_email); |
| 36 | 2× | let sk = SecretKey::generate(); |
| 37 | 2× | let mut id = sk.identity(&name, &email, Hlc::now().wall_ms); |
| 38 | 2× | id.expires = expires; |
| 39 | 2× | let key = id.hash(); |
| 40 | 2× | let keys = default_keys_dir(); |
| 41 | 2× | fs::create_dir_all(&keys)?; |
| 42 | 2× | crate::durable::write_atomic_private( |
| 43 | 2× | &keys.join(format!("{}.secret", key.to_hex())), |
| 44 | 2× | &sk.encode(&key), |
| 45 | 0× | )?; |
| 46 | 2× | write_atomic( |
| 47 | 2× | &keys.join(format!("{}.identity", key.to_hex())), |
| 48 | 2× | &id.encode(), |
| 49 | 0× | )?; |
| 50 | 2× | Config::set_user(&user, "author.name", &id.name)?; |
| 51 | 2× | Config::set_user(&user, "author.email", &id.email)?; |
| 52 | 2× | Config::set_user(&user, "author.key", &key.to_hex())?; |
| 53 | 2× | Ok((key, id)) |
| 54 | 2× | } |
| 55 | |
|
| 56 | |
|
| 57 | | #[derive(Clone, Debug)] |
| 58 | | pub struct Rotation { |
| 59 | | pub old: KeyId, |
| 60 | | pub new: KeyId, |
| 61 | | pub identity: Identity, |
| 62 | | |
| 63 | | |
| 64 | | pub policy: Option<PolicyUpdate>, |
| 65 | | } |
| 66 | |
|
| 67 | | impl Repo { |
| 68 | | |
| 69 | |
|
| 70 | | |
| 71 | | |
| 72 | 74× | pub fn create_identity( |
| 73 | 74× | &mut self, |
| 74 | 74× | name: &str, |
| 75 | 74× | email: &str, |
| 76 | 74× | scope: IdentityScope, |
| 77 | 74× | ) -> Result<(KeyId, Identity)> { |
| 78 | 74× | self.create_identity_with(name, email, scope, None) |
| 79 | 74× | } |
| 80 | |
|
| 81 | | |
| 82 | 79× | pub fn create_identity_with( |
| 83 | 79× | &mut self, |
| 84 | 79× | name: &str, |
| 85 | 79× | email: &str, |
| 86 | 79× | scope: IdentityScope, |
| 87 | 79× | expires: Option<u64>, |
| 88 | 79× | ) -> Result<(KeyId, Identity)> { |
| 89 | 79× | let sk = SecretKey::generate(); |
| 90 | 79× | let mut id = sk.identity(name, email, Hlc::now().wall_ms); |
| 91 | 79× | id.expires = expires; |
| 92 | 79× | let key = self.install_key(&id, &sk)?; |
| 93 | 79× | self.set_author(&id, &key, scope)?; |
| 94 | 79× | Ok((key, id)) |
| 95 | 79× | } |
| 96 | |
|
| 97 | | |
| 98 | | |
| 99 | 99× | fn install_key(&self, id: &Identity, sk: &SecretKey) -> Result<KeyId> { |
| 100 | 99× | let key = self.store.put_identity(id)?; |
| 101 | 99× | fs::create_dir_all(&self.keys_dir)?; |
| 102 | 99× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 103 | 99× | crate::durable::write_atomic_private(&path, &sk.encode(&key))?; |
| 104 | 99× | write_atomic( |
| 105 | 99× | &self.keys_dir.join(format!("{}.identity", key.to_hex())), |
| 106 | 99× | &id.encode(), |
| 107 | 0× | )?; |
| 108 | 99× | Ok(key) |
| 109 | 99× | } |
| 110 | |
|
| 111 | 101× | fn set_author(&mut self, id: &Identity, key: &KeyId, scope: IdentityScope) -> Result<()> { |
| 112 | 101× | match scope { |
| 113 | | IdentityScope::Repo => { |
| 114 | 38× | self.config.set("author.name", &id.name)?; |
| 115 | 38× | self.config.set("author.email", &id.email)?; |
| 116 | 38× | self.config.set("author.key", &key.to_hex())?; |
| 117 | 38× | self.save_config()?; |
| 118 | | } |
| 119 | | IdentityScope::User => { |
| 120 | 63× | Config::set_user(&self.user_config, "author.name", &id.name)?; |
| 121 | 63× | Config::set_user(&self.user_config, "author.email", &id.email)?; |
| 122 | 63× | Config::set_user(&self.user_config, "author.key", &key.to_hex())?; |
| 123 | 63× | self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?; |
| 124 | | } |
| 125 | | } |
| 126 | 101× | Ok(()) |
| 127 | 101× | } |
| 128 | |
|
| 129 | | |
| 130 | 0× | fn author_scope(&self) -> IdentityScope { |
| 131 | 0× | if self.config.repo_layer().contains_key("author.key") { |
| 132 | 0× | IdentityScope::Repo |
| 133 | | } else { |
| 134 | 0× | IdentityScope::User |
| 135 | | } |
| 136 | 0× | } |
| 137 | |
|
| 138 | | |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | |
| 143 | 20× | fn record_successor(&mut self, id: &Identity, old: &KeyId, new_key: &KeyId) -> Result<()> { |
| 144 | 40× | let names = |v: Option<&String>| v.and_then(|k| KeyId::from_hex(k)) == Some(*old); |
| 145 | 20× | let repo_pins = names(self.config.repo_layer().get("author.key")); |
| 146 | 20× | let user_entries = super::config::read_config_file(&self.user_config).unwrap_or_default(); |
| 147 | 20× | let user_names = names(user_entries.get("author.key")); |
| 148 | 20× | if !repo_pins && !user_names { |
| 149 | 0× | return self.set_author(id, new_key, self.author_scope()); |
| 150 | 20× | } |
| 151 | 20× | if user_names { |
| 152 | 9× | self.set_author(id, new_key, IdentityScope::User)?; |
| 153 | 11× | } |
| 154 | 20× | if repo_pins { |
| 155 | 13× | self.set_author(id, new_key, IdentityScope::Repo)?; |
| 156 | 7× | } |
| 157 | 20× | Ok(()) |
| 158 | 20× | } |
| 159 | |
|
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | 22× | fn replace_identity( |
| 164 | 22× | &mut self, |
| 165 | 22× | new: Identity, |
| 166 | 22× | sk: SecretKey, |
| 167 | 22× | command: &str, |
| 168 | 22× | ) -> Result<Rotation> { |
| 169 | 22× | let Some((old, old_sk)) = self.secret_key()? else { |
| 170 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 171 | | }; |
| 172 | 22× | if new.previous != Some(old) { |
| 173 | 0× | return fail("the successor identity must name the current key as previous"); |
| 174 | 22× | } |
| 175 | | |
| 176 | | |
| 177 | | |
| 178 | | |
| 179 | 22× | if let Some(newer) = self.successors(&old)?.into_iter().next() { |
| 180 | 2× | let newest = self.newest(&newer)?; |
| 181 | 2× | let fix = if self.config.is_repo_set("author.key") { |
| 182 | 2× | format!( |
| 183 | | "This repository's config pins author.key to the older key: `oxy config author.key {}` here \ |
| 184 | | (or `oxy config --unset author.key` to follow the user config), then run it again.", |
| 185 | 2× | newest.to_hex() |
| 186 | | ) |
| 187 | | } else { |
| 188 | 0× | format!( |
| 189 | | "The user config names the older key: `oxy config --user author.key {}`, then run it again.", |
| 190 | 0× | newest.to_hex() |
| 191 | | ) |
| 192 | | }; |
| 193 | 2× | return fail(format!( |
| 194 | | "`{command}` would start from key {}, which is not your identity's newest ({} succeeds it, and {} is the newest). \ |
| 195 | | Another successor would fork the identity into two branches.\n{fix}", |
| 196 | 2× | old.short(12), |
| 197 | 2× | newer.short(12), |
| 198 | 2× | newest.short(12), |
| 199 | | )); |
| 200 | 20× | } |
| 201 | 20× | let new_key = self.install_key(&new, &sk)?; |
| 202 | 20× | let attest = Attest { |
| 203 | 20× | repo: self.id()?, |
| 204 | 20× | hlc: Hlc::now(), |
| 205 | 20× | body: AttestBody::Rotate { new: new_key }, |
| 206 | | }; |
| 207 | 20× | let r = self.store.put_attest(&attest)?; |
| 208 | 20× | self.store |
| 209 | 20× | .put_signature(&r, &old_sk.sign(Domain::Attest, &old, &r))?; |
| 210 | 20× | let policy = match self.current_policy()? { |
| 211 | 9× | Some((_, sp)) if sp.policy.mentions(&old) => { |
| 212 | 9× | let mut p = self.next_policy()?; |
| 213 | 9× | p.substitute_key(&old, new.clone()); |
| 214 | 9× | Some(self.sign_policy_with(p, &[(old, &old_sk), (new_key, &sk)], command)?) |
| 215 | | } |
| 216 | 11× | _ => None, |
| 217 | | }; |
| 218 | 20× | self.record_successor(&new, &old, &new_key)?; |
| 219 | 20× | Ok(Rotation { |
| 220 | 20× | old, |
| 221 | 20× | new: new_key, |
| 222 | 20× | identity: new, |
| 223 | 20× | policy, |
| 224 | 20× | }) |
| 225 | 22× | } |
| 226 | |
|
| 227 | | |
| 228 | | |
| 229 | 33× | pub fn successors(&self, key: &KeyId) -> Result<Vec<KeyId>> { |
| 230 | 33× | let mut found = Vec::new(); |
| 231 | 33× | let entries = match fs::read_dir(&self.keys_dir) { |
| 232 | 33× | Ok(e) => e, |
| 233 | 0× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(found), |
| 234 | 0× | Err(e) => return Err(e.into()), |
| 235 | | }; |
| 236 | 106× | for entry in entries { |
| 237 | 106× | let path = entry?.path(); |
| 238 | 106× | if path.extension().and_then(|e| e.to_str()) != Some("identity") { |
| 239 | 53× | continue; |
| 240 | 53× | } |
| 241 | 53× | let Ok(bytes) = fs::read(&path) else { continue }; |
| 242 | 53× | let Ok(id) = Identity::decode(&bytes) else { |
| 243 | 0× | continue; |
| 244 | | }; |
| 245 | 53× | if id.previous == Some(*key) { |
| 246 | 7× | found.push(id.hash()); |
| 247 | 46× | } |
| 248 | | } |
| 249 | 33× | found.sort(); |
| 250 | 33× | Ok(found) |
| 251 | 33× | } |
| 252 | |
|
| 253 | | |
| 254 | | |
| 255 | 4× | pub fn newest(&self, key: &KeyId) -> Result<KeyId> { |
| 256 | 4× | let mut cur = *key; |
| 257 | 4× | for _ in 0..256 { |
| 258 | 7× | match self.successors(&cur)?.into_iter().next() { |
| 259 | 3× | Some(next) => cur = next, |
| 260 | 4× | None => break, |
| 261 | | } |
| 262 | | } |
| 263 | 4× | Ok(cur) |
| 264 | 4× | } |
| 265 | |
|
| 266 | | |
| 267 | | |
| 268 | 6× | pub fn rotate_identity(&mut self) -> Result<Rotation> { |
| 269 | 6× | let Some((old, _)) = self.secret_key()? else { |
| 270 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 271 | | }; |
| 272 | 6× | let cur = self.identity(&old)?; |
| 273 | 6× | let sk = SecretKey::generate(); |
| 274 | 6× | let mut id = sk.identity(&cur.name, &cur.email, Hlc::now().wall_ms); |
| 275 | 6× | id.previous = Some(old); |
| 276 | 6× | id.delegates = cur.delegates.clone(); |
| 277 | 6× | id.expires = cur.expires; |
| 278 | 6× | self.replace_identity(id, sk, "oxy identity rotate") |
| 279 | 6× | } |
| 280 | |
|
| 281 | | |
| 282 | 3× | pub fn renew_identity(&mut self, expires: Option<u64>) -> Result<Rotation> { |
| 283 | 3× | let Some((old, sk)) = self.secret_key()? else { |
| 284 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 285 | | }; |
| 286 | 3× | let cur = self.identity(&old)?; |
| 287 | 3× | let id = cur.successor(cur.delegates.clone(), expires, Hlc::now().wall_ms); |
| 288 | 3× | self.replace_identity(id, sk, "oxy identity renew") |
| 289 | 3× | } |
| 290 | |
|
| 291 | | |
| 292 | | |
| 293 | | |
| 294 | 12× | pub fn delegate_add( |
| 295 | 12× | &mut self, |
| 296 | 12× | delegate: Identity, |
| 297 | 12× | scope: Scope, |
| 298 | 12× | expires: Option<u64>, |
| 299 | 12× | ) -> Result<Rotation> { |
| 300 | 12× | self.delegate_add_many(vec![delegate], scope, expires) |
| 301 | 12× | } |
| 302 | |
|
| 303 | | |
| 304 | 13× | pub fn delegate_add_many( |
| 305 | 13× | &mut self, |
| 306 | 13× | new_delegates: Vec<Identity>, |
| 307 | 13× | scope: Scope, |
| 308 | 13× | expires: Option<u64>, |
| 309 | 13× | ) -> Result<Rotation> { |
| 310 | 13× | let Some((old, sk)) = self.secret_key()? else { |
| 311 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 312 | | }; |
| 313 | 13× | if scope.is_empty() { |
| 314 | 0× | return fail("a delegate needs at least one scope: changes, refs or seal"); |
| 315 | 13× | } |
| 316 | 13× | if new_delegates.is_empty() { |
| 317 | 0× | return fail("name at least one identity file"); |
| 318 | 13× | } |
| 319 | 13× | let cur = self.identity(&old)?; |
| 320 | 13× | let mut delegates = cur.delegates.clone(); |
| 321 | 13× | let mut added = Vec::new(); |
| 322 | 14× | for delegate in new_delegates { |
| 323 | 14× | let dk = self.store.put_identity(&delegate)?; |
| 324 | 14× | added.push(dk); |
| 325 | 14× | if dk == old { |
| 326 | 0× | return fail("an identity cannot delegate to itself"); |
| 327 | 14× | } |
| 328 | 14× | if cur.delegate(&dk).is_some() |
| 329 | 12× | || delegates |
| 330 | 12× | .iter() |
| 331 | 12× | .skip(cur.delegates.len()) |
| 332 | 12× | .any(|d| d.key == dk) |
| 333 | | { |
| 334 | 2× | return fail(format!( |
| 335 | | "{} is already a delegate; remove it first to change its scope or expiry", |
| 336 | 2× | dk.short(12) |
| 337 | | )); |
| 338 | 12× | } |
| 339 | 12× | delegates.push(Delegate { |
| 340 | 12× | key: dk, |
| 341 | 12× | scope, |
| 342 | 12× | expires, |
| 343 | 12× | }); |
| 344 | | } |
| 345 | 11× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 346 | 11× | self.replace_identity( |
| 347 | 11× | id, |
| 348 | 11× | sk, |
| 349 | 11× | &format!( |
| 350 | | "oxy identity delegate add {} --scope {scope}", |
| 351 | 11× | added |
| 352 | 11× | .iter() |
| 353 | 12× | .map(|k| k.short(12)) |
| 354 | 11× | .collect::<Vec<_>>() |
| 355 | 11× | .join(" ") |
| 356 | | ), |
| 357 | | ) |
| 358 | 13× | } |
| 359 | |
|
| 360 | | |
| 361 | 3× | pub fn delegate_remove(&mut self, delegate: &KeyId) -> Result<Rotation> { |
| 362 | 3× | let Some((old, sk)) = self.secret_key()? else { |
| 363 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 364 | | }; |
| 365 | 3× | let cur = self.identity(&old)?; |
| 366 | 3× | if cur.delegate(delegate).is_none() { |
| 367 | 1× | return fail(format!("{} is not a delegate", delegate.short(12))); |
| 368 | 2× | } |
| 369 | 2× | let delegates = cur |
| 370 | 2× | .delegates |
| 371 | 2× | .iter() |
| 372 | 2× | .copied() |
| 373 | 2× | .filter(|d| d.key != *delegate) |
| 374 | 2× | .collect(); |
| 375 | 2× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 376 | 2× | self.replace_identity( |
| 377 | 2× | id, |
| 378 | 2× | sk, |
| 379 | 2× | &format!("oxy identity delegate remove {}", delegate.short(12)), |
| 380 | | ) |
| 381 | 3× | } |
| 382 | |
|
| 383 | | |
| 384 | | |
| 385 | | |
| 386 | | |
| 387 | | |
| 388 | 5× | pub fn revoke_identity(&self, key: &KeyId, reason: &str) -> Result<Option<PolicyUpdate>> { |
| 389 | 5× | let Some((own, sk)) = self.secret_key()? else { |
| 390 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 391 | | }; |
| 392 | 5× | let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 393 | 5× | let is_root = policy.as_ref().is_some_and(|p| p.roots.contains(&own)); |
| 394 | 5× | let is_delegate = self.identity(&own)?.delegate(key).is_some(); |
| 395 | 5× | if *key != own && !is_root && !is_delegate { |
| 396 | 1× | return fail("only the key's holder, the identity that delegated to it, or a policy root can revoke it"); |
| 397 | 4× | } |
| 398 | 4× | let attest = Attest { |
| 399 | 4× | repo: self.id()?, |
| 400 | 4× | hlc: Hlc::now(), |
| 401 | 4× | body: AttestBody::Revoke { |
| 402 | 4× | key: *key, |
| 403 | 4× | reason: reason.into(), |
| 404 | 4× | }, |
| 405 | | }; |
| 406 | 4× | let r = self.store.put_attest(&attest)?; |
| 407 | 4× | self.store |
| 408 | 4× | .put_signature(&r, &sk.sign(Domain::Attest, &own, &r))?; |
| 409 | 4× | let dir = self.oxy.join("revoked"); |
| 410 | 4× | fs::create_dir_all(&dir)?; |
| 411 | 4× | write_atomic(&dir.join(key.to_hex()), r.to_hex().as_bytes())?; |
| 412 | 1× | match policy { |
| 413 | 1× | Some(p) if p.mentions(key) => Ok(Some(self.policy_revoke(key)?)), |
| 414 | 3× | _ => Ok(None), |
| 415 | | } |
| 416 | 5× | } |
| 417 | |
|
| 418 | | |
| 419 | 265× | pub fn is_revoked(&self, key: &KeyId) -> Result<bool> { |
| 420 | 265× | if self.oxy.join("revoked").join(key.to_hex()).exists() { |
| 421 | 5× | return Ok(true); |
| 422 | 260× | } |
| 423 | 260× | Ok(self |
| 424 | 260× | .current_policy()? |
| 425 | 260× | .is_some_and(|(_, sp)| sp.policy.is_revoked(key))) |
| 426 | 265× | } |
| 427 | |
|
| 428 | | |
| 429 | | |
| 430 | 1049× | pub fn signing_key(&self) -> Option<KeyId> { |
| 431 | 1049× | if self.config.signer_keyless { |
| 432 | | |
| 433 | 1× | return self.keyless_key.borrow().as_ref().map(|(k, _, _)| *k); |
| 434 | 1048× | } |
| 435 | 1048× | self.config.signer_key.or(self.config.author_key) |
| 436 | 1049× | } |
| 437 | |
|
| 438 | | |
| 439 | | |
| 440 | | |
| 441 | | |
| 442 | 826× | pub fn secret_key(&self) -> Result<Option<(KeyId, SecretKey)>> { |
| 443 | 826× | if self.config.signer_keyless { |
| 444 | 1× | return self.keyless_secret().map(Some); |
| 445 | 825× | } |
| 446 | 825× | let Some(key) = self.signing_key() else { |
| 447 | 237× | return Ok(None); |
| 448 | | }; |
| 449 | 588× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 450 | 588× | match fs::read(&path) { |
| 451 | 587× | Ok(b) => { |
| 452 | 587× | let (k, sk) = SecretKey::decode(&b)?; |
| 453 | 587× | if k != key { |
| 454 | 0× | return fail(format!( |
| 455 | | "{} holds a different key than its name", |
| 456 | 0× | path.display() |
| 457 | | )); |
| 458 | 587× | } |
| 459 | 587× | if !self.store.has(&key) { |
| 460 | 4× | let id_path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 461 | 4× | if let Ok(bytes) = fs::read(&id_path) { |
| 462 | 4× | let id = Identity::decode(&bytes)?; |
| 463 | 4× | if id.hash() == key { |
| 464 | 4× | self.store.put_identity(&id)?; |
| 465 | 0× | } |
| 466 | 0× | } |
| 467 | 583× | } |
| 468 | 587× | Ok(Some((k, sk))) |
| 469 | | } |
| 470 | 1× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 471 | 0× | Err(e) => Err(e.into()), |
| 472 | | } |
| 473 | 826× | } |
| 474 | |
|
| 475 | | |
| 476 | | |
| 477 | | |
| 478 | 340× | pub fn identity(&self, key: &KeyId) -> Result<Identity> { |
| 479 | 340× | match self.store.get_identity(key) { |
| 480 | 336× | Ok(id) => Ok(id), |
| 481 | 4× | Err(e) => { |
| 482 | 4× | let path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 483 | 4× | match fs::read(&path) { |
| 484 | 2× | Ok(bytes) => { |
| 485 | 2× | let id = Identity::decode(&bytes)?; |
| 486 | 2× | if id.hash() != *key { |
| 487 | 1× | return fail(format!( |
| 488 | | "{} does not hold identity {}", |
| 489 | 1× | path.display(), |
| 490 | 1× | key.short(12) |
| 491 | | )); |
| 492 | 1× | } |
| 493 | 1× | Ok(id) |
| 494 | | } |
| 495 | | |
| 496 | | |
| 497 | | |
| 498 | 2× | Err(_) if self.config.author_key == Some(*key) => fail(format!( |
| 499 | | "this repository has no identity {} (author.key, set in {}): it is not in the repository, nor in {}.\n\ |
| 500 | | An agent home names its human's identity, and each delegation rotates that identity, so the key goes stale. \ |
| 501 | | Pin the identity this repository uses, as its owner sees it: `oxy identity show` in the owner's shell gives the key, \ |
| 502 | | then `oxy config author.key <KEY>` here; or ask the owner.", |
| 503 | 2× | key.short(12), |
| 504 | 2× | if self.config.is_repo_set("author.key") { |
| 505 | 1× | "this repository's config" |
| 506 | | } else { |
| 507 | 1× | "the user config" |
| 508 | | }, |
| 509 | 2× | self.keys_dir.display() |
| 510 | | )), |
| 511 | 0× | Err(_) => Err(e.into()), |
| 512 | | } |
| 513 | | } |
| 514 | | } |
| 515 | 340× | } |
| 516 | |
|
| 517 | | |
| 518 | 47× | pub fn sign_change(&self, hash: &ChangeHash) -> Result<Signature> { |
| 519 | 47× | let Some((key, sk)) = self.secret_key()? else { |
| 520 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 521 | | }; |
| 522 | 47× | let sig = sk.sign(Domain::Change, &key, hash); |
| 523 | 47× | self.store.put_signature(hash, &sig)?; |
| 524 | 47× | Ok(sig) |
| 525 | 47× | } |
| 526 | |
|
| 527 | | |
| 528 | 24× | pub fn verify_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 529 | 24× | self.verify_change_known(hash, None) |
| 530 | 24× | } |
| 531 | |
|
| 532 | | |
| 533 | | |
| 534 | | |
| 535 | 448× | fn verify_change_known( |
| 536 | 448× | &self, |
| 537 | 448× | hash: &ChangeHash, |
| 538 | 448× | known: Option<&crate::state::ChangeMeta>, |
| 539 | 448× | ) -> Result<SignatureStatus> { |
| 540 | 448× | let sigs = self.store.signatures(hash)?; |
| 541 | 448× | if sigs.is_empty() { |
| 542 | 351× | return Ok(SignatureStatus::Unsigned); |
| 543 | 97× | } |
| 544 | | let read; |
| 545 | 97× | let change = match known { |
| 546 | 18× | Some(m) => m, |
| 547 | | None => { |
| 548 | 79× | read = self.store.get_change_meta(hash)?; |
| 549 | 79× | &read |
| 550 | | } |
| 551 | | }; |
| 552 | 97× | let mut last_err = String::new(); |
| 553 | 97× | for sig in &sigs { |
| 554 | 97× | match self.check_signer(change, sig, Domain::Change, hash) { |
| 555 | 91× | Ok(id) => { |
| 556 | | return Ok(SignatureStatus::Valid { |
| 557 | 91× | key: sig.key, |
| 558 | 91× | name: self.certified_subject(&sig.key).unwrap_or(id.name), |
| 559 | 91× | email: id.email, |
| 560 | | sealed: false, |
| 561 | 91× | revoked: self.is_revoked(&sig.key)?, |
| 562 | | }) |
| 563 | | } |
| 564 | 6× | Err(e) => last_err = e, |
| 565 | | } |
| 566 | | } |
| 567 | 6× | Ok(SignatureStatus::Invalid(last_err)) |
| 568 | 448× | } |
| 569 | |
|
| 570 | | |
| 571 | | |
| 572 | | |
| 573 | | |
| 574 | | |
| 575 | | |
| 576 | | |
| 577 | 14× | fn granted( |
| 578 | 14× | &self, |
| 579 | 14× | author: &KeyId, |
| 580 | 14× | signer: &KeyId, |
| 581 | 14× | needed: Scope, |
| 582 | 14× | at_ms: u64, |
| 583 | 14× | ) -> std::result::Result<Option<String>, String> { |
| 584 | 14× | let neither = || { |
| 585 | 7× | Some(format!( |
| 586 | 7× | "key {} is neither the author nor a delegate", |
| 587 | 7× | signer.short(12) |
| 588 | 7× | )) |
| 589 | 7× | }; |
| 590 | 14× | let Ok(Some(c)) = self.store.certificate_of(signer) else { |
| 591 | 4× | return Ok(neither()); |
| 592 | | }; |
| 593 | 10× | let Ok(sc) = self.store.get_certificate(&c) else { |
| 594 | 0× | return Ok(neither()); |
| 595 | | }; |
| 596 | 10× | let Some(g) = &sc.cert.grant else { |
| 597 | 1× | return Ok(neither()); |
| 598 | | }; |
| 599 | 9× | let who = signer.short(12); |
| 600 | 9× | let issuer = self |
| 601 | 9× | .store |
| 602 | 9× | .get_identity(&sc.cert.issuer) |
| 603 | 9× | .map_err(|_| format!("key {who}'s certificate names an unknown issuer"))?; |
| 604 | 9× | sc.verify(&issuer) |
| 605 | 9× | .map_err(|e| format!("key {who}'s certificate: {e}"))?; |
| 606 | | |
| 607 | | |
| 608 | 9× | if g.grant.grantor != *author && !self.granted_before_rotation(author, &g.grant) { |
| 609 | 2× | return Ok(neither()); |
| 610 | 7× | } |
| 611 | 7× | let grantor = self |
| 612 | 7× | .identity(&g.grant.grantor) |
| 613 | 7× | .map_err(|_| format!("key {who}'s grant is by an unknown author"))?; |
| 614 | 7× | g.verify(&grantor) |
| 615 | 7× | .map_err(|e| format!("key {who}'s grant: {e}"))?; |
| 616 | 7× | if g.grant.subject != sc.cert.subject || g.grant.issuer != sc.cert.issuer { |
| 617 | 0× | return Err(format!( |
| 618 | 0× | "key {who}'s grant is for {} from issuer {}, not its certificate's", |
| 619 | 0× | g.grant.subject, |
| 620 | 0× | g.grant.issuer.short(12) |
| 621 | 0× | )); |
| 622 | 7× | } |
| 623 | 7× | if !sc.cert.scope.has(needed) || !g.grant.scope.has(needed) { |
| 624 | 0× | return Err(format!("key {who} is not granted {needed}")); |
| 625 | 7× | } |
| 626 | 7× | if !g.grant.covers(needed, at_ms) { |
| 627 | 2× | return Err(format!( |
| 628 | 2× | "key {who}'s grant did not cover the time the change was made" |
| 629 | 2× | )); |
| 630 | 5× | } |
| 631 | 5× | Ok(None) |
| 632 | 14× | } |
| 633 | |
|
| 634 | | |
| 635 | | |
| 636 | | |
| 637 | | |
| 638 | | |
| 639 | | |
| 640 | 3× | fn granted_before_rotation(&self, author: &KeyId, grant: &crate::cert::Grant) -> bool { |
| 641 | 3× | let mut cur = self.identity(author).ok(); |
| 642 | 3× | for _ in 0..32 { |
| 643 | 3× | let Some(id) = cur else { |
| 644 | 0× | return false; |
| 645 | | }; |
| 646 | 2× | match id.previous { |
| 647 | 2× | Some(prev) if prev == grant.grantor => return grant.not_before < id.created, |
| 648 | 0× | Some(prev) => cur = self.identity(&prev).ok(), |
| 649 | 1× | None => return false, |
| 650 | | } |
| 651 | | } |
| 652 | 0× | false |
| 653 | 3× | } |
| 654 | |
|
| 655 | | |
| 656 | | |
| 657 | | |
| 658 | 249× | pub fn certified_subject(&self, key: &KeyId) -> Option<String> { |
| 659 | 249× | let c = self.store.certificate_of(key).ok()??; |
| 660 | 9× | Some(self.store.get_certificate(&c).ok()?.cert.subject) |
| 661 | 249× | } |
| 662 | |
|
| 663 | | |
| 664 | 262× | fn check_signer( |
| 665 | 262× | &self, |
| 666 | 262× | change: &crate::state::ChangeMeta, |
| 667 | 262× | sig: &Signature, |
| 668 | 262× | domain: Domain, |
| 669 | 262× | target: &ObjectRef, |
| 670 | 262× | ) -> std::result::Result<Identity, String> { |
| 671 | 262× | let id = self |
| 672 | 262× | .store |
| 673 | 262× | .get_identity(&sig.key) |
| 674 | 262× | .map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 675 | | |
| 676 | | |
| 677 | | |
| 678 | | |
| 679 | | |
| 680 | | |
| 681 | 262× | if !change.author.is_zero() && sig.key != change.author { |
| 682 | 34× | let needed = match domain { |
| 683 | 22× | Domain::Change => Scope::CHANGES, |
| 684 | 12× | _ => Scope::SEAL, |
| 685 | | }; |
| 686 | 34× | let delegation = self |
| 687 | 34× | .store |
| 688 | 34× | .get_identity(&change.author) |
| 689 | 34× | .ok() |
| 690 | 34× | .and_then(|a| a.delegate(&sig.key).copied()); |
| 691 | 19× | match delegation { |
| 692 | | |
| 693 | | None => { |
| 694 | 7× | if let Some(why) = |
| 695 | 14× | self.granted(&change.author, &sig.key, needed, change.hlc.wall_ms)? |
| 696 | | { |
| 697 | 7× | return Err(why); |
| 698 | 5× | } |
| 699 | | } |
| 700 | 20× | Some(d) if !d.scope.has(needed) => { |
| 701 | 1× | return Err(format!( |
| 702 | | "key {} is a delegate for {} only and may not {}", |
| 703 | 1× | sig.key.short(12), |
| 704 | | d.scope, |
| 705 | 1× | if needed == Scope::SEAL { |
| 706 | 1× | "seal" |
| 707 | | } else { |
| 708 | 0× | "sign changes" |
| 709 | | } |
| 710 | | )) |
| 711 | | } |
| 712 | 19× | Some(d) if !d.allows(needed, change.hlc.wall_ms) => { |
| 713 | 1× | return Err(format!( |
| 714 | 1× | "key {}'s delegation had expired when the change was made", |
| 715 | 1× | sig.key.short(12) |
| 716 | 1× | )) |
| 717 | | } |
| 718 | 18× | Some(_) => {} |
| 719 | | } |
| 720 | 228× | } |
| 721 | 251× | if id.is_expired_at(change.hlc.wall_ms) { |
| 722 | 1× | return Err(format!( |
| 723 | 1× | "key {} had expired when the change was made", |
| 724 | 1× | sig.key.short(12) |
| 725 | 1× | )); |
| 726 | 250× | } |
| 727 | 250× | verify_signature(&id, sig, domain, target).map_err(|e| e.to_string())?; |
| 728 | 249× | Ok(id) |
| 729 | 262× | } |
| 730 | |
|
| 731 | | |
| 732 | | |
| 733 | 344× | pub fn authenticate_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 734 | 344× | self.authenticate_change_known(hash, None) |
| 735 | 344× | } |
| 736 | |
|
| 737 | | |
| 738 | | |
| 739 | 424× | pub fn authenticate_change_known( |
| 740 | 424× | &self, |
| 741 | 424× | hash: &ChangeHash, |
| 742 | 424× | known: Option<&crate::state::ChangeMeta>, |
| 743 | 424× | ) -> Result<SignatureStatus> { |
| 744 | 424× | let direct = self.verify_change_known(hash, known)?; |
| 745 | 424× | if matches!(direct, SignatureStatus::Valid { .. }) { |
| 746 | 76× | return Ok(direct); |
| 747 | 348× | } |
| 748 | 348× | let mut last_err = match &direct { |
| 749 | 2× | SignatureStatus::Invalid(e) => e.clone(), |
| 750 | 346× | _ => String::new(), |
| 751 | | }; |
| 752 | 348× | let seals = self.store.seals_for(hash)?; |
| 753 | 348× | if seals.is_empty() { |
| 754 | 183× | return Ok(match last_err.is_empty() { |
| 755 | 181× | true => SignatureStatus::Unsigned, |
| 756 | 2× | false => SignatureStatus::Invalid(last_err), |
| 757 | | }); |
| 758 | 165× | } |
| 759 | | let read; |
| 760 | 165× | let change = match known { |
| 761 | 0× | Some(m) => m, |
| 762 | | None => { |
| 763 | 165× | read = self.store.get_change_meta(hash)?; |
| 764 | 165× | &read |
| 765 | | } |
| 766 | | }; |
| 767 | 165× | for seal_ref in seals { |
| 768 | 165× | let attest = self.store.get_attest(&seal_ref)?; |
| 769 | 165× | let AttestBody::Seal { hashes } = &attest.body else { |
| 770 | 0× | continue; |
| 771 | | }; |
| 772 | 165× | if !hashes.contains(hash) { |
| 773 | 0× | continue; |
| 774 | 165× | } |
| 775 | 165× | for sig in self.store.signatures(&seal_ref)? { |
| 776 | 165× | match self.check_signer(change, &sig, Domain::Attest, &seal_ref) { |
| 777 | 158× | Ok(id) => { |
| 778 | | return Ok(SignatureStatus::Valid { |
| 779 | 158× | key: sig.key, |
| 780 | 158× | name: self.certified_subject(&sig.key).unwrap_or(id.name), |
| 781 | 158× | email: id.email, |
| 782 | | sealed: true, |
| 783 | 158× | revoked: self.is_revoked(&sig.key)?, |
| 784 | | }) |
| 785 | | } |
| 786 | 7× | Err(e) => last_err = e, |
| 787 | | } |
| 788 | | } |
| 789 | | } |
| 790 | 7× | if last_err.is_empty() { |
| 791 | 0× | Ok(SignatureStatus::Unsigned) |
| 792 | | } else { |
| 793 | 7× | Ok(SignatureStatus::Invalid(last_err)) |
| 794 | | } |
| 795 | 424× | } |
| 796 | |
|
| 797 | | |
| 798 | | |
| 799 | | |
| 800 | | |
| 801 | 3× | pub fn seal(&self, hashes: &[ChangeHash]) -> Result<ObjectRef> { |
| 802 | 3× | if hashes.is_empty() { |
| 803 | 0× | return fail("nothing to seal"); |
| 804 | 3× | } |
| 805 | 3× | if hashes.len() > crate::attest::MAX_SEAL_HASHES { |
| 806 | 0× | return fail(format!( |
| 807 | | "a seal covers at most {} changes", |
| 808 | | crate::attest::MAX_SEAL_HASHES |
| 809 | | )); |
| 810 | 3× | } |
| 811 | 3× | for h in hashes { |
| 812 | 3× | if !self.store.has(h) { |
| 813 | 0× | return fail(format!("unknown change {}", h.short(12))); |
| 814 | 3× | } |
| 815 | | } |
| 816 | 3× | let mut sorted = hashes.to_vec(); |
| 817 | 3× | sorted.sort(); |
| 818 | 3× | sorted.dedup(); |
| 819 | 3× | let sa = self.sign_attest(&Attest::seal(self.id()?, sorted.clone()))?; |
| 820 | 3× | let r = self.store.put_attest(&sa.attest)?; |
| 821 | 3× | for sig in &sa.sigs { |
| 822 | 3× | self.store.put_signature(&r, sig)?; |
| 823 | | } |
| 824 | 3× | for h in &sorted { |
| 825 | 3× | self.store.index_seal(h, &r)?; |
| 826 | | } |
| 827 | 3× | Ok(r) |
| 828 | 3× | } |
| 829 | |
|
| 830 | | |
| 831 | | |
| 832 | | |
| 833 | | |
| 834 | 82× | pub(crate) fn seals_for_author(&self, author: &KeyId, signer: &KeyId) -> bool { |
| 835 | 82× | author == signer |
| 836 | 8× | || author.is_zero() |
| 837 | 7× | || ((self.config.signer_key.is_some() || self.config.signer_keyless) |
| 838 | 5× | && self.config.author_key == Some(*author)) |
| 839 | 82× | } |
| 840 | |
|
| 841 | | |
| 842 | | |
| 843 | | |
| 844 | 0× | pub fn sealable(&self) -> Result<Vec<ChangeHash>> { |
| 845 | 0× | let Some((key, _)) = self.secret_key()? else { |
| 846 | 0× | return fail("no identity: run oxy identity create"); |
| 847 | | }; |
| 848 | 0× | let heads = self.bookmark_heads()?; |
| 849 | 0× | let state = self.load_state_history(&heads)?; |
| 850 | 0× | let mut out = Vec::new(); |
| 851 | 0× | for (h, meta) in state.changes() { |
| 852 | 0× | if self.seals_for_author(&meta.author, &key) |
| 853 | 0× | && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 854 | 0× | { |
| 855 | 0× | out.push(*h); |
| 856 | 0× | } |
| 857 | | } |
| 858 | 0× | out.sort(); |
| 859 | 0× | Ok(out) |
| 860 | 0× | } |
| 861 | |
|
| 862 | 186× | pub(crate) fn sign_attest(&self, attest: &Attest) -> Result<SignedAttest> { |
| 863 | 186× | let Some((key, sk)) = self.secret_key()? else { |
| 864 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 865 | | }; |
| 866 | 186× | let r = self.store.put_attest(attest)?; |
| 867 | 184× | let sig = sk.sign(Domain::Attest, &key, &r); |
| 868 | 184× | self.store.put_signature(&r, &sig)?; |
| 869 | 184× | if let AttestBody::Seal { hashes } = &attest.body { |
| 870 | 55× | for h in hashes { |
| 871 | 55× | self.store.index_seal(h, &r)?; |
| 872 | | } |
| 873 | 132× | } |
| 874 | 184× | Ok(SignedAttest { |
| 875 | 184× | attest: attest.clone(), |
| 876 | 184× | sigs: vec![sig], |
| 877 | 184× | }) |
| 878 | 186× | } |
| 879 | | } |