cache.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2022 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls
  5. import (
  6. "crypto/x509"
  7. "runtime"
  8. "sync"
  9. "sync/atomic"
  10. )
  11. type cacheEntry struct {
  12. refs atomic.Int64
  13. cert *x509.Certificate
  14. }
  15. // certCache implements an intern table for reference counted x509.Certificates,
  16. // implemented in a similar fashion to BoringSSL's CRYPTO_BUFFER_POOL. This
  17. // allows for a single x509.Certificate to be kept in memory and referenced from
  18. // multiple Conns. Returned references should not be mutated by callers. Certificates
  19. // are still safe to use after they are removed from the cache.
  20. //
  21. // Certificates are returned wrapped in a activeCert struct that should be held by
  22. // the caller. When references to the activeCert are freed, the number of references
  23. // to the certificate in the cache is decremented. Once the number of references
  24. // reaches zero, the entry is evicted from the cache.
  25. //
  26. // The main difference between this implementation and CRYPTO_BUFFER_POOL is that
  27. // CRYPTO_BUFFER_POOL is a more generic structure which supports blobs of data,
  28. // rather than specific structures. Since we only care about x509.Certificates,
  29. // certCache is implemented as a specific cache, rather than a generic one.
  30. //
  31. // See https://boringssl.googlesource.com/boringssl/+/master/include/openssl/pool.h
  32. // and https://boringssl.googlesource.com/boringssl/+/master/crypto/pool/pool.c
  33. // for the BoringSSL reference.
  34. type certCache struct {
  35. sync.Map
  36. }
  37. var clientCertCache = new(certCache)
  38. // activeCert is a handle to a certificate held in the cache. Once there are
  39. // no alive activeCerts for a given certificate, the certificate is removed
  40. // from the cache by a finalizer.
  41. type activeCert struct {
  42. cert *x509.Certificate
  43. }
  44. // active increments the number of references to the entry, wraps the
  45. // certificate in the entry in a activeCert, and sets the finalizer.
  46. //
  47. // Note that there is a race between active and the finalizer set on the
  48. // returned activeCert, triggered if active is called after the ref count is
  49. // decremented such that refs may be > 0 when evict is called. We consider this
  50. // safe, since the caller holding an activeCert for an entry that is no longer
  51. // in the cache is fine, with the only side effect being the memory overhead of
  52. // there being more than one distinct reference to a certificate alive at once.
  53. func (cc *certCache) active(e *cacheEntry) *activeCert {
  54. e.refs.Add(1)
  55. a := &activeCert{e.cert}
  56. runtime.SetFinalizer(a, func(_ *activeCert) {
  57. if e.refs.Add(-1) == 0 {
  58. cc.evict(e)
  59. }
  60. })
  61. return a
  62. }
  63. // evict removes a cacheEntry from the cache.
  64. func (cc *certCache) evict(e *cacheEntry) {
  65. cc.Delete(string(e.cert.Raw))
  66. }
  67. // newCert returns a x509.Certificate parsed from der. If there is already a copy
  68. // of the certificate in the cache, a reference to the existing certificate will
  69. // be returned. Otherwise, a fresh certificate will be added to the cache, and
  70. // the reference returned. The returned reference should not be mutated.
  71. func (cc *certCache) newCert(der []byte) (*activeCert, error) {
  72. if entry, ok := cc.Load(string(der)); ok {
  73. return cc.active(entry.(*cacheEntry)), nil
  74. }
  75. cert, err := x509.ParseCertificate(der)
  76. if err != nil {
  77. return nil, err
  78. }
  79. entry := &cacheEntry{cert: cert}
  80. if entry, loaded := cc.LoadOrStore(string(der), entry); loaded {
  81. return cc.active(entry.(*cacheEntry)), nil
  82. }
  83. return cc.active(entry), nil
  84. }