forked from square/certstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expiry.go
51 lines (43 loc) · 1.28 KB
/
expiry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"fmt"
"regexp"
"strconv"
"time"
)
var nowFunc = time.Now
func parseExpiry(fromNow string) (time.Time, error) {
now := nowFunc().UTC()
re := regexp.MustCompile("\\s*(\\d+)\\s*(day|month|year|hour)s?")
matches := re.FindAllStringSubmatch(fromNow, -1)
addDate := map[string]int{
"day": 0,
"month": 0,
"year": 0,
"hour": 0,
}
for _, r := range matches {
number, err := strconv.ParseInt(r[1], 10, 32)
if err != nil {
return now, err
}
addDate[r[2]] = int(number)
}
// Ensure that we do not overflow time.Duration.
// Doing so is silent and causes signed integer overflow like issues.
if _, err := time.ParseDuration(fmt.Sprintf("%dh", addDate["hour"])); err != nil {
return now, fmt.Errorf("hour unit too large to process")
}
result := now.
AddDate(addDate["year"], addDate["month"], addDate["day"]).
Add(time.Duration(addDate["hour"]) * time.Hour)
if now == result {
return now, fmt.Errorf("invalid or empty format")
}
// ASN.1 (encoding format used by SSL) only supports up to year 9999
// https://www.openssl.org/docs/man1.1.0/crypto/ASN1_TIME_check.html
if result.Year() > 9999 {
return now, fmt.Errorf("proposed date too far in to the future: %s. Expiry year must be less than or equal to 9999", result)
}
return result, nil
}