silk.time.text_asn1.
The two time types of ASN.1 in the DER form that X.509 certificates use: UTCTime and GeneralizedTime.
When to use#
Use this module to read or write the time fields of DER structures such as certificates,
revocation lists, and signed timestamps. Certificate.notBefore and Certificate.notAfter
already return instants, so a program that only reads certificates does not need it.
Details#
Both functions work on the content bytes of the ASN.1 value, without its tag and length.
UTCTime is
YYMMDDHHMMSSZ, 13 bytes. The years50to99are 1950 to 1999 and the years00to49are 2000 to 2049, as RFC 5280 defines.GeneralizedTime is
YYYYMMDDHHMMSSZ, 15 bytes.
The seconds are required, the zone is always Z, and a fraction of a second is not allowed,
which is the profile of RFC 5280. Second 60 is not valid.
Examples#
Read a UTCTime and write it as a GeneralizedTime#
import silk.core.result { Result }
import silk.time.instant { Instant }
import silk.time.text { TimeParseError }
import silk.time.text_asn1 { Asn1Time }
pub fn main() -> i32 {
match Asn1Time.parseUtcTime(b"110523203821Z") {
Result<Instant, TimeParseError>.Success { value } => {
let mut text: [u8; 15] = [0; 15]
let length = Asn1Time.writeGeneralizedTime(value, &mut text, 0)
// `text` is now `20110523203821Z`.
if length == 15 && text[0] == 50 && text[14] == 90 {
return 0
}
return 1
}
Result<Instant, TimeParseError>.Failure { error: _ } => {
return 2
}
}
}Import as Asn1Time with import silk.time.text_asn1 { Asn1Time }.
Public declarations: 1.
Asn1Time#
pub struct Asn1TimeFunctions that read and write the DER time types UTCTime and GeneralizedTime.
Implementation Asn1Time#
impl Asn1TimeAssociated function parseUtcTime#
pub fn parseUtcTime(bytes: &[u8]) -> Result<Instant, TimeParseError>Reads an instant from the content of a UTCTime, such as 110523203821Z.
Errors#
UnexpectedEndorUnexpectedCharacterifbytesis notYYMMDDHHMMSSZ.OutOfRangeif the date or the time does not exist, such as month13or second60.
Associated function parseGeneralizedTime#
pub fn parseGeneralizedTime(bytes: &[u8]) -> Result<Instant, TimeParseError>Reads an instant from the content of a GeneralizedTime, such as 20500101000000Z.
Errors#
UnexpectedEndorUnexpectedCharacterifbytesis notYYYYMMDDHHMMSSZ.OutOfRangeif the date or the time does not exist, such as month13or second60.
Associated function writeUtcTime#
pub fn writeUtcTime(instant: Instant, output: &mut [u8], at: usize) -> usizeWrites instant as the 13 content bytes of a UTCTime, such as 110523203821Z, at
output[at] and returns at + 13.
Details#
A fraction of a second is dropped. The year of instant in UTC must be from 1950 to 2049,
or the function traps. RFC 5280 requires GeneralizedTime for the other years.
Associated function writeGeneralizedTime#
pub fn writeGeneralizedTime(instant: Instant, output: &mut [u8], at: usize) -> usizeWrites instant as the 15 content bytes of a GeneralizedTime, such as 20500101000000Z,
at output[at] and returns at + 15.
Details#
A fraction of a second is dropped. The year of instant in UTC must be from 0 to 9999, or
the function traps.
Category: Time.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.