obfuscated.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. )
  23. // Obfuscated Session Tickets
  24. //
  25. // Obfuscated session tickets is a network traffic obfuscation protocol that appears
  26. // to be valid TLS using session tickets. The client actually generates the session
  27. // ticket and encrypts it with a shared secret, enabling a TLS session that entirely
  28. // skips the most fingerprintable aspects of TLS.
  29. // The scheme is described here:
  30. // https://lists.torproject.org/pipermail/tor-dev/2016-September/011354.html
  31. //
  32. // Circumvention notes:
  33. // - TLS session ticket implementations are widespread:
  34. // https://istlsfastyet.com/#cdn-paas.
  35. // - An adversary cannot easily block session ticket capability, as this requires
  36. // a downgrade attack against TLS.
  37. // - Anti-probing defence is provided, as the adversary must use the correct obfuscation
  38. // shared secret to form valid obfuscation session ticket; otherwise server offers
  39. // standard session tickets.
  40. // - Limitation: TLS protocol and session ticket size correspond to golang implementation
  41. // and not more common OpenSSL.
  42. // - Limitation: an adversary with the obfuscation shared secret can decrypt the session
  43. // ticket and observe the plaintext traffic. It's assumed that the adversary will not
  44. // learn the obfuscated shared secret without also learning the address of the TLS
  45. // server and blocking it anyway; it's also assumed that the TLS payload is not
  46. // plaintext but is protected with some other security layer (e.g., SSH).
  47. //
  48. // Implementation notes:
  49. // - Client should set its ClientSessionCache to a NewObfuscatedTLSClientSessionCache.
  50. // This cache ignores the session key and always produces obfuscated session tickets.
  51. // - The TLS ClientHello includes an SNI field, even when using session tickets, so
  52. // the client should populate the ServerName.
  53. // - Server should set its SetSessionTicketKeys with first a standard key, followed by
  54. // the obfuscation shared secret.
  55. // - Since the client creates the session ticket, it selects parameters that were not
  56. // negotiated with the server, such as the cipher suite. It's implicitly assumed that
  57. // the server can support the selected parameters.
  58. //
  59. func NewObfuscatedClientSessionCache(sharedSecret [32]byte) ClientSessionCache {
  60. return &obfuscatedClientSessionCache{
  61. sharedSecret: sharedSecret,
  62. realTickets: NewLRUClientSessionCache(-1),
  63. }
  64. }
  65. type obfuscatedClientSessionCache struct {
  66. sharedSecret [32]byte
  67. realTickets ClientSessionCache
  68. }
  69. func (cache *obfuscatedClientSessionCache) Put(key string, state *ClientSessionState) {
  70. // When new, real session tickets are issued, use them.
  71. cache.realTickets.Put(key, state)
  72. }
  73. func (cache *obfuscatedClientSessionCache) Get(key string) (*ClientSessionState, bool) {
  74. clientSessionState, ok := cache.realTickets.Get(key)
  75. if ok {
  76. return clientSessionState, true
  77. }
  78. // Bootstrap with an obfuscated session ticket.
  79. clientSessionState, err := newObfuscatedClientSessionState(cache.sharedSecret)
  80. if err != nil {
  81. // TODO: log error
  82. // This will fall back to regular TLS
  83. return nil, false
  84. }
  85. return clientSessionState, true
  86. }
  87. func newObfuscatedClientSessionState(sharedSecret [32]byte) (*ClientSessionState, error) {
  88. // Create a session ticket that wasn't actually issued by the server.
  89. vers := uint16(VersionTLS12)
  90. cipherSuite := TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  91. masterSecret := make([]byte, masterSecretLength)
  92. _, err := rand.Read(masterSecret)
  93. if err != nil {
  94. return nil, err
  95. }
  96. serverState := &sessionState{
  97. vers: vers,
  98. cipherSuite: cipherSuite,
  99. masterSecret: masterSecret,
  100. certificates: nil,
  101. }
  102. c := &Conn{
  103. config: &Config{
  104. sessionTicketKeys: []ticketKey{ticketKeyFromBytes(sharedSecret)},
  105. },
  106. }
  107. sessionTicket, err := c.encryptTicket(serverState)
  108. if err != nil {
  109. return nil, err
  110. }
  111. // Pretend we got that session ticket from the server.
  112. clientState := &ClientSessionState{
  113. sessionTicket: sessionTicket,
  114. vers: vers,
  115. cipherSuite: cipherSuite,
  116. masterSecret: masterSecret,
  117. }
  118. return clientState, nil
  119. }