-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlsv2serverresponse.go
67 lines (61 loc) · 1.98 KB
/
tlsv2serverresponse.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
package main
import (
"bufio"
"bytes"
"crypto/x509"
"fmt"
"net"
"strings"
)
type TlsV2ServerResponse struct {
Ciphersuites []Ciphersuite
Certificate x509.Certificate
}
func (shello *TlsV2ServerResponse) read(socket net.Conn) (*TlsV2ServerResponse, error) {
data := getBytesFromSocket(socket, 2)
if len(data) < 2 {
return nil, ErrorNotSSLV2ServerHello
}
if bytes.Compare(data, []byte{0x80, 0x00}) == 0 {
return nil, ErrorNotSSLV2ServerHello
}
serverMessageLength := bytesToInt(data) & 0x7FFF
if serverMessageLength < 11 {
return nil, ErrorNotSSLV2ServerHello
}
serverMessageHex := getBytesFromSocket(socket, serverMessageLength)
if serverMessageHex[0] != 0x04 {
return nil, ErrorNotSSLV2ServerHello
}
certificateLength := bytesToInt(serverMessageHex[5:7])
ciphersuiteLength := bytesToInt(serverMessageHex[7:9])
connectionIdLength := bytesToInt(serverMessageHex[9:11])
if serverMessageLength != 11+certificateLength+ciphersuiteLength+connectionIdLength {
return nil, ErrorNotSSLV2ServerHello
}
if ciphersuiteLength%3 != 0 {
return nil, ErrorNotSSLV2ServerHello
}
cnt := 11
certificateData, cnt := nextBytes(serverMessageHex, cnt, certificateLength)
cert, _ := x509.ParseCertificate(certificateData)
shello.Certificate = *cert
ciphersuiteData, cnt := nextBytes(serverMessageHex, cnt, ciphersuiteLength)
for i := 0; i < len(ciphersuiteData); i += 3 {
shello.Ciphersuites = append(shello.Ciphersuites, Ciphersuite{ciphersuiteData[i : i+3]})
}
_, cnt = nextBytes(serverMessageHex, cnt, connectionIdLength)
return shello, nil
}
func (shello *TlsV2ServerResponse) String() string {
lines := []string{}
lines = append(lines, "Ciphersuites:")
for _, ciphersuite := range shello.Ciphersuites {
lines = append(lines, fmt.Sprintf(" - %s", ciphersuite.String()))
}
scanner := bufio.NewScanner(strings.NewReader(X509CertificateString(shello.Certificate)))
for scanner.Scan() {
lines = append(lines, fmt.Sprintf(" %s", scanner.Text()))
}
return strings.Join(lines, "\n")
}