This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
/
cert.go
125 lines (104 loc) · 2.75 KB
/
cert.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
package pki
import (
"fmt"
"net"
"regexp"
"strings"
"time"
)
// format for NotBefore and NotAfter fields to make output similar to openssl
var ValidityFormat = "Jan _2 15:04:05 2006 MST"
type Certificates []Certificate
// returns certificate that matches subject CN match regex (Subject.CommonName), if the certificate cannot be found,
// second returned value will be false
func (cs Certificates) GetBySubjectCommonNamePattern(subjectCNMatch string) (cert Certificate, ok bool) {
for _, c := range cs {
if match, _ := regexp.MatchString(subjectCNMatch, c.Subject.CommonName); match {
return c, true
}
}
return
}
type Certificate struct {
Issuer DN
NotBefore time.Time
NotAfter time.Time
Subject DN
DNSNames []string
IPAddresses []net.IP
}
func (c Certificate) IsExpired() bool {
return time.Now().After(c.NotAfter)
}
func (c Certificate) ContainsDNSName(name string) bool {
for _, d := range c.DNSNames {
if d == name {
return true
}
}
return false
}
func (c Certificate) ContainsIPAddress(ip net.IP) bool {
for _, i := range c.IPAddresses {
if i.Equal(ip) {
return true
}
}
return false
}
func (c Certificate) String() string {
notBefore := c.NotBefore.Format(ValidityFormat)
notAfter := c.NotAfter.Format(ValidityFormat)
dnsNames := strings.Join(c.DNSNames, ", ")
var ips []string
for _, ip := range c.IPAddresses {
ips = append(ips, fmt.Sprintf("%s", ip))
}
ipAddresses := strings.Join(ips, ", ")
return fmt.Sprintf(
"Issuer: %s\nValidity\n Not Before: %s\n Not After : %s\nSubject: %s\nDNS Names: %s\nIP Addresses: %s",
c.Issuer, notBefore, notAfter, c.Subject, dnsNames, ipAddresses)
}
type DN struct {
Organization []string
CommonName string
}
func (dn DN) String() string {
var fields []string
if len(dn.Organization) != 0 {
fields = append(fields, fmt.Sprintf("O=%s", strings.Join(dn.Organization, ", ")))
}
if dn.CommonName != "" {
fields = append(fields, fmt.Sprintf("CN=%s", dn.CommonName))
}
return strings.Join(fields, " ")
}
// converts raw certificate bytes to certificate, if the supplied data is cert bundle (or chain)
// all the certificates will be returned
func CertificatesFromBytes(data []byte) (Certificates, error) {
cs, err := DecodeCertificatesPEM(data)
if err != nil {
return nil, err
}
var certificates []Certificate
for _, c := range cs {
certificates = append(
certificates,
Certificate{
Issuer: DN{
Organization: c.Issuer.Organization,
CommonName: c.Issuer.CommonName,
},
NotAfter: c.NotAfter,
NotBefore: c.NotBefore,
Subject: DN{
Organization: c.Subject.Organization,
CommonName: c.Subject.CommonName,
},
DNSNames: c.DNSNames,
IPAddresses: c.IPAddresses,
},
)
}
return certificates, nil
}