obfuscator.go 25 KB

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