1
//! Code to help with building certificate going in the router descriptor.
2
//!
3
//! This is not a lot of code but it helps for a better modular mental model of all the
4
//! code needed for building descriptors.
5

            
6
use anyhow::Context;
7

            
8
use tor_cert::KeyUnknownCert;
9
use tor_checkable::TimeBound as _;
10
use tor_key_forge::ToEncodableCert as _;
11
use tor_llcrypto::pk::ed25519::Ed25519Identity;
12
use tor_netdoc::types::{Ed25519IdentityCert, EmbeddedCert};
13
use tor_relay_crypto::RelaySigningKeyCert;
14
use web_time_compat::SystemTime;
15

            
16
/// Build an embedded identity certificate.
17
///
18
/// This verifies the generated certificate against the given time and identity.
19
5
pub(super) fn build_identity_cert(
20
5
    cert: RelaySigningKeyCert,
21
5
    identity: Ed25519Identity,
22
5
    now: SystemTime,
23
5
) -> anyhow::Result<EmbeddedCert<Ed25519IdentityCert, KeyUnknownCert>> {
24
5
    let signing_cert = tor_cert::Ed25519Cert::decode(&cert.to_encodable_cert())
25
5
        .context("Failed to decode relay signing cert")?;
26
5
    let identity_cert = Ed25519IdentityCert::verify(signing_cert.clone())
27
5
        .context("Failed to verify relay signing cert")?
28
4
        .if_valid_at(&now)
29
4
        .context("Relay signing cert is not valid at publication time")?;
30
3
    anyhow::ensure!(
31
3
        identity_cert.id_ed25519 == identity,
32
1
        "Relay signing cert does not match relay identity"
33
    );
34
2
    Ok(EmbeddedCert::new(identity_cert, signing_cert))
35
5
}