passthrough.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "io"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  27. "golang.org/x/crypto/hkdf"
  28. )
  29. const (
  30. TLS_PASSTHROUGH_NONCE_SIZE = 16
  31. TLS_PASSTHROUGH_KEY_SIZE = 32
  32. TLS_PASSTHROUGH_MESSAGE_SIZE = 32
  33. )
  34. // DeriveTLSPassthroughKey derives a TLS passthrough key from a master
  35. // obfuscated key. The resulting key can be cached and passed to
  36. // VerifyTLSPassthroughMessage.
  37. func DeriveTLSPassthroughKey(obfuscatedKey string) ([]byte, error) {
  38. secret := []byte(obfuscatedKey)
  39. salt := []byte("passthrough-obfuscation-key")
  40. key := make([]byte, TLS_PASSTHROUGH_KEY_SIZE)
  41. _, err := io.ReadFull(hkdf.New(sha256.New, secret, salt, nil), key)
  42. if err != nil {
  43. return nil, errors.Trace(err)
  44. }
  45. return key, nil
  46. }
  47. // MakeTLSPassthroughMessage generates a unique TLS passthrough message
  48. // using the passthrough key derived from a master obfuscated key.
  49. //
  50. // The passthrough message demonstrates knowledge of the obfuscated key.
  51. func MakeTLSPassthroughMessage(obfuscatedKey string) ([]byte, error) {
  52. passthroughKey, err := DeriveTLSPassthroughKey(obfuscatedKey)
  53. if err != nil {
  54. return nil, errors.Trace(err)
  55. }
  56. message := make([]byte, TLS_PASSTHROUGH_MESSAGE_SIZE)
  57. _, err = rand.Read(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  58. if err != nil {
  59. return nil, errors.Trace(err)
  60. }
  61. h := hmac.New(sha256.New, passthroughKey)
  62. h.Write(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  63. copy(message[TLS_PASSTHROUGH_NONCE_SIZE:], h.Sum(nil))
  64. return message, nil
  65. }
  66. // VerifyTLSPassthroughMessage checks that the specified passthrough message
  67. // was generated using the passthrough key.
  68. func VerifyTLSPassthroughMessage(passthroughKey, message []byte) bool {
  69. if len(message) != TLS_PASSTHROUGH_MESSAGE_SIZE {
  70. return false
  71. }
  72. h := hmac.New(sha256.New, passthroughKey)
  73. h.Write(message[0:TLS_PASSTHROUGH_NONCE_SIZE])
  74. return 1 == subtle.ConstantTimeCompare(
  75. message[TLS_PASSTHROUGH_NONCE_SIZE:],
  76. h.Sum(nil)[0:TLS_PASSTHROUGH_MESSAGE_SIZE-TLS_PASSTHROUGH_NONCE_SIZE])
  77. }