forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_helper.go
56 lines (48 loc) · 1.04 KB
/
ssl_helper.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
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"time"
)
type x509Chain []x509.Certificate
func (xc *x509Chain) CommonNames() []string {
if xc == nil || len(*xc) == 0 {
return []string{}
}
return (*xc)[0].DNSNames
}
func (xc *x509Chain) Expires() time.Time {
if xc == nil || len(*xc) == 0 {
return time.Time{}
}
return (*xc)[0].NotAfter
}
func decodeCertChain(chainPEM string) (chain x509Chain, err error) {
certPEMBlock := []byte(chainPEM)
var certDERBlock *pem.Block
var cert tls.Certificate
for {
certDERBlock, certPEMBlock = pem.Decode([]byte(certPEMBlock))
if certDERBlock == nil {
break
}
if certDERBlock.Type == "CERTIFICATE" {
cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
}
}
if len(cert.Certificate) == 0 {
err = errors.New("failed to parse certificate PEM data")
return
}
var x509Cert *x509.Certificate
for _, c := range cert.Certificate {
x509Cert, err = x509.ParseCertificate(c)
if err != nil {
return
}
chain = append(chain, *x509Cert)
}
return
}