diff --git a/openssl-sys/src/handwritten/x509_sbgp.rs b/openssl-sys/src/handwritten/x509_sbgp.rs index 1b32fc46e44105fe92ea6cc001a352674bd435d4..1adf4dc0aee895e94f6d975a5e43d0c25aa9d532 100644 --- a/openssl-sys/src/handwritten/x509_sbgp.rs +++ b/openssl-sys/src/handwritten/x509_sbgp.rs @@ -1,73 +1,103 @@ +#[cfg(ossl110)] use super::super::*; +#[cfg(ossl110)] use libc::*; #[repr(C)] +#[cfg(ossl110)] pub struct ASRange { pub min: *mut ASN1_INTEGER, pub max: *mut ASN1_INTEGER, } #[repr(C)] +#[cfg(ossl110)] pub struct ASIdOrRange { pub type_: c_int, pub u: ASIdOrRange_st_anon_union, } #[repr(C)] +#[cfg(ossl110)] pub union ASIdOrRange_st_anon_union { pub id: *mut ASN1_INTEGER, pub range: *mut ASRange, } +#[cfg(ossl110)] stack!(stack_st_ASIdOrRange); #[repr(C)] +#[cfg(ossl110)] +pub union ASIdentifierChoice_st_anon_union { + pub asIdsOrRanges: *mut stack_st_ASIdOrRange, +} + +#[repr(C)] +#[cfg(ossl110)] pub struct ASIdentifierChoice { pub type_: c_int, - pub asIdsOrRanges: *mut stack_st_ASIdOrRange, + pub u: ASIdentifierChoice_st_anon_union, } #[repr(C)] +#[cfg(ossl110)] pub struct ASIdentifiers { pub asnum: *mut ASIdentifierChoice, pub rdi: *mut ASIdentifierChoice, } #[repr(C)] +#[cfg(ossl110)] pub struct IPAddressRange { pub min: *mut ASN1_BIT_STRING, pub max: *mut ASN1_BIT_STRING, } #[repr(C)] +#[cfg(ossl110)] pub struct IPAddressOrRange { pub type_: c_int, pub u: IPAddressOrRange_st_anon_union, } #[repr(C)] +#[cfg(ossl110)] pub union IPAddressOrRange_st_anon_union { pub addressPrefix: *mut ASN1_BIT_STRING, pub addressRange: *mut IPAddressRange, } +#[cfg(ossl110)] stack!(stack_st_IPAddressOrRange); +#[cfg(ossl110)] type IPAddressOrRanges = stack_st_IPAddressOrRange; #[repr(C)] +#[cfg(ossl110)] +pub union IPAddressChoice_st_anon_union { + pub addressesOrRanges: *mut IPAddressOrRanges, +} + +#[repr(C)] +#[cfg(ossl110)] pub struct IPAddressChoice { pub type_: c_int, - pub addressesOrRanges: *mut IPAddressOrRanges, + pub u: IPAddressChoice_st_anon_union, } #[repr(C)] +#[cfg(ossl110)] pub struct IPAddressFamily { pub addressFamily: *mut ASN1_OCTET_STRING, pub ipAddressChoice: *mut IPAddressChoice, } +#[cfg(ossl110)] stack!(stack_st_IPAddressFamily); +#[cfg(ossl110)] type IPAddrBlocks = stack_st_IPAddressFamily; +#[cfg(ossl110)] extern "C" { pub fn ASIdentifiers_free(asi: *mut ASIdentifiers); pub fn ASIdOrRange_free(asi: *mut ASIdOrRange); @@ -75,6 +105,7 @@ extern "C" { pub fn IPAddressOrRange_free(asi: *mut IPAddressOrRange); } +#[cfg(ossl110)] pub unsafe fn X509v3_addr_get_afi(f: *mut IPAddressFamily) -> c_int { if f.is_null() { 0 @@ -84,11 +115,12 @@ pub unsafe fn X509v3_addr_get_afi(f: *mut IPAddressFamily) -> c_int { 0 } else { let raw = ASN1_STRING_get0_data(d); - ((*raw.offset(0) as i32) << 8) | *raw.offset(1) as i32 + ((*raw.offset(0) as c_int) << 8) | *raw.offset(1) as c_int } } } +#[cfg(ossl110)] fn length_from_afi(afi: c_int) -> isize { match afi { IANA_AFI_IPV4 => 4, @@ -97,6 +129,7 @@ fn length_from_afi(afi: c_int) -> isize { } } +#[cfg(ossl110)] struct ASN1_STRING_internal { length: c_int, type_: c_int, @@ -108,10 +141,12 @@ struct ASN1_STRING_internal { */ flags: c_int, } + /* * Expand the bitstring form of an address into a raw byte array. * At the moment this is coded for simplicity, not speed. */ +#[cfg(ossl110)] fn addr_expand(addr: *mut u8, bs: *const ASN1_BIT_STRING, length: isize, fill: u8) -> bool { unsafe { let str = bs as *mut ASN1_STRING; @@ -151,6 +186,7 @@ fn addr_expand(addr: *mut u8, bs: *const ASN1_BIT_STRING, length: isize, fill: u /* * Extract min and max values from an IPAddressOrRange. */ +#[cfg(ossl110)] fn extract_min_max(aor: *mut IPAddressOrRange, min: *mut u8, max: *mut u8, length: isize) -> bool { unsafe { match (*aor).type_ { @@ -167,6 +203,7 @@ fn extract_min_max(aor: *mut IPAddressOrRange, min: *mut u8, max: *mut u8, lengt } } +#[cfg(ossl110)] pub fn X509v3_addr_get_range( aor: *mut IPAddressOrRange, afi: c_int, diff --git a/openssl-sys/src/x509_sbgp.rs b/openssl-sys/src/x509_sbgp.rs index 8bfcf6d81ff7886840e823c396ed4e95d397f506..02278a000176cf31e15e89bfe9351eb697e7ff25 100644 --- a/openssl-sys/src/x509_sbgp.rs +++ b/openssl-sys/src/x509_sbgp.rs @@ -1,18 +1,30 @@ +#[cfg(ossl110)] use libc::*; +#[cfg(ossl110)] use super::*; +#[cfg(ossl110)] pub const ASIdOrRange_id: c_int = 0; +#[cfg(ossl110)] pub const ASIdOrRange_range: c_int = 1; +#[cfg(ossl110)] pub const ASIdentifierChoice_inherit: c_int = 0; +#[cfg(ossl110)] pub const ASIdentifierChoice_asIdsOrRanges: c_int = 1; +#[cfg(ossl110)] pub const IPAddressOrRange_addressPrefix: c_int = 0; +#[cfg(ossl110)] pub const IPAddressOrRange_addressRange: c_int = 1; +#[cfg(ossl110)] pub const IPAddressChoice_inherit: c_int = 0; +#[cfg(ossl110)] pub const IPAddressChoice_addressesOrRanges: c_int = 1; +#[cfg(ossl110)] pub const IANA_AFI_IPV4: c_int = 1; +#[cfg(ossl110)] pub const IANA_AFI_IPV6: c_int = 2; diff --git a/openssl/src/x509/extension.rs b/openssl/src/x509/extension.rs index 62a8e93de95ffd41e69fcbb432e7b7e2399bcacc..fd164e827efa6e1a98d202c904e4a6ba8e43dc36 100644 --- a/openssl/src/x509/extension.rs +++ b/openssl/src/x509/extension.rs @@ -428,17 +428,20 @@ impl AuthorityKeyIdentifier { } } +#[cfg(ossl110)] pub struct SbgpAsIdentifier { critical: bool, asn: Vec<(u32, u32)>, } +#[cfg(ossl110)] impl Default for SbgpAsIdentifier { fn default() -> SbgpAsIdentifier { SbgpAsIdentifier::new() } } +#[cfg(ossl110)] impl SbgpAsIdentifier { /// Construct a new `SbgpAsIdentifier` extension. pub fn new() -> SbgpAsIdentifier { @@ -484,17 +487,20 @@ impl SbgpAsIdentifier { } } +#[cfg(ossl110)] pub struct SbgpIpAddressIdentifier { critical: bool, ip_ranges: Vec<(std::net::IpAddr, std::net::IpAddr)>, } +#[cfg(ossl110)] impl Default for SbgpIpAddressIdentifier { fn default() -> SbgpIpAddressIdentifier { SbgpIpAddressIdentifier::new() } } +#[cfg(ossl110)] impl SbgpIpAddressIdentifier { /// Construct a new `SbgpIpAddressIdentifier` extension. pub fn new() -> SbgpIpAddressIdentifier { diff --git a/openssl/src/x509/sbgp.rs b/openssl/src/x509/sbgp.rs index 7175979d95cd5c631d8e4091407fe25942496d18..3a6aa04fc42c065607a923aac9228a63bb85e204 100644 --- a/openssl/src/x509/sbgp.rs +++ b/openssl/src/x509/sbgp.rs @@ -1,20 +1,28 @@ +#[cfg(ossl110)] +use std::convert::TryInto; +#[cfg(ossl110)] use std::mem::MaybeUninit; +#[cfg(ossl110)] use ffi::{ ASIdOrRange_id, ASIdOrRange_range, ASIdentifierChoice_asIdsOrRanges, ASIdentifierChoice_inherit, IPAddressChoice_addressesOrRanges, X509v3_addr_get_afi, X509v3_addr_get_range, ASN1_INTEGER, IANA_AFI_IPV4, IANA_AFI_IPV6, }; +#[cfg(ossl110)] use foreign_types::{ForeignType, ForeignTypeRef}; +#[cfg(ossl110)] use crate::{ asn1::Asn1IntegerRef, stack::{Stack, StackRef, Stackable}, util::{ForeignTypeExt, ForeignTypeRefExt}, }; +#[cfg(ossl110)] use super::X509; +#[cfg(ossl110)] foreign_type_and_impl_send_sync! { type CType = ffi::ASIdOrRange; fn drop = ffi::ASIdOrRange_free; @@ -24,11 +32,12 @@ foreign_type_and_impl_send_sync! { /// Reference to `ASIdOrRange`. pub struct ASIdOrRangeRef; } - +#[cfg(ossl110)] impl Stackable for ASIdOrRange { type StackType = ffi::stack_st_ASIdOrRange; } +#[cfg(ossl110)] foreign_type_and_impl_send_sync! { type CType = ffi::ASIdentifiers; fn drop = ffi::ASIdentifiers_free; @@ -39,6 +48,7 @@ foreign_type_and_impl_send_sync! { pub struct ASIdentifiersRef; } +#[cfg(ossl110)] impl ASIdentifiers { pub fn inherited(&self) -> bool { unsafe { @@ -56,7 +66,7 @@ impl ASIdentifiers { if (*asnum).type_ != ASIdentifierChoice_asIdsOrRanges { return None; } - if let Some(s) = StackRef::::from_const_ptr_opt((*asnum).asIdsOrRanges) { + if let Some(s) = StackRef::::from_const_ptr_opt((*asnum).u.asIdsOrRanges) { for a_ptr in s { let a = a_ptr.as_ptr(); if (*a).type_ == ASIdOrRange_id { @@ -85,6 +95,7 @@ impl ASIdentifiers { } } +#[cfg(ossl110)] foreign_type_and_impl_send_sync! { type CType = ffi::IPAddressOrRange; fn drop = ffi::IPAddressOrRange_free; @@ -95,10 +106,12 @@ foreign_type_and_impl_send_sync! { pub struct IPAddressOrRangeRef; } +#[cfg(ossl110)] impl Stackable for IPAddressOrRange { type StackType = ffi::stack_st_IPAddressOrRange; } +#[cfg(ossl110)] foreign_type_and_impl_send_sync! { type CType = ffi::IPAddressFamily; fn drop = ffi::IPAddressFamily_free; @@ -109,21 +122,24 @@ foreign_type_and_impl_send_sync! { pub struct IPAddressFamilyRef; } +#[cfg(ossl110)] impl Stackable for IPAddressFamily { type StackType = ffi::stack_st_IPAddressFamily; } #[derive(PartialEq, Eq, Debug)] +#[cfg(ossl110)] pub enum IPVersion { V4, V6, } +#[cfg(ossl110)] impl IPAddressFamily { pub fn fam(&self) -> Option { let ptr = self.0; unsafe { - match X509v3_addr_get_afi(ptr) { + match X509v3_addr_get_afi(ptr) as libc::c_int { IANA_AFI_IPV4 => Some(IPVersion::V4), IANA_AFI_IPV6 => Some(IPVersion::V6), _ => None, @@ -140,7 +156,7 @@ impl IPAddressFamily { return None; } let stack = - StackRef::::from_const_ptr_opt((*choice).addressesOrRanges)?; + StackRef::::from_const_ptr_opt((*choice).u.addressesOrRanges)?; for e in stack { let mut min = MaybeUninit::<[u8; 16]>::uninit(); let mut max = MaybeUninit::<[u8; 16]>::uninit(); @@ -152,8 +168,10 @@ impl IPAddressFamily { 16, ); r.push(( - Self::data_to_ip_addr(min.assume_init(), size)?, - Self::data_to_ip_addr(max.assume_init(), size)?, + #[allow(clippy::useless_conversion)] + Self::data_to_ip_addr(min.assume_init(), size.try_into().unwrap())?, + #[allow(clippy::useless_conversion)] + Self::data_to_ip_addr(max.assume_init(), size.try_into().unwrap())?, )) } } @@ -180,11 +198,13 @@ impl IPAddressFamily { } } +#[cfg(ossl110)] pub trait ExtractSBGPInfo { fn asn(&self) -> Option; fn ip_addresses(&self) -> Option>; } +#[cfg(ossl110)] impl ExtractSBGPInfo for X509 { fn asn(&self) -> Option { unsafe { diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs index b29a8ba5cc7f581d7f482e0c9a6af971daccfba5..9adc7450df4267e9be98c5441f25d96c55554094 100644 --- a/openssl/src/x509/tests.rs +++ b/openssl/src/x509/tests.rs @@ -1,5 +1,7 @@ use std::cmp::Ordering; +#[cfg(ossl110)] use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +#[cfg(ossl110)] use std::str::FromStr; use crate::asn1::{Asn1Object, Asn1OctetString, Asn1Time}; @@ -13,8 +15,12 @@ use crate::ssl::SslFiletype; use crate::stack::Stack; use crate::x509::extension::{ AuthorityKeyIdentifier, BasicConstraints, ExtendedKeyUsage, KeyUsage, SubjectAlternativeName, - SubjectKeyIdentifier, SbgpAsIdentifier, SbgpIpAddressIdentifier + SubjectKeyIdentifier, }; +#[cfg(ossl110)] +use crate::x509::extension::{SbgpAsIdentifier, SbgpIpAddressIdentifier}; +#[cfg(ossl110)] +use crate::x509::sbgp::ExtractSBGPInfo; #[cfg(not(boringssl))] use crate::x509::store::X509Lookup; use crate::x509::store::X509StoreBuilder; @@ -29,7 +35,6 @@ use crate::x509::{CrlReason, X509Builder}; use crate::x509::{ CrlStatus, X509Crl, X509Extension, X509Name, X509Req, X509StoreContext, X509VerifyResult, X509, }; -use crate::x509::sbgp::ExtractSBGPInfo; #[cfg(ossl110)] use foreign_types::ForeignType; @@ -1182,13 +1187,14 @@ fn test_dist_point_null() { } #[test] +#[cfg(ossl110)] fn test_sbgp_extensions_parsing() { let cert = include_bytes!("../../test/rfc3779.pem"); let cert = X509::from_pem(cert).unwrap(); let asn_ranges = cert.asn().unwrap().ranges().unwrap(); - assert_eq!(asn_ranges[0], (10,18)); - assert_eq!(asn_ranges[1], (20,20)); + assert_eq!(asn_ranges[0], (10, 18)); + assert_eq!(asn_ranges[1], (20, 20)); let families = cert.ip_addresses().unwrap(); for family in families { @@ -1196,7 +1202,10 @@ fn test_sbgp_extensions_parsing() { for (ip_min, ip_max) in ranges { if let (IpAddr::V6(a_v6_min), IpAddr::V6(a_v6_max)) = (ip_min, ip_max) { assert_eq!(a_v6_min, Ipv6Addr::from_str("fd00::").unwrap()); - assert_eq!(a_v6_max, Ipv6Addr::from_str("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap()); + assert_eq!( + a_v6_max, + Ipv6Addr::from_str("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap() + ); } if let (IpAddr::V4(a_v4_min), IpAddr::V4(a_v4_max)) = (ip_min, ip_max) { assert_eq!(a_v4_min, Ipv4Addr::from_str("10.0.0.0").unwrap()); @@ -1207,19 +1216,29 @@ fn test_sbgp_extensions_parsing() { } #[test] +#[cfg(ossl110)] fn test_sbgp_extensions_builder() { let mut builder = X509Builder::new().unwrap(); let asn_ext = SbgpAsIdentifier::new() .critical() .add_asn(32) - .add_asn_range(10,20) - .build(&builder.x509v3_context(None, None)).unwrap(); + .add_asn_range(10, 20) + .build(&builder.x509v3_context(None, None)) + .unwrap(); builder.append_extension(asn_ext).unwrap(); let mut ip_addr_ext = SbgpIpAddressIdentifier::new(); ip_addr_ext.critical(); - ip_addr_ext.add_ipv6_addr_range(Ipv6Addr::from_str("fd00::").unwrap(), Ipv6Addr::from_str("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap()); - ip_addr_ext.add_ipv4_addr_range(Ipv4Addr::from_str("10.0.0.0").unwrap(), Ipv4Addr::from_str("10.0.0.255").unwrap()); - let build_ext = ip_addr_ext.build(&builder.x509v3_context(None, None)).unwrap(); + ip_addr_ext.add_ipv6_addr_range( + Ipv6Addr::from_str("fd00::").unwrap(), + Ipv6Addr::from_str("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").unwrap(), + ); + ip_addr_ext.add_ipv4_addr_range( + Ipv4Addr::from_str("10.0.0.0").unwrap(), + Ipv4Addr::from_str("10.0.0.255").unwrap(), + ); + let build_ext = ip_addr_ext + .build(&builder.x509v3_context(None, None)) + .unwrap(); builder.append_extension(build_ext).unwrap(); -} \ No newline at end of file +} diff --git a/systest/build.rs b/systest/build.rs index 295122e1b3ad2545425a5dcf1fb253c21f678a5e..a4da53d42207d31e344dc68995d9a43da2a84c81 100644 --- a/systest/build.rs +++ b/systest/build.rs @@ -120,9 +120,11 @@ fn main() { s == "DIST_POINT_NAME_st_anon_union" || // inline union s == "ASIdOrRange_st_anon_union" || // inline union s == "ASIdOrRange" || + s == "ASIdentifierChoice_st_anon_union" || // inline union s == "ASIdentifierChoice" || s == "IPAddressOrRange_st_anon_union" || // inline union s == "IPAddressOrRange" || + s == "IPAddressChoice_st_anon_union" || // inline union s == "IPAddressChoice" || s == "PKCS7_data" || s == "ASN1_TYPE_value"