tlsCache.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. const TLS_NULL_SESSION_KEY = ""
  25. // TLSClientSessionCacheWrapper is a wrapper around tls.ClientSessionCache
  26. // that provides a hard-coded key for the cache.
  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. // WrapUtlsClientSessionCache wraps a tls.ClientSessionCache with an alternative
  33. // hard-coded session key, ignoring the SNI-based key that crypto/tls passes to Put/Get,
  34. // which may be incompatible with the SNI obfuscation transforms.
  35. // If the sessionKey is empty (TLS_NULL_SESSION_KEY), SetSessionKey has to be called
  36. // before using the cache.
  37. func WrapClientSessionCache(
  38. cache tls.ClientSessionCache,
  39. hardCodedSessionKey string,
  40. ) *TLSClientSessionCacheWrapper {
  41. return &TLSClientSessionCacheWrapper{
  42. ClientSessionCache: cache,
  43. sessionKey: hardCodedSessionKey,
  44. }
  45. }
  46. // Get retrieves the session from the cache using the hard-coded session key.
  47. func (c *TLSClientSessionCacheWrapper) Get(_ string) (session *tls.ClientSessionState, ok bool) {
  48. if c.sessionKey == "" {
  49. return nil, false
  50. }
  51. return c.ClientSessionCache.Get(c.sessionKey)
  52. }
  53. // Put stores the session in the cache using the hard-coded session key.
  54. func (c *TLSClientSessionCacheWrapper) Put(_ string, cs *tls.ClientSessionState) {
  55. if c.sessionKey == "" {
  56. return
  57. }
  58. cs.ResumptionState()
  59. c.ClientSessionCache.Put(c.sessionKey, cs)
  60. }
  61. // RemoveCacheEntry removes the cache entry for the hard-coded session key.
  62. func (c *TLSClientSessionCacheWrapper) RemoveCacheEntry() {
  63. if c.sessionKey == "" {
  64. return
  65. }
  66. c.ClientSessionCache.Put(c.sessionKey, nil)
  67. }
  68. // SetSessionKey sets the hard-coded session key if not already set.
  69. func (c *TLSClientSessionCacheWrapper) SetSessionKey(key string) {
  70. if c.sessionKey != TLS_NULL_SESSION_KEY {
  71. return
  72. }
  73. c.sessionKey = key
  74. }
  75. // UtlClientSessionCacheWrapper is a wrapper around utls.ClientSessionCache
  76. // that provides a hard-coded key for the cache.
  77. // It implements the TLSClientSessionCacheWrapper interface.
  78. type UtlsClientSessionCacheWrapper struct {
  79. utls.ClientSessionCache
  80. // sessionKey specifies the value of the hard-coded TLS session cache key.
  81. sessionKey string
  82. }
  83. // WrapUtlsClientSessionCache wraps a utls.ClientSessionCache with an alternative
  84. // hard-coded session key, ignoring the SNI-based key that crypto/tls passes to Put/Get,
  85. // which may be incompatible with the SNI obfuscation transforms.
  86. // If the sessionKey is empty (TLS_NULL_SESSION_KEY), SetSessionKey has to be called
  87. // before using the cache.
  88. func WrapUtlsClientSessionCache(
  89. cache utls.ClientSessionCache,
  90. hardCodedSessionKey string,
  91. ) *UtlsClientSessionCacheWrapper {
  92. return &UtlsClientSessionCacheWrapper{
  93. ClientSessionCache: cache,
  94. sessionKey: hardCodedSessionKey,
  95. }
  96. }
  97. // Get retrieves the session from the cache using the hard-coded session key.
  98. func (c *UtlsClientSessionCacheWrapper) Get(_ string) (session *utls.ClientSessionState, ok bool) {
  99. if c.sessionKey == "" {
  100. return nil, false
  101. }
  102. return c.ClientSessionCache.Get(c.sessionKey)
  103. }
  104. // Put stores the session in the cache using the hard-coded session key.
  105. func (c *UtlsClientSessionCacheWrapper) Put(_ string, cs *utls.ClientSessionState) {
  106. if c.sessionKey == "" {
  107. return
  108. }
  109. c.ClientSessionCache.Put(c.sessionKey, cs)
  110. }
  111. // RemoveCacheEntry removes the cache entry for the hard-coded session key.
  112. func (c *UtlsClientSessionCacheWrapper) RemoveCacheEntry() {
  113. if c.sessionKey != "" {
  114. c.ClientSessionCache.Put(c.sessionKey, nil)
  115. }
  116. }
  117. // SetSessionKey sets the hard-coded session key if not already set.
  118. // If the session key is already set, it does nothing.
  119. func (c *UtlsClientSessionCacheWrapper) SetSessionKey(key string) {
  120. if c.sessionKey != TLS_NULL_SESSION_KEY {
  121. return
  122. }
  123. c.sessionKey = key
  124. }