| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use anyhow::{bail, Context, Result}; |
| 6 | | use oxy_core::identity::Identity; |
| 7 | | use serde_json::{json, Value as Json}; |
| 8 | | use spectra_core::event::Event; |
| 9 | | use spectra_core::keys::KeyPair; |
| 10 | | use spectra_core::ledger::Ledger; |
| 11 | | use spectra_core::proof::Proof; |
| 12 | |
|
| 13 | |
|
| 14 | 35× | pub async fn latest_tls(ledger: &Ledger) -> Result<Option<Event>> { |
| 15 | 35× | let mut last = None; |
| 16 | 35× | let mut from = 1; |
| 17 | | loop { |
| 18 | 64× | let rows = ledger.events_of_kinds(from, &["instance.tls"], 1000).await?; |
| 19 | 64× | match rows.last() { |
| 20 | 29× | Some(e) => { |
| 21 | 29× | from = e.seq + 1; |
| 22 | 29× | last = Some(e.clone()); |
| 23 | 29× | } |
| 24 | 35× | None => return Ok(last), |
| 25 | | } |
| 26 | | } |
| 27 | 35× | } |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 8× | pub async fn record_tls(ledger: &Ledger, instance: &KeyPair, spki: &[u8; 32], names: &[String]) -> Result<bool> { |
| 32 | 8× | let hex_spki = hex::encode(spki); |
| 33 | 8× | if let Some(e) = latest_tls(ledger).await? { |
| 34 | 2× | if e.json().get("spki_sha256").and_then(Json::as_str) == Some(hex_spki.as_str()) { |
| 35 | 1× | return Ok(false); |
| 36 | 1× | } |
| 37 | 6× | } |
| 38 | 7× | let payload = json!({ "spki_sha256": hex_spki, "names": names }); |
| 39 | 7× | ledger.append_one(spectra_core::principals::signed_event(instance, "system", "instance.tls", payload)).await?; |
| 40 | 7× | Ok(true) |
| 41 | 8× | } |
| 42 | |
|
| 43 | |
|
| 44 | | #[derive(Clone, Debug)] |
| 45 | | pub struct Checked { |
| 46 | | pub name: String, |
| 47 | | pub instance: Identity, |
| 48 | | pub owner: Option<String>, |
| 49 | | |
| 50 | | pub spki_sha256: [u8; 32], |
| 51 | | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 9× | pub fn check(answer: &Json, seen: &[u8; 32]) -> Result<Checked> { |
| 56 | 9× | let ip = answer.get("instance").context("no instance in the answer")?; |
| 57 | 9× | let bytes = ip.get("identity").and_then(Json::as_str).and_then(|h| hex::decode(h).ok()).context("no instance identity")?; |
| 58 | 9× | let instance = Identity::decode(&bytes).map_err(|e| anyhow::anyhow!("malformed instance identity: {e}"))?; |
| 59 | 9× | let tls = answer.get("tls").filter(|t| !t.is_null()).context("the server has not signed a TLS certificate")?; |
| 60 | 9× | let payload = tls.get("payload").context("no TLS payload")?; |
| 61 | 9× | let proof = tls.get("proof").and_then(Json::as_str).and_then(|h| hex::decode(h).ok()).context("no TLS proof")?; |
| 62 | 9× | let proof = Proof::decode(&proof).map_err(|e| anyhow::anyhow!(e))?; |
| 63 | 9× | if proof.key() != instance.hash().0 || proof.method != "cli" { |
| 64 | 0× | bail!("the certificate is not vouched for by the instance key"); |
| 65 | 9× | } |
| 66 | 9× | proof |
| 67 | 9× | .verify(&instance, Some(&spectra_core::event::canonical_json(payload))) |
| 68 | 9× | .map_err(|e| anyhow::anyhow!("the instance's signature on its certificate: {e}"))?; |
| 69 | 9× | let named = payload.get("spki_sha256").and_then(Json::as_str).context("no pin in the TLS record")?; |
| 70 | 9× | if named != hex::encode(seen) { |
| 71 | 1× | bail!("the server presented a certificate its instance key did not sign"); |
| 72 | 8× | } |
| 73 | 8× | Ok(Checked { |
| 74 | 8× | name: answer.get("name").and_then(Json::as_str).unwrap_or("").to_string(), |
| 75 | 8× | instance, |
| 76 | 8× | owner: answer.get("owner").and_then(Json::as_str).map(String::from), |
| 77 | 8× | spki_sha256: *seen, |
| 78 | 8× | }) |
| 79 | 9× | } |