Skip to content

Commit

Permalink
fix cert validity (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
aspacewalz authored Aug 26, 2021
1 parent d461981 commit 30e1a9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 2 additions & 3 deletions x509/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var kMinTime, kMaxTime time.Time

func init() {
var err error
kMinTime, err = time.Parse(time.RFC3339, "1970-01-01T00:00:00Z")
kMinTime, err = time.Parse(time.RFC3339, "0001-01-01T00:00:00Z")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (v *validity) MarshalJSON() ([]byte, error) {
aux := auxValidity{
Start: clampTime(v.NotBefore.UTC()).Format(time.RFC3339),
End: clampTime(v.NotAfter.UTC()).Format(time.RFC3339),
ValidityPeriod: int(v.NotAfter.Sub(v.NotBefore).Seconds()),
ValidityPeriod: int(v.NotAfter.Unix() - v.NotBefore.Unix()),
}
return json.Marshal(&aux)
}
Expand All @@ -235,7 +235,6 @@ func (v *validity) UnmarshalJSON(b []byte) error {
if v.NotAfter, err = time.Parse(time.RFC3339, aux.End); err != nil {
return err
}

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions x509/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ func TestPublicKeyAlgorithmJSON(t *testing.T) {
}

func TestValidityJSON(t *testing.T) {
preEpoch, err := time.Parse("20060102150405", "19040824000000")
if err != nil {
t.Error(err)
}
tests := []validity{
{
NotBefore: time.Unix(1400000000, 0),
Expand All @@ -245,6 +249,18 @@ func TestValidityJSON(t *testing.T) {
NotBefore: time.Unix(1000000000, 0),
NotAfter: time.Unix(1700000000, 0),
},
{
NotBefore: preEpoch,
NotAfter: preEpoch,
},
{
NotBefore: kMinTime,
NotAfter: kMaxTime,
},
{
NotBefore: kMaxTime,
NotAfter: kMinTime,
},
}
for i, v := range tests {
j, err := json.Marshal(&v)
Expand Down

0 comments on commit 30e1a9e

Please sign in to comment.