-
Notifications
You must be signed in to change notification settings - Fork 77
Start basic SCT support #423
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
Open
djc
wants to merge
5
commits into
main
Choose a base branch
from
jbp-sct
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
5 commits
Select commit
Hold shift + click to select a range
48935dd
Add support for `ExtensionOid` for SCT lists
ctz 0d0c4da
Extract signedCertificateTimestampList for certificates
ctz 8d2034d
Introduce module for decoding RFC 6962 SCTs
ctz 3e8c939
Add `VerifiedPath::issuer_spki()`
ctz 2decec1
tmp: also expose SCTs for EndEntity
ctz 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,136 @@ | ||
| /// Reads a `SignedCertificateTimestampList` encoding, yielding each `SignedCertificateTimestamp`. | ||
| pub(crate) fn iter_scts<'a>( | ||
| bytes: untrusted::Input<'a>, | ||
| ) -> Result<impl Iterator<Item = Result<SignedCertificateTimestamp<'a>, Error>> + 'a, Error> { | ||
| let items_body = bytes.read_all(Error::MalformedSct, |rd| read_field(rd, u16_field_len, 1))?; | ||
|
|
||
| let mut reader = untrusted::Reader::new(items_body); | ||
|
|
||
| Ok(core::iter::from_fn(move || { | ||
| let item = read_field(&mut reader, u16_field_len, 1).ok()?; | ||
| Some(SignedCertificateTimestamp::try_from( | ||
| item.as_slice_less_safe(), | ||
| )) | ||
| })) | ||
| } | ||
|
|
||
| pub(crate) struct SctParser<'a> { | ||
| reader: untrusted::Reader<'a>, | ||
| } | ||
|
|
||
| impl<'a> SctParser<'a> { | ||
| pub(crate) fn new(input: Option<untrusted::Input<'a>>) -> Result<Self, Error> { | ||
| Ok(SctParser { | ||
| reader: match input { | ||
| Some(input) => untrusted::Reader::new( | ||
| input.read_all(Error::MalformedSct, |rd| read_field(rd, u16_field_len, 1))?, | ||
| ), | ||
| None => untrusted::Reader::new(untrusted::Input::from(&[])), | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> Iterator for SctParser<'a> { | ||
| type Item = Result<SignedCertificateTimestamp<'a>, Error>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| if self.reader.at_end() { | ||
| return None; | ||
| } | ||
|
|
||
| Some(match read_field(&mut self.reader, u16_field_len, 1) { | ||
| Ok(item) => SignedCertificateTimestamp::try_from(item.as_slice_less_safe()), | ||
| Err(_) => Err(Error::MalformedSct), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// This is `SignedCertificateTimestamp` defined in [RFC6962][]. | ||
| /// | ||
| /// [RFC6962]: https://www.rfc-editor.org/rfc/rfc6962.html#section-3.2 | ||
| #[derive(Debug)] | ||
| pub(crate) struct SignedCertificateTimestamp<'a> { | ||
| pub(crate) log_id: LogId, | ||
| pub(crate) timestamp: Timestamp, | ||
| #[allow(dead_code)] // pending sct verification | ||
| extensions: untrusted::Input<'a>, | ||
| #[allow(dead_code)] // pending sct verification | ||
| signature_algorithm: u16, | ||
| #[allow(dead_code)] // pending sct verification | ||
| signature: untrusted::Input<'a>, | ||
| } | ||
|
|
||
| impl<'a> TryFrom<&'a [u8]> for SignedCertificateTimestamp<'a> { | ||
| type Error = Error; | ||
|
|
||
| fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> { | ||
| let input = untrusted::Input::from(bytes); | ||
| input.read_all(Error::MalformedSct, |rd| { | ||
| match read_array(rd)? { | ||
| [0] => {} | ||
| _ => return Err(Error::UnsupportedSctVersion), | ||
| }; | ||
|
|
||
| let log_id = LogId(read_array(rd)?); | ||
| let timestamp = Timestamp(u64::from_be_bytes(read_array(rd)?)); | ||
| let extensions = read_field(rd, u16_field_len, 0)?; | ||
| let signature_algorithm = u16::from_be_bytes(read_array(rd)?); | ||
| let signature = read_field(rd, u16_field_len, 1)?; | ||
|
|
||
| Ok(SignedCertificateTimestamp { | ||
| log_id, | ||
| timestamp, | ||
| extensions, | ||
| signature_algorithm, | ||
| signature, | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LogId(pub [u8; 32]); | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Timestamp(pub u64); | ||
|
|
||
| /// Read a length-prefixed field from `rd`. | ||
| /// | ||
| /// The length is encoded in `N` bytes and those bytes are decoded by `into_size`, | ||
| /// and must be at least `min_size` bytes. | ||
| fn read_field<'a, const N: usize>( | ||
| rd: &mut untrusted::Reader<'a>, | ||
| into_size: fn([u8; N]) -> usize, | ||
| min_size: usize, | ||
| ) -> Result<untrusted::Input<'a>, Error> { | ||
| let len = into_size(read_array::<N>(rd)?); | ||
| if len < min_size { | ||
| return Err(Error::MalformedSct); | ||
| } | ||
| rd.read_bytes(len).map_err(|_| Error::MalformedSct) | ||
| } | ||
|
|
||
| /// Read `N` bytes from `rd` as an array. | ||
| fn read_array<const N: usize>(rd: &mut untrusted::Reader<'_>) -> Result<[u8; N], Error> { | ||
| rd.read_bytes(N) | ||
| .map_err(|_| Error::MalformedSct)? | ||
| .as_slice_less_safe() | ||
| .try_into() | ||
| .map_err(|_| Error::MalformedSct) | ||
| } | ||
|
|
||
| fn u16_field_len(bytes: [u8; 2]) -> usize { | ||
| usize::from(u16::from_be_bytes(bytes)) | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub enum Error { | ||
| /// The SCT was somehow misencoded, truncated or otherwise corrupt. | ||
| MalformedSct, | ||
|
|
||
| /// An unsupported SCT version was encountered. | ||
| /// | ||
| /// This library only supports `v1(0)`. | ||
| UnsupportedSctVersion, | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
What kind of verification needs to happen? Thoughts on a testing strategy?