Skip to main content

witness_core/
unl.rs

1//! A publisher's signed UNL.
2//!
3//! The publisher serves its manifest, the list (`blob`, base64 JSON) and the
4//! list signature. The check follows xrpld (`ValidatorList::verify`):
5//! 1. the publisher manifest is signed and not revoked;
6//! 2. the master key in the manifest is the one we trust as the publisher;
7//! 3. the list bytes are signed by the current signing key from that manifest.
8//!
9//! What is inside the list (validators, sequence, validity dates) is for the
10//! caller to parse: the core checks that the bytes are authentic, not what they mean.
11
12use std::fmt;
13
14use crate::keys::{VerifyError, verify};
15use crate::manifest::Manifest;
16use crate::stobject::Error as CodecError;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum ListError {
20    Manifest(CodecError),
21    ManifestSignature(VerifyError),
22    /// The manifest is signed by a master key other than the one we trust.
23    WrongPublisher,
24    Revoked,
25    ListSignature(VerifyError),
26}
27
28impl fmt::Display for ListError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            ListError::Manifest(e) => write!(f, "cannot parse publisher manifest: {e}"),
32            ListError::ManifestSignature(e) => write!(f, "publisher manifest: {e}"),
33            ListError::WrongPublisher => f.write_str("manifest of a different publisher"),
34            ListError::Revoked => f.write_str("publisher key revoked"),
35            ListError::ListSignature(e) => write!(f, "list signature: {e}"),
36        }
37    }
38}
39
40impl std::error::Error for ListError {}
41
42/// Checks the list and returns the verified publisher manifest.
43pub fn verify_list(
44    publisher_master: &[u8],
45    manifest: &[u8],
46    blob: &[u8],
47    signature: &[u8],
48) -> Result<Manifest, ListError> {
49    let m = Manifest::parse(manifest).map_err(ListError::Manifest)?;
50    m.verify().map_err(ListError::ManifestSignature)?;
51    if m.master_key != publisher_master {
52        return Err(ListError::WrongPublisher);
53    }
54    if m.is_revoked() {
55        return Err(ListError::Revoked);
56    }
57    let key = m
58        .signing_key
59        .as_deref()
60        .ok_or(ListError::ManifestSignature(VerifyError::BadKey))?;
61    verify(key, blob, signature, true).map_err(ListError::ListSignature)?;
62    Ok(m)
63}