config.go 5.2 KB

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