integrity.go 3.7 KB

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