cookie.go 2.7 KB

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