obfuscator.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. //go:build !PSIPHON_DISABLE_QUIC
  2. // +build !PSIPHON_DISABLE_QUIC
  3. /*
  4. * Copyright (c) 2018, Psiphon Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package quic
  22. import (
  23. "crypto/sha256"
  24. std_errors "errors"
  25. "io"
  26. "net"
  27. "sync"
  28. "sync/atomic"
  29. "time"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/Yawning/chacha20"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
  34. ietf_quic "github.com/Psiphon-Labs/quic-go"
  35. "golang.org/x/crypto/hkdf"
  36. "golang.org/x/net/ipv4"
  37. )
  38. const (
  39. // MAX_PACKET_SIZE is the largest packet size quic-go will produce,
  40. // including post MTU discovery. This value is quic-go
  41. // internal/protocol.MaxPacketBufferSize, which is the Ethernet MTU of
  42. // 1500 less IPv6 and UDP header sizes.
  43. //
  44. // Legacy gQUIC quic-go will produce packets no larger than
  45. // MAX_PRE_DISCOVERY_PACKET_SIZE_IPV4/IPV6.
  46. MAX_PACKET_SIZE = 1452
  47. // MAX_PRE_DISCOVERY_PACKET_SIZE_IPV4/IPV6 are the largest packet sizes
  48. // quic-go will produce before MTU discovery, 1280 less IP and UDP header
  49. // sizes. These values, which match quic-go
  50. // internal/protocol.InitialPacketSizeIPv4/IPv6, are used to calculate
  51. // maximum padding sizes.
  52. MAX_PRE_DISCOVERY_PACKET_SIZE_IPV4 = 1252
  53. MAX_PRE_DISCOVERY_PACKET_SIZE_IPV6 = 1232
  54. // OBFUSCATED_MAX_PACKET_SIZE_ADJUSTMENT is the minimum amount of bytes
  55. // required for obfuscation overhead, the nonce and the padding length.
  56. // In IETF quic-go, this adjustment value is passed into quic-go and
  57. // applied to packet construction so that quic-go produces max packet
  58. // sizes reduced by this adjustment value.
  59. OBFUSCATED_MAX_PACKET_SIZE_ADJUSTMENT = NONCE_SIZE + 1
  60. // MIN_INITIAL_PACKET_SIZE is the minimum UDP packet payload size for
  61. // Initial packets, an anti-amplification measure (see RFC 9000, section
  62. // 14.1). To accomodate obfuscation prefix messages within the same
  63. // Initial UDP packet, quic-go's enforcement of this size requirement is
  64. // disabled and the enforcment is done by ObfuscatedPacketConn.
  65. MIN_INITIAL_PACKET_SIZE = 1200
  66. MAX_PADDING_SIZE = 255
  67. MAX_GQUIC_PADDING_SIZE = 64
  68. MIN_DECOY_PACKETS = 0
  69. MAX_DECOY_PACKETS = 10
  70. NONCE_SIZE = 12
  71. RANDOM_STREAM_LIMIT = 1<<38 - 64
  72. )
  73. // ObfuscatedPacketConn wraps a QUIC net.PacketConn with an obfuscation layer
  74. // that obscures QUIC packets, adding random padding and producing uniformly
  75. // random payload.
  76. //
  77. // The crypto performed by ObfuscatedPacketConn is purely for obfuscation to
  78. // frustrate wire-speed DPI and does not add privacy/security. The small
  79. // nonce space and single key per server is not cryptographically secure.
  80. //
  81. // A server-side ObfuscatedPacketConn performs simple QUIC DPI to distinguish
  82. // between obfuscated and non-obfsucated peer flows and responds accordingly.
  83. //
  84. // The header and padding added by ObfuscatedPacketConn on top of the QUIC
  85. // payload will increase UDP packets beyond the QUIC max of 1280 bytes,
  86. // introducing some risk of fragmentation and/or dropped packets.
  87. type ObfuscatedPacketConn struct {
  88. net.PacketConn
  89. isServer bool
  90. isIETFClient bool
  91. isDecoyClient bool
  92. isClosed int32
  93. runWaitGroup *sync.WaitGroup
  94. stopBroadcast chan struct{}
  95. obfuscationKey [32]byte
  96. peerModesMutex sync.Mutex
  97. peerModes map[string]*peerMode
  98. noncePRNG *prng.PRNG
  99. paddingPRNG *prng.PRNG
  100. nonceTransformerParameters *transforms.ObfuscatorSeedTransformerParameters
  101. decoyPacketCount int32
  102. decoyBuffer []byte
  103. }
  104. type peerMode struct {
  105. isObfuscated bool
  106. isIETF bool
  107. lastPacketTime time.Time
  108. }
  109. func (p *peerMode) isStale() bool {
  110. return time.Since(p.lastPacketTime) >= SERVER_IDLE_TIMEOUT
  111. }
  112. func NewClientObfuscatedPacketConn(
  113. packetConn net.PacketConn,
  114. isServer bool,
  115. isIETFClient bool,
  116. isDecoyClient bool,
  117. obfuscationKey string,
  118. paddingSeed *prng.Seed,
  119. obfuscationNonceTransformerParameters *transforms.ObfuscatorSeedTransformerParameters,
  120. ) (*ObfuscatedPacketConn, error) {
  121. return newObfuscatedPacketConn(
  122. packetConn,
  123. isServer,
  124. isIETFClient,
  125. isDecoyClient,
  126. obfuscationKey,
  127. paddingSeed,
  128. obfuscationNonceTransformerParameters)
  129. }
  130. func NewServerObfuscatedPacketConn(
  131. packetConn net.PacketConn,
  132. isServer bool,
  133. isIETFClient bool,
  134. isDecoyClient bool,
  135. obfuscationKey string,
  136. paddingSeed *prng.Seed) (*ObfuscatedPacketConn, error) {
  137. return newObfuscatedPacketConn(
  138. packetConn,
  139. isServer,
  140. isIETFClient,
  141. isDecoyClient,
  142. obfuscationKey,
  143. paddingSeed,
  144. nil)
  145. }
  146. // newObfuscatedPacketConn creates a new ObfuscatedPacketConn.
  147. func newObfuscatedPacketConn(
  148. packetConn net.PacketConn,
  149. isServer bool,
  150. isIETFClient bool,
  151. isDecoyClient bool,
  152. obfuscationKey string,
  153. paddingSeed *prng.Seed,
  154. obfuscationNonceTransformerParameters *transforms.ObfuscatorSeedTransformerParameters,
  155. ) (*ObfuscatedPacketConn, error) {
  156. // There is no replay of obfuscation "encryption", just padding.
  157. nonceSeed, err := prng.NewSeed()
  158. if err != nil {
  159. return nil, errors.Trace(err)
  160. }
  161. conn := &ObfuscatedPacketConn{
  162. PacketConn: packetConn,
  163. isServer: isServer,
  164. isIETFClient: isIETFClient,
  165. isDecoyClient: isDecoyClient,
  166. peerModes: make(map[string]*peerMode),
  167. noncePRNG: prng.NewPRNGWithSeed(nonceSeed),
  168. paddingPRNG: prng.NewPRNGWithSeed(paddingSeed),
  169. nonceTransformerParameters: obfuscationNonceTransformerParameters,
  170. }
  171. secret := []byte(obfuscationKey)
  172. salt := []byte("quic-obfuscation-key")
  173. _, err = io.ReadFull(
  174. hkdf.New(sha256.New, secret, salt, nil), conn.obfuscationKey[:])
  175. if err != nil {
  176. return nil, errors.Trace(err)
  177. }
  178. if isDecoyClient {
  179. conn.decoyPacketCount = int32(conn.paddingPRNG.Range(
  180. MIN_DECOY_PACKETS, MAX_DECOY_PACKETS))
  181. conn.decoyBuffer = make([]byte, MAX_PACKET_SIZE)
  182. }
  183. if isServer {
  184. conn.runWaitGroup = new(sync.WaitGroup)
  185. conn.stopBroadcast = make(chan struct{})
  186. // Reap stale peer mode information to reclaim memory.
  187. conn.runWaitGroup.Add(1)
  188. go func() {
  189. defer conn.runWaitGroup.Done()
  190. ticker := time.NewTicker(SERVER_IDLE_TIMEOUT / 2)
  191. defer ticker.Stop()
  192. for {
  193. select {
  194. case <-ticker.C:
  195. conn.peerModesMutex.Lock()
  196. for address, mode := range conn.peerModes {
  197. if mode.isStale() {
  198. delete(conn.peerModes, address)
  199. }
  200. }
  201. conn.peerModesMutex.Unlock()
  202. case <-conn.stopBroadcast:
  203. return
  204. }
  205. }
  206. }()
  207. }
  208. return conn, nil
  209. }
  210. func (conn *ObfuscatedPacketConn) Close() error {
  211. // Ensure close channel only called once.
  212. if !atomic.CompareAndSwapInt32(&conn.isClosed, 0, 1) {
  213. return nil
  214. }
  215. if conn.isServer {
  216. close(conn.stopBroadcast)
  217. conn.runWaitGroup.Wait()
  218. }
  219. return conn.PacketConn.Close()
  220. }
  221. type temporaryNetError struct {
  222. err error
  223. }
  224. func newTemporaryNetError(err error) *temporaryNetError {
  225. return &temporaryNetError{err: err}
  226. }
  227. func (e *temporaryNetError) Timeout() bool {
  228. return false
  229. }
  230. func (e *temporaryNetError) Temporary() bool {
  231. return true
  232. }
  233. func (e *temporaryNetError) Error() string {
  234. return e.err.Error()
  235. }
  236. func (conn *ObfuscatedPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
  237. n, _, _, addr, _, err := conn.readPacketWithType(p, nil)
  238. // Do not wrap any I/O err returned by conn.PacketConn
  239. return n, addr, err
  240. }
  241. func (conn *ObfuscatedPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
  242. udpAddr, ok := addr.(*net.UDPAddr)
  243. if !ok {
  244. return 0, errors.TraceNew("unexpected addr type")
  245. }
  246. n, _, err := conn.writePacket(p, nil, udpAddr)
  247. // Do not wrap any I/O err returned by conn.PacketConn
  248. return n, err
  249. }
  250. // ReadMsgUDP, and WriteMsgUDP satisfy the ietf_quic.OOBCapablePacketConn
  251. // interface. In non-muxListener mode, quic-go will access the
  252. // ObfuscatedPacketConn directly and use these functions to set ECN bits.
  253. //
  254. // ReadBatch implements ietf_quic.batchConn. Providing this implementation
  255. // effectively disables the quic-go batch packet reading optimization, which
  256. // would otherwise bypass deobfuscation. Note that ipv4.Message is an alias
  257. // for x/net/internal/socket.Message and quic-go uses this one type for both
  258. // IPv4 and IPv6 packets.
  259. //
  260. // Read, Write, and RemoteAddr are present to satisfy the net.Conn interface,
  261. // to which ObfuscatedPacketConn is converted internally, via quic-go, in
  262. // x/net/ipv[4|6] for OOB manipulation. These functions do not need to be
  263. // implemented.
  264. func (conn *ObfuscatedPacketConn) ReadMsgUDP(p, oob []byte) (int, int, int, *net.UDPAddr, error) {
  265. n, oobn, flags, addr, _, err := conn.readPacketWithType(p, nil)
  266. // Do not wrap any I/O err returned by conn.PacketConn
  267. return n, oobn, flags, addr, err
  268. }
  269. func (conn *ObfuscatedPacketConn) WriteMsgUDP(p, oob []byte, addr *net.UDPAddr) (int, int, error) {
  270. n, oobn, err := conn.writePacket(p, oob, addr)
  271. // Do not wrap any I/O err returned by conn.PacketConn
  272. return n, oobn, err
  273. }
  274. func (conn *ObfuscatedPacketConn) ReadBatch(ms []ipv4.Message, _ int) (int, error) {
  275. // Read a "batch" of 1 message, with any necessary deobfuscation performed
  276. // by readPacketWithType.
  277. //
  278. // TODO: implement proper batch packet reading here, along with batch
  279. // deobfuscation.
  280. if len(ms) < 1 || len(ms[0].Buffers[0]) < 1 {
  281. return 0, errors.TraceNew("unexpected message buffer size")
  282. }
  283. var err error
  284. ms[0].N, ms[0].NN, ms[0].Flags, ms[0].Addr, _, err =
  285. conn.readPacketWithType(ms[0].Buffers[0], ms[0].OOB)
  286. if err != nil {
  287. // Do not wrap any I/O err returned by conn.PacketConn
  288. return 0, err
  289. }
  290. return 1, nil
  291. }
  292. var notSupported = std_errors.New("not supported")
  293. func (conn *ObfuscatedPacketConn) Read(_ []byte) (int, error) {
  294. return 0, errors.Trace(notSupported)
  295. }
  296. func (conn *ObfuscatedPacketConn) Write(_ []byte) (int, error) {
  297. return 0, errors.Trace(notSupported)
  298. }
  299. func (conn *ObfuscatedPacketConn) RemoteAddr() net.Addr {
  300. return nil
  301. }
  302. func (conn *ObfuscatedPacketConn) readPacketWithType(
  303. p, oob []byte) (int, int, int, *net.UDPAddr, bool, error) {
  304. for {
  305. n, oobn, flags, addr, isIETF, err := conn.readPacket(p, oob)
  306. // When enabled, and when a packet is received, sometimes immediately
  307. // respond with a decoy packet, which is entirely random. Sending a
  308. // small number of these packets early in the connection is intended
  309. // to frustrate simple traffic fingerprinting which looks for a
  310. // certain number of packets client->server, followed by a certain
  311. // number of packets server->client, and so on.
  312. //
  313. // TODO: use a more sophisticated distribution; configure via tactics
  314. // parameters; add server-side decoy packet injection.
  315. //
  316. // See also:
  317. //
  318. // Tor Project's Sharknado concept:
  319. // https://gitlab.torproject.org/legacy/trac/-/issues/30716#note_2326086
  320. //
  321. // Lantern's OQUIC specification:
  322. // https://github.com/getlantern/quicwrapper/blob/master/OQUIC.md
  323. if err == nil && conn.isIETFClient && conn.isDecoyClient {
  324. count := atomic.LoadInt32(&conn.decoyPacketCount)
  325. if count > 0 && conn.paddingPRNG.FlipCoin() {
  326. if atomic.CompareAndSwapInt32(&conn.decoyPacketCount, count, count-1) {
  327. packetSize := conn.paddingPRNG.Range(
  328. 1, getMaxPreDiscoveryPacketSize(addr))
  329. // decoyBuffer is all zeros, so the QUIC Fixed Bit is zero.
  330. // Ignore any errors when writing decoy packets.
  331. _, _ = conn.WriteTo(conn.decoyBuffer[:packetSize], addr)
  332. }
  333. }
  334. }
  335. // Ignore/drop packets with an invalid QUIC Fixed Bit (see RFC 9000,
  336. // Packet Formats).
  337. if err == nil && (isIETF || conn.isIETFClient) && n > 0 && (p[0]&0x40) == 0 {
  338. continue
  339. }
  340. // Do not wrap any I/O err returned by conn.PacketConn
  341. return n, oobn, flags, addr, isIETF, err
  342. }
  343. }
  344. func (conn *ObfuscatedPacketConn) readPacket(
  345. p, oob []byte) (int, int, int, *net.UDPAddr, bool, error) {
  346. var n, oobn, flags int
  347. var addr *net.UDPAddr
  348. var err error
  349. oobCapablePacketConn, ok := conn.PacketConn.(ietf_quic.OOBCapablePacketConn)
  350. if ok {
  351. // Read OOB ECN bits when supported by the packet conn.
  352. n, oobn, flags, addr, err = oobCapablePacketConn.ReadMsgUDP(p, oob)
  353. } else {
  354. // Fall back to a generic ReadFrom, supported by any packet conn.
  355. var netAddr net.Addr
  356. n, netAddr, err = conn.PacketConn.ReadFrom(p)
  357. if netAddr != nil {
  358. // Directly convert from net.Addr to *net.UDPAddr, if possible.
  359. addr, ok = netAddr.(*net.UDPAddr)
  360. if !ok {
  361. addr, err = net.ResolveUDPAddr("udp", netAddr.String())
  362. }
  363. }
  364. }
  365. // Data is processed even when err != nil, as ReadFrom may return both
  366. // a packet and an error, such as io.EOF.
  367. // See: https://golang.org/pkg/net/#PacketConn.
  368. // In client mode, obfuscation is always performed as the client knows it is
  369. // using obfuscation. In server mode, DPI is performed to distinguish whether
  370. // the QUIC packet for a new flow is obfuscated or not, and whether it's IETF
  371. // or gQUIC. The isIETF return value is set only in server mode and is set
  372. // only when the function returns no error.
  373. isObfuscated := true
  374. isIETF := true
  375. var address string
  376. var firstFlowPacket bool
  377. var lastPacketTime time.Time
  378. if n > 0 {
  379. if conn.isServer {
  380. // The server handles both plain and obfuscated QUIC packets.
  381. // isQUIC performs DPI to determine whether the packet appears to
  382. // be QUIC, in which case deobfuscation is not performed. Not all
  383. // plain QUIC packets will pass the DPI test, but the initial
  384. // packet(s) in a flow are expected to match; so the server
  385. // records a peer "mode", referenced by peer address to know when
  386. // to skip deobfuscation for later packets.
  387. //
  388. // It's possible for clients to redial QUIC connections,
  389. // transitioning from obfuscated to plain, using the same source
  390. // address (IP and port). This is more likely when many clients
  391. // are behind NAT. If a packet appears to be QUIC, this will reset
  392. // any existing peer "mode" to plain. The obfuscator checks that
  393. // its obfuscated packets don't pass the QUIC DPI test.
  394. //
  395. // TODO: delete peerMode when a packet is a client connection
  396. // termination QUIC packet? Will reclaim peerMode memory faster
  397. // than relying on reaper.
  398. lastPacketTime = time.Now()
  399. // isIETF is not meaningful if not the first packet in a flow and is not
  400. // meaningful when first packet is obfuscated. To correctly indicate isIETF
  401. // when obfuscated, the isIETFQUICClientHello test is repeated after
  402. // deobfuscating the packet.
  403. var isQUIC bool
  404. isQUIC, isIETF = isQUICClientHello(p[:n])
  405. isObfuscated = !isQUIC
  406. if isObfuscated && isIETF {
  407. return n, oobn, flags, addr, false, newTemporaryNetError(
  408. errors.Tracef("unexpected isQUIC result"))
  409. }
  410. // Without addr, the mode cannot be determined.
  411. if addr == nil {
  412. return n, oobn, flags, addr, true, newTemporaryNetError(
  413. errors.Tracef("missing addr"))
  414. }
  415. conn.peerModesMutex.Lock()
  416. address = addr.String()
  417. mode, ok := conn.peerModes[address]
  418. if !ok {
  419. // This is a new flow.
  420. mode = &peerMode{isObfuscated: isObfuscated, isIETF: isIETF}
  421. conn.peerModes[address] = mode
  422. firstFlowPacket = true
  423. } else if mode.isStale() ||
  424. (isQUIC && (mode.isObfuscated || (mode.isIETF != isIETF))) {
  425. // The address for this flow has been seen before, but either (1) it's
  426. // stale and not yet reaped; or (2) the client has redialed and switched
  427. // from obfuscated to non-obfuscated; or (3) the client has redialed and
  428. // switched non-obfuscated gQUIC<-->IETF. These cases are treated like a
  429. // new flow.
  430. //
  431. // Limitation: since the DPI doesn't detect QUIC in post-Hello
  432. // non-obfuscated packets, some client redial cases are not identified as
  433. // and handled like new flows and the QUIC session will fail. These cases
  434. // include the client immediately redialing and switching from
  435. // non-obfuscated to obfuscated or switching obfuscated gQUIC<-->IETF.
  436. mode.isObfuscated = isObfuscated
  437. mode.isIETF = isIETF
  438. firstFlowPacket = true
  439. } else {
  440. isObfuscated = mode.isObfuscated
  441. isIETF = mode.isIETF
  442. }
  443. mode.lastPacketTime = lastPacketTime
  444. isIETF = mode.isIETF
  445. conn.peerModesMutex.Unlock()
  446. } else {
  447. isIETF = conn.isIETFClient
  448. }
  449. if isObfuscated {
  450. // We can use p as a scratch buffer for deobfuscation, and this
  451. // avoids allocting a buffer.
  452. if n < (NONCE_SIZE + 1) {
  453. return n, oobn, flags, addr, true, newTemporaryNetError(
  454. errors.Tracef("unexpected obfuscated QUIC packet length: %d", n))
  455. }
  456. cipher, err := chacha20.NewCipher(conn.obfuscationKey[:], p[0:NONCE_SIZE])
  457. if err != nil {
  458. return n, oobn, flags, addr, true, errors.Trace(err)
  459. }
  460. cipher.XORKeyStream(p[NONCE_SIZE:], p[NONCE_SIZE:])
  461. // The padding length check allows legacy gQUIC padding to exceed
  462. // its 64 byte maximum, as we don't yet know if this is gQUIC or
  463. // IETF QUIC.
  464. paddingLen := int(p[NONCE_SIZE])
  465. if paddingLen > MAX_PADDING_SIZE || paddingLen > n-(NONCE_SIZE+1) {
  466. return n, oobn, flags, addr, true, newTemporaryNetError(
  467. errors.Tracef("unexpected padding length: %d, %d", paddingLen, n))
  468. }
  469. n -= (NONCE_SIZE + 1) + paddingLen
  470. copy(p[0:n], p[(NONCE_SIZE+1)+paddingLen:n+(NONCE_SIZE+1)+paddingLen])
  471. if conn.isServer && firstFlowPacket {
  472. isIETF = isIETFQUICClientHello(p[0:n])
  473. // When an obfuscated packet looks like neither IETF nor
  474. // gQUIC, force it through the IETF code path which will
  475. // perform anti-probing check before sending any response
  476. // packet. The gQUIC stack may respond with a version
  477. // negotiation packet.
  478. //
  479. // Ensure that mode.isIETF is set to true before returning,
  480. // so subsequent packets in the same flow are also forced
  481. // through the same anti-probing code path.
  482. //
  483. // Limitation: the following race condition check is not
  484. // consistent with this constraint. This will be resolved by
  485. // disabling gQUIC or once gQUIC is ultimatel retired.
  486. if !isIETF && !isGQUICClientHello(p[0:n]) {
  487. isIETF = true
  488. }
  489. conn.peerModesMutex.Lock()
  490. mode, ok := conn.peerModes[address]
  491. // There's a possible race condition between the two instances of locking
  492. // peerModesMutex: the client might redial in the meantime. Check that the
  493. // mode state is unchanged from when the lock was last held.
  494. if !ok || mode.isObfuscated != true || mode.isIETF != false ||
  495. mode.lastPacketTime != lastPacketTime {
  496. conn.peerModesMutex.Unlock()
  497. return n, oobn, flags, addr, true, newTemporaryNetError(
  498. errors.Tracef("unexpected peer mode"))
  499. }
  500. mode.isIETF = isIETF
  501. conn.peerModesMutex.Unlock()
  502. // Enforce the MIN_INITIAL_PACKET_SIZE size requirement for new flows.
  503. //
  504. // Limitations:
  505. //
  506. // - The Initial packet may be sent more than once, but we
  507. // only check the very first packet.
  508. // - For session resumption, the first packet may be a
  509. // Handshake packet, not an Initial packet, and can be smaller.
  510. if isIETF && n < MIN_INITIAL_PACKET_SIZE {
  511. return n, oobn, flags, addr, true, newTemporaryNetError(errors.Tracef(
  512. "unexpected first QUIC packet length: %d", n))
  513. }
  514. }
  515. }
  516. }
  517. // Do not wrap any I/O err returned by conn.PacketConn
  518. return n, oobn, flags, addr, isIETF, err
  519. }
  520. type obfuscatorBuffer struct {
  521. buffer [MAX_PACKET_SIZE]byte
  522. }
  523. var obfuscatorBufferPool = &sync.Pool{
  524. New: func() interface{} {
  525. return new(obfuscatorBuffer)
  526. },
  527. }
  528. func (conn *ObfuscatedPacketConn) writePacket(
  529. p, oob []byte, addr *net.UDPAddr) (int, int, error) {
  530. n := len(p)
  531. isObfuscated := true
  532. isIETF := true
  533. if conn.isServer {
  534. conn.peerModesMutex.Lock()
  535. address := addr.String()
  536. mode, ok := conn.peerModes[address]
  537. if ok {
  538. isObfuscated = mode.isObfuscated
  539. isIETF = mode.isIETF
  540. }
  541. conn.peerModesMutex.Unlock()
  542. } else {
  543. isIETF = conn.isIETFClient
  544. }
  545. if isObfuscated {
  546. if n > MAX_PACKET_SIZE {
  547. return 0, 0, newTemporaryNetError(errors.Tracef(
  548. "unexpected QUIC packet length: %d", n))
  549. }
  550. // Note: escape analysis showed a local array escaping to the heap,
  551. // so use a buffer pool instead to avoid heap allocation per packet.
  552. b := obfuscatorBufferPool.Get().(*obfuscatorBuffer)
  553. buffer := b.buffer[:]
  554. defer obfuscatorBufferPool.Put(b)
  555. for {
  556. // Note: this zero-memory pattern is compiler optimized:
  557. // https://golang.org/cl/137880043
  558. for i := range buffer {
  559. buffer[i] = 0
  560. }
  561. nonce := buffer[0:NONCE_SIZE]
  562. conn.noncePRNG.Read(nonce)
  563. // This transform may reduce the entropy of the nonce, which increases
  564. // the chance of nonce reuse. However, this chacha20 encryption is for
  565. // obfuscation purposes only.
  566. if conn.nonceTransformerParameters != nil {
  567. err := conn.nonceTransformerParameters.Apply(nonce)
  568. if err != nil {
  569. return 0, 0, errors.Trace(err)
  570. }
  571. }
  572. maxPadding := getMaxPaddingSize(isIETF, addr, n)
  573. paddingLen := conn.paddingPRNG.Intn(maxPadding + 1)
  574. buffer[NONCE_SIZE] = uint8(paddingLen)
  575. padding := buffer[(NONCE_SIZE + 1) : (NONCE_SIZE+1)+paddingLen]
  576. conn.paddingPRNG.Read(padding)
  577. copy(buffer[(NONCE_SIZE+1)+paddingLen:], p)
  578. dataLen := (NONCE_SIZE + 1) + paddingLen + n
  579. cipher, err := chacha20.NewCipher(conn.obfuscationKey[:], nonce)
  580. if err != nil {
  581. return 0, 0, errors.Trace(err)
  582. }
  583. packet := buffer[NONCE_SIZE:dataLen]
  584. cipher.XORKeyStream(packet, packet)
  585. p = buffer[:dataLen]
  586. // Don't use obfuscation that looks like QUIC, or the
  587. // peer will not treat this packet as obfuscated.
  588. isQUIC, _ := isQUICClientHello(p)
  589. if !isQUIC {
  590. break
  591. }
  592. }
  593. }
  594. var oobn int
  595. var err error
  596. oobCapablePacketConn, ok := conn.PacketConn.(ietf_quic.OOBCapablePacketConn)
  597. if ok {
  598. // Write OOB bits if supported by the packet conn.
  599. //
  600. // At this time, quic-go reads but does not write ECN OOB bits. On the
  601. // client-side, the Dial function arranges for conn.PacketConn to not
  602. // implement OOBCapablePacketConn when using obfuscated QUIC, and so
  603. // quic-go is not expected to write ECN bits -- a potential
  604. // obfuscation fingerprint -- in the future, on the client-side.
  605. //
  606. // Limitation: on the server-side, the single UDP server socket is
  607. // wrapped with ObfuscatedPacketConn and supports both obfuscated and
  608. // regular QUIC; as it stands, this logic will support writing ECN
  609. // bits for both obfuscated and regular QUIC.
  610. _, oobn, err = oobCapablePacketConn.WriteMsgUDP(p, oob, addr)
  611. } else {
  612. // Fall back to WriteTo, supported by any packet conn. If there are
  613. // OOB bits to be written, fail.
  614. if oob != nil {
  615. return 0, 0, errors.TraceNew("unexpected OOB payload for non-OOBCapablePacketConn")
  616. }
  617. _, err = conn.PacketConn.WriteTo(p, addr)
  618. }
  619. // Return n = len(input p) bytes written even when p is an obfuscated
  620. // buffer and longer than the input p.
  621. // Do not wrap any I/O err returned by conn.PacketConn
  622. return n, oobn, err
  623. }
  624. func getMaxPreDiscoveryPacketSize(addr net.Addr) int {
  625. maxPacketSize := MAX_PRE_DISCOVERY_PACKET_SIZE_IPV4
  626. if udpAddr, ok := addr.(*net.UDPAddr); ok && udpAddr.IP.To4() == nil {
  627. maxPacketSize = MAX_PRE_DISCOVERY_PACKET_SIZE_IPV6
  628. }
  629. return maxPacketSize
  630. }
  631. func getMaxPaddingSize(isIETF bool, addr net.Addr, packetSize int) int {
  632. maxPacketSize := getMaxPreDiscoveryPacketSize(addr)
  633. maxPadding := 0
  634. if isIETF {
  635. // quic-go starts with a maximum packet size of 1280, which is the
  636. // IPv6 minimum MTU as well as very commonly supported for IPv4
  637. // (quic-go may increase the maximum packet size via MTU discovery).
  638. // Do not pad beyond that initial maximum size. As a result, padding
  639. // is only added for smaller packets.
  640. // OBFUSCATED_PACKET_SIZE_ADJUSTMENT is already factored in via
  641. // Client/ServerInitalPacketPaddingAdjustment.
  642. maxPadding = maxPacketSize - packetSize
  643. if maxPadding < 0 {
  644. maxPadding = 0
  645. }
  646. if maxPadding > MAX_PADDING_SIZE {
  647. maxPadding = MAX_PADDING_SIZE
  648. }
  649. } else {
  650. // Legacy gQUIC has a strict maximum packet size of 1280, and legacy
  651. // obfuscation adds padding on top of that.
  652. maxPadding = (maxPacketSize + NONCE_SIZE + 1 + MAX_GQUIC_PADDING_SIZE) - packetSize
  653. if maxPadding < 0 {
  654. maxPadding = 0
  655. }
  656. if maxPadding > MAX_GQUIC_PADDING_SIZE {
  657. maxPadding = MAX_GQUIC_PADDING_SIZE
  658. }
  659. }
  660. return maxPadding
  661. }
  662. func (conn *ObfuscatedPacketConn) serverMaxPacketSizeAdjustment(
  663. addr net.Addr) int {
  664. if !conn.isServer {
  665. return 0
  666. }
  667. conn.peerModesMutex.Lock()
  668. address := addr.String()
  669. mode, ok := conn.peerModes[address]
  670. isObfuscated := ok && mode.isObfuscated
  671. conn.peerModesMutex.Unlock()
  672. if isObfuscated {
  673. return OBFUSCATED_MAX_PACKET_SIZE_ADJUSTMENT
  674. }
  675. return 0
  676. }
  677. func isQUICClientHello(buffer []byte) (bool, bool) {
  678. // As this function is called for every packet, it needs to be fast.
  679. //
  680. // As QUIC header parsing is complex, with many cases, we are not
  681. // presently doing that, although this might improve accuracy as we should
  682. // be able to identify the precise offset of indicators based on header
  683. // values.
  684. if isIETFQUICClientHello(buffer) {
  685. return true, true
  686. } else if isGQUICClientHello(buffer) {
  687. return true, false
  688. }
  689. return false, false
  690. }
  691. func isGQUICClientHello(buffer []byte) bool {
  692. // In all currently supported versions, the first client packet contains
  693. // the "CHLO" tag at one of the following offsets. The offset can vary for
  694. // a single version.
  695. //
  696. // Note that v44 does not include the "QUIC version" header field in its
  697. // first client packet.
  698. if (len(buffer) >= 33 &&
  699. buffer[29] == 'C' &&
  700. buffer[30] == 'H' &&
  701. buffer[31] == 'L' &&
  702. buffer[32] == 'O') ||
  703. (len(buffer) >= 35 &&
  704. buffer[31] == 'C' &&
  705. buffer[32] == 'H' &&
  706. buffer[33] == 'L' &&
  707. buffer[34] == 'O') ||
  708. (len(buffer) >= 38 &&
  709. buffer[34] == 'C' &&
  710. buffer[35] == 'H' &&
  711. buffer[36] == 'L' &&
  712. buffer[37] == 'O') {
  713. return true
  714. }
  715. return false
  716. }
  717. func isIETFQUICClientHello(buffer []byte) bool {
  718. // https://tools.ietf.org/html/draft-ietf-quic-transport-23#section-17.2:
  719. //
  720. // Check 1st nibble of byte 0:
  721. // 1... .... = Header Form: Long Header (1)
  722. // .1.. .... = Fixed Bit: True
  723. // ..00 .... = Packet Type: Initial (0)
  724. //
  725. // Then check bytes 1..4 for expected version number.
  726. if len(buffer) < 5 {
  727. return false
  728. }
  729. if buffer[0]>>4 != 0x0c {
  730. return false
  731. }
  732. // IETF QUIC version 1, RFC 9000
  733. return buffer[1] == 0 &&
  734. buffer[2] == 0 &&
  735. buffer[3] == 0 &&
  736. buffer[4] == 0x1
  737. }