obfuscated.go 5.0 KB

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