-
Notifications
You must be signed in to change notification settings - Fork 0
/
certview.go
136 lines (121 loc) · 2.92 KB
/
certview.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
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/cortiz/certview/internal/types"
"github.com/fatih/color"
"gopkg.in/yaml.v3"
)
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func printError(message string) {
fmt.Printf("%s \n", color.RedString(message))
}
func printHelp() {
fmt.Println("certview <flags> [certificate Path or Host]")
fmt.Println()
printError("One or more certificates are needed")
}
func readCertFile(certFilePath string) *x509.Certificate {
if fileExists(certFilePath) {
r, err := ioutil.ReadFile(certFilePath)
if err != nil {
printError(err.Error())
os.Exit(2)
}
block, _ := pem.Decode(r)
if block == nil {
printError("Unable to read " + certFilePath)
os.Exit(3)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
printError(err.Error())
}
return cert
}
printError("File " + certFilePath + " not found")
return nil
}
func readRemoteCertFile(remoteHost string, allowInsecureTLS bool) *x509.Certificate {
conf := &tls.Config{
InsecureSkipVerify: allowInsecureTLS,
}
conn, err := tls.Dial("tcp", remoteHost, conf)
if err != nil {
printError("Error in Dial " + err.Error())
return nil
}
defer conn.Close()
return conn.ConnectionState().PeerCertificates[0]
}
func main() {
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
var useUrl = flag.Bool("remote", false, "Given Arguments are urls an not files")
var allowInsecureTLS = flag.Bool("allowInsecureTLS", false, "Allow insecure TLS connections")
var outputFormat = flag.String("output", "txt", "Ouput format (txt,json,yaml)")
flag.Parse()
if *flagNoColor {
color.NoColor = true // disables colorized output
}
if len(os.Args) <= 1 {
printHelp()
fmt.Println()
fmt.Println("Allowed flags")
flag.PrintDefaults()
return
}
var certificates []types.Cert
for _, a := range flag.Args() {
var cert *x509.Certificate
if *useUrl {
cert = readRemoteCertFile(a, *allowInsecureTLS)
} else {
cert = readCertFile(a)
}
if cert != nil {
certificate := types.BuildCert(cert)
certificates = append(certificates, *certificate)
}
}
switch strings.ToLower(*outputFormat) {
case "json":
outputJson(certificates)
case "txt":
for _, cert := range certificates {
fmt.Println()
fmt.Println(cert.ToTxt(!*flagNoColor))
fmt.Println()
}
case "yaml":
outputYaml(certificates)
default:
fmt.Println("Unsupported OutFormant")
}
}
func outputYaml(certificates []types.Cert) {
byte, err := yaml.Marshal(certificates)
if err != nil {
fmt.Printf("Unable to output yaml %s", err)
}
fmt.Println(string(byte))
}
func outputJson(certificates []types.Cert) {
byte, err := json.Marshal(certificates)
if err != nil {
fmt.Printf("Unable to output json %s", err)
}
fmt.Println(string(byte))
}