[openssl-users] ASN1_TIME to time_t

Michael Wojcik Michael.Wojcik at microfocus.com
Wed Sep 6 12:17:32 UTC 2017

> From: openssl-users [mailto:openssl-users-bounces at openssl.org] On Behalf Of Dmitry Belyavsky
> Sent: Wednesday, September 06, 2017 04:12

> Is there a way to convert ASN1_TIME to time_t or smth compatible? Quick googling does not show good results.

We just implemented it ourselves, by parsing the data field of the (1.0.2) OpenSSL ASN1_TIME structure into a struct tm and using mktime.

That's not ideal, since it's looking at an internal representation. Also, for some reason, 1.0.2 seems to keep the year as a two-digit value for ASN.1 UTC Time, with a window centered on 1970, while it uses a 4-digit year for ASN.1 Generalized Time. The code handles that but it makes me nervous - it's the sort of internal detail that seems likely to change.

struct tm is local time, so you need to adjust for timezone. With Generalized Time you need to parse the offset from UTC; then you adjust your struct tm based on the difference between the ASN.1 time's offset and your local offset.

I should really review that code at some point. But for now it appears to work.

Michael Wojcik 
Distinguished Engineer, Micro Focus 

More information about the openssl-users mailing list