-
Notifications
You must be signed in to change notification settings - Fork 286
/
tls.go
189 lines (176 loc) · 4.31 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package shuttle
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"github.com/sipt/shuttle/config"
connect "github.com/sipt/shuttle/conn"
"math/big"
"reflect"
"time"
"unsafe"
)
var ca *x509.Certificate
var caBytes []byte
var key *rsa.PrivateKey
type IMITMConfig interface {
GetMITM() *config.Mitm
SetMITM(*config.Mitm)
}
func ApplyMITMConfig(config IMITMConfig) error {
mitm := config.GetMITM()
if mitm == nil {
return nil
}
caBytes, err := base64.RawStdEncoding.DecodeString(mitm.CA)
if err != nil {
return err
}
keyBytes, err := base64.RawStdEncoding.DecodeString(mitm.Key)
if err != nil {
return err
}
ca, key, err = LoadCA(caBytes, keyBytes)
MitMRules = mitm.Rules
return err
}
func GetCACert() []byte {
l := len(caBytes)
if l == 0 {
return nil
}
bak := make([]byte, l)
copy(bak, caBytes)
return bak
}
func GenerateCA() (mitm *config.Mitm, err error) {
key, err = rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return
}
names := pkix.Name{
Organization: []string{"Shuttle"},
CommonName: "Shuttle Generated CA",
Names: []pkix.AttributeTypeAndValue{
{
Type: []int{2, 5, 4, 10},
Value: "Shuttle",
},
{
Type: []int{2, 5, 4, 3},
Value: "Shuttle Generated CA",
},
},
}
template := &x509.Certificate{
Version: 1,
SerialNumber: big.NewInt(1),
Subject: names,
Issuer: names,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(5, 0, 0),
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
}
caBytes, err = x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return
}
ca, err = x509.ParseCertificate(caBytes)
if err != nil {
return
}
// Generate cert
certBuffer := bytes.Buffer{}
if err = pem.Encode(&certBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: caBytes}); err != nil {
return
}
// Generate key
keyBuffer := bytes.Buffer{}
if err = pem.Encode(&keyBuffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
return
}
mitm = &config.Mitm{
CA: base64.RawStdEncoding.EncodeToString(certBuffer.Bytes()),
Key: base64.RawStdEncoding.EncodeToString(keyBuffer.Bytes()),
Rules: MitMRules,
}
return
}
func LoadCA(caPem, keyPem []byte) (*x509.Certificate, *rsa.PrivateKey, error) {
ca, _ := pem.Decode(caPem)
if ca == nil {
return nil, nil, errors.New("CA load failed")
}
caBytes = ca.Bytes
caCert, err := x509.ParseCertificate(ca.Bytes)
if err != nil {
return nil, nil, err
}
key, _ := pem.Decode(keyPem)
if key == nil {
return nil, nil, errors.New("key load failed")
}
privateKey, err := x509.ParsePKCS1PrivateKey(key.Bytes)
return caCert, privateKey, err
}
func makeCert(cert *x509.Certificate) ([]byte, error) {
derBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &key.PublicKey, key)
if err != nil {
return nil, err
}
return derBytes, nil
}
func Mimt(lc, sc connect.IConn) (connect.IConn, connect.IConn, error) {
if ca == nil {
return nil, nil, errors.New("please first generate CA")
}
conf := &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true,
}
lcID, scID := lc.GetID(), sc.GetID()
scTls := tls.Client(sc, conf)
err := scTls.Handshake()
if err != nil {
return nil, nil, fmt.Errorf("tls hand shake: %v", err)
}
//解析sc证书
rt := reflect.TypeOf(scTls).Elem()
filed, ok := rt.FieldByName("peerCertificates")
var cert *x509.Certificate
if ok {
ptr := (uintptr)(unsafe.Pointer(scTls))
cert = (*(*[]*x509.Certificate)(unsafe.Pointer(ptr + filed.Offset)))[0]
}
sc, err = connect.DefaultDecorateForTls(scTls, connect.TCP, scID)
if err != nil {
return nil, nil, err
}
derCert, err := makeCert(cert)
if err != nil {
return nil, nil, err
}
conf = &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true,
Certificates: []tls.Certificate{
{
Certificate: [][]byte{derCert},
PrivateKey: key,
},
},
}
lcTls := tls.Server(lc, conf)
lc, err = connect.DefaultDecorateForTls(lcTls, connect.TCP, lcID)
return lc, sc, err
}