]> git.ipfire.org Git - thirdparty/squid.git/blob - src/security/Session.h
Move session tls-options= assignment out of CreateSession() function
[thirdparty/squid.git] / src / security / Session.h
1 /*
2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_SRC_SECURITY_SESSION_H
10 #define SQUID_SRC_SECURITY_SESSION_H
11
12 #include "base/HardFun.h"
13 #include "comm/forward.h"
14 #include "security/LockingPointer.h"
15
16 #include <memory>
17
18 #if USE_OPENSSL
19 #include "compat/openssl.h"
20 #if HAVE_OPENSSL_SSL_H
21 #include <openssl/ssl.h>
22 #endif
23 #endif
24
25 #if USE_GNUTLS
26 #if HAVE_GNUTLS_GNUTLS_H
27 #include <gnutls/gnutls.h>
28 #endif
29 #endif
30
31 namespace Security {
32
33 /// Creates TLS Client connection structure (aka 'session' state) and initializes TLS/SSL I/O (Comm and BIO).
34 /// On errors, emits DBG_IMPORTANT with details and returns false.
35 bool CreateClientSession(const Security::ContextPointer &, const Comm::ConnectionPointer &, const char *squidCtx);
36
37 class PeerOptions;
38
39 /// Creates TLS Server connection structure (aka 'session' state) and initializes TLS/SSL I/O (Comm and BIO).
40 /// On errors, emits DBG_IMPORTANT with details and returns false.
41 bool CreateServerSession(const Security::ContextPointer &, const Comm::ConnectionPointer &, Security::PeerOptions &, const char *squidCtx);
42
43 #if USE_OPENSSL
44 typedef std::shared_ptr<SSL> SessionPointer;
45
46 typedef std::unique_ptr<SSL_SESSION, HardFun<void, SSL_SESSION*, &SSL_SESSION_free>> SessionStatePointer;
47
48 #elif USE_GNUTLS
49 typedef std::shared_ptr<struct gnutls_session_int> SessionPointer;
50
51 // wrapper function to get around gnutls_free being a typedef
52 inline void squid_gnutls_free(void *d) {gnutls_free(d);}
53 typedef std::unique_ptr<gnutls_datum_t, HardFun<void, void*, &Security::squid_gnutls_free>> SessionStatePointer;
54
55 #else
56 typedef std::shared_ptr<void> SessionPointer;
57
58 typedef std::unique_ptr<int> SessionStatePointer;
59
60 #endif
61
62 /// send the shutdown/bye notice for an active TLS session.
63 void SessionSendGoodbye(const Security::SessionPointer &);
64
65 /// whether the session is a resumed one
66 bool SessionIsResumed(const Security::SessionPointer &);
67
68 /**
69 * When the session is not a resumed session, retrieve the details needed to
70 * resume a later connection and store them in 'data'. This may result in 'data'
71 * becoming a nil Pointer if no details exist or an error occurs.
72 *
73 * When the session is already a resumed session, do nothing and leave 'data'
74 * unhanged.
75 * XXX: is this latter behaviour always correct?
76 */
77 void MaybeGetSessionResumeData(const Security::SessionPointer &, Security::SessionStatePointer &data);
78
79 /// Set the data for resuming a previous session.
80 /// Needs to be done before using the SessionPointer for a handshake.
81 void SetSessionResumeData(const Security::SessionPointer &, const Security::SessionStatePointer &);
82
83 #if USE_OPENSSL
84 // TODO: remove from public API. It is only public because of Security::ServerOptions::updateContextConfig
85 /// Setup the given TLS context with callbacks used to manage the session cache
86 void SetSessionCacheCallbacks(Security::ContextPointer &);
87
88 /// Helper function to retrieve a (non-locked) ContextPointer from a SessionPointer
89 inline Security::ContextPointer
90 GetFrom(Security::SessionPointer &s)
91 {
92 auto *ctx = SSL_get_SSL_CTX(s.get());
93 return Security::ContextPointer(ctx, [](SSL_CTX *) {/* nothing to unlock/free */});
94 }
95
96 /// \deprecated use the PeerOptions/ServerOptions API methods instead.
97 /// Wraps SessionPointer value creation to reduce risk of
98 /// a nasty hack in ssl/support.cc.
99 Security::SessionPointer NewSessionObject(const Security::ContextPointer &);
100 #endif
101
102 } // namespace Security
103
104 #endif /* SQUID_SRC_SECURITY_SESSION_H */
105