-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.go
171 lines (148 loc) · 4.6 KB
/
bundle.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package certutil
import (
"bytes"
"crypto/x509"
"crypto/x509/pkix"
"io/ioutil"
"sort"
"strings"
"time"
"github.com/effective-security/xlog"
"github.com/pkg/errors"
)
// BundleStatus is designated for various status reporting.
type BundleStatus struct {
// A list of SKIs of expiring certificates
ExpiringSKIs []string `json:"expiring_SKIs"`
// A list of untrusted root store names
Untrusted []string `json:"untrusted_root_stores"`
// A list of human readable warning messages based on the bundle status.
Messages []string `json:"messages"`
Code int `json:"code"`
}
// IsExpiring returns true if bundle is expiring in less than 30 days
func (b *BundleStatus) IsExpiring() bool {
return len(b.ExpiringSKIs) > 0
}
// IsUntrusted returns true if the cert's issuers are not trusted
func (b *BundleStatus) IsUntrusted() bool {
return len(b.Untrusted) > 0
}
// A Bundle contains a certificate and its trust chain. It is intended
// to store the most widely applicable chain, with shortness an
// explicit goal.
type Bundle struct {
// Chain does not include the root anchor
Chain []*x509.Certificate
Cert *x509.Certificate
IssuerCert *x509.Certificate
RootCert *x509.Certificate
Issuer *pkix.Name
Subject *pkix.Name
IssuerID string
SubjectID string
Expires time.Time
Hostnames []string
CertPEM string
CACertsPEM string
RootCertPEM string
}
// ExpiresInHours returns cert expiration rounded up in hours
func (b *Bundle) ExpiresInHours() time.Duration {
return b.Expires.Sub(time.Now().UTC()) / time.Hour * time.Hour
}
// VerifyBundleFromPEM constructs and verifies the cert chain
func VerifyBundleFromPEM(certPEM, intCAPEM, rootPEM []byte, opt ...Option) (bundle *Bundle, status *BundleStatus, err error) {
b, err := NewBundlerFromPEM(rootPEM, intCAPEM, opt...)
if err != nil {
err = errors.WithMessage(err, "failed to create bundler")
return
}
c, err := b.ChainFromPEM(certPEM, nil, "")
if err != nil {
err = errors.WithMessage(err, "failed to bundle")
return
}
return BuildBundle(c)
}
// BuildBundle returns Bundle
func BuildBundle(c *Chain) (bundle *Bundle, status *BundleStatus, err error) {
var pemCert, pemRoot, pemCA string
pemCert, _ = EncodeToPEMString(false, c.Cert)
pemRoot, _ = EncodeToPEMString(false, c.Root)
if len(c.Chain) > 1 {
pemCA, _ = EncodeToPEMString(false, c.Chain[1:]...)
}
bundle = &Bundle{
Chain: c.Chain,
Cert: c.Cert,
RootCert: c.Root,
IssuerCert: FindIssuer(c.Cert, c.Chain, c.Root),
Issuer: c.Issuer,
IssuerID: GetIssuerID(c.Cert),
Subject: c.Subject,
SubjectID: GetSubjectID(c.Cert),
Expires: c.Expires,
Hostnames: c.Hostnames,
CertPEM: pemCert,
CACertsPEM: pemCA,
RootCertPEM: pemRoot,
}
if len(c.Status.Messages) > 0 {
logger.KV(xlog.WARNING, "CN", c.Cert.Subject.CommonName, "messages", strings.Join(c.Status.Messages, ";"))
}
if len(c.Status.ExpiringSKIs) > 0 {
logger.KV(xlog.WARNING, "CN", c.Cert.Subject.CommonName, "ExpiringSKIs", strings.Join(c.Status.ExpiringSKIs, ";"))
}
if len(c.Status.Untrusted) > 0 {
logger.KV(xlog.WARNING, "CN", c.Cert.Subject.CommonName, "Untrusted", strings.Join(c.Status.Untrusted, ";"))
}
status = &BundleStatus{
ExpiringSKIs: c.Status.ExpiringSKIs,
Untrusted: c.Status.Untrusted,
Messages: c.Status.Messages,
}
return
}
// LoadAndVerifyBundleFromPEM constructs and verifies the cert chain
func LoadAndVerifyBundleFromPEM(certFile, intCAFile, rootFile string, opt ...Option) (*Bundle, *BundleStatus, error) {
var err error
var certPEM, intCAPEM, rootPEM []byte
certPEM, err = ioutil.ReadFile(certFile)
if err != nil {
return nil, nil, errors.WithStack(err)
}
if intCAFile != "" {
intCAPEM, err = ioutil.ReadFile(intCAFile)
if err != nil {
return nil, nil, errors.WithStack(err)
}
}
if rootFile != "" {
rootPEM, err = ioutil.ReadFile(rootFile)
if err != nil {
return nil, nil, errors.WithStack(err)
}
}
return VerifyBundleFromPEM(certPEM, intCAPEM, rootPEM, opt...)
}
// FindIssuer returns an issuer cert
func FindIssuer(crt *x509.Certificate, chain []*x509.Certificate, root *x509.Certificate) *x509.Certificate {
if root != nil && bytes.Equal(crt.RawIssuer, root.RawSubject) {
return root
}
for _, c := range chain {
if c != nil && bytes.Equal(crt.RawIssuer, c.RawSubject) {
return c
}
}
return nil
}
// SortBundlesByExpiration returns bundles sorted by expiration in descending order
func SortBundlesByExpiration(bundles []*Bundle) []*Bundle {
sorted := bundles[:]
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Expires.After(sorted[j].Expires)
})
return sorted
}