Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating certificate resources covering all addresses and ASNs. #226

Merged
merged 1 commit into from
Aug 26, 2022
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
28 changes: 27 additions & 1 deletion src/repository/resources/asres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use routecore::asn::ParseAsnError;
use super::super::cert::Overclaim;
use super::super::x509::encode_extension;
use super::super::error::VerificationError;
use super::chain::{Block, SharedChain};
use super::chain::{Block, OwnedChain, SharedChain};
use super::choice::{InheritedResources, ResourcesChoice};


Expand Down Expand Up @@ -296,6 +296,17 @@ impl AsBlocks {
AsBlocks(SharedChain::empty())
}

/// Creates a value covering all ASNs.
pub fn all() -> Self {
AsBlocks(SharedChain::from_owned(
unsafe {
OwnedChain::from_vec_unchecked(vec![
AsBlock::all()
])
}
))
}

/// Creates AS blocks from AS resources.
///
/// If the AS resources are of the inherited variant, a validation error
Expand Down Expand Up @@ -603,6 +614,11 @@ pub enum AsBlock {
}

impl AsBlock {
/// Returns an AS block covering all ASNs.
pub fn all() -> AsBlock {
AsBlock::Range(AsRange::all())
}

/// The smallest AS number that is part of this block.
pub fn min(&self) -> Asn {
match *self {
Expand Down Expand Up @@ -885,6 +901,11 @@ impl AsRange {
AsRange { min, max }
}

/// Returns an AS block covering all ASNs.
pub fn all() -> AsRange {
AsRange::new(Asn::MIN, Asn::MAX)
}

/// Returns the smallest AS number that is part of this range.
pub fn min(self) -> Asn {
self.min
Expand Down Expand Up @@ -1061,6 +1082,11 @@ impl From<OverclaimedAsResources> for VerificationError {
mod test {
use super::*;

#[test]
fn as_blocks_all() {
assert_eq!(AsBlocks::all().to_string(), "AS0-AS4294967295");
}

#[test]
fn as_block_from_str() {
// Good
Expand Down
2 changes: 1 addition & 1 deletion src/repository/resources/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl<T: Block> Eq for Chain<T> {}
pub struct OwnedChain<T: Block>(Vec<T>);

impl<T: Block> OwnedChain<T> {
unsafe fn from_vec_unchecked(vec: Vec<T>) -> Self {
pub unsafe fn from_vec_unchecked(vec: Vec<T>) -> Self {
OwnedChain(vec)
}

Expand Down
39 changes: 38 additions & 1 deletion src/repository/resources/ipres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::super::cert::Overclaim;
use super::super::error::VerificationError;
use super::super::roa::RoaIpAddress;
use super::super::x509::encode_extension;
use super::chain::{Block, SharedChain};
use super::chain::{Block, OwnedChain, SharedChain};
use super::choice::{InheritedResources, ResourcesChoice};


Expand Down Expand Up @@ -310,6 +310,17 @@ impl IpBlocks {
IpBlocks(SharedChain::empty())
}

/// Creates a value covering all addresses.
pub fn all() -> Self {
IpBlocks(SharedChain::from_owned(
unsafe {
OwnedChain::from_vec_unchecked(vec![
IpBlock::all()
])
}
))
}

/// Creates address blocks from address resources.
///
/// If the resources are of the inherited variant, returns an error.
Expand Down Expand Up @@ -654,6 +665,11 @@ pub enum IpBlock {
}

impl IpBlock {
/// Creates a new block covering all addresses.
pub fn all() -> Self {
IpBlock::Prefix(Prefix::all())
}

/// Creates a new block from an IPv4 representation.
pub fn from_v4_str(s: &str) -> Result<Self, FromStrError> {
if let Some(sep) = s.find('/') {
Expand Down Expand Up @@ -1225,6 +1241,11 @@ impl Prefix {
}
}

/// Creates a prefix covering all addresses.
pub fn all() -> Self {
Prefix::new(0, 0)
}

/// Creates a new prefix from its encoding as a BIT STRING.
pub fn from_bit_string(
src: &BitString
Expand Down Expand Up @@ -1702,6 +1723,11 @@ impl Ipv4Blocks {
Ipv4Blocks(IpBlocks::empty())
}

/// Creates a value covering all addresses.
pub fn all() -> Self {
Ipv4Blocks(IpBlocks::all())
}

pub fn to_ip_resources(&self) -> IpResources {
IpResources::blocks(self.0.clone())
}
Expand Down Expand Up @@ -1788,6 +1814,11 @@ impl Ipv6Blocks {
Ipv6Blocks(IpBlocks::empty())
}

/// Creates a value covering all addresses.
pub fn all() -> Self {
Ipv6Blocks(IpBlocks::all())
}

pub fn to_ip_resources(&self) -> IpResources {
IpResources::blocks(self.0.clone())
}
Expand Down Expand Up @@ -2051,6 +2082,12 @@ mod test {
use bcder::encode::Values;
use super::*;

#[test]
fn ip_blocks_all() {
assert_eq!(Ipv4Blocks::all().to_string(), "0.0.0.0/0");
assert_eq!(Ipv6Blocks::all().to_string(), "::/0");
}

#[test]
fn ip_blocks_to_v4_str() {
let expected_str = "10.0.0.0, 10.1.0.0-10.1.2.255, 192.168.0.0/16";
Expand Down
10 changes: 5 additions & 5 deletions src/repository/resources/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl ResourceSet {
}

pub fn all() -> ResourceSet {
let asns = "0-4294967295";
let v4 = "0.0.0.0-255.255.255.255";
let v6 = "::0/0";

ResourceSet::from_strs(asns, v4, v6).unwrap()
ResourceSet {
asn: AsBlocks::all(),
ipv4: Ipv4Blocks::all(),
ipv6: Ipv6Blocks::all(),
}
}

pub fn is_empty(&self) -> bool {
Expand Down