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

Budget tweaks #164

Merged
merged 3 commits into from
Sep 6, 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
6 changes: 6 additions & 0 deletions src/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::der::{self, DerIterator, FromDer, Tag, CONSTRUCTED, CONTEXT_SPECIFIC}
use crate::error::{DerTypeId, Error};
use crate::signed_data::{self, SignedData};
use crate::subject_name::GeneralName;
use crate::verify_cert::Budget;
use crate::x509::{remember_extension, set_extension_once, DistributionPointName, Extension};
use crate::{SignatureVerificationAlgorithm, Time};
use core::fmt::Debug;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub trait CertRevocationList: Sealed + Debug {
&self,
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
budget: &mut Budget,
) -> Result<(), Error>;
}

Expand Down Expand Up @@ -97,11 +99,13 @@ impl CertRevocationList for OwnedCertRevocationList {
&self,
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
budget: &mut Budget,
) -> Result<(), Error> {
signed_data::verify_signed_data(
supported_sig_algs,
untrusted::Input::from(issuer_spki),
&self.signed_data.borrow(),
budget,
)
}
}
Expand Down Expand Up @@ -239,11 +243,13 @@ impl CertRevocationList for BorrowedCertRevocationList<'_> {
&self,
supported_sig_algs: &[&dyn SignatureVerificationAlgorithm],
issuer_spki: &[u8],
budget: &mut Budget,
) -> Result<(), Error> {
signed_data::verify_signed_data(
supported_sig_algs,
untrusted::Input::from(issuer_spki),
&self.signed_data,
budget,
)
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/ring_algs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ mod tests {
use base64::{engine::general_purpose, Engine as _};

use crate::error::{DerTypeId, Error};
use crate::verify_cert::Budget;
use crate::{der, signed_data};
use alloc::{string::String, vec::Vec};

Expand Down Expand Up @@ -215,7 +216,8 @@ mod tests {
signed_data::verify_signed_data(
SUPPORTED_ALGORITHMS_IN_TESTS,
spki_value,
&signed_data
&signed_data,
&mut Budget::default(),
)
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/signed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use crate::der::{self, FromDer};
use crate::error::{DerTypeId, Error};
use crate::verify_cert::Budget;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
Expand Down Expand Up @@ -153,7 +154,10 @@ pub(crate) fn verify_signed_data(
supported_algorithms: &[&dyn SignatureVerificationAlgorithm],
spki_value: untrusted::Input,
signed_data: &SignedData,
budget: &mut Budget,
) -> Result<(), Error> {
budget.consume_signature()?;

// We need to verify the signature in `signed_data` using the public key
// in `public_key`. In order to know which *ring* signature verification
// algorithm to use, we need to know the public key algorithm (ECDSA,
Expand Down
17 changes: 10 additions & 7 deletions src/verify_cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use core::default::Default;

use pki_types::{CertificateDer, TrustAnchor};

use crate::cert::{Cert, EndEntityOrCa};
Expand Down Expand Up @@ -260,8 +262,7 @@ fn check_signatures(
let mut issuer_key_usage = None; // TODO(XXX): Consider whether to track TrustAnchor KU.
let mut cert = cert_chain;
loop {
budget.consume_signature()?;
signed_data::verify_signed_data(supported_sig_algs, spki_value, &cert.signed_data)?;
signed_data::verify_signed_data(supported_sig_algs, spki_value, &cert.signed_data, budget)?;

if let Some(revocation_opts) = &revocation {
check_crls(
Expand All @@ -271,6 +272,7 @@ fn check_signatures(
spki_value,
issuer_key_usage,
revocation_opts,
budget,
)?;
}

Expand All @@ -290,14 +292,14 @@ fn check_signatures(
Ok(())
}

struct Budget {
pub struct Budget {
signatures: usize,
build_chain_calls: usize,
}

impl Budget {
#[inline]
fn consume_signature(&mut self) -> Result<(), Error> {
pub(crate) fn consume_signature(&mut self) -> Result<(), Error> {
self.signatures = self
.signatures
.checked_sub(1)
Expand All @@ -315,7 +317,7 @@ impl Budget {
}
}

impl core::default::Default for Budget {
impl Default for Budget {
fn default() -> Self {
Self {
// This limit is taken from the remediation for golang CVE-2018-16875. However,
Expand All @@ -326,7 +328,7 @@ impl core::default::Default for Budget {

// This limit is taken from NSS libmozpkix, see:
// <https://github.com/nss-dev/nss/blob/bb4a1d38dd9e92923525ac6b5ed0288479f3f3fc/lib/mozpkix/lib/pkixbuild.cpp#L381-L393>
build_chain_calls: 200000,
build_chain_calls: 200_000,
}
}
}
Expand All @@ -349,6 +351,7 @@ fn check_crls(
issuer_spki: untrusted::Input,
issuer_ku: Option<untrusted::Input>,
revocation: &RevocationOptions,
budget: &mut Budget,
) -> Result<Option<CertNotRevoked>, Error> {
assert_eq!(cert.issuer, issuer_subject);

Expand Down Expand Up @@ -379,7 +382,7 @@ fn check_crls(
// TODO(XXX): consider whether we can refactor so this happens once up-front, instead
// of per-lookup.
// https://github.com/rustls/webpki/issues/81
crl.verify_signature(supported_sig_algs, issuer_spki.as_slice_less_safe())
crl.verify_signature(supported_sig_algs, issuer_spki.as_slice_less_safe(), budget)
.map_err(crl_signature_err)?;

// Verify that if the issuer has a KeyUsage bitstring it asserts cRLSign.
Expand Down