]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/context_storage.h
merge from SslServerCertValidator r12332
[thirdparty/squid.git] / src / ssl / context_storage.h
1 #ifndef SQUID_SSL_CONTEXT_STORAGE_H
2 #define SQUID_SSL_CONTEXT_STORAGE_H
3
4 #if USE_SSL
5
6 #include "SquidTime.h"
7 #include "CacheManager.h"
8 #include "ip/Address.h"
9 #include "mgr/Action.h"
10 #include "mgr/Command.h"
11 #if HAVE_MAP
12 #include <map>
13 #endif
14 #if HAVE_LIST
15 #include <list>
16 #endif
17 #include <openssl/ssl.h>
18
19 /// TODO: Replace on real size.
20 #define SSL_CTX_SIZE 1024
21
22 namespace Ssl
23 {
24
25 /** Reports cached SSL certificate stats to Cache Manager.
26 * TODO: Use "Report" functions instead friend class.
27 */
28 class CertificateStorageAction : public Mgr::Action
29 {
30 public:
31 CertificateStorageAction(const Mgr::Command::Pointer &cmd);
32 static Pointer Create(const Mgr::Command::Pointer &cmd);
33 virtual void dump (StoreEntry *sentry);
34 /**
35 * We do not support aggregation of information across workers
36 * TODO: aggregate these stats
37 */
38 virtual bool aggregatable() const { return false; }
39 };
40
41 /**
42 * Memory cache for store generated SSL context. Enforces total size limits
43 * using an LRU algorithm.
44 */
45 class LocalContextStorage
46 {
47 friend class CertificateStorageAction;
48 public:
49 /// Cache item is an (SSL_CTX, host name) tuple.
50 class Item
51 {
52 public:
53 Item(SSL_CTX * aSsl_ctx, std::string const & aName);
54 ~Item();
55 public:
56 SSL_CTX * ssl_ctx; ///< The SSL context.
57 std::string host_name; ///< The host name of the SSL context.
58 };
59
60 typedef std::list<Item *> Queue;
61 typedef Queue::iterator QueueIterator;
62
63 /// host_name:queue_item mapping for fast lookups by host name
64 typedef std::map<std::string, QueueIterator> Map;
65 typedef Map::iterator MapIterator;
66 typedef std::pair<std::string, QueueIterator> MapPair;
67
68 LocalContextStorage(size_t aMax_memory);
69 ~LocalContextStorage();
70 /// Set maximum memory size for this storage.
71 void SetSize(size_t aMax_memory);
72 /// Return a pointer to the added ssl_ctx or NULL if fails (eg. max cache size equal 0).
73 SSL_CTX * add(char const * host_name, SSL_CTX * ssl_ctx);
74 /// Find SSL_CTX in storage by host name. Lru queue will be updated.
75 SSL_CTX * find(char const * host_name);
76 void remove(char const * host_name); ///< Delete the SSL context by hostname
77
78 private:
79 void purgeOne(); ///< Delete oldest object.
80 /// Delete object by iterator. It is used in deletePurge() and remove(...) methods.
81 void deleteAt(MapIterator i);
82
83 size_t max_memory; ///< Max cache size.
84 size_t memory_used; ///< Used cache size.
85 Map storage; ///< The hostnames/SSL_CTX * pairs
86 Queue lru_queue; ///< LRU cache index
87 };
88
89 /// Class for storing/manipulating LocalContextStorage per local listening address/port.
90 class GlobalContextStorage
91 {
92 friend class CertificateStorageAction;
93 public:
94 GlobalContextStorage();
95 ~GlobalContextStorage();
96 /// Create new SSL context storage for the local listening address/port.
97 void addLocalStorage(Ip::Address const & address, size_t size_of_store);
98 /// Return the local storage for the given listening address/port.
99 LocalContextStorage & getLocalStorage(Ip::Address const & address);
100 /// When reconfigring should be called this method.
101 void reconfigureStart();
102 private:
103 /// Called by getLocalStorage method
104 void reconfigureFinish();
105 bool reconfiguring; ///< True if system reconfiguring now.
106 /// Storage used on configure or reconfigure.
107 std::map<Ip::Address, size_t> configureStorage;
108 /// Map for storing all local ip address and their local storages.
109 std::map<Ip::Address, LocalContextStorage *> storage;
110 };
111
112 /// Global cache for store all SSL server certificates.
113 extern GlobalContextStorage TheGlobalContextStorage;
114 } //namespace Ssl
115 #endif // USE_SSL
116
117 #endif // SQUID_SSL_CONTEXT_STORAGE_H