Skip to content

Commit a7dc993

Browse files
nomickthillux
authored andcommitted
add possibility to define sbgp extensions
1 parent 525dcbb commit a7dc993

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

openssl/src/x509/extension.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,137 @@ impl AuthorityKeyIdentifier {
428428
}
429429
}
430430

431+
pub struct SbgpAsIdentifier {
432+
critical: bool,
433+
asn: Vec<(u32, u32)>,
434+
}
435+
436+
impl Default for SbgpAsIdentifier {
437+
fn default() -> SbgpAsIdentifier {
438+
SbgpAsIdentifier::new()
439+
}
440+
}
441+
442+
impl SbgpAsIdentifier {
443+
/// Construct a new `SbgpAsIdentifier` extension.
444+
pub fn new() -> SbgpAsIdentifier {
445+
SbgpAsIdentifier {
446+
critical: false,
447+
asn: Vec::new(),
448+
}
449+
}
450+
451+
/// Sets the `critical` flag to `true`. The extension will be critical.
452+
pub fn critical(&mut self) -> &mut SbgpAsIdentifier {
453+
self.critical = true;
454+
self
455+
}
456+
457+
/// Adds an AS number.
458+
pub fn add_asn(&mut self, asn: u32) -> &mut SbgpAsIdentifier {
459+
self.asn.push((asn, asn));
460+
self
461+
}
462+
463+
/// Adds a range of AS numbers.
464+
pub fn add_asn_range(&mut self, asn_min: u32, asn_max: u32) -> &mut SbgpAsIdentifier {
465+
self.asn.push((asn_min, asn_max));
466+
self
467+
}
468+
469+
/// Return a `SbgpAsIdentifier` extension as an `X509Extension`.
470+
pub fn build(&self, ctx: &X509v3Context<'_>) -> Result<X509Extension, ErrorStack> {
471+
let mut value = String::new();
472+
let mut first = true;
473+
append(&mut value, &mut first, self.critical, "critical");
474+
for (asn_min, asn_max) in &self.asn {
475+
let asn_format = if asn_min == asn_max {
476+
format!("AS:{asn_min}")
477+
} else {
478+
format!("AS:{asn_min}-{asn_max}")
479+
};
480+
append(&mut value, &mut first, true, &asn_format);
481+
}
482+
X509Extension::new_nid(None, Some(ctx), Nid::SBGP_AUTONOMOUSSYSNUM, &value)
483+
}
484+
}
485+
486+
pub struct SbgpIpAddressIdentifier {
487+
critical: bool,
488+
ip_ranges: Vec<(std::net::IpAddr, std::net::IpAddr)>,
489+
}
490+
491+
impl Default for SbgpIpAddressIdentifier {
492+
fn default() -> SbgpIpAddressIdentifier {
493+
SbgpIpAddressIdentifier::new()
494+
}
495+
}
496+
497+
impl SbgpIpAddressIdentifier {
498+
/// Construct a new `SbgpIpAddressIdentifier` extension.
499+
pub fn new() -> SbgpIpAddressIdentifier {
500+
SbgpIpAddressIdentifier {
501+
critical: false,
502+
ip_ranges: Vec::new(),
503+
}
504+
}
505+
506+
/// Sets the `critical` flag to `true`. The extension will be critical.
507+
pub fn critical(&mut self) -> &mut SbgpIpAddressIdentifier {
508+
self.critical = true;
509+
self
510+
}
511+
512+
/// Adds an IP adress.
513+
pub fn add_ip_addr(&mut self, ip_addr: std::net::IpAddr) -> &mut SbgpIpAddressIdentifier {
514+
self.ip_ranges.push((ip_addr, ip_addr));
515+
self
516+
}
517+
518+
/// Adds a range of IPv4 adresses.
519+
pub fn add_ipv4_addr_range(
520+
&mut self,
521+
ip_addr_min: std::net::Ipv4Addr,
522+
ip_addr_max: std::net::Ipv4Addr,
523+
) -> &mut SbgpIpAddressIdentifier {
524+
self.ip_ranges.push((
525+
std::net::IpAddr::V4(ip_addr_min),
526+
std::net::IpAddr::V4(ip_addr_max),
527+
));
528+
self
529+
}
530+
531+
/// Adds a range of IPv6 adresses.
532+
pub fn add_ipv6_addr_range(
533+
&mut self,
534+
ip_addr_min: std::net::Ipv6Addr,
535+
ip_addr_max: std::net::Ipv6Addr,
536+
) -> &mut SbgpIpAddressIdentifier {
537+
self.ip_ranges.push((
538+
std::net::IpAddr::V6(ip_addr_min),
539+
std::net::IpAddr::V6(ip_addr_max),
540+
));
541+
self
542+
}
543+
544+
/// Return a `SbgpIpAddressIdentifier` extension as an `X509Extension`.
545+
pub fn build(&self, ctx: &X509v3Context<'_>) -> Result<X509Extension, ErrorStack> {
546+
let mut value = String::new();
547+
let mut first = true;
548+
append(&mut value, &mut first, self.critical, "critical");
549+
for (ip_addr_min, ip_addr_max) in &self.ip_ranges {
550+
let version = if ip_addr_min.is_ipv4() { 4 } else { 6 };
551+
let ip_addr_format = if ip_addr_min == ip_addr_max {
552+
format!("IPv{version}:{ip_addr_min}")
553+
} else {
554+
format!("IPv{version}:{ip_addr_min}-{ip_addr_max}")
555+
};
556+
append(&mut value, &mut first, true, &ip_addr_format);
557+
}
558+
X509Extension::new_nid(None, Some(ctx), Nid::SBGP_IPADDRBLOCK, &value)
559+
}
560+
}
561+
431562
enum RustGeneralName {
432563
Dns(String),
433564
Email(String),

0 commit comments

Comments
 (0)