obfuscation.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2023, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package inproxy
  20. import (
  21. "crypto/aes"
  22. "crypto/cipher"
  23. "crypto/rand"
  24. "crypto/sha256"
  25. "encoding/binary"
  26. "io"
  27. "sync"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  31. "github.com/panmari/cuckoofilter"
  32. "golang.org/x/crypto/hkdf"
  33. )
  34. const (
  35. obfuscationSessionPacketNonceSize = 12
  36. obfuscationAntiReplayTimePeriod = 20 * time.Minute
  37. obfuscationAntiReplayHistorySize = 10000000
  38. )
  39. // ObfuscationSecret is shared, semisecret value used in obfuscation layers.
  40. type ObfuscationSecret [32]byte
  41. // GenerateRootObfuscationSecret creates a new ObfuscationSecret using
  42. // crypto/rand.
  43. func GenerateRootObfuscationSecret() (ObfuscationSecret, error) {
  44. var secret ObfuscationSecret
  45. _, err := rand.Read(secret[:])
  46. if err != nil {
  47. return secret, errors.Trace(err)
  48. }
  49. return secret, nil
  50. }
  51. // antiReplayTimeFactorPeriodSeconds is variable, to enable overriding the value in
  52. // tests. This value should not be overridden outside of test
  53. // cases.
  54. var antiReplayTimeFactorPeriodSeconds = int64(
  55. obfuscationAntiReplayTimePeriod / time.Second)
  56. // deriveObfuscationSecret derives an obfuscation secret from the root secret,
  57. // a context, and an optional time factor.
  58. //
  59. // With a time factor, derived secrets remain valid only for a limited time
  60. // period. Both ends of an obfuscated communication will derive the same
  61. // secret based on a shared root secret, a common context, and local clocks.
  62. // The current time is rounded, allowing the one end's clock to be slightly
  63. // ahead of or behind of the other end's clock.
  64. //
  65. // The time factor can be used in concert with a replay history, bounding the
  66. // number of historical messages that need to be retained in the history.
  67. func deriveObfuscationSecret(
  68. rootObfuscationSecret ObfuscationSecret,
  69. useTimeFactor bool,
  70. context string) (ObfuscationSecret, error) {
  71. var salt []byte
  72. if useTimeFactor {
  73. roundedTimePeriod := (time.Now().Unix() +
  74. (antiReplayTimeFactorPeriodSeconds / 2)) / antiReplayTimeFactorPeriodSeconds
  75. var timeFactor [8]byte
  76. binary.BigEndian.PutUint64(timeFactor[:], uint64(roundedTimePeriod))
  77. salt = timeFactor[:]
  78. }
  79. var key ObfuscationSecret
  80. _, err := io.ReadFull(
  81. hkdf.New(sha256.New, rootObfuscationSecret[:], salt, []byte(context)), key[:])
  82. if err != nil {
  83. return key, errors.Trace(err)
  84. }
  85. return key, nil
  86. }
  87. // deriveSessionPacketObfuscationSecret derives a common session obfuscation
  88. // secret for either end of a session. Set isInitiator to true for packets
  89. // sent or received by the initator; and false for packets sent or received
  90. // by a responder. Set isObfuscating to true for sent packets, and false for
  91. // received packets.
  92. func deriveSessionPacketObfuscationSecret(
  93. rootObfuscationSecret ObfuscationSecret,
  94. isInitiator bool,
  95. isObfuscating bool) (ObfuscationSecret, error) {
  96. // Upstream is packets from the initiator to the responder; or,
  97. // (isInitiator && isObfuscating) || (!isInitiator && !isObfuscating)
  98. isUpstream := (isInitiator == isObfuscating)
  99. // Derive distinct keys for each flow direction, to ensure that the two
  100. // flows can't simply be xor'd.
  101. context := "in-proxy-session-packet-intiator-to-responder"
  102. if !isUpstream {
  103. context = "in-proxy-session-packet-responder-to-initiator"
  104. }
  105. // The time factor is set for upstream; the responder uses an anti-replay
  106. // history for packets received from initiators.
  107. key, err := deriveObfuscationSecret(rootObfuscationSecret, isUpstream, context)
  108. if err != nil {
  109. return key, errors.Trace(err)
  110. }
  111. return key, nil
  112. }
  113. // obfuscateSessionPacket wraps a session packet with an obfuscation layer
  114. // which provides:
  115. //
  116. // - indistiguishability from fully random
  117. // - random padding
  118. // - anti-replay
  119. //
  120. // The full-random and padding properties make obfuscated packets appropriate
  121. // to embed in otherwise plaintext transports, such as HTTP, without being
  122. // trivially fingerprintable.
  123. //
  124. // While Noise protocol sessions messages have nonces and associated
  125. // anti-replay for nonces, this measure doen't cover the session handshake,
  126. // so an independent anti-replay mechanism is implemented here.
  127. func obfuscateSessionPacket(
  128. rootObfuscationSecret ObfuscationSecret,
  129. isInitiator bool,
  130. packet []byte,
  131. paddingMin int,
  132. paddingMax int) ([]byte, error) {
  133. // For simplicity, the secret is derived here for each packet. Derived
  134. // keys could be cached, but we need to be updated when a time factor is
  135. // active. Typical in-proxy sessions will exchange only a handful of
  136. // packets per event: the session handshake, and an API request round
  137. // trip or two. We don't attempt to avoid allocations here.
  138. //
  139. // Benchmark for secret derivation:
  140. //
  141. // BenchmarkDeriveObfuscationSecret
  142. // BenchmarkDeriveObfuscationSecret-8 1303953 902.7 ns/op
  143. key, err := deriveSessionPacketObfuscationSecret(
  144. rootObfuscationSecret, isInitiator, true)
  145. if err != nil {
  146. return nil, errors.Trace(err)
  147. }
  148. obfuscatedPacket := make([]byte, obfuscationSessionPacketNonceSize)
  149. _, err = prng.Read(obfuscatedPacket[:])
  150. if err != nil {
  151. return nil, errors.Trace(err)
  152. }
  153. var paddedPacket []byte
  154. paddingSize := prng.Range(paddingMin, paddingMax)
  155. paddedPacket = binary.AppendUvarint(paddedPacket, uint64(paddingSize))
  156. paddedPacket = append(paddedPacket, make([]byte, paddingSize)...)
  157. paddedPacket = append(paddedPacket, packet...)
  158. block, err := aes.NewCipher(key[:])
  159. if err != nil {
  160. return nil, errors.Trace(err)
  161. }
  162. aesgcm, err := cipher.NewGCM(block)
  163. if err != nil {
  164. return nil, errors.Trace(err)
  165. }
  166. obfuscatedPacket = aesgcm.Seal(
  167. obfuscatedPacket,
  168. obfuscatedPacket[:obfuscationSessionPacketNonceSize],
  169. paddedPacket,
  170. nil)
  171. return obfuscatedPacket, nil
  172. }
  173. // deobfuscateSessionPacket deobfuscates a session packet obfuscated with
  174. // obfuscateSessionPacket and the same deobfuscateSessionPacket.
  175. //
  176. // Responders must supply an obfuscationReplayHistory, which checks for
  177. // replayed session packets (within the time factor). Responders should drop
  178. // into anti-probing response behavior when deobfuscateSessionPacket returns
  179. // an error: the obfuscated packet may have been created by a prober without
  180. // the correct secret; or replayed by a prober.
  181. func deobfuscateSessionPacket(
  182. rootObfuscationSecret ObfuscationSecret,
  183. isInitiator bool,
  184. replayHistory *obfuscationReplayHistory,
  185. obfuscatedPacket []byte) ([]byte, error) {
  186. // A responder must provide a relay history, or it's misconfigured.
  187. if isInitiator == (replayHistory != nil) {
  188. return nil, errors.TraceNew("unexpected replay history")
  189. }
  190. // imitateDeobfuscateSessionPacketDuration is called in early failure
  191. // cases to imitate the elapsed time of lookups and cryptographic
  192. // operations that would otherwise be skipped. This is intended to
  193. // mitigate timing attacks by probers.
  194. //
  195. // Limitation: this doesn't result in a constant time.
  196. if len(obfuscatedPacket) < obfuscationSessionPacketNonceSize {
  197. imitateDeobfuscateSessionPacketDuration(replayHistory)
  198. return nil, errors.TraceNew("invalid nonce")
  199. }
  200. nonce := obfuscatedPacket[:obfuscationSessionPacketNonceSize]
  201. if replayHistory != nil && replayHistory.Lookup(nonce) {
  202. imitateDeobfuscateSessionPacketDuration(nil)
  203. return nil, errors.TraceNew("replayed nonce")
  204. }
  205. key, err := deriveSessionPacketObfuscationSecret(
  206. rootObfuscationSecret, isInitiator, false)
  207. if err != nil {
  208. return nil, errors.Trace(err)
  209. }
  210. // As an AEAD, AES-GCM authenticates that the sender used the expected
  211. // key, and so has the root obfuscation secret.
  212. block, err := aes.NewCipher(key[:])
  213. if err != nil {
  214. return nil, errors.Trace(err)
  215. }
  216. aesgcm, err := cipher.NewGCM(block)
  217. if err != nil {
  218. return nil, errors.Trace(err)
  219. }
  220. plaintext, err := aesgcm.Open(
  221. nil,
  222. nonce,
  223. obfuscatedPacket[obfuscationSessionPacketNonceSize:],
  224. nil)
  225. if err != nil {
  226. return nil, errors.Trace(err)
  227. }
  228. offset := 0
  229. paddingSize, n := binary.Uvarint(plaintext[offset:])
  230. if n < 1 {
  231. return nil, errors.TraceNew("invalid padding size")
  232. }
  233. offset += n
  234. if len(plaintext[offset:]) < int(paddingSize) {
  235. return nil, errors.TraceNew("invalid padding")
  236. }
  237. offset += int(paddingSize)
  238. if replayHistory != nil {
  239. // Now that it's validated, add this packet to the replay history. The
  240. // nonce is expected to be unique, so it's used as the history key.
  241. err = replayHistory.Insert(nonce)
  242. if err != nil {
  243. return nil, errors.Trace(err)
  244. }
  245. }
  246. return plaintext[offset:], nil
  247. }
  248. func imitateDeobfuscateSessionPacketDuration(replayHistory *obfuscationReplayHistory) {
  249. // Limitations: only one block is decrypted; crypto/aes or
  250. // crypto/cipher.GCM may not be constant time, depending on hardware
  251. // support; at best, this all-zeros invocation will make it as far as
  252. // GCM.Open, and not check padding.
  253. const (
  254. blockSize = 16
  255. tagSize = 16
  256. )
  257. var secret ObfuscationSecret
  258. var packet [obfuscationSessionPacketNonceSize + blockSize + tagSize]byte
  259. if replayHistory != nil {
  260. _ = replayHistory.Lookup(packet[:obfuscationSessionPacketNonceSize])
  261. }
  262. _, _ = deobfuscateSessionPacket(secret, true, nil, packet[:])
  263. }
  264. // obfuscationReplayHistory provides a lookup for recently observed obfuscated
  265. // session packet nonces. History is maintained for
  266. // 2*antiReplayTimeFactorPeriodSeconds; it's assumed that older packets, if
  267. // replayed, will fail to decrypt due to using an expired time factor.
  268. type obfuscationReplayHistory struct {
  269. mutex sync.Mutex
  270. filters [2]*cuckoo.Filter
  271. currentFilter int
  272. switchTime time.Time
  273. }
  274. func newObfuscationReplayHistory() *obfuscationReplayHistory {
  275. // Replay history is implemented using cuckoo filters, which use fixed
  276. // space overhead, and less space overhead than storing nonces explictly
  277. // under anticipated loads. With cuckoo filters, false positive lookups
  278. // are possible, but false negative lookups are not. So there's a small
  279. // chance that a non-replayed nonce will be flagged as in the history,
  280. // but no chance that a replayed nonce will pass as not in the history.
  281. //
  282. // From github.com/panmari/cuckoofilter:
  283. // > With the 16 bit fingerprint size in this repository, you can expect r
  284. // > ~= 0.0001. Other implementations use 8 bit, which correspond to a
  285. // > false positive rate of r ~= 0.03. NewFilter returns a new
  286. // > cuckoofilter suitable for the given number of elements. When
  287. // > inserting more elements, insertion speed will drop significantly and
  288. // > insertions might fail altogether. A capacity of 1000000 is a normal
  289. // > default, which allocates about ~2MB on 64-bit machines.
  290. //
  291. // With obfuscationAntiReplayHistorySize set to 10M, the session_test test
  292. // case with 10k clients making 100 requests each all within one time
  293. // period consistently produces no false positives.
  294. //
  295. // To accomodate the rolling time factor window, there are two cuckoo
  296. // filters, the "current" filter and the "next" filter. New nonces are
  297. // inserted into both the current and next filter. Every
  298. // antiReplayTimeFactorPeriodSeconds, the next filter replaces the
  299. // current filter. The previous current filter is reset and becomes the
  300. // new next filter.
  301. return &obfuscationReplayHistory{
  302. filters: [2]*cuckoo.Filter{
  303. cuckoo.NewFilter(obfuscationAntiReplayHistorySize),
  304. cuckoo.NewFilter(obfuscationAntiReplayHistorySize),
  305. },
  306. currentFilter: 0,
  307. switchTime: time.Now(),
  308. }
  309. }
  310. func (h *obfuscationReplayHistory) Insert(value []byte) error {
  311. h.mutex.Lock()
  312. defer h.mutex.Unlock()
  313. h.switchFilters()
  314. if !h.filters[0].Insert(value) || !h.filters[1].Insert(value) {
  315. return errors.TraceNew("replay history insert failed")
  316. }
  317. return nil
  318. }
  319. func (h *obfuscationReplayHistory) Lookup(value []byte) bool {
  320. h.mutex.Lock()
  321. defer h.mutex.Unlock()
  322. h.switchFilters()
  323. return h.filters[h.currentFilter].Lookup(value)
  324. }
  325. func (h *obfuscationReplayHistory) switchFilters() {
  326. // Assumes caller holds h.mutex lock.
  327. now := time.Now()
  328. if h.switchTime.Before(now.Add(-time.Duration(antiReplayTimeFactorPeriodSeconds) * time.Second)) {
  329. h.filters[h.currentFilter].Reset()
  330. h.currentFilter = (h.currentFilter + 1) % 2
  331. h.switchTime = now
  332. }
  333. }