authid.go 2.6 KB

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