-
Notifications
You must be signed in to change notification settings - Fork 172
x509-cert: SerialNumber is now a specialized object #795
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
Merged
tarcieri
merged 5 commits into
RustCrypto:master
from
baloo:baloo/owned-api/serial-number
Dec 18, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
76b3d0c
der: Introduce `Uint` as an owned version of `UintRef`
baloo 62bbe0a
x509-cert: SerialNumber is now a specialized object
baloo 85340da
Update der/src/asn1/integer/bigint.rs
tarcieri 22ea7be
impl `OwnedToRef`/`RefToOwned` for UInt and Bytes
baloo e536d68
use `SerialNumber::MAX_LEN`
baloo 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
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,75 @@ | ||
| //! X.509 serial number | ||
|
|
||
| use der::{ | ||
| asn1::Uint, DecodeValue, EncodeValue, ErrorKind, FixedTag, Header, Length, Reader, Result, Tag, | ||
| ValueOrd, Writer, | ||
| }; | ||
|
|
||
baloo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// [RFC 5280 Section 4.1.2.2.] Serial Number | ||
| /// | ||
| /// The serial number MUST be a positive integer assigned by the CA to | ||
| /// each certificate. It MUST be unique for each certificate issued by a | ||
| /// given CA (i.e., the issuer name and serial number identify a unique | ||
| /// certificate). CAs MUST force the serialNumber to be a non-negative | ||
| /// integer. | ||
| /// | ||
| /// Given the uniqueness requirements above, serial numbers can be | ||
| /// expected to contain long integers. Certificate users MUST be able to | ||
| /// handle serialNumber values up to 20 octets. Conforming CAs MUST NOT | ||
| /// use serialNumber values longer than 20 octets. | ||
| /// | ||
| /// Note: Non-conforming CAs may issue certificates with serial numbers | ||
| /// that are negative or zero. Certificate users SHOULD be prepared to | ||
| /// gracefully handle such certificates. | ||
| #[derive(Clone, Debug, Eq, PartialEq, ValueOrd, PartialOrd, Ord)] | ||
| pub struct SerialNumber { | ||
| inner: Uint, | ||
| } | ||
|
|
||
| impl SerialNumber { | ||
| /// Maximum length in bytes for a [`SerialNumber`] | ||
| pub const MAX_LEN: Length = Length::new(20); | ||
|
|
||
| /// Create a new [`SerialNumber`] from a byte slice. | ||
| pub fn new(bytes: &[u8]) -> Result<Self> { | ||
| let inner = Uint::new(bytes)?; | ||
|
|
||
| if inner.len() > SerialNumber::MAX_LEN { | ||
| return Err(ErrorKind::Overlength.into()); | ||
| } | ||
|
|
||
| Ok(Self { inner }) | ||
| } | ||
|
|
||
| /// Borrow the inner byte slice which contains the least significant bytes | ||
| /// of a big endian integer value with all leading zeros stripped. | ||
| pub fn as_bytes(&self) -> &[u8] { | ||
| self.inner.as_bytes() | ||
| } | ||
| } | ||
|
|
||
| impl EncodeValue for SerialNumber { | ||
| fn value_len(&self) -> Result<Length> { | ||
| self.inner.value_len() | ||
| } | ||
|
|
||
| fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { | ||
| self.inner.encode_value(writer) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> DecodeValue<'a> for SerialNumber { | ||
| fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { | ||
| let inner = Uint::decode_value(reader, header)?; | ||
|
|
||
| if inner.len() > SerialNumber::MAX_LEN { | ||
| return Err(ErrorKind::Overlength.into()); | ||
| } | ||
|
|
||
| Ok(Self { inner }) | ||
| } | ||
| } | ||
|
|
||
| impl FixedTag for SerialNumber { | ||
| const TAG: Tag = <Uint as FixedTag>::TAG; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.