obfuscator.go 24 KB

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