tlsCache.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/refraction-networking/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 a hard-coded key
  33. // derived from the ipAddress and dialPortNumber.
  34. func WrapClientSessionCache(
  35. cache tls.ClientSessionCache,
  36. hardCodedSessionKey string,
  37. ) *TLSClientSessionCacheWrapper {
  38. return &TLSClientSessionCacheWrapper{
  39. ClientSessionCache: cache,
  40. sessionKey: hardCodedSessionKey,
  41. }
  42. }
  43. func (c *TLSClientSessionCacheWrapper) Get(_ string) (session *tls.ClientSessionState, ok bool) {
  44. return c.ClientSessionCache.Get(c.sessionKey)
  45. }
  46. func (c *TLSClientSessionCacheWrapper) Put(_ string, cs *tls.ClientSessionState) {
  47. c.ClientSessionCache.Put(c.sessionKey, cs)
  48. }
  49. func (c *TLSClientSessionCacheWrapper) IsSessionResumptionAvailable() bool {
  50. // Ignore the ok return value, as the session may still be till if ok is true.
  51. session, _ := c.Get(c.sessionKey)
  52. return session != nil
  53. }
  54. func (c *TLSClientSessionCacheWrapper) RemoveCacheEntry() {
  55. c.ClientSessionCache.Put(c.sessionKey, nil)
  56. }
  57. // UtlClientSessionCacheWrapper is a wrapper around utls.ClientSessionCache
  58. // that provides a hard-coded key for the cache.
  59. // It implements the TLSClientSessionCacheWrapper interface.
  60. type UtlsClientSessionCacheWrapper struct {
  61. utls.ClientSessionCache
  62. // sessionKey specifies the value of the hard-coded TLS session cache key.
  63. sessionKey string
  64. }
  65. // WrapUtlsClientSessionCache wraps a utls.ClientSessionCache with a hard-coded key
  66. // derived from the ipAddress and dialPortNumber.
  67. func WrapUtlsClientSessionCache(
  68. cache utls.ClientSessionCache,
  69. hardCodedSessionKey string,
  70. ) *UtlsClientSessionCacheWrapper {
  71. return &UtlsClientSessionCacheWrapper{
  72. ClientSessionCache: cache,
  73. sessionKey: hardCodedSessionKey,
  74. }
  75. }
  76. func (c *UtlsClientSessionCacheWrapper) Get(_ string) (session *utls.ClientSessionState, ok bool) {
  77. return c.ClientSessionCache.Get(c.sessionKey)
  78. }
  79. func (c *UtlsClientSessionCacheWrapper) Put(_ string, cs *utls.ClientSessionState) {
  80. c.ClientSessionCache.Put(c.sessionKey, cs)
  81. }
  82. func (c *UtlsClientSessionCacheWrapper) IsSessionResumptionAvailable() bool {
  83. // Ignore the ok return value, as the session may still be till if ok is true.
  84. session, _ := c.Get(c.sessionKey)
  85. return session != nil
  86. }
  87. func (c *UtlsClientSessionCacheWrapper) RemoveCacheEntry() {
  88. c.ClientSessionCache.Put(c.sessionKey, nil)
  89. }