-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/asn1: time.Time values can be encoded incorrectly #64892
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
Comments
When outside of the UTCTime range, it will be encoded as GeneralizedTime. go/src/encoding/asn1/marshal.go Lines 655 to 659 in 988b718
t := time.Date(2023, 12, 28, 16, 49, 55, 00, time.UTC)
tVal, err := asn1.MarshalWithParams(t, "tag:24,generalized")
if err != nil {
panic(err)
} Doesn't that make sense? There might be an implicit tag used, so |
Hi @mateusz834, thanks for replying! So where my confusion comes from, and why I believe this is a bug, is that because I had specified the tag for GeneralizedTime (24) I expected the asn1 package to use that time format. It doesn't make sense to me that you would want a field tagged as GeneralizedTime but with a UTCTime value, when they aren't compatible. I feel the asn1 package should reference a tag value (if specified) when determining what time format to use, if no format was explicitly set. |
From the encoding/asn1 docs:
so this is not a 24 + Universal bits (24), but rather 24 + Context-specific bits (152). |
t := time.Date(2023, 12, 28, 16, 49, 55, 00, time.UTC)
tVal, err := asn1.MarshalWithParams(t, "tag:24,generalized")
if err != nil {
panic(err)
}
// tVal = 18 0f 32 30 32 33 31 32 32 38 31 36 34 39 35 35 5a (..20231228164955Z) EDIT: actually it returns the correct tag, see https://go.dev/play/p/xc0pfnyWyXY |
(attn @FiloSottile @rolandshoemaker @golang/security per https://dev.golang.org/owners) |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
Go version
1.21.5
What operating system and processor architecture are you using (
go env
)?What did you do?
The
encoding/asn1
package supports marshallingtime.Time
structures to either a ASN.1UTCTime
orGeneralizedTime
. The behaviour is controlled by using struct tags or passing parameters withasn1.MarshalWithParams
, but by default the package will use UTCTime. (Aside: this seems to be a really poor choice for a default behaviour, considering UTCTime is not Y2K compliant)However, it's possible for the asn1 package to encode an invalid GeneralizedTime value without producing an error.
Take the following example:
Here, we're passing the ASN1 tag for GeneralizedTime (24), but the value of tVal only uses 2 digits for the year because it's actually being encoded as UTCTime.
Despite me telling the package that I want to use GeneralizedTime, I have to explicitly tell it with a second parameter:
Go playground: https://go.dev/play/p/WSxtB5cOMaQ
What did you expect to see?
I expected the asn1 package to encode the time using GeneralizedTime as I had specified using the ASN1 tag value. I also expected that the default encoding would be the Y2K-compliant GeneralizedTime. Perhaps a separate discussion is needed as to if this should be changed going forward.
What did you see instead?
A confusion between a ASN1 field that is tagged as GeneralizedTime but actually has a UTCTime value.
The text was updated successfully, but these errors were encountered: