obfuscated.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package tls
  20. import (
  21. "crypto/rand"
  22. "math/big"
  23. )
  24. // NewObfuscatedClientSessionCache produces obfuscated session tickets.
  25. //
  26. // [Psiphon]
  27. // Obfuscated Session Tickets
  28. //
  29. // Obfuscated session tickets is a network traffic obfuscation protocol that appears
  30. // to be valid TLS using session tickets. The client actually generates the session
  31. // ticket and encrypts it with a shared secret, enabling a TLS session that entirely
  32. // skips the most fingerprintable aspects of TLS.
  33. // The scheme is described here:
  34. // https://lists.torproject.org/pipermail/tor-dev/2016-September/011354.html
  35. //
  36. // Circumvention notes:
  37. // - TLS session ticket implementations are widespread:
  38. // https://istlsfastyet.com/#cdn-paas.
  39. // - An adversary cannot easily block session ticket capability, as this requires
  40. // a downgrade attack against TLS.
  41. // - Anti-probing defence is provided, as the adversary must use the correct obfuscation
  42. // shared secret to form valid obfuscation session ticket; otherwise server offers
  43. // standard session tickets.
  44. // - Limitation: TLS protocol and session ticket size correspond to golang implementation
  45. // and not more common OpenSSL.
  46. // - Limitation: an adversary with the obfuscation shared secret can decrypt the session
  47. // ticket and observe the plaintext traffic. It's assumed that the adversary will not
  48. // learn the obfuscated shared secret without also learning the address of the TLS
  49. // server and blocking it anyway; it's also assumed that the TLS payload is not
  50. // plaintext but is protected with some other security layer (e.g., SSH).
  51. //
  52. // Implementation notes:
  53. // - Client should set its ClientSessionCache to a NewObfuscatedTLSClientSessionCache.
  54. // This cache ignores the session key and always produces obfuscated session tickets.
  55. // - The TLS ClientHello includes an SNI field, even when using session tickets, so
  56. // the client should populate the ServerName.
  57. // - Server should set its SetSessionTicketKeys with first a standard key, followed by
  58. // the obfuscation shared secret.
  59. // - Since the client creates the session ticket, it selects parameters that were not
  60. // negotiated with the server, such as the cipher suite. It's implicitly assumed that
  61. // the server can support the selected parameters.
  62. //
  63. func NewObfuscatedClientSessionCache(sharedSecret [32]byte) ClientSessionCache {
  64. return &obfuscatedClientSessionCache{
  65. sharedSecret: sharedSecret,
  66. realTickets: NewLRUClientSessionCache(-1),
  67. }
  68. }
  69. type obfuscatedClientSessionCache struct {
  70. sharedSecret [32]byte
  71. realTickets ClientSessionCache
  72. }
  73. func (cache *obfuscatedClientSessionCache) Put(key string, state *ClientSessionState) {
  74. // When new, real session tickets are issued, use them.
  75. cache.realTickets.Put(key, state)
  76. }
  77. func (cache *obfuscatedClientSessionCache) Get(key string) (*ClientSessionState, bool) {
  78. clientSessionState, ok := cache.realTickets.Get(key)
  79. if ok {
  80. return clientSessionState, true
  81. }
  82. // Bootstrap with an obfuscated session ticket.
  83. clientSessionState, err := NewObfuscatedClientSessionState(cache.sharedSecret)
  84. if err != nil {
  85. // TODO: log error
  86. // This will fall back to regular TLS
  87. return nil, false
  88. }
  89. return clientSessionState, true
  90. }
  91. func NewObfuscatedClientSessionState(sharedSecret [32]byte) (*ClientSessionState, error) {
  92. // Pad golang TLS session ticket to a more typical size.
  93. paddingSize := 72
  94. randomInt, err := rand.Int(rand.Reader, big.NewInt(18))
  95. if err != nil {
  96. return nil, err
  97. }
  98. paddingSize += int(randomInt.Int64()) * 2
  99. // Create a session ticket that wasn't actually issued by the server.
  100. vers := uint16(VersionTLS12)
  101. cipherSuite := TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  102. masterSecret := make([]byte, masterSecretLength)
  103. _, err = rand.Read(masterSecret)
  104. if err != nil {
  105. return nil, err
  106. }
  107. serverState := &sessionState{
  108. vers: vers,
  109. cipherSuite: cipherSuite,
  110. masterSecret: masterSecret,
  111. certificates: nil,
  112. paddingSize: paddingSize,
  113. }
  114. c := &Conn{
  115. config: &Config{
  116. sessionTicketKeys: []ticketKey{ticketKeyFromBytes(sharedSecret)},
  117. },
  118. }
  119. sessionTicket, err := c.encryptTicket(serverState)
  120. if err != nil {
  121. return nil, err
  122. }
  123. // Pretend we got that session ticket from the server.
  124. clientState := &ClientSessionState{
  125. sessionTicket: sessionTicket,
  126. vers: vers,
  127. cipherSuite: cipherSuite,
  128. masterSecret: masterSecret,
  129. }
  130. return clientState, nil
  131. }