|
| 1 | +// Copyright 2020 Jigsaw Operations LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package shadowsocks |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "crypto" |
| 20 | + "crypto/hmac" |
| 21 | + "crypto/rand" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + |
| 25 | + "golang.org/x/crypto/hkdf" |
| 26 | +) |
| 27 | + |
| 28 | +// SaltGenerator generates unique salts to use in Shadowsocks connections. |
| 29 | +type SaltGenerator interface { |
| 30 | + // Returns a new salt |
| 31 | + GetSalt(salt []byte) error |
| 32 | +} |
| 33 | + |
| 34 | +// ServerSaltGenerator offers the ability to check if a salt was marked as |
| 35 | +// server-originated. |
| 36 | +type ServerSaltGenerator interface { |
| 37 | + SaltGenerator |
| 38 | + // IsServerSalt returns true if the salt was created by this generator |
| 39 | + // and is marked as server-originated. |
| 40 | + IsServerSalt(salt []byte) bool |
| 41 | +} |
| 42 | + |
| 43 | +// randomSaltGenerator generates a new random salt. |
| 44 | +type randomSaltGenerator struct{} |
| 45 | + |
| 46 | +// GetSalt outputs a random salt. |
| 47 | +func (randomSaltGenerator) GetSalt(salt []byte) error { |
| 48 | + _, err := rand.Read(salt) |
| 49 | + return err |
| 50 | +} |
| 51 | + |
| 52 | +func (randomSaltGenerator) IsServerSalt(salt []byte) bool { |
| 53 | + return false |
| 54 | +} |
| 55 | + |
| 56 | +// RandomSaltGenerator is a basic SaltGenerator. |
| 57 | +var RandomSaltGenerator ServerSaltGenerator = randomSaltGenerator{} |
| 58 | + |
| 59 | +// serverSaltGenerator generates unique salts that are secretly marked. |
| 60 | +type serverSaltGenerator struct { |
| 61 | + key []byte |
| 62 | +} |
| 63 | + |
| 64 | +// ServerSaltMarkLen is the number of bytes of salt to use as a marker. |
| 65 | +// Increasing this value reduces the false positive rate, but increases |
| 66 | +// the likelihood of salt collisions. |
| 67 | +const ServerSaltMarkLen = 4 // Must be less than or equal to SHA1.Size() |
| 68 | + |
| 69 | +// Constant to identify this marking scheme. |
| 70 | +var serverSaltLabel = []byte("outline-server-salt") |
| 71 | + |
| 72 | +// NewServerSaltGenerator returns a SaltGenerator whose output is apparently |
| 73 | +// random, but is secretly marked as being issued by the server. |
| 74 | +// This is useful to prevent the server from accepting its own output in a |
| 75 | +// reflection attack. |
| 76 | +func NewServerSaltGenerator(secret string) ServerSaltGenerator { |
| 77 | + // Shadowsocks already uses HKDF-SHA1 to derive the AEAD key, so we use |
| 78 | + // the same derivation with a different "info" to generate our HMAC key. |
| 79 | + keySource := hkdf.New(crypto.SHA1.New, []byte(secret), nil, serverSaltLabel) |
| 80 | + // The key can be any size, but matching the block size is most efficient. |
| 81 | + key := make([]byte, crypto.SHA1.Size()) |
| 82 | + io.ReadFull(keySource, key) |
| 83 | + return serverSaltGenerator{key} |
| 84 | +} |
| 85 | + |
| 86 | +func (sg serverSaltGenerator) splitSalt(salt []byte) (prefix, mark []byte, err error) { |
| 87 | + prefixLen := len(salt) - ServerSaltMarkLen |
| 88 | + if prefixLen < 0 { |
| 89 | + return nil, nil, fmt.Errorf("Salt is too short: %d < %d", len(salt), ServerSaltMarkLen) |
| 90 | + } |
| 91 | + return salt[:prefixLen], salt[prefixLen:], nil |
| 92 | +} |
| 93 | + |
| 94 | +// getTag takes in a salt prefix and returns the tag. |
| 95 | +func (sg serverSaltGenerator) getTag(prefix []byte) []byte { |
| 96 | + // Use HMAC-SHA1, even though SHA1 is broken, because HMAC-SHA1 is still |
| 97 | + // secure, and we're already using HKDF-SHA1. |
| 98 | + hmac := hmac.New(crypto.SHA1.New, sg.key) |
| 99 | + hmac.Write(prefix) // Hash.Write never returns an error. |
| 100 | + return hmac.Sum(nil) |
| 101 | +} |
| 102 | + |
| 103 | +// GetSalt returns an apparently random salt that can be identified |
| 104 | +// as server-originated by anyone who knows the Shadowsocks key. |
| 105 | +func (sg serverSaltGenerator) GetSalt(salt []byte) error { |
| 106 | + prefix, mark, err := sg.splitSalt(salt) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if _, err := rand.Read(prefix); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + tag := sg.getTag(prefix) |
| 114 | + copy(mark, tag) |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func (sg serverSaltGenerator) IsServerSalt(salt []byte) bool { |
| 119 | + prefix, mark, err := sg.splitSalt(salt) |
| 120 | + if err != nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + tag := sg.getTag(prefix) |
| 124 | + return bytes.Equal(tag[:ServerSaltMarkLen], mark) |
| 125 | +} |
0 commit comments