cookie.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/retro-aim-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. }
  24. func NewHMACCookieBaker() (HMACCookieBaker, error) {
  25. cb := HMACCookieBaker{}
  26. cb.key = make([]byte, 32)
  27. if _, err := io.ReadFull(rand.Reader, cb.key); err != nil {
  28. return cb, fmt.Errorf("cannot generate random HMAC key: %w", err)
  29. }
  30. return cb, nil
  31. }
  32. type HMACCookieBaker struct {
  33. key []byte
  34. }
  35. func (c HMACCookieBaker) Issue(data []byte) ([]byte, error) {
  36. payload := hmacTokenPayload{
  37. Expiry: uint32(time.Now().Add(1 * time.Minute).Unix()),
  38. Data: data,
  39. }
  40. buf := &bytes.Buffer{}
  41. if err := wire.MarshalBE(payload, buf); err != nil {
  42. return nil, fmt.Errorf("unable to marshal auth authCookie: %w", err)
  43. }
  44. hmacTok := hmacToken{
  45. Data: buf.Bytes(),
  46. }
  47. hmacTok.hash(c.key)
  48. buf.Reset()
  49. if err := wire.MarshalBE(hmacTok, buf); err != nil {
  50. return nil, fmt.Errorf("unable to marshal auth authCookie: %w", err)
  51. }
  52. // Some clients (such as perl NET::OSCAR) expect the auth cookie to be
  53. // exactly 256 bytes, even though the cookie is stored in a
  54. // variable-length TLV. Pad the auth cookie to make sure it's exactly
  55. // 256 bytes.
  56. if buf.Len() > authCookieLen {
  57. return nil, fmt.Errorf("sess is too long, expect 256 bytes, got %d", buf.Len())
  58. }
  59. buf.Write(make([]byte, authCookieLen-buf.Len()))
  60. return buf.Bytes(), nil
  61. }
  62. func (c HMACCookieBaker) Crack(data []byte) ([]byte, error) {
  63. hmacTok := hmacToken{}
  64. if err := wire.UnmarshalBE(&hmacTok, bytes.NewBuffer(data)); err != nil {
  65. return nil, fmt.Errorf("unable to unmarshal HMAC cooie: %w", err)
  66. }
  67. if !hmacTok.validate(c.key) {
  68. return nil, errors.New("invalid HMAC cookie")
  69. }
  70. payload := hmacTokenPayload{}
  71. if err := wire.UnmarshalBE(&payload, bytes.NewBuffer(hmacTok.Data)); err != nil {
  72. return nil, fmt.Errorf("unable to unmarshal HMAC cookie payload: %w", err)
  73. }
  74. expiry := time.Unix(int64(payload.Expiry), 0)
  75. if expiry.Before(time.Now()) {
  76. return nil, errors.New("HMAC cookie expired")
  77. }
  78. return payload.Data, nil
  79. }
  80. type hmacTokenPayload struct {
  81. Expiry uint32
  82. Data []byte `oscar:"len_prefix=uint16"`
  83. }
  84. type hmacToken struct {
  85. Data []byte `oscar:"len_prefix=uint16"`
  86. Sig []byte `oscar:"len_prefix=uint16"`
  87. }
  88. func (h *hmacToken) hash(key []byte) {
  89. hs := hmac.New(sha256.New, key)
  90. if _, err := hs.Write(h.Data); err != nil {
  91. // according to Hash interface, Write() should never return an error
  92. panic("unable to compute hmac token")
  93. }
  94. h.Sig = hs.Sum(nil)
  95. }
  96. func (h *hmacToken) validate(key []byte) bool {
  97. cp := *h
  98. cp.hash(key)
  99. return hmac.Equal(h.Sig, cp.Sig)
  100. }