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 years 50 to 99 are 1950 to 1999 and the years 00 to 49 are 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#

silk
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#

silk
pub struct Asn1Time

Functions that read and write the DER time types UTCTime and GeneralizedTime.

Implementation Asn1Time#

silk
impl Asn1Time

Associated function parseUtcTime#

silk
pub fn parseUtcTime(bytes: &[u8]) -> Result<Instant, TimeParseError>

Reads an instant from the content of a UTCTime, such as 110523203821Z.

Errors#

  • UnexpectedEnd or UnexpectedCharacter if bytes is not YYMMDDHHMMSSZ.

  • OutOfRange if the date or the time does not exist, such as month 13 or second 60.

Associated function parseGeneralizedTime#

silk
pub fn parseGeneralizedTime(bytes: &[u8]) -> Result<Instant, TimeParseError>

Reads an instant from the content of a GeneralizedTime, such as 20500101000000Z.

Errors#

  • UnexpectedEnd or UnexpectedCharacter if bytes is not YYYYMMDDHHMMSSZ.

  • OutOfRange if the date or the time does not exist, such as month 13 or second 60.

Associated function writeUtcTime#

silk
pub fn writeUtcTime(instant: Instant, output: &mut [u8], at: usize) -> usize

Writes 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#

silk
pub fn writeGeneralizedTime(instant: Instant, output: &mut [u8], at: usize) -> usize

Writes 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.

View the source of this page