2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* HACK: This prevents OpenSSL on OS X 10.7 and later from spewing deprecation
25 * warnings for every single function call. As far as I (SaberUK) know, Apple
26 * have no plans to remove OpenSSL so this warning just causes needless spam.
29 # define __AVAILABILITYMACROS__
30 # define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
34 #include <openssl/ssl.h>
35 #include <openssl/err.h>
36 #include <openssl/dh.h>
40 # pragma comment(lib, "ssleay32.lib")
41 # pragma comment(lib, "libeay32.lib")
42 # undef MAX_DESCRIPTORS
43 # define MAX_DESCRIPTORS 10000
46 // Compatibility layer to allow OpenSSL 1.0 to use the 1.1 API.
47 #if ((defined LIBRESSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x10100000L))
48 # define X509_getm_notAfter X509_get_notAfter
49 # define X509_getm_notBefore X509_get_notBefore
50 # define OPENSSL_init_ssl(OPTIONS, SETTINGS) \
52 SSL_load_error_strings();
55 /* $ModDesc: Provides SSL support for clients */
57 /* $LinkerFlags: if("USE_FREEBSD_BASE_SSL") -lssl -lcrypto */
58 /* $CompileFlags: if(!"USE_FREEBSD_BASE_SSL") pkgconfversion("openssl","0.9.7") pkgconfincludes("openssl","/openssl/ssl.h","") */
59 /* $LinkerFlags: if(!"USE_FREEBSD_BASE_SSL") rpath("pkg-config --libs openssl") pkgconflibs("openssl","/libssl.so","-lssl -lcrypto -ldl") */
64 class ModuleSSLOpenSSL;
66 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING, ISSL_OPEN };
68 static bool SelfSigned = false;
70 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
71 static ModuleSSLOpenSSL* opensslmod = NULL;
76 return ERR_error_string(ERR_get_error(), NULL);
79 static int error_callback(const char *str, size_t len, void *u);
81 /** Represents an SSL user's extra data
88 reference<ssl_cert> cert;
98 data_to_write = false;
102 static int OnVerify(int preverify_ok, X509_STORE_CTX *ctx)
104 /* XXX: This will allow self signed certificates.
105 * In the future if we want an option to not allow this,
106 * we can just return preverify_ok here, and openssl
107 * will boot off self-signed and invalid peer certs.
109 int ve = X509_STORE_CTX_get_error(ctx);
111 SelfSigned = (ve == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT);
116 class ModuleSSLOpenSSL : public Module
118 issl_session* sessions;
126 std::string sslports;
129 ServiceProvider iohook;
131 static void SetContextOptions(SSL_CTX* ctx, long defoptions, const std::string& ctxname, ConfigTag* tag)
133 long setoptions = tag->getInt(ctxname + "setoptions");
134 // User-friendly config options for setting context options
135 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
136 if (tag->getBool("cipherserverpref"))
137 setoptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
139 #ifdef SSL_OP_NO_COMPRESSION
140 if (!tag->getBool("compression", true))
141 setoptions |= SSL_OP_NO_COMPRESSION;
143 if (!tag->getBool("sslv3", true))
144 setoptions |= SSL_OP_NO_SSLv3;
145 if (!tag->getBool("tlsv1", true))
146 setoptions |= SSL_OP_NO_TLSv1;
148 long clearoptions = tag->getInt(ctxname + "clearoptions");
149 ServerInstance->Logs->Log("m_ssl_openssl", DEBUG, "Setting OpenSSL %s context options, default: %ld set: %ld clear: %ld", ctxname.c_str(), defoptions, setoptions, clearoptions);
152 SSL_CTX_clear_options(ctx, SSL_CTX_get_options(ctx));
154 // Set the default options and what is in the conf
155 SSL_CTX_set_options(ctx, defoptions | setoptions);
156 long final = SSL_CTX_clear_options(ctx, clearoptions);
157 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "OpenSSL %s context options: %ld", ctxname.c_str(), final);
160 #ifdef INSPIRCD_OPENSSL_ENABLE_ECDH
161 void SetupECDH(ConfigTag* tag)
163 std::string curvename = tag->getString("ecdhcurve", "prime256v1");
164 if (curvename.empty())
167 int nid = OBJ_sn2nid(curvename.c_str());
170 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Unknown curve: \"%s\"", curvename.c_str());
174 EC_KEY* eckey = EC_KEY_new_by_curve_name(nid);
177 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Unable to create EC key object");
182 if (SSL_CTX_set_tmp_ecdh(ctx, eckey) < 0)
184 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Couldn't set ECDH parameters");
185 ERR_print_errors_cb(error_callback, this);
192 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
193 static void SSLInfoCallback(const SSL* ssl, int where, int rc)
195 int fd = SSL_get_fd(const_cast<SSL*>(ssl));
196 issl_session& session = opensslmod->sessions[fd];
198 if ((where & SSL_CB_HANDSHAKE_START) && (session.status == ISSL_OPEN))
200 // The other side is trying to renegotiate, kill the connection and change status
201 // to ISSL_NONE so CheckRenego() closes the session
202 session.status = ISSL_NONE;
203 ServerInstance->SE->Shutdown(fd, 2);
207 bool CheckRenego(StreamSocket* sock, issl_session* session)
209 if (session->status != ISSL_NONE)
212 ServerInstance->Logs->Log("m_ssl_openssl", DEBUG, "Session %p killed, attempted to renegotiate", (void*)session->sess);
213 CloseSession(session);
214 sock->SetError("Renegotiation is not allowed");
221 ModuleSSLOpenSSL() : iohook(this, "ssl/openssl", SERVICE_IOHOOK)
223 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
226 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
228 /* Global SSL library initialization*/
229 OPENSSL_init_ssl(0, NULL);
231 /* Build our SSL contexts:
232 * NOTE: OpenSSL makes us have two contexts, one for servers and one for clients. ICK.
234 ctx = SSL_CTX_new( SSLv23_server_method() );
235 clictx = SSL_CTX_new( SSLv23_client_method() );
237 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
238 SSL_CTX_set_mode(clictx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
240 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, OnVerify);
241 SSL_CTX_set_verify(clictx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, OnVerify);
243 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
244 SSL_CTX_set_session_cache_mode(clictx, SSL_SESS_CACHE_OFF);
246 long opts = SSL_OP_NO_SSLv2 | SSL_OP_SINGLE_DH_USE;
247 // Only turn options on if they exist
248 #ifdef SSL_OP_SINGLE_ECDH_USE
249 opts |= SSL_OP_SINGLE_ECDH_USE;
251 #ifdef SSL_OP_NO_TICKET
252 opts |= SSL_OP_NO_TICKET;
255 ctx_options = SSL_CTX_set_options(ctx, opts);
256 clictx_options = SSL_CTX_set_options(clictx, opts);
261 // Needs the flag as it ignores a plain /rehash
262 OnModuleRehash(NULL,"ssl");
263 Implementation eventlist[] = { I_On005Numeric, I_OnRehash, I_OnModuleRehash, I_OnHookIO, I_OnUserConnect };
264 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
265 ServerInstance->Modules->AddService(iohook);
268 void OnHookIO(StreamSocket* user, ListenSocket* lsb)
270 if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "openssl")
272 /* Hook the user with our module */
273 user->AddIOHook(this);
277 void OnRehash(User* user)
281 ConfigTag* Conf = ServerInstance->Config->ConfValue("openssl");
283 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
284 // Set the callback if we are not allowing renegotiations, unset it if we do
285 if (Conf->getBool("renegotiation", true))
287 SSL_CTX_set_info_callback(ctx, NULL);
288 SSL_CTX_set_info_callback(clictx, NULL);
292 SSL_CTX_set_info_callback(ctx, SSLInfoCallback);
293 SSL_CTX_set_info_callback(clictx, SSLInfoCallback);
297 if (Conf->getBool("showports", true))
299 sslports = Conf->getString("advertisedports");
300 if (!sslports.empty())
303 for (size_t i = 0; i < ServerInstance->ports.size(); i++)
305 ListenSocket* port = ServerInstance->ports[i];
306 if (port->bind_tag->getString("ssl") != "openssl")
309 const std::string& portid = port->bind_desc;
310 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %s", portid.c_str());
312 if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1")
315 * Found an SSL port for clients that is not bound to 127.0.0.1 and handled by us, display
316 * the IP:port in ISUPPORT.
318 * We used to advertise all ports seperated by a ';' char that matched the above criteria,
319 * but this resulted in too long ISUPPORT lines if there were lots of ports to be displayed.
320 * To solve this by default we now only display the first IP:port found and let the user
321 * configure the exact value for the 005 token, if necessary.
330 void OnModuleRehash(User* user, const std::string ¶m)
336 std::string certfile;
341 ConfigTag* conf = ServerInstance->Config->ConfValue("openssl");
343 cafile = conf->getString("cafile", CONFIG_PATH "/ca.pem");
344 certfile = conf->getString("certfile", CONFIG_PATH "/cert.pem");
345 keyfile = conf->getString("keyfile", CONFIG_PATH "/key.pem");
346 dhfile = conf->getString("dhfile", CONFIG_PATH "/dhparams.pem");
347 std::string hash = conf->getString("hash", "md5");
348 if (hash != "sha1" && hash != "md5")
349 throw ModuleException("Unknown hash type " + hash);
350 use_sha = (hash == "sha1");
352 if (conf->getBool("customcontextoptions"))
354 SetContextOptions(ctx, ctx_options, "server", conf);
355 SetContextOptions(clictx, clictx_options, "client", conf);
358 std::string ciphers = conf->getString("ciphers", "");
360 if (!ciphers.empty())
363 if ((!SSL_CTX_set_cipher_list(ctx, ciphers.c_str())) || (!SSL_CTX_set_cipher_list(clictx, ciphers.c_str())))
365 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_openssl.so: Can't set cipher list to %s.", ciphers.c_str());
366 ERR_print_errors_cb(error_callback, this);
370 /* Load our keys and certificates
371 * NOTE: OpenSSL's error logging API sucks, don't blame us for this clusterfuck.
374 if ((!SSL_CTX_use_certificate_chain_file(ctx, certfile.c_str())) || (!SSL_CTX_use_certificate_chain_file(clictx, certfile.c_str())))
376 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_openssl.so: Can't read certificate file %s. %s", certfile.c_str(), strerror(errno));
377 ERR_print_errors_cb(error_callback, this);
381 if (((!SSL_CTX_use_PrivateKey_file(ctx, keyfile.c_str(), SSL_FILETYPE_PEM))) || (!SSL_CTX_use_PrivateKey_file(clictx, keyfile.c_str(), SSL_FILETYPE_PEM)))
383 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_openssl.so: Can't read key file %s. %s", keyfile.c_str(), strerror(errno));
384 ERR_print_errors_cb(error_callback, this);
387 /* Load the CAs we trust*/
389 if (((!SSL_CTX_load_verify_locations(ctx, cafile.c_str(), 0))) || (!SSL_CTX_load_verify_locations(clictx, cafile.c_str(), 0)))
391 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_openssl.so: Can't read CA list from %s. This is only a problem if you want to verify client certificates, otherwise it's safe to ignore this message. Error: %s", cafile.c_str(), strerror(errno));
392 ERR_print_errors_cb(error_callback, this);
396 BIO* dhpfile = BIO_new_file(dhfile.c_str(), "r");
398 FILE* dhpfile = fopen(dhfile.c_str(), "r");
404 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "m_ssl_openssl.so Couldn't open DH file %s: %s", dhfile.c_str(), strerror(errno));
405 throw ModuleException("Couldn't open DH file " + dhfile + ": " + strerror(errno));
410 ret = PEM_read_bio_DHparams(dhpfile, NULL, NULL, NULL);
413 ret = PEM_read_DHparams(dhpfile, NULL, NULL, NULL);
419 if ((SSL_CTX_set_tmp_dh(ctx, ret) < 0) || (SSL_CTX_set_tmp_dh(clictx, ret) < 0))
421 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Couldn't set DH parameters %s. SSL errors follow:", dhfile.c_str());
422 ERR_print_errors_cb(error_callback, this);
428 ServerInstance->Logs->Log("m_ssl_openssl", DEFAULT, "m_ssl_openssl.so: Couldn't set DH parameters %s.", dhfile.c_str());
436 #ifdef INSPIRCD_OPENSSL_ENABLE_ECDH
441 void On005Numeric(std::string &output)
443 if (!sslports.empty())
444 output.append(" SSL=" + sslports);
450 SSL_CTX_free(clictx);
454 void OnUserConnect(LocalUser* user)
456 if (user->eh.GetIOHook() == this)
458 if (sessions[user->eh.GetFd()].sess)
460 if (!sessions[user->eh.GetFd()].cert->fingerprint.empty())
461 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\""
462 " and your SSL fingerprint is %s", user->nick.c_str(), SSL_get_cipher(sessions[user->eh.GetFd()].sess), sessions[user->eh.GetFd()].cert->fingerprint.c_str());
464 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), SSL_get_cipher(sessions[user->eh.GetFd()].sess));
469 void OnCleanup(int target_type, void* item)
471 if (target_type == TYPE_USER)
473 LocalUser* user = IS_LOCAL((User*)item);
475 if (user && user->eh.GetIOHook() == this)
477 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
478 // Potentially there could be multiple SSL modules loaded at once on different ports.
479 ServerInstance->Users->QuitUser(user, "SSL module unloading");
486 return Version("Provides SSL support for clients", VF_VENDOR);
489 void OnRequest(Request& request)
491 if (strcmp("GET_SSL_CERT", request.id) == 0)
493 SocketCertificateRequest& req = static_cast<SocketCertificateRequest&>(request);
494 int fd = req.sock->GetFd();
495 issl_session* session = &sessions[fd];
497 req.cert = session->cert;
499 else if (!strcmp("GET_RAW_SSL_SESSION", request.id))
501 SSLRawSessionRequest& req = static_cast<SSLRawSessionRequest&>(request);
502 if ((req.fd >= 0) && (req.fd < ServerInstance->SE->GetMaxFds()))
503 req.data = reinterpret_cast<void*>(sessions[req.fd].sess);
507 void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
509 int fd = user->GetFd();
511 issl_session* session = &sessions[fd];
513 session->sess = SSL_new(ctx);
514 session->status = ISSL_NONE;
515 session->outbound = false;
516 session->data_to_write = false;
518 if (session->sess == NULL)
521 if (SSL_set_fd(session->sess, fd) == 0)
523 ServerInstance->Logs->Log("m_ssl_openssl",DEBUG,"BUG: Can't set fd with SSL_set_fd: %d", fd);
527 Handshake(user, session);
530 void OnStreamSocketConnect(StreamSocket* user)
532 int fd = user->GetFd();
533 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
534 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() -1))
537 issl_session* session = &sessions[fd];
539 session->sess = SSL_new(clictx);
540 session->status = ISSL_NONE;
541 session->outbound = true;
542 session->data_to_write = false;
544 if (session->sess == NULL)
547 if (SSL_set_fd(session->sess, fd) == 0)
549 ServerInstance->Logs->Log("m_ssl_openssl",DEBUG,"BUG: Can't set fd with SSL_set_fd: %d", fd);
553 Handshake(user, session);
556 void OnStreamSocketClose(StreamSocket* user)
558 int fd = user->GetFd();
559 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
560 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
563 CloseSession(&sessions[fd]);
566 int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
568 int fd = user->GetFd();
569 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
570 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
573 issl_session* session = &sessions[fd];
577 CloseSession(session);
581 if (session->status == ISSL_HANDSHAKING)
583 // The handshake isn't finished and it wants to read, try to finish it.
584 if (!Handshake(user, session))
586 // Couldn't resume handshake.
587 if (session->status == ISSL_NONE)
593 // If we resumed the handshake then session->status will be ISSL_OPEN
595 if (session->status == ISSL_OPEN)
598 char* buffer = ServerInstance->GetReadBuffer();
599 size_t bufsiz = ServerInstance->Config->NetBufferSize;
600 int ret = SSL_read(session->sess, buffer, bufsiz);
602 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
603 if (!CheckRenego(user, session))
609 recvq.append(buffer, ret);
612 // Schedule a read if there is still data in the OpenSSL buffer
613 if (SSL_pending(session->sess) > 0)
614 mask |= FD_ADD_TRIAL_READ;
615 if (session->data_to_write)
616 mask |= FD_WANT_POLL_READ | FD_WANT_SINGLE_WRITE;
618 ServerInstance->SE->ChangeEventMask(user, mask);
623 // Client closed connection.
624 CloseSession(session);
625 user->SetError("Connection closed");
630 int err = SSL_get_error(session->sess, ret);
632 if (err == SSL_ERROR_WANT_READ)
634 ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ);
637 else if (err == SSL_ERROR_WANT_WRITE)
639 ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
644 CloseSession(session);
653 int OnStreamSocketWrite(StreamSocket* user, std::string& buffer)
655 int fd = user->GetFd();
657 issl_session* session = &sessions[fd];
661 CloseSession(session);
665 session->data_to_write = true;
667 if (session->status == ISSL_HANDSHAKING)
669 if (!Handshake(user, session))
671 // Couldn't resume handshake.
672 if (session->status == ISSL_NONE)
678 if (session->status == ISSL_OPEN)
681 int ret = SSL_write(session->sess, buffer.data(), buffer.size());
683 #ifdef INSPIRCD_OPENSSL_ENABLE_RENEGO_DETECTION
684 if (!CheckRenego(user, session))
688 if (ret == (int)buffer.length())
690 session->data_to_write = false;
691 ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
696 buffer = buffer.substr(ret);
697 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
702 CloseSession(session);
707 int err = SSL_get_error(session->sess, ret);
709 if (err == SSL_ERROR_WANT_WRITE)
711 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
714 else if (err == SSL_ERROR_WANT_READ)
716 ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ);
721 CloseSession(session);
729 bool Handshake(StreamSocket* user, issl_session* session)
734 if (session->outbound)
735 ret = SSL_connect(session->sess);
737 ret = SSL_accept(session->sess);
741 int err = SSL_get_error(session->sess, ret);
743 if (err == SSL_ERROR_WANT_READ)
745 ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
746 session->status = ISSL_HANDSHAKING;
749 else if (err == SSL_ERROR_WANT_WRITE)
751 ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
752 session->status = ISSL_HANDSHAKING;
757 CloseSession(session);
764 // Handshake complete.
765 VerifyCertificate(session, user);
767 session->status = ISSL_OPEN;
769 ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE | FD_ADD_TRIAL_WRITE);
775 CloseSession(session);
780 void CloseSession(issl_session* session)
784 SSL_shutdown(session->sess);
785 SSL_free(session->sess);
788 session->sess = NULL;
789 session->status = ISSL_NONE;
790 session->cert = NULL;
793 void VerifyCertificate(issl_session* session, StreamSocket* user)
795 if (!session->sess || !user)
799 ssl_cert* certinfo = new ssl_cert;
800 session->cert = certinfo;
802 unsigned char md[EVP_MAX_MD_SIZE];
803 const EVP_MD *digest = use_sha ? EVP_sha1() : EVP_md5();
805 cert = SSL_get_peer_certificate((SSL*)session->sess);
809 certinfo->error = "Could not get peer certificate: "+std::string(get_error());
813 certinfo->invalid = (SSL_get_verify_result(session->sess) != X509_V_OK);
817 certinfo->unknownsigner = false;
818 certinfo->trusted = true;
822 certinfo->unknownsigner = true;
823 certinfo->trusted = false;
827 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
829 // Make sure there are no chars in the string that we consider invalid
830 if (certinfo->dn.find_first_of("\r\n") != std::string::npos)
831 certinfo->dn.clear();
833 X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf));
834 certinfo->issuer = buf;
835 if (certinfo->issuer.find_first_of("\r\n") != std::string::npos)
836 certinfo->issuer.clear();
838 if (!X509_digest(cert, digest, md, &n))
840 certinfo->error = "Out of memory generating fingerprint";
844 certinfo->fingerprint = irc::hex(md, n);
847 if ((ASN1_UTCTIME_cmp_time_t(X509_getm_notAfter(cert), ServerInstance->Time()) == -1) || (ASN1_UTCTIME_cmp_time_t(X509_getm_notBefore(cert), ServerInstance->Time()) == 0))
849 certinfo->error = "Not activated, or expired certificate";
856 static int error_callback(const char *str, size_t len, void *u)
858 ServerInstance->Logs->Log("m_ssl_openssl",DEFAULT, "SSL error: " + std::string(str, len - 1));
861 // XXX: Remove this line, it causes valgrind warnings...
863 // MD_update(&m, buf, j);
866 // ... ONLY JOKING! :-)
872 MODULE_INIT(ModuleSSLOpenSSL)