-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.go
More file actions
95 lines (83 loc) · 2.33 KB
/
report.go
File metadata and controls
95 lines (83 loc) · 2.33 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"crypto/x509"
"fmt"
"strings"
"time"
)
// Record holds verification results for a single certificate.
type Record struct {
Cert *Certificate
Error error
IsRoot bool
Validity Validity
}
type Validity struct {
OK bool
NotBeforeOK bool
NotAfterOK bool
Period Duration
ExpiresIn Duration
}
func NewRecord(cert *Certificate, err error, opts *VerifyOptions) *Record {
return &Record{
Cert: cert,
Error: err,
IsRoot: isRootCert(cert, opts.Roots),
Validity: Validity{
OK: isValid(cert.inner, opts.Time),
NotBeforeOK: opts.Time.After(cert.inner.NotBefore),
NotAfterOK: opts.Time.Before(cert.inner.NotAfter),
Period: validity(cert.inner),
ExpiresIn: expiresIn(cert.inner, opts.Time),
},
}
}
func (r *Record) String() string {
var parts []string
parts = append(parts, "Record{")
if r.Cert != nil {
parts = append(parts, fmt.Sprintf(" Cert: %s", r.Cert.String()))
} else {
parts = append(parts, " Cert: <nil>")
}
if r.Error != nil {
parts = append(parts, fmt.Sprintf(" Error: %v", r.Error))
} else {
parts = append(parts, " Error: <nil>")
}
parts = append(parts, fmt.Sprintf(" IsRoot: %t", r.IsRoot))
parts = append(parts, fmt.Sprintf(" Valid: %t", r.Validity.OK))
parts = append(parts, fmt.Sprintf(" Validity: %s", r.Validity.Period))
parts = append(parts, fmt.Sprintf(" ExpiresIn: %s", r.Validity.ExpiresIn))
parts = append(parts, "}")
return strings.Join(parts, "\n")
}
// Report is a collection of verification records for a certificate chain.
type Report []*Record
func (r Report) String() string {
var parts []string
parts = append(parts, "Report{")
for i, rec := range r {
recStr := rec.String()
lines := strings.Split(recStr, "\n")
parts = append(parts, fmt.Sprintf(" [%d]: %s", i, lines[0]))
for _, line := range lines[1:] {
parts = append(parts, " "+line)
}
}
parts = append(parts, "}")
return strings.Join(parts, "\n")
}
func isRootCert(cert *Certificate, roots Bundle) bool {
return false
}
func isValid(cert *x509.Certificate, t time.Time) bool {
return t.After(cert.NotBefore) && t.Before(cert.NotAfter)
}
func validity(cert *x509.Certificate) Duration {
return Duration(cert.NotAfter.Sub(cert.NotBefore))
}
func expiresIn(cert *x509.Certificate, t time.Time) Duration {
return Duration(cert.NotAfter.Sub(t))
}