-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(transport): implement TLS record fragmentation #114
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||||||
package tlsrecordfrag | ||||||||||||
jyyi1 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make the directory match the package name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tlssplit or tlsfrag? |
||||||||||||
|
||||||||||||
import ( | ||||||||||||
"context" | ||||||||||||
"errors" | ||||||||||||
|
||||||||||||
"github.com/Jigsaw-Code/outline-sdk/transport" | ||||||||||||
) | ||||||||||||
|
||||||||||||
type tlsRecordFragDialer struct { | ||||||||||||
dialer transport.StreamDialer | ||||||||||||
splitPoint uint32 | ||||||||||||
} | ||||||||||||
|
||||||||||||
var _ transport.StreamDialer = (*tlsRecordFragDialer)(nil) | ||||||||||||
|
||||||||||||
// NewStreamDialer creates a [transport.StreamDialer] that splits the Client Hello Message | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to clarify the behavior.
Suggested change
@jyyi1 let's make sure we agree on this behavior. |
||||||||||||
func NewStreamDialer(dialer transport.StreamDialer, prefixBytes uint32) (transport.StreamDialer, error) { | ||||||||||||
if dialer == nil { | ||||||||||||
return nil, errors.New("argument dialer must not be nil") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original code is consistent with the split Dialer:
I think we can keep it as is for now, and change them all later. |
||||||||||||
} | ||||||||||||
return &tlsRecordFragDialer{dialer: dialer, splitPoint: prefixBytes}, nil | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current code is consistent with
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
} | ||||||||||||
|
||||||||||||
// Dial implements [transport.StreamDialer].Dial. | ||||||||||||
func (d *tlsRecordFragDialer) Dial(ctx context.Context, remoteAddr string) (transport.StreamConn, error) { | ||||||||||||
innerConn, err := d.dialer.Dial(ctx, remoteAddr) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need to inspect
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. We should be able to restrict to specific ports. @jyyi1 what are your thoughts on the questions below? How to specify the ports? Or as a setter. What's the default behavior? Or perhaps we can force the user to make a decision by making the function mandatory in the constructor. That's probably my favorite. We can provide constant functions for "all" or "443 only". Also a EnableForPorts(portList) that returns a function that enables for that list. That way someone can do: dialer, err := tlsfrag.NewStreamDialer(inner, 10, tlsfrag.EnablePorts(int[]{443})) or dialer, err := tlsfrag.NewStreamDialer(inner, 10, tlsfrag.Enable443) Reference |
||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
return transport.WrapConn(innerConn, innerConn, NewWriter(innerConn, d.splitPoint)), nil | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package tlsrecordfrag | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
type tlsRecordFragWriter struct { | ||
writer io.Writer | ||
prefixBytes uint32 | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const maxRecordLength = 16384 | ||
|
||
func NewWriter(writer io.Writer, prefixBytes uint32) *tlsRecordFragWriter { | ||
return &tlsRecordFragWriter{writer, prefixBytes} | ||
} | ||
|
||
func (w *tlsRecordFragWriter) dontFrag(first []byte, source io.Reader) (written int64, err error) { | ||
tmp, err := w.writer.Write(first) | ||
written = int64(tmp) | ||
w.prefixBytes = 0 | ||
if err != nil { | ||
return written, err | ||
} | ||
n, err := io.Copy(w.writer, source) | ||
written += n | ||
return written, err | ||
} | ||
|
||
func (w *tlsRecordFragWriter) ReadFrom(source io.Reader) (written int64, err error) { | ||
if 0 < w.prefixBytes { | ||
var first [5]byte | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_, err := io.ReadFull(source, first[:]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
recordLength := uint32(first[3]) << 8 | uint32(first[4]) | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if w.prefixBytes >= recordLength { | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return w.dontFrag(first[:], source) | ||
} | ||
if recordLength > maxRecordLength { | ||
return 0, errors.New("Broken handshake message") | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
buf := make([]byte, recordLength+10) | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n2, err := io.ReadFull(source, buf[5:5+w.prefixBytes]) | ||
if err != nil { | ||
w.prefixBytes = 0 | ||
return 0, err | ||
} | ||
n3, err := io.ReadFull(source, buf[10+w.prefixBytes:]) | ||
if err != nil { | ||
w.prefixBytes = 0 | ||
return 0, err | ||
} | ||
|
||
header := first[:3] | ||
|
||
copy(buf, header) | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buf[3] = byte(uint32(n2) >> 8) | ||
buf[4] = byte(uint32(n2) & 0xff) | ||
|
||
copy(buf[5+n2:], header) | ||
buf[5+n2+3] = byte(uint32(n3) >> 8) | ||
buf[5+n2+4] = byte(uint32(n3) & 0xff) | ||
|
||
tmp, err := w.writer.Write(buf) | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.prefixBytes = 0 | ||
written = int64(tmp) | ||
if err != nil { | ||
return written, err | ||
} | ||
} | ||
n, err := io.Copy(w.writer, source) | ||
written += n | ||
return written, err | ||
} | ||
|
||
func (w *tlsRecordFragWriter) Write(data []byte) (written int, err error) { | ||
length := len(data) | ||
if length > 5+maxRecordLength { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the correct behavior, even if the connections is guaranteed TLS. A given write may have a single record, a partial record or multiple records. We need to fix this.
We should assume the first write has the record length. |
||
return 0, errors.New("Broken handshake message") | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if 0 < w.prefixBytes && w.prefixBytes < uint32(length -5) { | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buf := make([]byte, length+5) | ||
header := data[:3] | ||
record1 := data[5 : 5+w.prefixBytes] | ||
record2 := data[5+w.prefixBytes:] | ||
|
||
copy(buf, header) | ||
buf[3] = byte(w.prefixBytes >> 8) | ||
buf[4] = byte(w.prefixBytes & 0xff) | ||
copy(buf[5:], record1) | ||
|
||
copy(buf[5+w.prefixBytes:], header) | ||
buf[5+3+w.prefixBytes] = byte(len(record2) >> 8) | ||
buf[5+4+w.prefixBytes] = byte(len(record2) & 0xff) | ||
copy(buf[5+5+w.prefixBytes:], record2) | ||
|
||
w.prefixBytes = 0 | ||
return w.writer.Write(buf) | ||
Lanius-collaris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
w.prefixBytes = 0 | ||
return w.writer.Write(data) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tlsrecordfrag | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type collectWrites struct { | ||
writes [][]byte | ||
} | ||
|
||
var _ io.Writer = (*collectWrites)(nil) | ||
|
||
func (w *collectWrites) Write(data []byte) (int, error) { | ||
dataCopy := make([]byte, len(data)) | ||
copy(dataCopy, data) | ||
w.writes = append(w.writes, dataCopy) | ||
return len(data), nil | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
data := []byte{0x16, 0x03, 0x01, 0, 10, 0x01, 0, 0, 6, 0x03, 0x03, 1, 2, 3, 4} | ||
var innerWriter collectWrites | ||
trfWriter := NewWriter(&innerWriter, 1) | ||
n, err := trfWriter.Write(data) | ||
require.NoError(t, err) | ||
require.Equal(t, n, len(data)+5) | ||
require.Equal(t, [][]byte{[]byte{0x16, 0x03, 0x01, 0, 1, 0x1, 0x16, 0x03, 0x01, 0, 9, 0, 0, 6, 0x03, 0x03, 1, 2, 3, 4}}, innerWriter.writes) | ||
} | ||
|
||
func TestReadFrom(t *testing.T) { | ||
data := []byte{0x16, 0x03, 0x01, 0, 10, 0x01, 0, 0, 6, 0x03, 0x03, 1, 2, 3, 4, 0xff} | ||
var innerWriter collectWrites | ||
trfWriter := NewWriter(&innerWriter, 2) | ||
n, err := trfWriter.ReadFrom(bytes.NewReader(data)) | ||
require.NoError(t, err) | ||
require.Equal(t, n, int64(len(data))+5) | ||
require.Equal(t, [][]byte{[]byte{0x16, 0x03, 0x01, 0, 2, 0x1, 0, 0x16, 0x03, 0x01, 0, 8, 0, 6, 0x03, 0x03, 1, 2, 3, 4}, []byte{0xff}}, innerWriter.writes) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call the package
tlssplit
, so it can be more easily used. Or perhapstlsfrag
is better.@jyyi1 thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
tlsfrag
. And for the stream dialer's name, I prefertlsClientHelloFragStreamDialer
to indicate that it will only be applied to client hello packets.