tlsCache.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2024, 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 common
  20. import (
  21. tls "github.com/Psiphon-Labs/psiphon-tls"
  22. utls "github.com/Psiphon-Labs/utls"
  23. )
  24. // TLSClientSessionCacheWrapper is a wrapper around tls.ClientSessionCache
  25. // that provides a hard-coded key for the cache.
  26. // It implements the TLSClientSessionCacheWrapper interface.
  27. type TLSClientSessionCacheWrapper struct {
  28. tls.ClientSessionCache
  29. // sessionKey specifies the value of the hard-coded TLS session cache key.
  30. sessionKey string
  31. }
  32. // WrapClientSessionCache wraps a tls.ClientSessionCache with an alternative
  33. // key, ignoring the SNI-based key that crypto/tls passes to Put/Get, which
  34. // may be incompatible with SNI obfuscation transforms.
  35. func WrapClientSessionCache(
  36. cache tls.ClientSessionCache,
  37. hardCodedSessionKey string,
  38. ) *TLSClientSessionCacheWrapper {
  39. return &TLSClientSessionCacheWrapper{
  40. ClientSessionCache: cache,
  41. sessionKey: hardCodedSessionKey,
  42. }
  43. }
  44. func (c *TLSClientSessionCacheWrapper) Get(_ string) (session *tls.ClientSessionState, ok bool) {
  45. return c.ClientSessionCache.Get(c.sessionKey)
  46. }
  47. func (c *TLSClientSessionCacheWrapper) Put(_ string, cs *tls.ClientSessionState) {
  48. c.ClientSessionCache.Put(c.sessionKey, cs)
  49. }
  50. func (c *TLSClientSessionCacheWrapper) RemoveCacheEntry() {
  51. c.ClientSessionCache.Put(c.sessionKey, nil)
  52. }
  53. // UtlClientSessionCacheWrapper is a wrapper around utls.ClientSessionCache
  54. // that provides a hard-coded key for the cache.
  55. // It implements the TLSClientSessionCacheWrapper interface.
  56. type UtlsClientSessionCacheWrapper struct {
  57. utls.ClientSessionCache
  58. // sessionKey specifies the value of the hard-coded TLS session cache key.
  59. sessionKey string
  60. }
  61. // WrapUtlsClientSessionCache wraps a utls.ClientSessionCache with an alternative
  62. // key, ignoring the SNI-based key that crypto/tls passes to Put/Get, which
  63. // may be incompatible with SNI obfuscation transforms.
  64. func WrapUtlsClientSessionCache(
  65. cache utls.ClientSessionCache,
  66. hardCodedSessionKey string,
  67. ) *UtlsClientSessionCacheWrapper {
  68. return &UtlsClientSessionCacheWrapper{
  69. ClientSessionCache: cache,
  70. sessionKey: hardCodedSessionKey,
  71. }
  72. }
  73. func (c *UtlsClientSessionCacheWrapper) Get(_ string) (session *utls.ClientSessionState, ok bool) {
  74. return c.ClientSessionCache.Get(c.sessionKey)
  75. }
  76. func (c *UtlsClientSessionCacheWrapper) Put(_ string, cs *utls.ClientSessionState) {
  77. c.ClientSessionCache.Put(c.sessionKey, cs)
  78. }
  79. func (c *UtlsClientSessionCacheWrapper) RemoveCacheEntry() {
  80. c.ClientSessionCache.Put(c.sessionKey, nil)
  81. }