-
Notifications
You must be signed in to change notification settings - Fork 165
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
Support for IPv4 and IPv6 #122
Open
alex-berger
wants to merge
2
commits into
briansmith:main
Choose a base branch
from
alex-berger:feature/ip_address_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
use crate::{ | ||
der::read_tag_and_get_value, | ||
name::{iterate_names, presented_dns_id_matches_reference_dns_id, GeneralName, NameIteration}, | ||
Error, | ||
}; | ||
use std::{ | ||
net::{IpAddr, Ipv4Addr, Ipv6Addr}, | ||
str::FromStr, | ||
}; | ||
|
||
/// Subject Alternative Name (SAN) | ||
#[non_exhaustive] | ||
pub enum SubjectAlternativeName { | ||
/// DNS name | ||
Dns(String), | ||
/// IPv4 or IPv6 address | ||
Ip(IpAddr), | ||
} | ||
|
||
impl SubjectAlternativeName { | ||
/// Binary OID of CommonName (CN) (id-at-commonName). | ||
const OID_CN: [u8; 3] = [85, 4, 3]; | ||
|
||
fn traverse<'a>( | ||
input: &'a untrusted::Input, agg: &mut Vec<(u8, Vec<u8>)>, | ||
) -> Result<(), Error> { | ||
let mut reader = untrusted::Reader::new(input.clone()); | ||
while let Ok((tag, value)) = read_tag_and_get_value(&mut reader) { | ||
agg.push((tag, value.as_slice_less_safe().to_vec())); | ||
Self::traverse(&value.clone(), agg)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Strings in Rust are unicode (UTF-8), and unicode codepoints are a | ||
/// superset of iso-8859-1 characters. This specific conversion is | ||
/// actually trivial. | ||
fn latin1_to_string(s: &[u8]) -> String { s.iter().map(|&c| c as char).collect() } | ||
|
||
fn ucs4_to_string(s: &[u8]) -> Result<String, Error> { | ||
if s.len() % 4 == 0 { | ||
let mut tmp = String::with_capacity(s.len() / 4); | ||
for i in (0..s.len()).step_by(4) { | ||
match std::char::from_u32( | ||
(u32::from(s[i]) << 24) | ||
| (u32::from(s[i]) << 16) | ||
| (u32::from(s[i]) << 8) | ||
| u32::from(s[i + 1]), | ||
) { | ||
Some(c) => tmp.push(c), | ||
_ => return Err(Error::BadDER), | ||
} | ||
} | ||
Ok(tmp) | ||
} else { | ||
Err(Error::BadDER) | ||
} | ||
} | ||
|
||
fn bmp_to_string(s: &[u8]) -> Result<String, Error> { | ||
if s.len() % 2 == 0 { | ||
let mut tmp = String::with_capacity(s.len() / 2); | ||
for i in (0..s.len()).step_by(2) { | ||
match std::char::from_u32((u32::from(s[i]) << 8) | u32::from(s[i + 1])) { | ||
Some(c) => tmp.push(c), | ||
_ => return Err(Error::BadDER), | ||
} | ||
} | ||
Ok(tmp) | ||
} else { | ||
Err(Error::BadDER) | ||
} | ||
} | ||
|
||
fn extract_common_name(der: &untrusted::Input) -> Option<String> { | ||
let mut input = vec![]; | ||
Self::traverse(der, &mut input).unwrap(); | ||
if let Some(oid_position) = input | ||
.iter() | ||
.position(|(tag, value)| *tag == 6u8 && value.as_slice() == Self::OID_CN) | ||
{ | ||
match input.get(oid_position + 1) { | ||
// PrintableString (Subset of ASCII, therefore valid UTF8) | ||
Some((19u8, value)) => String::from_utf8(value.clone()).ok(), | ||
// UTF8String | ||
Some((12u8, value)) => String::from_utf8(value.clone()).ok(), | ||
// UniversalString (UCS-4 32-bit encoded) | ||
Some((28u8, value)) => Self::ucs4_to_string(value).ok(), | ||
// BMPString (UCS-2 16-bit encoded) | ||
Some((30u8, value)) => Self::bmp_to_string(value).ok(), | ||
// VideotexString resp. TeletexString ISO-8859-1 encoded | ||
Some((21u8, value)) => Some(Self::latin1_to_string(value.as_slice())), | ||
_ => None, | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn matches_dns(dns: &str, name: &GeneralName) -> bool { | ||
let dns_input = untrusted::Input::from(dns.as_bytes()); | ||
match name { | ||
GeneralName::DNSName(d) => | ||
presented_dns_id_matches_reference_dns_id(d.clone(), dns_input).unwrap_or(false), | ||
GeneralName::DirectoryName(d) => { | ||
if let Some(x) = Self::extract_common_name(d) { | ||
//x == dns | ||
presented_dns_id_matches_reference_dns_id( | ||
untrusted::Input::from(x.as_bytes()), | ||
dns_input, | ||
) | ||
.unwrap_or(false) | ||
} else { | ||
false | ||
} | ||
}, | ||
_ => false, | ||
} | ||
} | ||
|
||
fn matches_ip(ip: &IpAddr, name: &GeneralName) -> Result<bool, untrusted::EndOfInput> { | ||
match name { | ||
GeneralName::IPAddress(d) => match ip { | ||
IpAddr::V4(v4) if d.len() == 4 => { | ||
let mut reader = untrusted::Reader::new(d.clone()); | ||
let mut raw_ip_address: [u8; 4] = Default::default(); | ||
raw_ip_address.clone_from_slice(reader.read_bytes(4)?.as_slice_less_safe()); | ||
Ok(Ipv4Addr::from(raw_ip_address) == *v4) | ||
}, | ||
IpAddr::V6(v6) if d.len() == 16 => { | ||
let mut reader = untrusted::Reader::new(d.clone()); | ||
let mut raw_ip_address: [u8; 16] = Default::default(); | ||
raw_ip_address.clone_from_slice(reader.read_bytes(16)?.as_slice_less_safe()); | ||
Ok(Ipv6Addr::from(raw_ip_address) == *v6) | ||
}, | ||
_ => Ok(false), | ||
}, | ||
GeneralName::DirectoryName(d) => | ||
if let Some(x) = Self::extract_common_name(d) { | ||
match IpAddr::from_str(x.as_str()) { | ||
Ok(a) => Ok(a == *ip), | ||
Err(_) => Ok(false), | ||
} | ||
} else { | ||
Ok(false) | ||
}, | ||
_ => Ok(false), | ||
} | ||
} | ||
|
||
fn matches(&self, _cert: &super::EndEntityCert, name: &GeneralName) -> Result<bool, Error> { | ||
match self { | ||
SubjectAlternativeName::Dns(d) => Ok(Self::matches_dns(d, name)), | ||
SubjectAlternativeName::Ip(ip) => Self::matches_ip(ip, name).map_err(|_| Error::BadDER), | ||
//_ => Ok(false), | ||
} | ||
} | ||
|
||
/// Check if this name is the subject of the provided certificate. | ||
pub fn is_subject_of_legacy( | ||
&self, cert: &super::EndEntityCert, check_cn: bool, | ||
) -> Result<(), Error> { | ||
let crt = &cert.inner; | ||
iterate_names( | ||
if check_cn { Some(crt.subject) } else { None }, | ||
crt.subject_alt_name, | ||
Err(Error::CertNotValidForName), | ||
&|name| match self.matches(cert, &name) { | ||
Ok(true) => NameIteration::Stop(Ok(())), | ||
Ok(false) => NameIteration::KeepGoing, | ||
Err(e) => NameIteration::Stop(Err(e)), | ||
}, | ||
) | ||
} | ||
|
||
/// Check if this name is the subject of the provided certificate. | ||
pub fn is_subject_of(&self, cert: &super::EndEntityCert) -> Result<(), Error> { | ||
self.is_subject_of_legacy(cert, false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#[cfg(feature = "std")] | ||
mod tests { | ||
|
||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; | ||
use webpki::{Error, SubjectAlternativeName}; | ||
|
||
#[test] | ||
fn dns_in_cn_legacy() { | ||
let der = include_bytes!("san/dns_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("example.com".to_string()); | ||
assert_eq!(name.is_subject_of_legacy(&cert, true), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn dns_in_cn() { | ||
let der = include_bytes!("san/dns_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("example.com".to_string()); | ||
assert_eq!(name.is_subject_of(&cert), Err(Error::CertNotValidForName)); | ||
} | ||
|
||
#[test] | ||
fn dns_wildcard_in_cn_legacy() { | ||
let der = include_bytes!("san/dns_wildcard_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("sub.example.com".to_string()); | ||
assert_eq!(name.is_subject_of_legacy(&cert, true), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn dns_wildcard_in_cn() { | ||
let der = include_bytes!("san/dns_wildcard_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("sub.example.com".to_string()); | ||
assert_eq!(name.is_subject_of(&cert), Err(Error::CertNotValidForName)); | ||
} | ||
|
||
#[test] | ||
fn dns_in_san() { | ||
let der = include_bytes!("san/dns_in_san.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("example.org".to_string()); | ||
assert_eq!(name.is_subject_of(&cert), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn dns_wildcard_in_san() { | ||
let der = include_bytes!("san/dns_wildcard_in_san.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Dns("sub.example.org".to_string()); | ||
assert_eq!(name.is_subject_of(&cert), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn ip_in_cn_legacy() { | ||
let der = include_bytes!("san/ip_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V4(Ipv4Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of_legacy(&cert, true), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn ip_in_cn() { | ||
let der = include_bytes!("san/ip_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V4(Ipv4Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of(&cert), Err(Error::CertNotValidForName)); | ||
} | ||
|
||
#[test] | ||
fn ipv6_in_cn_legacy() { | ||
let der = include_bytes!("san/ipv6_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V6(Ipv6Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of_legacy(&cert, true), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn ipv6_in_cn() { | ||
let der = include_bytes!("san/ipv6_in_cn.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V6(Ipv6Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of(&cert), Err(Error::CertNotValidForName)); | ||
} | ||
|
||
#[test] | ||
fn ip_in_san() { | ||
let der = include_bytes!("san/ip_in_san.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V4(Ipv4Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of(&cert), Ok(())); | ||
} | ||
|
||
#[test] | ||
fn ipv6_in_san() { | ||
let der = include_bytes!("san/ipv6_in_san.der"); | ||
let cert = webpki::EndEntityCert::from(der).unwrap(); | ||
let name = SubjectAlternativeName::Ip(IpAddr::V6(Ipv6Addr::LOCALHOST)); | ||
assert_eq!(name.is_subject_of(&cert), Ok(())); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"CN": "example.com", | ||
"key": { | ||
"algo": "rsa", | ||
"size": 2048 | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be misunderstanding how webpki, before this PR, is using
subject
. webpki usessubject
ONLY to verify that the subject conforms to any directoryName constraints that may be present in the chain. It doesn't do any DNS name or IP address matching of the common name. And, as was mentioned in the initial review of this PR, we don't want to do any such processing of the common name. Instead we only want to look at iPAddress subjectAltNames.Thus, unless I'm massively understanding what you're trying to accomplish in this PR, everything related to the subject and especially the subject's common name should be removed.