diff --git a/internal/rtmp/handshake/c1s1.go b/internal/rtmp/handshake/c1s1.go index 100c8b16..9a48adce 100644 --- a/internal/rtmp/handshake/c1s1.go +++ b/internal/rtmp/handshake/c1s1.go @@ -122,16 +122,19 @@ func (c *C1S1) Write(w io.Writer, isC1 bool) error { // signature gap := hsCalcDigestPos(buf, 8) + var peerKey []byte var key []byte if isC1 { + peerKey = hsServerFullKey key = hsClientPartialKey } else { + peerKey = hsClientFullKey key = hsServerPartialKey } digest := hsMakeDigest(key, buf, gap) copy(buf[gap:], digest) - pos := hsFindDigest(buf, hsClientPartialKey, 8) - c.Digest = hsMakeDigest(hsServerFullKey, buf[pos:pos+32], -1) + pos := hsFindDigest(buf, key, 8) + c.Digest = hsMakeDigest(peerKey, buf[pos:pos+32], -1) _, err := w.Write(buf) return err diff --git a/internal/rtmp/handshake/c1s1_test.go b/internal/rtmp/handshake/c1s1_test.go index e4152eb7..6ac96ccd 100644 --- a/internal/rtmp/handshake/c1s1_test.go +++ b/internal/rtmp/handshake/c1s1_test.go @@ -7,74 +7,132 @@ import ( "github.com/stretchr/testify/require" ) +var c1enc = append( + []byte{ + 0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, + 0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, + 0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, + 0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, + 0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, + }, + bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., +) + +var s1enc = append( + []byte{ + 0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x95, 0xc1, + 0xb6, 0x2c, 0x99, 0xbe, 0xa0, 0x0c, 0x07, 0x98, + 0xb0, 0xf1, 0xbe, 0x54, 0x50, 0x63, 0xa1, 0x25, + 0x1c, 0x9a, 0xcd, 0x12, 0x10, 0x98, 0x74, 0x8b, + 0x18, 0x66, 0x8d, 0xef, 0xcf, 0x22, 0x03, 0x04, + }, + bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., +) + func TestC1S1Read(t *testing.T) { - c1s1dec := C1S1{ - Time: 435234723, - Random: append( - []byte{ - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, - 0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, - 0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, - 0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, - 0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, + for _, ca := range []struct { + isC1 bool + enc []byte + dec C1S1 + }{ + { + true, + c1enc, + C1S1{ + Time: 435234723, + Random: append( + []byte{ + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, + 0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, + 0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, + 0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, + 0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, + }, + bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., + ), + Digest: []byte{ + 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, + 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, + 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, + 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, + }, }, - bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., - ), - Digest: []byte{ - 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, - 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, - 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, - 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, }, + { + false, + s1enc, + C1S1{ + Time: 435234723, + Random: append( + []byte{ + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x95, 0xc1, + 0xb6, 0x2c, 0x99, 0xbe, 0xa0, 0x0c, 0x07, 0x98, + 0xb0, 0xf1, 0xbe, 0x54, 0x50, 0x63, 0xa1, 0x25, + 0x1c, 0x9a, 0xcd, 0x12, 0x10, 0x98, 0x74, 0x8b, + 0x18, 0x66, 0x8d, 0xef, 0xcf, 0x22, 0x03, 0x04, + }, + bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., + ), + Digest: []byte{ + 0x0e, 0x8f, 0x96, 0x19, 0x19, 0xe6, 0xb7, 0xf2, + 0xac, 0x9a, 0xc8, 0x7e, 0x6e, 0xe9, 0xd4, 0x72, + 0xed, 0x82, 0x87, 0xf1, 0xfa, 0xbd, 0x93, 0xb8, + 0x7c, 0x48, 0x85, 0x03, 0x01, 0x7b, 0x54, 0xbe, + }, + }, + }, + } { + var c1s1 C1S1 + err := c1s1.Read((bytes.NewReader(ca.enc)), ca.isC1) + require.NoError(t, err) + require.Equal(t, ca.dec, c1s1) } - - c1s1enc := append( - []byte{ - 0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, - 0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, - 0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, - 0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, - 0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, - }, - bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., - ) - - var c1s1 C1S1 - err := c1s1.Read((bytes.NewReader(c1s1enc)), true) - require.NoError(t, err) - require.Equal(t, c1s1dec, c1s1) } func TestC1S1Write(t *testing.T) { - c1s1dec := C1S1{ - Time: 435234723, - Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382), - Digest: []byte{ - 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, - 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, - 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, - 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, + for _, ca := range []struct { + isC1 bool + enc []byte + dec C1S1 + }{ + { + true, + c1enc, + C1S1{ + Time: 435234723, + Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382), + Digest: []byte{ + 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, + 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, + 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, + 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, + }, + }, }, + { + false, + s1enc, + C1S1{ + Time: 435234723, + Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382), + Digest: []byte{ + 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, + 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, + 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, + 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, + }, + }, + }, + } { + var buf bytes.Buffer + err := ca.dec.Write(&buf, ca.isC1) + require.NoError(t, err) + require.Equal(t, ca.enc, buf.Bytes()) } - - c1s1enc := append( - []byte{ - 0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, - 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, - 0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, - 0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, - 0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, - 0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, - }, - bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., - ) - - var buf bytes.Buffer - err := c1s1dec.Write(&buf, true) - require.NoError(t, err) - require.Equal(t, c1s1enc, buf.Bytes()) }