encoding/asn1: support fractions of a second when unmarshaling GeneralizedTime

A GeneralizedTime value may contain an optional fractional seconds
element (according to X.680 46.2, restricted by X.690 11.7.3). This
change adds support for this fractional part, up to nine digits, so that
Unmarshal won't fail when decoding a DER encoded GeneralizedTime value
with fractional digits.  Also, test cases related to this change have
been added.

X.680 and X.690 can be found at:
	https://www.itu.int/rec/T-REC-X.680
	https://www.itu.int/rec/T-REC-X.690

Fixes #15842

Change-Id: If217c007e01b686db508a940e9e2ed3bfb901879
Reviewed-on: https://go-review.googlesource.com/c/go/+/108355
Run-TryBot: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Michael Teichgraeber 2018-04-20 01:22:30 +02:00 committed by Gopher Robot
parent 4c49d52439
commit f7156f63b5
2 changed files with 5 additions and 1 deletions

View File

@ -365,7 +365,7 @@ func parseUTCTime(bytes []byte) (ret time.Time, err error) {
// parseGeneralizedTime parses the GeneralizedTime from the given byte slice
// and returns the resulting time.
func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) {
const formatStr = "20060102150405Z0700"
const formatStr = "20060102150405.999999999Z0700"
s := string(bytes)
if ret, err = time.Parse(formatStr, s); err != nil {

View File

@ -323,6 +323,10 @@ func TestUTCTime(t *testing.T) {
var generalizedTimeTestData = []timeTest{
{"20100102030405Z", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.UTC)},
{"20100102030405", false, time.Time{}},
{"20100102030405.123456Z", true, time.Date(2010, 01, 02, 03, 04, 05, 123456e3, time.UTC)},
{"20100102030405.123456", false, time.Time{}},
{"20100102030405.Z", false, time.Time{}},
{"20100102030405.", false, time.Time{}},
{"20100102030405+0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))},
{"20100102030405-0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", -6*60*60-7*60))},
/* These are invalid times. However, the time package normalises times