srtp.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. // Package srtp implements Secure Real-time Transport Protocol
  4. package srtp
  5. import (
  6. "github.com/pion/rtp"
  7. )
  8. func (c *Context) decryptRTP(dst, ciphertext []byte, header *rtp.Header, headerLen int) ([]byte, error) {
  9. s := c.getSRTPSSRCState(header.SSRC)
  10. roc, diff, _ := s.nextRolloverCount(header.SequenceNumber)
  11. markAsValid, ok := s.replayDetector.Check(
  12. (uint64(roc) << 16) | uint64(header.SequenceNumber),
  13. )
  14. if !ok {
  15. return nil, &duplicatedError{
  16. Proto: "srtp", SSRC: header.SSRC, Index: uint32(header.SequenceNumber),
  17. }
  18. }
  19. authTagLen, err := c.cipher.rtpAuthTagLen()
  20. if err != nil {
  21. return nil, err
  22. }
  23. dst = growBufferSize(dst, len(ciphertext)-authTagLen)
  24. dst, err = c.cipher.decryptRTP(dst, ciphertext, header, headerLen, roc)
  25. if err != nil {
  26. return nil, err
  27. }
  28. markAsValid()
  29. s.updateRolloverCount(header.SequenceNumber, diff)
  30. return dst, nil
  31. }
  32. // DecryptRTP decrypts a RTP packet with an encrypted payload
  33. func (c *Context) DecryptRTP(dst, encrypted []byte, header *rtp.Header) ([]byte, error) {
  34. if header == nil {
  35. header = &rtp.Header{}
  36. }
  37. headerLen, err := header.Unmarshal(encrypted)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return c.decryptRTP(dst, encrypted, header, headerLen)
  42. }
  43. // EncryptRTP marshals and encrypts an RTP packet, writing to the dst buffer provided.
  44. // If the dst buffer does not have the capacity to hold `len(plaintext) + 10` bytes, a new one will be allocated and returned.
  45. // If a rtp.Header is provided, it will be Unmarshaled using the plaintext.
  46. func (c *Context) EncryptRTP(dst []byte, plaintext []byte, header *rtp.Header) ([]byte, error) {
  47. if header == nil {
  48. header = &rtp.Header{}
  49. }
  50. headerLen, err := header.Unmarshal(plaintext)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return c.encryptRTP(dst, header, plaintext[headerLen:])
  55. }
  56. // encryptRTP marshals and encrypts an RTP packet, writing to the dst buffer provided.
  57. // If the dst buffer does not have the capacity, a new one will be allocated and returned.
  58. // Similar to above but faster because it can avoid unmarshaling the header and marshaling the payload.
  59. func (c *Context) encryptRTP(dst []byte, header *rtp.Header, payload []byte) (ciphertext []byte, err error) {
  60. s := c.getSRTPSSRCState(header.SSRC)
  61. roc, diff, ovf := s.nextRolloverCount(header.SequenceNumber)
  62. if ovf {
  63. // ... when 2^48 SRTP packets or 2^31 SRTCP packets have been secured with the same key
  64. // (whichever occurs before), the key management MUST be called to provide new master key(s)
  65. // (previously stored and used keys MUST NOT be used again), or the session MUST be terminated.
  66. // https://www.rfc-editor.org/rfc/rfc3711#section-9.2
  67. return nil, errExceededMaxPackets
  68. }
  69. s.updateRolloverCount(header.SequenceNumber, diff)
  70. return c.cipher.encryptRTP(dst, header, payload, roc)
  71. }