2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** ssl_cert is a class which abstracts SSL certificate
27 * and key information.
29 * Because gnutls and openssl represent key information in
30 * wildly different ways, this class allows it to be accessed
31 * in a unified manner. These classes are attached to ssl-
32 * connected local users using SSLCertExt
34 class ssl_cert : public refcountbase
40 std::string fingerprint;
41 bool trusted, invalid, unknownsigner, revoked;
43 ssl_cert() : trusted(false), invalid(true), unknownsigner(true), revoked(false) {}
45 /** Get certificate distinguished name
46 * @return Certificate DN
48 const std::string& GetDN()
53 /** Get Certificate issuer
54 * @return Certificate issuer
56 const std::string& GetIssuer()
61 /** Get error string if an error has occured
62 * @return The error associated with this users certificate,
63 * or an empty string if there is no error.
65 const std::string& GetError()
70 /** Get key fingerprint.
71 * @return The key fingerprint as a hex string.
73 const std::string& GetFingerprint()
79 * @return True if this is a trusted certificate
80 * (the certificate chain validates)
87 /** Get validity status
88 * @return True if the certificate itself is
97 * @return True if the certificate appears to be
100 bool IsUnknownSigner()
102 return unknownsigner;
105 /** Get revokation status.
106 * @return True if the certificate is revoked.
107 * Note that this only works properly for GnuTLS
115 /** Get certificate usability
116 * @return True if the certificate is not expired nor revoked
120 return !invalid && !revoked && error.empty();
123 /** Get CA trust status
124 * @return True if the certificate is issued by a CA
129 return IsUsable() && trusted && !unknownsigner;
132 std::string GetMetaLine()
134 std::stringstream value;
135 bool hasError = !error.empty();
136 value << (IsInvalid() ? "v" : "V") << (IsTrusted() ? "T" : "t") << (IsRevoked() ? "R" : "r")
137 << (IsUnknownSigner() ? "s" : "S") << (hasError ? "E" : "e") << " ";
141 value << GetFingerprint() << " " << GetDN() << " " << GetIssuer();
146 class SSLIOHook : public IOHook
149 /** Peer SSL certificate, set by the SSL module
151 reference<ssl_cert> certificate;
153 /** Reduce elements in a send queue by appending later elements to the first element until there are no more
154 * elements to append or a desired length is reached
155 * @param sendq SendQ to work on
156 * @param targetsize Target size of the front element
158 static void FlattenSendQueue(StreamSocket::SendQueue& sendq, size_t targetsize)
160 if ((sendq.size() <= 1) || (sendq.front().length() >= targetsize))
163 // Avoid multiple repeated SSL encryption invocations
164 // This adds a single copy of the queue, but avoids
165 // much more overhead in terms of system calls invoked
168 tmp.reserve(std::min(targetsize, sendq.bytes())+1);
171 tmp.append(sendq.front());
174 while (!sendq.empty() && tmp.length() < targetsize);
175 sendq.push_front(tmp);
179 static SSLIOHook* IsSSL(StreamSocket* sock)
181 IOHook* const iohook = sock->GetIOHook();
182 if ((iohook) && ((iohook->prov->type == IOHookProvider::IOH_SSL)))
183 return static_cast<SSLIOHook*>(iohook);
187 SSLIOHook(IOHookProvider* hookprov)
193 * Get the certificate sent by this peer
194 * @return The SSL certificate sent by the peer, NULL if no cert was sent
196 virtual ssl_cert* GetCertificate() const
202 * Get the fingerprint of the peer's certificate
203 * @return The fingerprint of the SSL client certificate sent by the peer,
204 * empty if no cert was sent
206 virtual std::string GetFingerprint() const
208 ssl_cert* cert = GetCertificate();
209 if (cert && cert->IsUsable())
210 return cert->GetFingerprint();
215 * Get the ciphersuite negotiated with the peer
216 * @param out String where the ciphersuite string will be appended to
218 virtual void GetCiphersuite(std::string& out) const = 0;
221 /** Retrieves the name of the SSL connection which is sent via SNI.
222 * @param out String that the server name will be appended to.
223 * returns True if the server name was retrieved; otherwise, false.
225 virtual bool GetServerName(std::string& out) const = 0;
228 /** Helper functions for obtaining SSL client certificates and key fingerprints
235 * Get the client certificate from a socket
236 * @param sock The socket to get the certificate from, the socket does not have to use SSL
237 * @return The SSL client certificate information, NULL if the peer is not using SSL
239 static ssl_cert* GetCertificate(StreamSocket* sock)
241 SSLIOHook* ssliohook = SSLIOHook::IsSSL(sock);
245 return ssliohook->GetCertificate();
249 * Get the fingerprint of a client certificate from a socket
250 * @param sock The socket to get the certificate fingerprint from, the
251 * socket does not have to use SSL
252 * @return The key fingerprint from the SSL certificate sent by the peer,
253 * empty if no cert was sent or the peer is not using SSL
255 static std::string GetFingerprint(StreamSocket* sock)
257 ssl_cert* cert = SSLClientCert::GetCertificate(sock);
259 return cert->GetFingerprint();
264 class UserCertificateAPIBase : public DataProvider
267 UserCertificateAPIBase(Module* parent)
268 : DataProvider(parent, "m_sslinfo_api")
272 /** Get the SSL certificate of a user
273 * @param user The user whose certificate to get, user may be remote
274 * @return The SSL certificate of the user or NULL if the user is not using SSL
276 virtual ssl_cert* GetCertificate(User* user) = 0;
278 /** Set the SSL certificate of a user.
279 * @param user The user whose certificate to set.
280 * @param cert The SSL certificate to set for the user.
282 virtual void SetCertificate(User* user, ssl_cert* cert) = 0;
284 /** Get the key fingerprint from a user's certificate
285 * @param user The user whose key fingerprint to get, user may be remote
286 * @return The key fingerprint from the user's SSL certificate or an empty string
287 * if the user is not using SSL or did not provide a client certificate
289 std::string GetFingerprint(User* user)
291 ssl_cert* cert = GetCertificate(user);
293 return cert->GetFingerprint();
298 /** API implemented by m_sslinfo that allows modules to retrive the SSL certificate
299 * information of local and remote users. It can also be used to find out whether a
300 * user is using SSL or not.
302 class UserCertificateAPI : public dynamic_reference<UserCertificateAPIBase>
305 UserCertificateAPI(Module* parent)
306 : dynamic_reference<UserCertificateAPIBase>(parent, "m_sslinfo_api")