Refactoring

Refactored Protocol.Name -> Protocol.Type; added Encapsulation field
Refactored TLS parsing; added support for ALPN
This commit is contained in:
2025-10-10 12:41:44 +02:00
parent f86a7f7a67
commit 81a3829382
20 changed files with 366 additions and 141 deletions

32
tls.go
View File

@@ -19,7 +19,7 @@ type TLSExtension struct {
type TLSRecord struct {
Raw []byte
Type uint8
Version uint16
Version TLSVersion
Length uint16
Data []byte
}
@@ -30,26 +30,24 @@ func DecodeTLSRecord(data []byte) (*TLSRecord, error) {
record = &TLSRecord{Raw: data}
)
var version uint16
if !stream.ReadUint8(&record.Type) ||
!stream.ReadUint16(&record.Version) ||
!stream.ReadUint16(&version) ||
!stream.ReadUint16(&record.Length) {
return nil, DecodeError{
Reason: "invalid TLS record header",
Err: io.ErrUnexpectedEOF,
}
}
record.Version = TLSVersion(version)
if !stream.ReadBytes(&record.Data, int(record.Length)) {
return nil, DecodeError{
Reason: "invalid TLS record data",
Err: io.ErrUnexpectedEOF,
}
}
if !stream.Empty() {
return nil, DecodeError{
Reason: "extraneous data after TLS record",
Err: ErrInvalid,
}
}
return record, nil
}
@@ -166,12 +164,6 @@ func DecodeTLSClientHello(data []byte) (*TLSClientHello, error) {
Err: io.ErrUnexpectedEOF,
}
}
if !record.Empty() {
return nil, DecodeError{
Reason: "extraneous TLS extension data",
Err: io.ErrUnexpectedEOF,
}
}
for !extensions.Empty() {
var (
@@ -260,6 +252,7 @@ type TLSServerHello struct {
CipherSuite uint16
CompressionMethod uint8
Extensions []TLSExtension
ALPNProtocols []string // RFC 7301, Section 3.1
}
func DecodeTLSServerHello(data []byte) (*TLSServerHello, error) {
@@ -347,12 +340,6 @@ func DecodeTLSServerHello(data []byte) (*TLSServerHello, error) {
Err: io.ErrUnexpectedEOF,
}
}
if !record.Empty() {
return nil, DecodeError{
Reason: "extraneous TLS extension data",
Err: io.ErrUnexpectedEOF,
}
}
for !extensions.Empty() {
var (
@@ -366,6 +353,11 @@ func DecodeTLSServerHello(data []byte) (*TLSServerHello, error) {
}
}
hello.Extensions = append(hello.Extensions, extension)
switch extension.Type {
case tlsExtensionALPN:
_ = readTLSALPN(extensionData, &hello.ALPNProtocols)
}
}
return hello, nil