Browse Source

rtmp: fix s1 generation

pull/1039/head
aler9 4 years ago
parent
commit
a19f45f736
  1. 7
      internal/rtmp/handshake/c1s1.go
  2. 180
      internal/rtmp/handshake/c1s1_test.go

7
internal/rtmp/handshake/c1s1.go

@ -122,16 +122,19 @@ func (c *C1S1) Write(w io.Writer, isC1 bool) error {
// signature // signature
gap := hsCalcDigestPos(buf, 8) gap := hsCalcDigestPos(buf, 8)
var peerKey []byte
var key []byte var key []byte
if isC1 { if isC1 {
peerKey = hsServerFullKey
key = hsClientPartialKey key = hsClientPartialKey
} else { } else {
peerKey = hsClientFullKey
key = hsServerPartialKey key = hsServerPartialKey
} }
digest := hsMakeDigest(key, buf, gap) digest := hsMakeDigest(key, buf, gap)
copy(buf[gap:], digest) copy(buf[gap:], digest)
pos := hsFindDigest(buf, hsClientPartialKey, 8) pos := hsFindDigest(buf, key, 8)
c.Digest = hsMakeDigest(hsServerFullKey, buf[pos:pos+32], -1) c.Digest = hsMakeDigest(peerKey, buf[pos:pos+32], -1)
_, err := w.Write(buf) _, err := w.Write(buf)
return err return err

180
internal/rtmp/handshake/c1s1_test.go

@ -7,74 +7,132 @@ import (
"github.com/stretchr/testify/require" "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) { func TestC1S1Read(t *testing.T) {
c1s1dec := C1S1{ for _, ca := range []struct {
Time: 435234723, isC1 bool
Random: append( enc []byte
[]byte{ dec C1S1
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, }{
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, {
0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, true,
0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, c1enc,
0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, C1S1{
0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, 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,
c1s1enc := append( s1enc,
[]byte{ C1S1{
0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, Time: 435234723,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, Random: append(
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, []byte{
0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04,
0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x95, 0xc1,
0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, 0xb6, 0x2c, 0x99, 0xbe, 0xa0, 0x0c, 0x07, 0x98,
0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, 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,
},
},
}, },
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., } {
) var c1s1 C1S1
err := c1s1.Read((bytes.NewReader(ca.enc)), ca.isC1)
var c1s1 C1S1 require.NoError(t, err)
err := c1s1.Read((bytes.NewReader(c1s1enc)), true) require.Equal(t, ca.dec, c1s1)
require.NoError(t, err) }
require.Equal(t, c1s1dec, c1s1)
} }
func TestC1S1Write(t *testing.T) { func TestC1S1Write(t *testing.T) {
c1s1dec := C1S1{ for _, ca := range []struct {
Time: 435234723, isC1 bool
Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382), enc []byte
Digest: []byte{ dec C1S1
0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3, }{
0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a, {
0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d, true,
0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99, 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,
c1s1enc := append( s1enc,
[]byte{ C1S1{
0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00, Time: 435234723,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382),
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a, Digest: []byte{
0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4, 0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3,
0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad, 0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a,
0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4, 0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d,
0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04, 0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99,
},
},
}, },
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)..., } {
) var buf bytes.Buffer
err := ca.dec.Write(&buf, ca.isC1)
var buf bytes.Buffer require.NoError(t, err)
err := c1s1dec.Write(&buf, true) require.Equal(t, ca.enc, buf.Bytes())
require.NoError(t, err) }
require.Equal(t, c1s1enc, buf.Bytes())
} }

Loading…
Cancel
Save