-
Notifications
You must be signed in to change notification settings - Fork 308
/
certificate_utils.go
93 lines (71 loc) · 2 KB
/
certificate_utils.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
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"os"
"path/filepath"
"regexp"
"github.com/rs/zerolog/log"
)
type certInfo struct {
date [3]int
rsaKeyBytes int
}
// CheckCertificate verifies if the attribute 'certificate_body' refers a file
func CheckCertificate(content string) string {
var re = regexp.MustCompile(`[0-9a-zA-Z-/\\_.]+\.pem`)
match := re.FindString(content)
return match
}
func getCertificateInfo(filePath string) (certInfo, error) {
certPEM, err := os.ReadFile(filePath)
if err != nil {
return certInfo{}, err
}
block, _ := pem.Decode(certPEM)
if block == nil {
return certInfo{}, errors.New("failed to parse the certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return certInfo{}, err
}
var certDate [3]int
certDate[0] = cert.NotAfter.Year()
certDate[1] = int(cert.NotAfter.Month())
certDate[2] = cert.NotAfter.Day()
var rsaBytes int
switch t := cert.PublicKey.(type) {
case *rsa.PublicKey:
_ = t
rsaBytes = cert.PublicKey.(*rsa.PublicKey).Size()
default:
rsaBytes = -1
}
return certInfo{date: certDate, rsaKeyBytes: rsaBytes}, nil
}
// AddCertificateInfo gets and adds certificate information of a certificate file
func AddCertificateInfo(path, content string) map[string]interface{} {
var filePath string
_, err := os.Stat(content)
if err != nil { // content is not a full valid path or is an incomplete path
log.Trace().Msgf("path to the certificate content is not a valid: %s", content)
filePath = filepath.Join(filepath.Dir(path), content)
} else { // content is a full valid path
filePath = content
}
date, err := getCertificateInfo(filePath)
if err == nil {
attributes := make(map[string]interface{})
attributes["file"] = filePath
attributes["expiration_date"] = date.date
if date.rsaKeyBytes != -1 {
attributes["rsa_key_bytes"] = date.rsaKeyBytes
}
return attributes
}
log.Error().Msgf("Failed to get certificate path %s: %s", filePath, err)
return nil
}