-
Notifications
You must be signed in to change notification settings - Fork 0
/
certs.go
124 lines (110 loc) · 3.03 KB
/
certs.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
package nettools
import (
"bufio"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
)
// CertReqFunc returns a function for tlsConfig.GetCertificate
func CertReqFunc(certfile, keyfile string) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
c, err := getCert(certfile, keyfile)
return func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
fmt.Printf("Received TLS Hello asking for %s: sending certificate\n", hello.ServerName)
if err != nil || certfile == "" {
fmt.Println("I have no certificate")
} else {
err := OutputPEMFile(certfile)
if err != nil {
log.Printf("\n%+v", err)
}
}
Wait()
return &c, nil
}
}
// ClientCertReqFunc returns a function for tlsConfig.GetClientCertificate
func ClientCertReqFunc(certfile, keyfile string) func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
c, err := getCert(certfile, keyfile)
return func(certReq *tls.CertificateRequestInfo) (*tls.Certificate, error) {
fmt.Println("Received certificate request: sending certificate")
if err != nil || certfile == "" {
fmt.Println("I have no certificate")
} else {
err := OutputPEMFile(certfile)
if err != nil {
log.Printf("\n%+v", err)
}
}
Wait()
return &c, nil
}
}
func getCert(certfile, keyfile string) (c tls.Certificate, err error) {
if certfile != "" && keyfile != "" {
c, err = tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
fmt.Printf("Error loading key pair: %v\n", err)
}
} else {
err = fmt.Errorf("I have no certificate")
}
return
}
// OutputPEMFile reads info from a PEM file and displays it
func OutputPEMFile(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
for len(data) > 0 {
var block *pem.Block
block, data = pem.Decode(data)
fmt.Printf("Type: %#v\n", block.Type)
switch block.Type {
case "CERTIFICATE":
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
fmt.Printf(CertificateInfo(cert))
default:
fmt.Println(block.Type)
}
}
return nil
}
// CertificateInfo returns a string describing the certificate
func CertificateInfo(cert *x509.Certificate) string {
if cert.Subject.CommonName == cert.Issuer.CommonName {
return fmt.Sprintf(" Self-signed certificate %v\n", cert.Issuer.CommonName)
}
s := fmt.Sprintf(" Subject %v\n", cert.DNSNames)
s += fmt.Sprintf(" Issued by %s\n", cert.Issuer.CommonName)
return s
}
// CertificateChains prints information about verified certificate chains
func CertificateChains(rawCerts [][]byte, chains [][]*x509.Certificate) error {
if len(chains) > 0 {
fmt.Println("Verified certificate chain from peer:")
for _, v := range chains {
// fmt.Printf("Chain %d:\n", j)
for i, cert := range v {
fmt.Printf(" Cert %d:\n", i)
fmt.Printf(CertificateInfo(cert))
}
}
Wait()
}
return nil
}
// Wait holds up proceedings until the user presses return
func Wait() {
fmt.Printf("[Press enter to proceed]")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
fmt.Println()
}