obfuscated.go 4.8 KB

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