tls_cache.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "fmt"
  22. tls "github.com/Psiphon-Labs/psiphon-tls"
  23. utls "github.com/refraction-networking/utls"
  24. )
  25. // TlsClientSessionCacheWrapper is a wrapper around tls.ClientSessionCache
  26. // that provides a hard-coded key for the cache.
  27. // It implements the TLSClientSessionCacheWrapper interface.
  28. type TlsClientSessionCacheWrapper struct {
  29. tls.ClientSessionCache
  30. // sessinoKey specifies the value of the hard-coded TLS session cache key.
  31. sessionKey string
  32. }
  33. // WrapClientSessionCache wraps a tls.ClientSessionCache with a hard-coded key
  34. // derived from the ipAddress and dialPortNumber.
  35. func WrapClientSessionCache(
  36. cache tls.ClientSessionCache,
  37. ipAddress string,
  38. dialPortNumber int) *TlsClientSessionCacheWrapper {
  39. return &TlsClientSessionCacheWrapper{
  40. ClientSessionCache: cache,
  41. sessionKey: sessionKey(ipAddress, dialPortNumber),
  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) IsSessionResumptionAvailable() bool {
  51. // Ignore the ok return value, as the session may still be till if ok is true.
  52. session, _ := c.Get(c.sessionKey)
  53. return session != nil
  54. }
  55. func (c *TlsClientSessionCacheWrapper) RemoveCacheEntry() {
  56. c.ClientSessionCache.Put(c.sessionKey, nil)
  57. }
  58. // UtlClientSessionCacheWrapper is a wrapper around utls.ClientSessionCache
  59. // that provides a hard-coded key for the cache.
  60. // It implements the TLSClientSessionCacheWrapper interface.
  61. type UtlsClientSessionCacheWrapper struct {
  62. utls.ClientSessionCache
  63. // sessinoKey specifies the value of the hard-coded TLS session cache key.
  64. sessionKey string
  65. }
  66. // WrapUtlsClientSessionCache wraps a utls.ClientSessionCache with a hard-coded key
  67. // derived from the ipAddress and dialPortNumber.
  68. func WrapUtlsClientSessionCache(
  69. cache utls.ClientSessionCache,
  70. ipAddress string,
  71. dialPortNumber int) *UtlsClientSessionCacheWrapper {
  72. return &UtlsClientSessionCacheWrapper{
  73. ClientSessionCache: cache,
  74. sessionKey: sessionKey(ipAddress, dialPortNumber),
  75. }
  76. }
  77. func (c *UtlsClientSessionCacheWrapper) Get(_ string) (session *utls.ClientSessionState, ok bool) {
  78. return c.ClientSessionCache.Get(c.sessionKey)
  79. }
  80. func (c *UtlsClientSessionCacheWrapper) Put(_ string, cs *utls.ClientSessionState) {
  81. c.ClientSessionCache.Put(c.sessionKey, cs)
  82. }
  83. func (c *UtlsClientSessionCacheWrapper) IsSessionResumptionAvailable() bool {
  84. // Ignore the ok return value, as the session may still be till if ok is true.
  85. session, _ := c.Get(c.sessionKey)
  86. return session != nil
  87. }
  88. func (c *UtlsClientSessionCacheWrapper) RemoveCacheEntry() {
  89. c.ClientSessionCache.Put(c.sessionKey, nil)
  90. }
  91. func sessionKey(ipAddress string, dialPortNumber int) string {
  92. return fmt.Sprintf("%s:%d", ipAddress, dialPortNumber)
  93. }