passthrough.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2020, 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 obfuscator
  20. import (
  21. "crypto/hmac"
  22. "crypto/rand"
  23. "crypto/sha256"
  24. "crypto/subtle"
  25. "encoding/binary"
  26. "io"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. "golang.org/x/crypto/hkdf"
  30. )
  31. const (
  32. TLS_PASSTHROUGH_NONCE_SIZE = 16
  33. TLS_PASSTHROUGH_KEY_SIZE = 32
  34. TLS_PASSTHROUGH_TIME_PERIOD = 20 * time.Minute
  35. TLS_PASSTHROUGH_MESSAGE_SIZE = 32
  36. )
  37. // MakeTLSPassthroughMessage generates a unique TLS passthrough message
  38. // using the passthrough key derived from a master obfuscated key.
  39. //
  40. // The passthrough message demonstrates knowledge of the obfuscated key.
  41. // When useTimeFactor is set, the message will also reflect the current
  42. // time period, limiting how long it remains valid.
  43. //
  44. // The configurable useTimeFactor enables support for legacy clients and
  45. // servers which don't use the time factor.
  46. func MakeTLSPassthroughMessage(
  47. useTimeFactor bool, obfuscatedKey string) ([]byte, error) {
  48. passthroughKey, err := derivePassthroughKey(useTimeFactor, obfuscatedKey)
  49. if err != nil {
  50. return nil, errors.Trace(err)
  51. }
  52. message := make([]byte, TLS_PASSTHROUGH_MESSAGE_SIZE)
  53. _, err = rand.Read(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  54. if err != nil {
  55. return nil, errors.Trace(err)
  56. }
  57. h := hmac.New(sha256.New, passthroughKey)
  58. h.Write(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  59. copy(message[TLS_PASSTHROUGH_NONCE_SIZE:], h.Sum(nil))
  60. return message, nil
  61. }
  62. // VerifyTLSPassthroughMessage checks that the specified passthrough message
  63. // was generated using the passthrough key.
  64. //
  65. // useTimeFactor must be set to the same value used in
  66. // MakeTLSPassthroughMessage.
  67. func VerifyTLSPassthroughMessage(
  68. useTimeFactor bool, obfuscatedKey string, message []byte) bool {
  69. // If the message is the wrong length, continue processing with a stub
  70. // message of the correct length. This is to avoid leaking the existence of
  71. // passthrough via timing differences.
  72. if len(message) != TLS_PASSTHROUGH_MESSAGE_SIZE {
  73. var stub [TLS_PASSTHROUGH_MESSAGE_SIZE]byte
  74. message = stub[:]
  75. }
  76. passthroughKey, err := derivePassthroughKey(useTimeFactor, obfuscatedKey)
  77. if err != nil {
  78. // TODO: log error
  79. return false
  80. }
  81. h := hmac.New(sha256.New, passthroughKey)
  82. h.Write(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  83. return 1 == subtle.ConstantTimeCompare(
  84. message[TLS_PASSTHROUGH_NONCE_SIZE:],
  85. h.Sum(nil)[0:TLS_PASSTHROUGH_MESSAGE_SIZE-TLS_PASSTHROUGH_NONCE_SIZE])
  86. }
  87. // timePeriodSeconds is variable, to enable overriding the value in
  88. // TestTLSPassthrough. This value should not be overridden outside of test
  89. // cases.
  90. var timePeriodSeconds = int64(TLS_PASSTHROUGH_TIME_PERIOD / time.Second)
  91. func derivePassthroughKey(
  92. useTimeFactor bool, obfuscatedKey string) ([]byte, error) {
  93. secret := []byte(obfuscatedKey)
  94. salt := []byte("passthrough-obfuscation-key")
  95. if useTimeFactor {
  96. // Include a time factor, so messages created with this key remain valid
  97. // only for a limited time period. The current time is rounded, allowing the
  98. // client clock to be slightly ahead of or behind of the server clock.
  99. //
  100. // This time factor mechanism is used in concert with SeedHistory to detect
  101. // passthrough message replay. SeedHistory, a history of recent passthrough
  102. // messages, is used to detect duplicate passthrough messages. The time
  103. // factor bounds the necessary history length: passthrough messages older
  104. // than the time period no longer need to be retained in history.
  105. //
  106. // We _always_ derive the passthrough key for each
  107. // MakeTLSPassthroughMessage, even for multiple calls in the same time
  108. // factor period, to avoid leaking the presense of passthough via timing
  109. // differences at time boundaries. We assume that the server always or never
  110. // sets useTimeFactor.
  111. roundedTimePeriod := (time.Now().Unix() + (timePeriodSeconds / 2)) / timePeriodSeconds
  112. var timeFactor [8]byte
  113. binary.LittleEndian.PutUint64(timeFactor[:], uint64(roundedTimePeriod))
  114. salt = append(salt, timeFactor[:]...)
  115. }
  116. key := make([]byte, TLS_PASSTHROUGH_KEY_SIZE)
  117. _, err := io.ReadFull(hkdf.New(sha256.New, secret, salt, nil), key)
  118. if err != nil {
  119. return nil, errors.Trace(err)
  120. }
  121. return key, nil
  122. }