-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
59 lines (49 loc) · 1.1 KB
/
file.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
package certinfo
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"os"
)
func fileInfo(filename string) {
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
fmt.Println("File does not exist:", filename)
os.Exit(1)
} else {
fmt.Println("Error checking file:", err)
os.Exit(1)
}
}
f, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(1)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
fmt.Println("Error closing file:", err)
}
}(f)
pemData, err := io.ReadAll(f)
if err != nil {
fmt.Println("Error reading data from file:", err)
os.Exit(1)
}
// Check if the file is a valid PEM certificate.
block, rest := pem.Decode(pemData)
if len(rest) != 0 || block == nil {
fmt.Println("File is not a valid PEM certificate")
os.Exit(1)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
fmt.Println("File is not a valid PEM certificate:", err)
os.Exit(1)
}
fmt.Println("Common Name:", cert.Subject)
fmt.Println("Issuer:", cert.Issuer)
fmt.Println("Expiration:", cert.NotAfter)
}