integrity.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package stun
  2. import ( // nolint:gci
  3. "crypto/md5" // nolint:gosec
  4. "crypto/sha1" // nolint:gosec
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "github.com/pion/stun/internal/hmac"
  9. )
  10. // separator for credentials.
  11. const credentialsSep = ":"
  12. // NewLongTermIntegrity returns new MessageIntegrity with key for long-term
  13. // credentials. Password, username, and realm must be SASL-prepared.
  14. func NewLongTermIntegrity(username, realm, password string) MessageIntegrity {
  15. k := strings.Join([]string{username, realm, password}, credentialsSep)
  16. h := md5.New() // nolint:gosec
  17. fmt.Fprint(h, k)
  18. return MessageIntegrity(h.Sum(nil))
  19. }
  20. // NewShortTermIntegrity returns new MessageIntegrity with key for short-term
  21. // credentials. Password must be SASL-prepared.
  22. func NewShortTermIntegrity(password string) MessageIntegrity {
  23. return MessageIntegrity(password)
  24. }
  25. // MessageIntegrity represents MESSAGE-INTEGRITY attribute.
  26. //
  27. // AddTo and Check methods are using zero-allocation version of hmac, see
  28. // newHMAC function and internal/hmac/pool.go.
  29. //
  30. // RFC 5389 Section 15.4
  31. type MessageIntegrity []byte
  32. func newHMAC(key, message, buf []byte) []byte {
  33. mac := hmac.AcquireSHA1(key)
  34. writeOrPanic(mac, message)
  35. defer hmac.PutSHA1(mac)
  36. return mac.Sum(buf)
  37. }
  38. func (i MessageIntegrity) String() string {
  39. return fmt.Sprintf("KEY: 0x%x", []byte(i))
  40. }
  41. const messageIntegritySize = 20
  42. // ErrFingerprintBeforeIntegrity means that FINGERPRINT attribute is already in
  43. // message, so MESSAGE-INTEGRITY attribute cannot be added.
  44. var ErrFingerprintBeforeIntegrity = errors.New("FINGERPRINT before MESSAGE-INTEGRITY attribute")
  45. // AddTo adds MESSAGE-INTEGRITY attribute to message.
  46. //
  47. // CPU costly, see BenchmarkMessageIntegrity_AddTo.
  48. func (i MessageIntegrity) AddTo(m *Message) error {
  49. for _, a := range m.Attributes {
  50. // Message should not contain FINGERPRINT attribute
  51. // before MESSAGE-INTEGRITY.
  52. if a.Type == AttrFingerprint {
  53. return ErrFingerprintBeforeIntegrity
  54. }
  55. }
  56. // The text used as input to HMAC is the STUN message,
  57. // including the header, up to and including the attribute preceding the
  58. // MESSAGE-INTEGRITY attribute.
  59. length := m.Length
  60. // Adjusting m.Length to contain MESSAGE-INTEGRITY TLV.
  61. m.Length += messageIntegritySize + attributeHeaderSize
  62. m.WriteLength() // writing length to m.Raw
  63. v := newHMAC(i, m.Raw, m.Raw[len(m.Raw):]) // calculating HMAC for adjusted m.Raw
  64. m.Length = length // changing m.Length back
  65. // Copy hmac value to temporary variable to protect it from resetting
  66. // while processing m.Add call.
  67. vBuf := make([]byte, sha1.Size)
  68. copy(vBuf, v)
  69. m.Add(AttrMessageIntegrity, vBuf)
  70. return nil
  71. }
  72. // ErrIntegrityMismatch means that computed HMAC differs from expected.
  73. var ErrIntegrityMismatch = errors.New("integrity check failed")
  74. // Check checks MESSAGE-INTEGRITY attribute.
  75. //
  76. // CPU costly, see BenchmarkMessageIntegrity_Check.
  77. func (i MessageIntegrity) Check(m *Message) error {
  78. v, err := m.Get(AttrMessageIntegrity)
  79. if err != nil {
  80. return err
  81. }
  82. // Adjusting length in header to match m.Raw that was
  83. // used when computing HMAC.
  84. var (
  85. length = m.Length
  86. afterIntegrity = false
  87. sizeReduced int
  88. )
  89. for _, a := range m.Attributes {
  90. if afterIntegrity {
  91. sizeReduced += nearestPaddedValueLength(int(a.Length))
  92. sizeReduced += attributeHeaderSize
  93. }
  94. if a.Type == AttrMessageIntegrity {
  95. afterIntegrity = true
  96. }
  97. }
  98. m.Length -= uint32(sizeReduced)
  99. m.WriteLength()
  100. // startOfHMAC should be first byte of integrity attribute.
  101. startOfHMAC := messageHeaderSize + m.Length - (attributeHeaderSize + messageIntegritySize)
  102. b := m.Raw[:startOfHMAC] // data before integrity attribute
  103. expected := newHMAC(i, b, m.Raw[len(m.Raw):])
  104. m.Length = length
  105. m.WriteLength() // writing length back
  106. return checkHMAC(v, expected)
  107. }