cookie.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package state
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "time"
  11. "github.com/mk6i/open-oscar-server/wire"
  12. )
  13. // authCookieLen is the fixed auth cookie length.
  14. const authCookieLen = 256
  15. // ServerCookie represents a token containing client metadata passed to the BOS
  16. // service upon connection.
  17. type ServerCookie struct {
  18. Service uint16
  19. ScreenName DisplayScreenName `oscar:"len_prefix=uint8"`
  20. ClientID string `oscar:"len_prefix=uint8"`
  21. ChatCookie string `oscar:"len_prefix=uint8"`
  22. MultiConnFlag uint8
  23. // KerberosAuth indicates whether the client used Kerberos for authentication.
  24. KerberosAuth uint8
  25. SessionNum uint8
  26. }
  27. func NewHMACCookieBaker() (HMACCookieBaker, error) {
  28. cb := HMACCookieBaker{}
  29. cb.key = make([]byte, 32)
  30. if _, err := io.ReadFull(rand.Reader, cb.key); err != nil {
  31. return cb, fmt.Errorf("cannot generate random HMAC key: %w", err)
  32. }
  33. return cb, nil
  34. }
  35. type HMACCookieBaker struct {
  36. key []byte
  37. }
  38. func (c HMACCookieBaker) Issue(data []byte) ([]byte, error) {
  39. payload := hmacTokenPayload{
  40. Expiry: uint32(time.Now().Add(1 * time.Minute).Unix()),
  41. Data: data,
  42. }
  43. buf := &bytes.Buffer{}
  44. if err := wire.MarshalBE(payload, buf); err != nil {
  45. return nil, fmt.Errorf("unable to marshal auth authCookie: %w", err)
  46. }
  47. hmacTok := hmacToken{
  48. Data: buf.Bytes(),
  49. }
  50. hmacTok.hash(c.key)
  51. buf.Reset()
  52. if err := wire.MarshalBE(hmacTok, buf); err != nil {
  53. return nil, fmt.Errorf("unable to marshal auth authCookie: %w", err)
  54. }
  55. // Some clients (such as perl NET::OSCAR) expect the auth cookie to be
  56. // exactly 256 bytes, even though the cookie is stored in a
  57. // variable-length TLV. Pad the auth cookie to make sure it's exactly
  58. // 256 bytes.
  59. if buf.Len() > authCookieLen {
  60. return nil, fmt.Errorf("sess is too long, expect 256 bytes, got %d", buf.Len())
  61. }
  62. buf.Write(make([]byte, authCookieLen-buf.Len()))
  63. return buf.Bytes(), nil
  64. }
  65. func (c HMACCookieBaker) Crack(data []byte) ([]byte, error) {
  66. hmacTok := hmacToken{}
  67. if err := wire.UnmarshalBE(&hmacTok, bytes.NewBuffer(data)); err != nil {
  68. return nil, fmt.Errorf("unable to unmarshal HMAC cookie: %w", err)
  69. }
  70. if !hmacTok.validate(c.key) {
  71. return nil, errors.New("invalid HMAC cookie")
  72. }
  73. payload := hmacTokenPayload{}
  74. if err := wire.UnmarshalBE(&payload, bytes.NewBuffer(hmacTok.Data)); err != nil {
  75. return nil, fmt.Errorf("unable to unmarshal HMAC cookie payload: %w", err)
  76. }
  77. expiry := time.Unix(int64(payload.Expiry), 0)
  78. if expiry.Before(time.Now()) {
  79. return nil, errors.New("HMAC cookie expired")
  80. }
  81. return payload.Data, nil
  82. }
  83. type hmacTokenPayload struct {
  84. Expiry uint32
  85. Data []byte `oscar:"len_prefix=uint16"`
  86. }
  87. type hmacToken struct {
  88. Data []byte `oscar:"len_prefix=uint16"`
  89. Sig []byte `oscar:"len_prefix=uint16"`
  90. }
  91. func (h *hmacToken) hash(key []byte) {
  92. hs := hmac.New(sha256.New, key)
  93. if _, err := hs.Write(h.Data); err != nil {
  94. // according to Hash interface, Write() should never return an error
  95. panic("unable to compute hmac token")
  96. }
  97. h.Sig = hs.Sum(nil)
  98. }
  99. func (h *hmacToken) validate(key []byte) bool {
  100. cp := *h
  101. cp.hash(key)
  102. return hmac.Equal(h.Sig, cp.Sig)
  103. }