]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_gnutls.cpp
Put ConfigReader deprecation warning in logs
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_gnutls.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <gnutls/gnutls.h>
16 #include <gnutls/x509.h>
17 #include "ssl.h"
18 #include "m_cap.h"
19
20 #ifdef WINDOWS
21 #pragma comment(lib, "libgnutls-13.lib")
22 #endif
23
24 /* $ModDesc: Provides SSL support for clients */
25 /* $CompileFlags: pkgconfincludes("gnutls","/gnutls/gnutls.h","") */
26 /* $LinkerFlags: rpath("pkg-config --libs gnutls") pkgconflibs("gnutls","/libgnutls.so","-lgnutls") */
27 /* $CopyInstall: conf/key.pem $(CONPATH) -m 0400 -o $(INSTUID) */
28 /* $CopyInstall: conf/cert.pem $(CONPATH) -m 0444 */
29
30 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
31
32 static gnutls_x509_crt_t x509_cert;
33 static gnutls_x509_privkey_t x509_key;
34 static int cert_callback (gnutls_session_t session, const gnutls_datum_t * req_ca_rdn, int nreqs,
35         const gnutls_pk_algorithm_t * sign_algos, int sign_algos_length, gnutls_retr_st * st) {
36
37         st->type = GNUTLS_CRT_X509;
38         st->ncerts = 1;
39         st->cert.x509 = &x509_cert;
40         st->key.x509 = x509_key;
41         st->deinit_all = 0;
42
43         return 0;
44 }
45
46 static ssize_t gnutls_pull_wrapper(gnutls_transport_ptr_t user_wrap, void* buffer, size_t size)
47 {
48         StreamSocket* user = reinterpret_cast<StreamSocket*>(user_wrap);
49         if (user->GetEventMask() & FD_READ_WILL_BLOCK)
50         {
51                 errno = EAGAIN;
52                 return -1;
53         }
54         int rv = recv(user->GetFd(), buffer, size, 0);
55         if (rv < (int)size)
56                 ServerInstance->SE->ChangeEventMask(user, FD_READ_WILL_BLOCK);
57         return rv;
58 }
59
60 static ssize_t gnutls_push_wrapper(gnutls_transport_ptr_t user_wrap, const void* buffer, size_t size)
61 {
62         StreamSocket* user = reinterpret_cast<StreamSocket*>(user_wrap);
63         if (user->GetEventMask() & FD_WRITE_WILL_BLOCK)
64         {
65                 errno = EAGAIN;
66                 return -1;
67         }
68         int rv = send(user->GetFd(), buffer, size, 0);
69         if (rv < (int)size)
70                 ServerInstance->SE->ChangeEventMask(user, FD_WRITE_WILL_BLOCK);
71         return rv;
72 }
73
74 /** Represents an SSL user's extra data
75  */
76 class issl_session
77 {
78 public:
79         gnutls_session_t sess;
80         issl_status status;
81         reference<ssl_cert> cert;
82         issl_session() : sess(NULL) {}
83 };
84
85 class CommandStartTLS : public SplitCommand
86 {
87  public:
88         CommandStartTLS (Module* mod) : SplitCommand(mod, "STARTTLS")
89         {
90                 works_before_reg = true;
91         }
92
93         CmdResult HandleLocal(const std::vector<std::string> &parameters, LocalUser *user)
94         {
95                 /* changed from == REG_ALL to catch clients sending STARTTLS
96                  * after NICK and USER but before OnUserConnect completes and
97                  * give a proper error message (see bug #645) - dz
98                  */
99                 if (user->registered != REG_NONE)
100                 {
101                         user->WriteNumeric(691, "%s :STARTTLS is not permitted after client registration has started", user->nick.c_str());
102                 }
103                 else
104                 {
105                         if (!user->eh.GetIOHook())
106                         {
107                                 user->WriteNumeric(670, "%s :STARTTLS successful, go ahead with TLS handshake", user->nick.c_str());
108                                 user->eh.AddIOHook(creator);
109                                 creator->OnStreamSocketAccept(&user->eh, NULL, NULL);
110                         }
111                         else
112                                 user->WriteNumeric(691, "%s :STARTTLS failure", user->nick.c_str());
113                 }
114
115                 return CMD_FAILURE;
116         }
117 };
118
119 class ModuleSSLGnuTLS : public Module
120 {
121         issl_session* sessions;
122
123         gnutls_certificate_credentials x509_cred;
124         gnutls_dh_params dh_params;
125
126         std::string keyfile;
127         std::string certfile;
128
129         std::string cafile;
130         std::string crlfile;
131         std::string sslports;
132         int dh_bits;
133
134         bool cred_alloc;
135
136         CommandStartTLS starttls;
137
138         GenericCap capHandler;
139         ServiceProvider iohook;
140  public:
141
142         ModuleSSLGnuTLS()
143                 : starttls(this), capHandler(this, "tls"), iohook(this, "ssl/gnutls", SERVICE_IOHOOK)
144         {
145                 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
146
147                 gnutls_global_init(); // This must be called once in the program
148                 gnutls_x509_crt_init(&x509_cert);
149                 gnutls_x509_privkey_init(&x509_key);
150
151                 cred_alloc = false;
152         }
153
154         void init()
155         {
156                 // Needs the flag as it ignores a plain /rehash
157                 OnModuleRehash(NULL,"ssl");
158
159                 // Void return, guess we assume success
160                 gnutls_certificate_set_dh_params(x509_cred, dh_params);
161                 Implementation eventlist[] = { I_On005Numeric, I_OnRehash, I_OnModuleRehash, I_OnUserConnect,
162                         I_OnEvent, I_OnHookIO };
163                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
164
165                 ServerInstance->Modules->AddService(iohook);
166                 ServerInstance->AddCommand(&starttls);
167         }
168
169         void OnRehash(User* user)
170         {
171                 sslports.clear();
172
173                 for (size_t i = 0; i < ServerInstance->ports.size(); i++)
174                 {
175                         ListenSocket* port = ServerInstance->ports[i];
176                         if (port->bind_tag->getString("ssl") != "gnutls")
177                                 continue;
178
179                         const std::string& portid = port->bind_desc;
180                         ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str());
181
182                         if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1")
183                                 sslports.append(portid).append(";");
184                 }
185
186                 if (!sslports.empty())
187                         sslports.erase(sslports.end() - 1);
188         }
189
190         void OnModuleRehash(User* user, const std::string &param)
191         {
192                 if(param != "ssl")
193                         return;
194
195                 OnRehash(user);
196
197                 ConfigTag* Conf = ServerInstance->Config->ConfValue("gnutls");
198
199                 cafile = Conf->getString("cafile");
200                 crlfile = Conf->getString("crlfile");
201                 certfile = Conf->getString("certfile");
202                 keyfile = Conf->getString("keyfile");
203                 dh_bits = Conf->getInt("dhbits");
204
205                 // Set all the default values needed.
206                 if (cafile.empty())
207                         cafile = "conf/ca.pem";
208
209                 if (crlfile.empty())
210                         crlfile = "conf/crl.pem";
211
212                 if (certfile.empty())
213                         certfile = "conf/cert.pem";
214
215                 if (keyfile.empty())
216                         keyfile = "conf/key.pem";
217
218                 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
219                         dh_bits = 1024;
220
221                 int ret;
222
223                 if (cred_alloc)
224                 {
225                         // Deallocate the old credentials
226                         gnutls_dh_params_deinit(dh_params);
227                         gnutls_certificate_free_credentials(x509_cred);
228                 }
229                 else
230                         cred_alloc = true;
231
232                 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
233                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
234
235                 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
236                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
237
238                 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
239                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
240
241                 FileReader reader;
242
243                 reader.LoadFile(certfile);
244                 std::string cert_string = reader.Contents();
245                 gnutls_datum_t cert_datum = { (unsigned char*)cert_string.data(), cert_string.length() };
246
247                 reader.LoadFile(keyfile);
248                 std::string key_string = reader.Contents();
249                 gnutls_datum_t key_datum = { (unsigned char*)key_string.data(), key_string.length() };
250
251                 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
252                 if((ret = gnutls_x509_crt_import(x509_cert, &cert_datum, GNUTLS_X509_FMT_PEM)) < 0)
253                         throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + "): " + std::string(gnutls_strerror(ret)));
254
255                 if((ret = gnutls_x509_privkey_import(x509_key, &key_datum, GNUTLS_X509_FMT_PEM)) < 0)
256                         throw ModuleException("Unable to load GnuTLS server private key (" + keyfile + "): " + std::string(gnutls_strerror(ret)));
257
258                 if((ret = gnutls_certificate_set_x509_key(x509_cred, &x509_cert, 1, x509_key)) < 0)
259                         throw ModuleException("Unable to set GnuTLS cert/key pair: " + std::string(gnutls_strerror(ret)));
260
261                 gnutls_certificate_client_set_retrieve_function (x509_cred, cert_callback);
262
263                 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
264                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
265
266                 // This may be on a large (once a day or week) timer eventually.
267                 GenerateDHParams();
268         }
269
270         void GenerateDHParams()
271         {
272                 // Generate Diffie Hellman parameters - for use with DHE
273                 // kx algorithms. These should be discarded and regenerated
274                 // once a day, once a week or once a month. Depending on the
275                 // security requirements.
276
277                 int ret;
278
279                 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
280                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
281         }
282
283         ~ModuleSSLGnuTLS()
284         {
285                 gnutls_x509_crt_deinit(x509_cert);
286                 gnutls_x509_privkey_deinit(x509_key);
287                 if (cred_alloc)
288                 {
289                         gnutls_dh_params_deinit(dh_params);
290                         gnutls_certificate_free_credentials(x509_cred);
291                 }
292                 gnutls_global_deinit();
293                 delete[] sessions;
294         }
295
296         void OnCleanup(int target_type, void* item)
297         {
298                 if(target_type == TYPE_USER)
299                 {
300                         LocalUser* user = IS_LOCAL(static_cast<User*>(item));
301
302                         if (user && user->eh.GetIOHook() == this)
303                         {
304                                 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
305                                 // Potentially there could be multiple SSL modules loaded at once on different ports.
306                                 ServerInstance->Users->QuitUser(user, "SSL module unloading");
307                         }
308                 }
309         }
310
311         Version GetVersion()
312         {
313                 return Version("Provides SSL support for clients", VF_VENDOR);
314         }
315
316
317         void On005Numeric(std::string &output)
318         {
319                 if (!sslports.empty())
320                         output.append(" SSL=" + sslports);
321                 output.append(" STARTTLS");
322         }
323
324         void OnHookIO(StreamSocket* user, ListenSocket* lsb)
325         {
326                 if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "gnutls")
327                 {
328                         /* Hook the user with our module */
329                         user->AddIOHook(this);
330                 }
331         }
332
333         void OnRequest(Request& request)
334         {
335                 if (strcmp("GET_SSL_CERT", request.id) == 0)
336                 {
337                         SocketCertificateRequest& req = static_cast<SocketCertificateRequest&>(request);
338                         int fd = req.sock->GetFd();
339                         issl_session* session = &sessions[fd];
340
341                         req.cert = session->cert;
342                 }
343         }
344
345         void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
346         {
347                 int fd = user->GetFd();
348                 issl_session* session = &sessions[fd];
349
350                 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
351                 if (session->sess)
352                         return;
353
354                 gnutls_init(&session->sess, GNUTLS_SERVER);
355
356                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
357                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
358                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
359
360                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
361                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
362                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
363
364                 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
365
366                 Handshake(session, user);
367         }
368
369         void OnStreamSocketConnect(StreamSocket* user)
370         {
371                 issl_session* session = &sessions[user->GetFd()];
372
373                 gnutls_init(&session->sess, GNUTLS_CLIENT);
374
375                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
376                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
377                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
378                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
379                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
380                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
381
382                 Handshake(session, user);
383         }
384
385         void OnStreamSocketClose(StreamSocket* user)
386         {
387                 CloseSession(&sessions[user->GetFd()]);
388         }
389
390         int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
391         {
392                 issl_session* session = &sessions[user->GetFd()];
393
394                 if (!session->sess)
395                 {
396                         CloseSession(session);
397                         user->SetError("No SSL session");
398                         return -1;
399                 }
400
401                 if (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE)
402                 {
403                         // The handshake isn't finished, try to finish it.
404
405                         if(!Handshake(session, user))
406                         {
407                                 if (session->status != ISSL_CLOSING)
408                                         return 0;
409                                 return -1;
410                         }
411                 }
412
413                 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
414
415                 if (session->status == ISSL_HANDSHAKEN)
416                 {
417                         char* buffer = ServerInstance->GetReadBuffer();
418                         size_t bufsiz = ServerInstance->Config->NetBufferSize;
419                         int ret = gnutls_record_recv(session->sess, buffer, bufsiz);
420                         if (ret > 0)
421                         {
422                                 recvq.append(buffer, ret);
423                                 return 1;
424                         }
425                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
426                         {
427                                 return 0;
428                         }
429                         else if (ret == 0)
430                         {
431                                 user->SetError("SSL Connection closed");
432                                 CloseSession(session);
433                                 return -1;
434                         }
435                         else
436                         {
437                                 user->SetError(gnutls_strerror(ret));
438                                 CloseSession(session);
439                                 return -1;
440                         }
441                 }
442                 else if (session->status == ISSL_CLOSING)
443                         return -1;
444
445                 return 0;
446         }
447
448         int OnStreamSocketWrite(StreamSocket* user, std::string& sendq)
449         {
450                 issl_session* session = &sessions[user->GetFd()];
451
452                 if (!session->sess)
453                 {
454                         CloseSession(session);
455                         user->SetError("No SSL session");
456                         return -1;
457                 }
458
459                 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
460                 {
461                         // The handshake isn't finished, try to finish it.
462                         Handshake(session, user);
463                         if (session->status != ISSL_CLOSING)
464                                 return 0;
465                         return -1;
466                 }
467
468                 int ret = 0;
469
470                 if (session->status == ISSL_HANDSHAKEN)
471                 {
472                         ret = gnutls_record_send(session->sess, sendq.data(), sendq.length());
473
474                         if (ret == (int)sendq.length())
475                         {
476                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_WRITE);
477                                 return 1;
478                         }
479                         else if (ret > 0)
480                         {
481                                 sendq = sendq.substr(ret);
482                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
483                                 return 0;
484                         }
485                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
486                         {
487                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
488                                 return 0;
489                         }
490                         else if (ret == 0)
491                         {
492                                 CloseSession(session);
493                                 user->SetError("SSL Connection closed");
494                                 return -1;
495                         }
496                         else // (ret < 0)
497                         {
498                                 user->SetError(gnutls_strerror(ret));
499                                 CloseSession(session);
500                                 return -1;
501                         }
502                 }
503
504                 return 0;
505         }
506
507         bool Handshake(issl_session* session, StreamSocket* user)
508         {
509                 int ret = gnutls_handshake(session->sess);
510
511                 if (ret < 0)
512                 {
513                         if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
514                         {
515                                 // Handshake needs resuming later, read() or write() would have blocked.
516
517                                 if(gnutls_record_get_direction(session->sess) == 0)
518                                 {
519                                         // gnutls_handshake() wants to read() again.
520                                         session->status = ISSL_HANDSHAKING_READ;
521                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
522                                 }
523                                 else
524                                 {
525                                         // gnutls_handshake() wants to write() again.
526                                         session->status = ISSL_HANDSHAKING_WRITE;
527                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
528                                 }
529                         }
530                         else
531                         {
532                                 user->SetError(std::string("Handshake Failed - ") + gnutls_strerror(ret));
533                                 CloseSession(session);
534                                 session->status = ISSL_CLOSING;
535                         }
536
537                         return false;
538                 }
539                 else
540                 {
541                         // Change the seesion state
542                         session->status = ISSL_HANDSHAKEN;
543
544                         VerifyCertificate(session,user);
545
546                         // Finish writing, if any left
547                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE | FD_ADD_TRIAL_WRITE);
548
549                         return true;
550                 }
551         }
552
553         void OnUserConnect(LocalUser* user)
554         {
555                 if (user->eh.GetIOHook() == this)
556                 {
557                         if (sessions[user->GetFd()].sess)
558                         {
559                                 SSLCertSubmission(user, this, ServerInstance->Modules->Find("m_sslinfo.so"), sessions[user->GetFd()].cert);
560                                 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
561                                 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
562                                 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
563                                 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
564                         }
565                 }
566         }
567
568         void CloseSession(issl_session* session)
569         {
570                 if (session->sess)
571                 {
572                         gnutls_bye(session->sess, GNUTLS_SHUT_WR);
573                         gnutls_deinit(session->sess);
574                 }
575                 session->sess = NULL;
576                 session->cert = NULL;
577                 session->status = ISSL_NONE;
578         }
579
580         void VerifyCertificate(issl_session* session, StreamSocket* user)
581         {
582                 if (!session->sess || !user || session->cert)
583                         return;
584
585                 unsigned int status;
586                 const gnutls_datum_t* cert_list;
587                 int ret;
588                 unsigned int cert_list_size;
589                 gnutls_x509_crt_t cert;
590                 char name[MAXBUF];
591                 unsigned char digest[MAXBUF];
592                 size_t digest_size = sizeof(digest);
593                 size_t name_size = sizeof(name);
594                 ssl_cert* certinfo = new ssl_cert;
595                 session->cert = certinfo;
596
597                 /* This verification function uses the trusted CAs in the credentials
598                  * structure. So you must have installed one or more CA certificates.
599                  */
600                 ret = gnutls_certificate_verify_peers2(session->sess, &status);
601
602                 if (ret < 0)
603                 {
604                         certinfo->error = std::string(gnutls_strerror(ret));
605                         return;
606                 }
607
608                 certinfo->invalid = (status & GNUTLS_CERT_INVALID);
609                 certinfo->unknownsigner = (status & GNUTLS_CERT_SIGNER_NOT_FOUND);
610                 certinfo->revoked = (status & GNUTLS_CERT_REVOKED);
611                 certinfo->trusted = !(status & GNUTLS_CERT_SIGNER_NOT_CA);
612
613                 /* Up to here the process is the same for X.509 certificates and
614                  * OpenPGP keys. From now on X.509 certificates are assumed. This can
615                  * be easily extended to work with openpgp keys as well.
616                  */
617                 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
618                 {
619                         certinfo->error = "No X509 keys sent";
620                         return;
621                 }
622
623                 ret = gnutls_x509_crt_init(&cert);
624                 if (ret < 0)
625                 {
626                         certinfo->error = gnutls_strerror(ret);
627                         return;
628                 }
629
630                 cert_list_size = 0;
631                 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
632                 if (cert_list == NULL)
633                 {
634                         certinfo->error = "No certificate was found";
635                         goto info_done_dealloc;
636                 }
637
638                 /* This is not a real world example, since we only check the first
639                  * certificate in the given chain.
640                  */
641
642                 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
643                 if (ret < 0)
644                 {
645                         certinfo->error = gnutls_strerror(ret);
646                         goto info_done_dealloc;
647                 }
648
649                 gnutls_x509_crt_get_dn(cert, name, &name_size);
650                 certinfo->dn = name;
651
652                 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
653                 certinfo->issuer = name;
654
655                 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
656                 {
657                         certinfo->error = gnutls_strerror(ret);
658                 }
659                 else
660                 {
661                         certinfo->fingerprint = irc::hex(digest, digest_size);
662                 }
663
664                 /* Beware here we do not check for errors.
665                  */
666                 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
667                 {
668                         certinfo->error = "Not activated, or expired certificate";
669                 }
670
671 info_done_dealloc:
672                 gnutls_x509_crt_deinit(cert);
673         }
674
675         void OnEvent(Event& ev)
676         {
677                 capHandler.HandleEvent(ev);
678         }
679 };
680
681 MODULE_INIT(ModuleSSLGnuTLS)