cookie.go 3.0 KB

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