Ai
10 Star 41 Fork 20

Gitee 极速下载/Pion-WebRTC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/pion/webrtc
克隆/下载
srtcp.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
Sean DuBois 提交于 2018-09-17 15:26 +08:00 . Implement SRTCP Encryption
package srtp
import (
"crypto/cipher"
"encoding/binary"
)
// DecryptRTCP decrypts a buffer that contains a RTCP packet
// We can't pass *rtcp.Packet as the encrypt will obscure significant fields
func (c *Context) DecryptRTCP(encrypted []byte) ([]byte, error) {
tailOffset := len(encrypted) - (authTagSize + srtcpIndexSize)
out := append([]byte{}, encrypted[0:tailOffset]...)
isEncrypted := encrypted[tailOffset] >> 7
if isEncrypted == 0 {
return out, nil
}
srtcpIndexBuffer := append([]byte{}, encrypted[tailOffset:tailOffset+srtcpIndexSize]...)
srtcpIndexBuffer[0] &= 0x7f // unset Encryption bit
index := binary.BigEndian.Uint32(srtcpIndexBuffer)
ssrc := binary.BigEndian.Uint32(encrypted[4:])
stream := cipher.NewCTR(c.srtcpBlock, c.generateCounter(uint16(index&0xffff), index>>16, ssrc, c.srtcpSessionSalt))
stream.XORKeyStream(out[8:], out[8:])
return out, nil
}
// EncryptRTCP encrypts a buffer that contains a RTCP packet
func (c *Context) EncryptRTCP(decrypted []byte) ([]byte, error) {
out := append([]byte{}, decrypted[:]...)
ssrc := binary.BigEndian.Uint32(decrypted[4:])
// We roll over early because MSB is used for marking as encrypted
c.srtcpIndex++
if c.srtcpIndex >= 2147483647 {
c.srtcpIndex = 0
}
// Encrypt everything after header
stream := cipher.NewCTR(c.srtcpBlock, c.generateCounter(uint16(c.srtcpIndex&0xffff), c.srtcpIndex>>16, ssrc, c.srtcpSessionSalt))
stream.XORKeyStream(out[8:], out[8:])
// Add SRTCP Index and set Encryption bit
out = append(out, make([]byte, 4)...)
binary.BigEndian.PutUint32(out[len(out)-4:], c.srtcpIndex)
out[len(out)-4] |= 0x80
authTag, err := c.generateAuthTag(out, c.srtcpSessionAuthTag)
if err != nil {
return nil, err
}
return append(out, authTag...), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/Pion-WebRTC.git
git@gitee.com:mirrors/Pion-WebRTC.git
mirrors
Pion-WebRTC
Pion-WebRTC
v1.1.1

搜索帮助