-
Notifications
You must be signed in to change notification settings - Fork 2
/
tls.go
77 lines (67 loc) · 1.59 KB
/
tls.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"time"
)
var CAcert *x509.Certificate
var CAkey *rsa.PrivateKey
func readPEM(path string) *pem.Block {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Panic(err)
}
block, _ := pem.Decode(data)
return block
}
func init() {
var err error
if CAcert, err = x509.ParseCertificate(readPEM("CA.crt").Bytes); err != nil {
log.Panic(err)
}
if CAkey, err = x509.ParsePKCS1PrivateKey(readPEM("CA.key").Bytes); err != nil {
log.Panic(err)
}
}
func getCert(server string) *x509.Certificate {
conn, err := tls.Dial("tcp", server, &tls.Config{InsecureSkipVerify: true})
if err != nil {
return nil
}
defer conn.Close()
if err := conn.Handshake(); err != nil {
return nil
}
for _, cert := range conn.ConnectionState().PeerCertificates {
if len(cert.DNSNames) != 0 {
return cert
}
}
return nil
}
func generateCert(server string) *tls.Certificate {
realcert := getCert(server)
if realcert == nil {
log.Panic("Cannot get certificate from the remote host")
}
serial := realcert.SerialNumber.String()
realcert.SignatureAlgorithm = x509.SHA256WithRSA
realcert.SerialNumber.SetInt64(time.Now().UnixNano())
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Panic("Serial: " + serial + err.Error())
}
fakecert, err := x509.CreateCertificate(rand.Reader, realcert, CAcert, &priv.PublicKey, CAkey)
if err != nil {
log.Panic("Serial: " + serial + err.Error())
}
return &tls.Certificate{
PrivateKey: priv,
Certificate: [][]byte{fakecert},
}
}