config.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package shadowsocks
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. "crypto/md5"
  6. "crypto/sha1"
  7. "io"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/xtls/xray-core/common"
  10. "github.com/xtls/xray-core/common/buf"
  11. "github.com/xtls/xray-core/common/crypto"
  12. "github.com/xtls/xray-core/common/errors"
  13. "github.com/xtls/xray-core/common/protocol"
  14. "golang.org/x/crypto/chacha20poly1305"
  15. "golang.org/x/crypto/hkdf"
  16. )
  17. // MemoryAccount is an account type converted from Account.
  18. type MemoryAccount struct {
  19. Cipher Cipher
  20. CipherType CipherType
  21. Key []byte
  22. Password string
  23. }
  24. var ErrIVNotUnique = errors.New("IV is not unique")
  25. // Equals implements protocol.Account.Equals().
  26. func (a *MemoryAccount) Equals(another protocol.Account) bool {
  27. if account, ok := another.(*MemoryAccount); ok {
  28. return bytes.Equal(a.Key, account.Key)
  29. }
  30. return false
  31. }
  32. func (a *MemoryAccount) ToProto() proto.Message {
  33. return &Account{
  34. CipherType: a.CipherType,
  35. Password: a.Password,
  36. }
  37. }
  38. func createAesGcm(key []byte) cipher.AEAD {
  39. return crypto.NewAesGcm(key)
  40. }
  41. func createChaCha20Poly1305(key []byte) cipher.AEAD {
  42. ChaChaPoly1305, err := chacha20poly1305.New(key)
  43. common.Must(err)
  44. return ChaChaPoly1305
  45. }
  46. func createXChaCha20Poly1305(key []byte) cipher.AEAD {
  47. XChaChaPoly1305, err := chacha20poly1305.NewX(key)
  48. common.Must(err)
  49. return XChaChaPoly1305
  50. }
  51. func (a *Account) getCipher() (Cipher, error) {
  52. switch a.CipherType {
  53. case CipherType_AES_128_GCM:
  54. return &AEADCipher{
  55. KeyBytes: 16,
  56. IVBytes: 16,
  57. AEADAuthCreator: createAesGcm,
  58. }, nil
  59. case CipherType_AES_256_GCM:
  60. return &AEADCipher{
  61. KeyBytes: 32,
  62. IVBytes: 32,
  63. AEADAuthCreator: createAesGcm,
  64. }, nil
  65. case CipherType_CHACHA20_POLY1305:
  66. return &AEADCipher{
  67. KeyBytes: 32,
  68. IVBytes: 32,
  69. AEADAuthCreator: createChaCha20Poly1305,
  70. }, nil
  71. case CipherType_XCHACHA20_POLY1305:
  72. return &AEADCipher{
  73. KeyBytes: 32,
  74. IVBytes: 32,
  75. AEADAuthCreator: createXChaCha20Poly1305,
  76. }, nil
  77. case CipherType_NONE:
  78. return NoneCipher{}, nil
  79. default:
  80. return nil, errors.New("Unsupported cipher.")
  81. }
  82. }
  83. // AsAccount implements protocol.AsAccount.
  84. func (a *Account) AsAccount() (protocol.Account, error) {
  85. Cipher, err := a.getCipher()
  86. if err != nil {
  87. return nil, errors.New("failed to get cipher").Base(err)
  88. }
  89. return &MemoryAccount{
  90. Cipher: Cipher,
  91. CipherType: a.CipherType,
  92. Key: passwordToCipherKey([]byte(a.Password), Cipher.KeySize()),
  93. Password: a.Password,
  94. }, nil
  95. }
  96. // Cipher is an interface for all Shadowsocks ciphers.
  97. type Cipher interface {
  98. KeySize() int32
  99. IVSize() int32
  100. NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error)
  101. NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error)
  102. IsAEAD() bool
  103. EncodePacket(key []byte, b *buf.Buffer) error
  104. DecodePacket(key []byte, b *buf.Buffer) error
  105. }
  106. type AEADCipher struct {
  107. KeyBytes int32
  108. IVBytes int32
  109. AEADAuthCreator func(key []byte) cipher.AEAD
  110. }
  111. func (*AEADCipher) IsAEAD() bool {
  112. return true
  113. }
  114. func (c *AEADCipher) KeySize() int32 {
  115. return c.KeyBytes
  116. }
  117. func (c *AEADCipher) IVSize() int32 {
  118. return c.IVBytes
  119. }
  120. func (c *AEADCipher) createAuthenticator(key []byte, iv []byte) *crypto.AEADAuthenticator {
  121. subkey := make([]byte, c.KeyBytes)
  122. hkdfSHA1(key, iv, subkey)
  123. aead := c.AEADAuthCreator(subkey)
  124. nonce := crypto.GenerateAEADNonceWithSize(aead.NonceSize())
  125. return &crypto.AEADAuthenticator{
  126. AEAD: aead,
  127. NonceGenerator: nonce,
  128. }
  129. }
  130. func (c *AEADCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  131. auth := c.createAuthenticator(key, iv)
  132. return crypto.NewAuthenticationWriter(auth, &crypto.AEADChunkSizeParser{
  133. Auth: auth,
  134. }, writer, protocol.TransferTypeStream, nil), nil
  135. }
  136. func (c *AEADCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  137. auth := c.createAuthenticator(key, iv)
  138. return crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
  139. Auth: auth,
  140. }, reader, protocol.TransferTypeStream, nil), nil
  141. }
  142. func (c *AEADCipher) EncodePacket(key []byte, b *buf.Buffer) error {
  143. ivLen := c.IVSize()
  144. payloadLen := b.Len()
  145. auth := c.createAuthenticator(key, b.BytesTo(ivLen))
  146. b.Extend(int32(auth.Overhead()))
  147. _, err := auth.Seal(b.BytesTo(ivLen), b.BytesRange(ivLen, payloadLen))
  148. return err
  149. }
  150. func (c *AEADCipher) DecodePacket(key []byte, b *buf.Buffer) error {
  151. if b.Len() <= c.IVSize() {
  152. return errors.New("insufficient data: ", b.Len())
  153. }
  154. ivLen := c.IVSize()
  155. payloadLen := b.Len()
  156. auth := c.createAuthenticator(key, b.BytesTo(ivLen))
  157. bbb, err := auth.Open(b.BytesTo(ivLen), b.BytesRange(ivLen, payloadLen))
  158. if err != nil {
  159. return err
  160. }
  161. b.Resize(ivLen, int32(len(bbb)))
  162. return nil
  163. }
  164. type NoneCipher struct{}
  165. func (NoneCipher) KeySize() int32 { return 0 }
  166. func (NoneCipher) IVSize() int32 { return 0 }
  167. func (NoneCipher) IsAEAD() bool {
  168. return false
  169. }
  170. func (NoneCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
  171. return buf.NewReader(reader), nil
  172. }
  173. func (NoneCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
  174. return buf.NewWriter(writer), nil
  175. }
  176. func (NoneCipher) EncodePacket(key []byte, b *buf.Buffer) error {
  177. return nil
  178. }
  179. func (NoneCipher) DecodePacket(key []byte, b *buf.Buffer) error {
  180. return nil
  181. }
  182. func passwordToCipherKey(password []byte, keySize int32) []byte {
  183. key := make([]byte, 0, keySize)
  184. md5Sum := md5.Sum(password)
  185. key = append(key, md5Sum[:]...)
  186. for int32(len(key)) < keySize {
  187. md5Hash := md5.New()
  188. common.Must2(md5Hash.Write(md5Sum[:]))
  189. common.Must2(md5Hash.Write(password))
  190. md5Hash.Sum(md5Sum[:0])
  191. key = append(key, md5Sum[:]...)
  192. }
  193. return key
  194. }
  195. func hkdfSHA1(secret, salt, outKey []byte) {
  196. r := hkdf.New(sha1.New, secret, salt, []byte("ss-subkey"))
  197. common.Must2(io.ReadFull(r, outKey))
  198. }