forked from zmap/zgrab
/
ztls_handshake.go
231 lines (202 loc) · 5.53 KB
/
ztls_handshake.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package ztls
import (
"crypto/rsa"
"encoding/binary"
"encoding/json"
"math/big"
"github.com/zmap/zgrab/ztools/x509"
)
type TLSVersion uint16
type ServerHello struct {
Version TLSVersion `json:"version"`
Random []byte `json:"random"`
SessionID []byte `json:"session_id"`
CipherSuite CipherSuite `json:"cipher_suite"`
CompressionMethod uint8 `json:"compression_method"`
OcspStapling bool `json:"ocsp_stapling"`
TicketSupported bool `json:"ticket"`
SecureRenegotiation bool `json:"secure_renegotiation"`
HeartbeatSupported bool `json:"heartbeat"`
}
// ServerCertificates represents a TLS certificates message in a format friendly to the golang JSON library.
// ValidationError should be non-nil whenever Valid is false.
type Certificates struct {
Certificates [][]byte
ParsedCertificates []*x509.Certificate
}
// ServerKeyExchange represents the raw key data sent by the server in TLS key exchange message
type ServerKeyExchange struct {
Key []byte `json:"key"`
}
// Finished represents a TLS Finished message
type Finished struct {
VerifyData []byte `json:"verify_data"`
}
// ServerHandshake stores all of the messages sent by the server during a standard TLS Handshake.
// It implements zgrab.EventData interface
type ServerHandshake struct {
ServerHello *ServerHello `json:"server_hello"`
ServerCertificates *Certificates `json:"server_certificates"`
ServerKeyExchange *ServerKeyExchange `json:"server_key_exchange"`
RSAExportParams *RSAExportParams `json:"rsa_export_params,omitempty"`
DHExportParams *DHParams `json:"dh_export_params,omitempty"`
DHParams *DHParams `json:"dh_params,omitempty"`
ServerFinished *Finished `json:"server_finished"`
}
func (c *Conn) GetHandshakeLog() *ServerHandshake {
return c.handshakeLog
}
func (m *serverHelloMsg) MakeLog() *ServerHello {
sh := new(ServerHello)
sh.Version = TLSVersion(m.vers)
sh.Random = make([]byte, len(m.random))
copy(sh.Random, m.random)
sh.SessionID = make([]byte, len(m.sessionId))
copy(sh.SessionID, m.sessionId)
sh.CipherSuite = CipherSuite(m.cipherSuite)
sh.CompressionMethod = m.compressionMethod
sh.OcspStapling = m.ocspStapling
sh.TicketSupported = m.ticketSupported
sh.SecureRenegotiation = m.secureRenegotiation
sh.HeartbeatSupported = m.heartbeatEnabled
return sh
}
func (m *certificateMsg) MakeLog() *Certificates {
sc := new(Certificates)
sc.Certificates = make([][]byte, len(m.certificates))
for idx, cert := range m.certificates {
sc.Certificates[idx] = make([]byte, len(cert))
copy(sc.Certificates[idx], cert)
}
return sc
}
func (m *serverKeyExchangeMsg) MakeLog() *ServerKeyExchange {
skx := new(ServerKeyExchange)
skx.Key = make([]byte, len(m.key))
copy(skx.Key, m.key)
return skx
}
func (m *finishedMsg) MakeLog() *Finished {
sf := new(Finished)
sf.VerifyData = make([]byte, len(m.verifyData))
copy(sf.VerifyData, m.verifyData)
return sf
}
type ExportSignatureAlgorithm uint16
var exportHashes = []string{
"MD5",
"SHA-1",
"SHA-224",
"SHA-256",
"SHA-384",
"SHA-512",
}
var exportAlgorithms = []string{
"anon",
"RSA",
"DSA",
"ECDSA",
}
func (esa *ExportSignatureAlgorithm) MarshalJSON() ([]byte, error) {
value := uint16(*esa)
hash := int(byte(value >> 8))
alg := int(byte(value))
var aux struct {
Value uint16 `json:"value"`
HashName string `json:"hash_name,omitempty"`
HashID int `json:"hash_id"`
AlgName string `json:"algorithm_name,omitempty"`
AlgID int `json:"algorithm_id"`
}
if hash < len(exportHashes) {
aux.HashName = exportHashes[hash]
}
if alg < len(exportAlgorithms) {
aux.AlgName = exportAlgorithms[alg]
}
aux.Value = value
aux.HashID = hash
aux.AlgID = alg
return json.Marshal(&aux)
}
type RSAExportParams struct {
PublicKey rsa.PublicKey `json:"-"`
Modulus []byte `json:"modulus"`
Exponent uint32 `json:"exponent"`
Length int `json:"length"`
}
type DHParams struct {
P *big.Int
G *big.Int
Ys *big.Int
}
func (p *DHParams) MarshalJSON() ([]byte, error) {
var aux struct {
P []byte `json:"prime"`
PrimeLen int `json:"prime_length"`
G []byte `json:"generator"`
Ys []byte `json:"public_exponent"`
}
aux.P = p.P.Bytes()
aux.PrimeLen = p.P.BitLen()
aux.G = p.G.Bytes()
aux.Ys = p.Ys.Bytes()
return json.Marshal(&aux)
}
func (p *rsaExportParams) MakeLog() *RSAExportParams {
out := new(RSAExportParams)
exponent := uint32(0)
for _, b := range p.rawExponent {
exponent <<= 8
exponent |= uint32(b)
}
modulus := big.NewInt(0)
modulus.SetBytes(p.rawModulus)
key := rsa.PublicKey{
N: modulus,
E: int(exponent),
}
out.PublicKey = key
out.Modulus = modulus.Bytes()
out.Exponent = exponent
out.Length = modulus.BitLen()
return out
}
func (p *DHParams) unmarshal(buf []byte) bool {
if len(buf) < 2 {
return false
}
pLength := binary.BigEndian.Uint16(buf)
buf = buf[2:]
if len(buf) < int(pLength) {
return false
}
pBytes := buf[0:pLength]
buf = buf[pLength:]
p.P = big.NewInt(0)
p.P.SetBytes(pBytes)
if len(buf) < 2 {
return false
}
gLength := binary.BigEndian.Uint16(buf)
buf = buf[2:]
if len(buf) < int(gLength) {
return false
}
gBytes := buf[0:gLength]
buf = buf[gLength:]
p.G = big.NewInt(0)
p.G.SetBytes(gBytes)
if len(buf) < 2 {
return false
}
ysLength := binary.BigEndian.Uint16(buf)
buf = buf[2:]
if len(buf) < int(ysLength) {
return false
}
ysBytes := buf[0:ysLength]
p.Ys = big.NewInt(0)
p.Ys.SetBytes(ysBytes)
return true
}