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

Make *ring* optional #134

Merged
merged 7 commits into from
Jul 28, 2023
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
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ license = "ISC"
name = "rustls-webpki"
readme = "README.md"
repository = "https://github.com/rustls/webpki"
version = "0.101.2"
version = "0.102.0-alpha.0"

include = [
"Cargo.toml",
Expand All @@ -43,6 +43,7 @@ include = [
"src/name/verify.rs",
"src/name/name.rs",
"src/signed_data.rs",
"src/ring_algs.rs",
"src/time.rs",
"src/trust_anchor.rs",
"src/x509.rs",
Expand All @@ -62,12 +63,13 @@ rustdoc-args = ["--cfg", "docsrs"]
name = "webpki"

[features]
default = ["std"]
default = ["std", "ring"]
ring = ["dep:ring"]
alloc = ["ring/alloc"]
std = ["alloc"]

[dependencies]
ring = { version = "0.16.19", default-features = false }
ring = { version = "0.16.19", default-features = false, optional = true }
untrusted = "0.7.1"

[dev-dependencies]
Expand Down
14 changes: 7 additions & 7 deletions src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ fn days_in_feb(year: u64) -> u64 {
}
}

#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162;
/// All the days up to and including 1969, plus the 477 leap days since AD began
/// (calculated in Gregorian rules).
const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 1969 * 365 + 477;

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -151,7 +152,6 @@ mod tests {
assert_eq!(days_in_month(2100, 2), 28);
}

#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
#[test]
fn test_time_from_ymdhms_utc() {
use super::{time_from_ymdhms_utc, Error, Time, UNIX_EPOCH_YEAR};
Expand Down Expand Up @@ -188,23 +188,23 @@ mod tests {

// year boundary
assert_eq!(
Time::from_seconds_since_unix_epoch(1483228799),
Time::from_seconds_since_unix_epoch(1_483_228_799),
time_from_ymdhms_utc(2016, 12, 31, 23, 59, 59).unwrap()
);
assert_eq!(
Time::from_seconds_since_unix_epoch(1483228800),
Time::from_seconds_since_unix_epoch(1_483_228_800),
time_from_ymdhms_utc(2017, 1, 1, 0, 0, 0).unwrap()
);

// not a leap year
assert_eq!(
Time::from_seconds_since_unix_epoch(1492449162),
Time::from_seconds_since_unix_epoch(1_492_449_162),
time_from_ymdhms_utc(2017, 4, 17, 17, 12, 42).unwrap()
);

// leap year, post-feb
assert_eq!(
Time::from_seconds_since_unix_epoch(1460913162),
Time::from_seconds_since_unix_epoch(1_460_913_162),
time_from_ymdhms_utc(2016, 4, 17, 17, 12, 42).unwrap()
);
}
Expand Down
14 changes: 7 additions & 7 deletions src/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::cert::lenient_certificate_serial_number;
use crate::der::{self, DerIterator, FromDer, Tag, CONSTRUCTED, CONTEXT_SPECIFIC};
use crate::signed_data::{self, SignedData};
use crate::x509::{remember_extension, set_extension_once, DistributionPointName, Extension};
use crate::{Error, SignatureAlgorithm, Time};
use crate::{Error, SignatureVerificationAlgorithm, Time};

#[cfg(feature = "alloc")]
use std::collections::HashMap;
Expand All @@ -40,10 +40,10 @@ pub trait CertRevocationList: Sealed {
fn find_serial(&self, serial: &[u8]) -> Result<Option<BorrowedRevokedCert>, Error>;

/// Verify the CRL signature using the issuer's subject public key information (SPKI)
/// and a list of supported signature algorithms.
/// and a list of supported signature verification algorithms.
fn verify_signature(
&self,
supported_sig_algs: &[&SignatureAlgorithm],
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
) -> Result<(), Error>;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl CertRevocationList for OwnedCertRevocationList {

fn verify_signature(
&self,
supported_sig_algs: &[&SignatureAlgorithm],
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
) -> Result<(), Error> {
signed_data::verify_signed_data(
Expand Down Expand Up @@ -172,9 +172,9 @@ impl<'a> BorrowedCertRevocationList<'a> {
// values longer than 20 octets.
//
extension.value.read_all(Error::InvalidCrlNumber, |der| {
let crl_number = ring::io::der::positive_integer(der)
let crl_number = der::nonnegative_integer(der)
.map_err(|_| Error::InvalidCrlNumber)?
.big_endian_without_leading_zero();
.as_slice_less_safe();
if crl_number.len() <= 20 {
Ok(crl_number)
} else {
Expand Down Expand Up @@ -235,7 +235,7 @@ impl CertRevocationList for BorrowedCertRevocationList<'_> {

fn verify_signature(
&self,
supported_sig_algs: &[&SignatureAlgorithm],
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
) -> Result<(), Error> {
signed_data::verify_signed_data(
Expand Down
126 changes: 124 additions & 2 deletions src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use core::marker::PhantomData;

use crate::{calendar, time, Error};
pub(crate) use ring::io::der::{CONSTRUCTED, CONTEXT_SPECIFIC};

#[derive(Debug)]
pub struct DerIterator<'a, T> {
Expand Down Expand Up @@ -69,6 +68,9 @@ pub(crate) enum Tag {
ContextSpecificConstructed3 = CONTEXT_SPECIFIC | CONSTRUCTED | 3,
}

pub(crate) const CONSTRUCTED: u8 = 0x20;
pub(crate) const CONTEXT_SPECIFIC: u8 = 0x80;

impl From<Tag> for usize {
#[allow(clippy::as_conversions)]
fn from(tag: Tag) -> Self {
Expand Down Expand Up @@ -377,8 +379,39 @@ pub(crate) fn optional_boolean(input: &mut untrusted::Reader) -> Result<bool, Er
})
}

pub(crate) fn nonnegative_integer<'a>(
input: &mut untrusted::Reader<'a>,
) -> Result<untrusted::Input<'a>, Error> {
let value = expect_tag_and_get_value(input, Tag::Integer)?;
match value
.as_slice_less_safe()
.split_first()
.ok_or(Error::BadDer)?
{
// Zero or leading zero.
(0, rest) => {
match rest.first() {
// Zero
None => Ok(value),
// Necessary leading zero.
Some(&second) if second & 0x80 == 0x80 => Ok(untrusted::Input::from(rest)),
// Unnecessary leading zero.
_ => Err(Error::BadDer),
}
}
// Positive value with no leading zero.
(first, _) if first & 0x80 == 0x00 => Ok(value),
// Negative value.
(_, _) => Err(Error::BadDer),
}
}

// Parse an integer in the range 0..=255.
pub(crate) fn small_nonnegative_integer(input: &mut untrusted::Reader) -> Result<u8, Error> {
ring::io::der::small_nonnegative_integer(input).map_err(|_| Error::BadDer)
match *nonnegative_integer(input)?.as_slice_less_safe() {
[b] => Ok(b),
_ => Err(Error::BadDer),
}
}

pub(crate) fn time_choice(input: &mut untrusted::Reader) -> Result<time::Time, Error> {
Expand Down Expand Up @@ -702,4 +735,93 @@ mod tests {
// Bits outside the range of values shouldn't be considered set.
assert!(!res.bit_set(256));
}

#[test]
fn test_small_nonnegative_integer() {
use super::{small_nonnegative_integer, Error, Tag};

for value in 0..=127 {
let data = [Tag::Integer.into(), 1, value];
let mut rd = untrusted::Reader::new(untrusted::Input::from(&data));
assert_eq!(small_nonnegative_integer(&mut rd), Ok(value),);
}

for value in 128..=255 {
let data = [Tag::Integer.into(), 2, 0x00, value];
let mut rd = untrusted::Reader::new(untrusted::Input::from(&data));
assert_eq!(small_nonnegative_integer(&mut rd), Ok(value),);
}

// not an integer
assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Sequence.into(),
1,
1
]))),
Err(Error::BadDer)
);

// negative
assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
1,
0xff
]))),
Err(Error::BadDer)
);

// positive but too large
assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
2,
0x01,
0x00
]))),
Err(Error::BadDer)
);

// unnecessary leading zero
assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
2,
0x00,
0x05
]))),
Err(Error::BadDer)
);

// truncations
assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[]))),
Err(Error::BadDer)
);

assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
]))),
Err(Error::BadDer)
);

assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
1,
]))),
Err(Error::BadDer)
);

assert_eq!(
small_nonnegative_integer(&mut untrusted::Reader::new(untrusted::Input::from(&[
Tag::Integer.into(),
2,
0
]))),
Err(Error::BadDer)
);
}
}
Loading