Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use curve25519_dalek::edwards::CompressedEdwardsY;
use ed25519_dalek::{SigningKey, VerifyingKey};
use n0_error::{e, stack_error};
use n0_error::{ensure, stack_error};
use rand_core::CryptoRng;
use serde::{Deserialize, Serialize, de, ser};

Expand Down Expand Up @@ -424,16 +424,18 @@ fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
} else {
let input = s.to_ascii_uppercase();
let input = input.as_bytes();
if data_encoding::BASE32_NOPAD.decode_len(input.len())? != bytes.len() {
return Err(e!(KeyParsingError::DecodeInvalidLength));
}
ensure!(
data_encoding::BASE32_NOPAD.decode_len(input.len())? == bytes.len(),
KeyParsingError::DecodeInvalidLength
);
data_encoding::BASE32_NOPAD.decode_mut(input, &mut bytes)
};
match res {
Ok(len) => {
if len != PublicKey::LENGTH {
return Err(e!(KeyParsingError::DecodeInvalidLength));
}
ensure!(
len == PublicKey::LENGTH,
KeyParsingError::DecodeInvalidLength
);
}
Err(partial) => return Err(partial.error.into()),
}
Expand Down
17 changes: 11 additions & 6 deletions iroh-dns-server/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

use std::{collections::BTreeMap, num::NonZeroUsize, path::Path, sync::Arc, time::Duration};

use hickory_server::proto::rr::{Name, RecordSet, RecordType, RrKey};
use hickory_server::proto::{
ProtoError,
rr::{Name, RecordSet, RecordType, RrKey},
};
use lru::LruCache;
use n0_error::Result;
use n0_error::{Result, StdResultExt};
use pkarr::{Client as PkarrClient, SignedPacket};
use tokio::sync::Mutex;
use tracing::{debug, trace, warn};
Expand Down Expand Up @@ -234,7 +237,7 @@ impl ZoneCache {
record_type: RecordType,
) -> Result<Option<Arc<RecordSet>>> {
let pubkey = PublicKeyBytes::from_signed_packet(signed_packet);
let zone = CachedZone::from_signed_packet(signed_packet)?;
let zone = CachedZone::from_signed_packet(signed_packet).anyerr()?;
let res = zone.resolve(name, record_type);
self.dht_cache.insert(pubkey, zone, DHT_CACHE_TTL);
Ok(res)
Expand All @@ -251,8 +254,10 @@ impl ZoneCache {
trace!("insert skip: cached is newer");
Ok(())
} else {
self.cache
.put(pubkey, CachedZone::from_signed_packet(signed_packet)?);
self.cache.put(
pubkey,
CachedZone::from_signed_packet(signed_packet).anyerr()?,
);
trace!("inserted into cache");
Ok(())
}
Expand All @@ -271,7 +276,7 @@ struct CachedZone {
}

impl CachedZone {
fn from_signed_packet(signed_packet: &SignedPacket) -> Result<Self> {
fn from_signed_packet(signed_packet: &SignedPacket) -> Result<Self, ProtoError> {
let (_label, records) =
signed_packet_to_hickory_records_without_origin(signed_packet, |_| true)?;
Ok(Self {
Expand Down
42 changes: 29 additions & 13 deletions iroh-dns-server/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,42 @@ use std::{
};

use hickory_server::proto::{
ProtoError,
op::Message,
rr::{
Name, Record, RecordSet, RecordType, RrKey,
domain::{IntoLabel, Label},
},
serialize::binary::BinDecodable,
};
use n0_error::{AnyError, Result, StdResultExt};
use n0_error::{AnyError, StdResultExt, e, stack_error};
use pkarr::SignedPacket;

#[derive(
derive_more::From, derive_more::Into, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy,
)]
pub struct PublicKeyBytes([u8; 32]);

#[stack_error(derive, add_meta, from_sources)]
pub enum InvalidPublicKeyBytes {
#[error(transparent)]
Encoding {
#[error(std_err)]
source: z32::Z32Error,
},
#[error("invalid length, must be 32 bytes")]
InvalidLength,
}

impl PublicKeyBytes {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}

pub fn from_z32(s: &str) -> Result<Self> {
let bytes = z32::decode(s.as_bytes()).anyerr()?;
let bytes = TryInto::<[u8; 32]>::try_into(&bytes[..]).std_context("invalid length")?;
pub fn from_z32(s: &str) -> Result<Self, InvalidPublicKeyBytes> {
let bytes = z32::decode(s.as_bytes())?;
let bytes = TryInto::<[u8; 32]>::try_into(&bytes[..])
.map_err(|_| e!(InvalidPublicKeyBytes::InvalidLength))?;
Ok(Self(bytes))
}

Expand Down Expand Up @@ -75,7 +88,8 @@ impl TryFrom<PublicKeyBytes> for pkarr::PublicKey {
}

impl FromStr for PublicKeyBytes {
type Err = AnyError;
type Err = InvalidPublicKeyBytes;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_z32(s)
}
Expand All @@ -87,17 +101,19 @@ impl AsRef<[u8; 32]> for PublicKeyBytes {
}
}

pub fn signed_packet_to_hickory_message(signed_packet: &SignedPacket) -> Result<Message> {
pub fn signed_packet_to_hickory_message(
signed_packet: &SignedPacket,
) -> Result<Message, ProtoError> {
let encoded = signed_packet.encoded_packet();
let message = Message::from_bytes(&encoded).anyerr()?;
let message = Message::from_bytes(&encoded)?;
Ok(message)
}

pub fn signed_packet_to_hickory_records_without_origin(
signed_packet: &SignedPacket,
filter: impl Fn(&Record) -> bool,
) -> Result<(Label, BTreeMap<RrKey, Arc<RecordSet>>)> {
let common_zone = Label::from_utf8(&signed_packet.public_key().to_z32()).anyerr()?;
) -> Result<(Label, BTreeMap<RrKey, Arc<RecordSet>>), ProtoError> {
let common_zone = Label::from_utf8(&signed_packet.public_key().to_z32())?;
let mut message = signed_packet_to_hickory_message(signed_packet)?;
let answers = message.take_answers();
let mut output: BTreeMap<RrKey, Arc<RecordSet>> = BTreeMap::new();
Expand All @@ -111,7 +127,7 @@ pub fn signed_packet_to_hickory_records_without_origin(
if name.num_labels() < 1 {
continue;
}
let zone = name.iter().next_back().unwrap().into_label().anyerr()?;
let zone = name.iter().next_back().unwrap().into_label()?;
if zone != common_zone {
continue;
}
Expand All @@ -120,7 +136,7 @@ pub fn signed_packet_to_hickory_records_without_origin(
}

let name_without_zone =
Name::from_labels(name.iter().take(name.num_labels() as usize - 1)).anyerr()?;
Name::from_labels(name.iter().take(name.num_labels() as usize - 1))?;
record.set_name(name_without_zone);

let rrkey = RrKey::new(record.name().into(), record.record_type());
Expand All @@ -144,8 +160,8 @@ pub fn record_set_append_origin(
input: &RecordSet,
origin: &Name,
serial: u32,
) -> Result<RecordSet> {
let new_name = input.name().clone().append_name(origin).anyerr()?;
) -> Result<RecordSet, ProtoError> {
let new_name = input.name().clone().append_name(origin)?;
let mut output = RecordSet::new(new_name.clone(), input.record_type(), serial);
// TODO: less clones
for record in input.records_without_rrsigs() {
Expand Down
Loading