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

SerialNumber::generate should be infallible #1606

Open
str4d opened this issue Nov 6, 2024 · 0 comments
Open

SerialNumber::generate should be infallible #1606

str4d opened this issue Nov 6, 2024 · 0 comments

Comments

@str4d
Copy link

str4d commented Nov 6, 2024

The method is currently fallible:

impl<P: Profile> SerialNumber<P> {
/// Generates a random serial number from RNG.
///
/// This follows the recommendation the CAB forum [ballot 164] and uses a minimum of 64 bits
/// of output from the CSPRNG. This currently defaults to a 17-bytes long serial number.
///
/// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/
pub fn generate(rng: &mut impl CryptoRngCore) -> Result<Self> {
Self::generate_with_prefix(&[], 17, rng)
}

However, it's a trivial wrapper around SerialNumber::generate_with_prefix with arguments that mean it cannot fail:

// CABF requires a minimum of 64 bits of random
if rand_len < 8 {
return Err(ErrorKind::Failed.into());
}
if rand_len + prefix.len() > 19 {
return Err(ErrorKind::Failed.into());
}

let inner = asn1::Uint::new(bytes)?;
// The user might give us a 20 byte unsigned integer with a high MSB,
// which we'd then encode with 21 octets to preserve the sign bit.
// RFC 5280 is ambiguous about whether this is valid, so we limit
// `SerialNumber` *encodings* to 20 bytes or fewer while permitting
// `SerialNumber` *decodings* to have up to 21 bytes below.
if inner.value_len()? > Self::MAX_LEN {
return Err(ErrorKind::Overlength.into());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant