-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
57 lines (52 loc) · 1.65 KB
/
parser.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
package tls_api
import (
"bufio"
"bytes"
"encoding/binary"
"github.com/k8spacket/tls-api/model"
"reflect"
)
func ParseTLSPayload(payload []byte) model.TLSWrapper {
reader := bufio.NewReader(bytes.NewReader(payload))
var tlsWrapper = model.TLSWrapper{}
tlsWrapper = parseTLSRecord(reader, tlsWrapper)
return tlsWrapper
}
func parseTLSRecord(reader *bufio.Reader, tlsWrapper model.TLSWrapper) model.TLSWrapper {
var b, _ = reader.Peek(1)
var recordLayer = model.RecordLayer{}
if b[0] == model.TLSRecord {
binary.Read(reader, binary.BigEndian, &recordLayer)
}
b, _ = reader.Peek(1)
if b[0] == model.ClientHelloTLS {
tlsWrapper.ClientHelloTLSRecord = parseClientHelloTLSRecord(reader)
} else if b[0] == model.ServerHelloTLS {
tlsWrapper.ServerHelloTLSRecord = parseServerHelloTLSRecord(reader)
} else if b[0] == model.CertificateTLS {
tlsWrapper.CertificateTLSRecord = parseCertificateTLSRecord(reader)
} else {
if !reflect.DeepEqual(recordLayer, model.RecordLayer{}) {
parseAnotherTLSRecord(reader, recordLayer.Length)
} else {
parseAnotherTLSHandshakeProtocol(reader)
}
}
var _, err = reader.Peek(1)
if err == nil {
tlsWrapper = parseTLSRecord(reader, tlsWrapper)
}
return tlsWrapper
}
func parseAnotherTLSRecord(reader *bufio.Reader, length uint16) {
bytes := make([]byte, int(length))
binary.Read(reader, binary.BigEndian, &bytes)
}
func parseAnotherTLSHandshakeProtocol(reader *bufio.Reader) {
var handshakeType uint8
var length [3]byte
binary.Read(reader, binary.BigEndian, &handshakeType)
binary.Read(reader, binary.BigEndian, &length)
bytes := make([]byte, bytesToInt(length))
binary.Read(reader, binary.BigEndian, &bytes)
}