]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ssl.h
017502fa90eec67ec5c227e7ae9f001894c8bd90
[user/henk/code/inspircd.git] / include / modules / ssl.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
6  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2013, 2015-2016 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
10  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #pragma once
29
30 #include <string>
31 #include "iohook.h"
32
33 /** ssl_cert is a class which abstracts TLS (SSL) certificate
34  * and key information.
35  *
36  * Because gnutls and openssl represent key information in
37  * wildly different ways, this class allows it to be accessed
38  * in a unified manner. These classes are attached to ssl-
39  * connected local users using SSLCertExt
40  */
41 class ssl_cert : public refcountbase
42 {
43  public:
44         std::string dn;
45         std::string issuer;
46         std::string error;
47         std::string fingerprint;
48         bool trusted, invalid, unknownsigner, revoked;
49
50         ssl_cert() : trusted(false), invalid(true), unknownsigner(true), revoked(false) {}
51
52         /** Get certificate distinguished name
53          * @return Certificate DN
54          */
55         const std::string& GetDN()
56         {
57                 return dn;
58         }
59
60         /** Get Certificate issuer
61          * @return Certificate issuer
62          */
63         const std::string& GetIssuer()
64         {
65                 return issuer;
66         }
67
68         /** Get error string if an error has occurred
69          * @return The error associated with this users certificate,
70          * or an empty string if there is no error.
71          */
72         const std::string& GetError()
73         {
74                 return error;
75         }
76
77         /** Get key fingerprint.
78          * @return The key fingerprint as a hex string.
79          */
80         const std::string& GetFingerprint()
81         {
82                 return fingerprint;
83         }
84
85         /** Get trust status
86          * @return True if this is a trusted certificate
87          * (the certificate chain validates)
88          */
89         bool IsTrusted()
90         {
91                 return trusted;
92         }
93
94         /** Get validity status
95          * @return True if the certificate itself is
96          * correctly formed.
97          */
98         bool IsInvalid()
99         {
100                 return invalid;
101         }
102
103         /** Get signer status
104          * @return True if the certificate appears to be
105          * self-signed.
106          */
107         bool IsUnknownSigner()
108         {
109                 return unknownsigner;
110         }
111
112         /** Get revokation status.
113          * @return True if the certificate is revoked.
114          * Note that this only works properly for GnuTLS
115          * right now.
116          */
117         bool IsRevoked()
118         {
119                 return revoked;
120         }
121
122         /** Get certificate usability
123         * @return True if the certificate is not expired nor revoked
124         */
125         bool IsUsable()
126         {
127                 return !invalid && !revoked && error.empty();
128         }
129
130         /** Get CA trust status
131         * @return True if the certificate is issued by a CA
132         * and valid.
133         */
134         bool IsCAVerified()
135         {
136                 return IsUsable() && trusted && !unknownsigner;
137         }
138
139         std::string GetMetaLine()
140         {
141                 std::stringstream value;
142                 bool hasError = !error.empty();
143                 value << (IsInvalid() ? "v" : "V") << (IsTrusted() ? "T" : "t") << (IsRevoked() ? "R" : "r")
144                         << (IsUnknownSigner() ? "s" : "S") << (hasError ? "E" : "e") << " ";
145                 if (hasError)
146                         value << GetError();
147                 else
148                         value << GetFingerprint() << " " << GetDN() << " " << GetIssuer();
149                 return value.str();
150         }
151 };
152
153 class SSLIOHook : public IOHook
154 {
155  protected:
156         /** Peer TLS (SSL) certificate, set by the TLS (SSL) module
157          */
158         reference<ssl_cert> certificate;
159
160         /** Reduce elements in a send queue by appending later elements to the first element until there are no more
161          * elements to append or a desired length is reached
162          * @param sendq SendQ to work on
163          * @param targetsize Target size of the front element
164          */
165         static void FlattenSendQueue(StreamSocket::SendQueue& sendq, size_t targetsize)
166         {
167                 if ((sendq.size() <= 1) || (sendq.front().length() >= targetsize))
168                         return;
169
170                 // Avoid multiple repeated TLS (SSL) encryption invocations
171                 // This adds a single copy of the queue, but avoids
172                 // much more overhead in terms of system calls invoked
173                 // by an IOHook.
174                 std::string tmp;
175                 tmp.reserve(std::min(targetsize, sendq.bytes())+1);
176                 do
177                 {
178                         tmp.append(sendq.front());
179                         sendq.pop_front();
180                 }
181                 while (!sendq.empty() && tmp.length() < targetsize);
182                 sendq.push_front(tmp);
183         }
184
185  public:
186         static SSLIOHook* IsSSL(StreamSocket* sock)
187         {
188                 IOHook* const iohook = sock->GetIOHook();
189                 if ((iohook) && ((iohook->prov->type == IOHookProvider::IOH_SSL)))
190                         return static_cast<SSLIOHook*>(iohook);
191                 return NULL;
192         }
193
194         SSLIOHook(IOHookProvider* hookprov)
195                 : IOHook(hookprov)
196         {
197         }
198
199         /**
200          * Get the certificate sent by this peer
201          * @return The TLS (SSL) certificate sent by the peer, NULL if no cert was sent
202          */
203         virtual ssl_cert* GetCertificate() const
204         {
205                 return certificate;
206         }
207
208         /**
209          * Get the fingerprint of the peer's certificate
210          * @return The fingerprint of the TLS (SSL) client certificate sent by the peer,
211          * empty if no cert was sent
212          */
213         virtual std::string GetFingerprint() const
214         {
215                 ssl_cert* cert = GetCertificate();
216                 if (cert && cert->IsUsable())
217                         return cert->GetFingerprint();
218                 return "";
219         }
220
221         /**
222          * Get the ciphersuite negotiated with the peer
223          * @param out String where the ciphersuite string will be appended to
224          */
225         virtual void GetCiphersuite(std::string& out) const = 0;
226
227
228         /** Retrieves the name of the TLS (SSL) connection which is sent via SNI.
229          * @param out String that the server name will be appended to.
230          * returns True if the server name was retrieved; otherwise, false.
231          */
232         virtual bool GetServerName(std::string& out) const = 0;
233 };
234
235 /** Helper functions for obtaining TLS (SSL) client certificates and key fingerprints
236  * from StreamSockets
237  */
238 class SSLClientCert
239 {
240  public:
241         /**
242          * Get the client certificate from a socket
243          * @param sock The socket to get the certificate from, the socket does not have to use TLS (SSL)
244          * @return The TLS (SSL) client certificate information, NULL if the peer is not using TLS (SSL)
245          */
246         static ssl_cert* GetCertificate(StreamSocket* sock)
247         {
248                 SSLIOHook* ssliohook = SSLIOHook::IsSSL(sock);
249                 if (!ssliohook)
250                         return NULL;
251
252                 return ssliohook->GetCertificate();
253         }
254
255         /**
256          * Get the fingerprint of a client certificate from a socket
257          * @param sock The socket to get the certificate fingerprint from, the
258          * socket does not have to use TLS (SSL)
259          * @return The key fingerprint from the TLS (SSL) certificate sent by the peer,
260          * empty if no cert was sent or the peer is not using TLS (SSL)
261          */
262         static std::string GetFingerprint(StreamSocket* sock)
263         {
264                 ssl_cert* cert = SSLClientCert::GetCertificate(sock);
265                 if (cert)
266                         return cert->GetFingerprint();
267                 return "";
268         }
269 };
270
271 class UserCertificateAPIBase : public DataProvider
272 {
273  public:
274         UserCertificateAPIBase(Module* parent)
275                 : DataProvider(parent, "m_sslinfo_api")
276         {
277         }
278
279         /** Get the TLS (SSL) certificate of a user
280          * @param user The user whose certificate to get, user may be remote
281          * @return The TLS (SSL) certificate of the user or NULL if the user is not using TLS (SSL)
282          */
283         virtual ssl_cert* GetCertificate(User* user) = 0;
284
285         /** Set the TLS (SSL) certificate of a user.
286          * @param user The user whose certificate to set.
287          * @param cert The TLS (SSL) certificate to set for the user.
288          */
289         virtual void SetCertificate(User* user, ssl_cert* cert) = 0;
290
291         /** Get the key fingerprint from a user's certificate
292          * @param user The user whose key fingerprint to get, user may be remote
293          * @return The key fingerprint from the user's TLS (SSL) certificate or an empty string
294          * if the user is not using TLS (SSL) or did not provide a client certificate
295          */
296         std::string GetFingerprint(User* user)
297         {
298                 ssl_cert* cert = GetCertificate(user);
299                 if (cert)
300                         return cert->GetFingerprint();
301                 return "";
302         }
303 };
304
305 /** API implemented by m_sslinfo that allows modules to retrieve the TLS (SSL) certificate
306  * information of local and remote users. It can also be used to find out whether a
307  * user is using TLS (SSL) or not.
308  */
309 class UserCertificateAPI : public dynamic_reference<UserCertificateAPIBase>
310 {
311  public:
312         UserCertificateAPI(Module* parent)
313                 : dynamic_reference<UserCertificateAPIBase>(parent, "m_sslinfo_api")
314         {
315         }
316 };