authid.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package aead
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. rand3 "crypto/rand"
  7. "encoding/binary"
  8. "errors"
  9. "hash/crc32"
  10. "io"
  11. "math"
  12. "time"
  13. "github.com/xtls/xray-core/common"
  14. "github.com/xtls/xray-core/common/antireplay"
  15. )
  16. var (
  17. ErrNotFound = errors.New("user do not exist")
  18. ErrReplay = errors.New("replayed request")
  19. )
  20. func CreateAuthID(cmdKey []byte, time int64) [16]byte {
  21. buf := bytes.NewBuffer(nil)
  22. common.Must(binary.Write(buf, binary.BigEndian, time))
  23. var zero uint32
  24. common.Must2(io.CopyN(buf, rand3.Reader, 4))
  25. zero = crc32.ChecksumIEEE(buf.Bytes())
  26. common.Must(binary.Write(buf, binary.BigEndian, zero))
  27. aesBlock := NewCipherFromKey(cmdKey)
  28. if buf.Len() != 16 {
  29. panic("Size unexpected")
  30. }
  31. var result [16]byte
  32. aesBlock.Encrypt(result[:], buf.Bytes())
  33. return result
  34. }
  35. func NewCipherFromKey(cmdKey []byte) cipher.Block {
  36. aesBlock, err := aes.NewCipher(KDF16(cmdKey, KDFSaltConstAuthIDEncryptionKey))
  37. if err != nil {
  38. panic(err)
  39. }
  40. return aesBlock
  41. }
  42. type AuthIDDecoder struct {
  43. s cipher.Block
  44. }
  45. func NewAuthIDDecoder(cmdKey []byte) *AuthIDDecoder {
  46. return &AuthIDDecoder{NewCipherFromKey(cmdKey)}
  47. }
  48. func (aidd *AuthIDDecoder) Decode(data [16]byte) (int64, uint32, int32, []byte) {
  49. aidd.s.Decrypt(data[:], data[:])
  50. var t int64
  51. var zero uint32
  52. var rand int32
  53. reader := bytes.NewReader(data[:])
  54. common.Must(binary.Read(reader, binary.BigEndian, &t))
  55. common.Must(binary.Read(reader, binary.BigEndian, &rand))
  56. common.Must(binary.Read(reader, binary.BigEndian, &zero))
  57. return t, zero, rand, data[:]
  58. }
  59. func NewAuthIDDecoderHolder() *AuthIDDecoderHolder {
  60. return &AuthIDDecoderHolder{make(map[string]*AuthIDDecoderItem), antireplay.NewReplayFilter(120)}
  61. }
  62. type AuthIDDecoderHolder struct {
  63. decoders map[string]*AuthIDDecoderItem
  64. filter *antireplay.ReplayFilter
  65. }
  66. type AuthIDDecoderItem struct {
  67. dec *AuthIDDecoder
  68. ticket interface{}
  69. }
  70. func NewAuthIDDecoderItem(key [16]byte, ticket interface{}) *AuthIDDecoderItem {
  71. return &AuthIDDecoderItem{
  72. dec: NewAuthIDDecoder(key[:]),
  73. ticket: ticket,
  74. }
  75. }
  76. func (a *AuthIDDecoderHolder) AddUser(key [16]byte, ticket interface{}) {
  77. a.decoders[string(key[:])] = NewAuthIDDecoderItem(key, ticket)
  78. }
  79. func (a *AuthIDDecoderHolder) RemoveUser(key [16]byte) {
  80. delete(a.decoders, string(key[:]))
  81. }
  82. func (a *AuthIDDecoderHolder) Match(authID [16]byte) (interface{}, error) {
  83. for _, v := range a.decoders {
  84. t, z, _, d := v.dec.Decode(authID)
  85. if z != crc32.ChecksumIEEE(d[:12]) {
  86. continue
  87. }
  88. if t < 0 {
  89. continue
  90. }
  91. if math.Abs(math.Abs(float64(t))-float64(time.Now().Unix())) > 120 {
  92. continue
  93. }
  94. if !a.filter.Check(authID[:]) {
  95. return nil, ErrReplay
  96. }
  97. return v.ticket, nil
  98. }
  99. return nil, ErrNotFound
  100. }