]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_gnutls.cpp
26fa5889808e2fe003f971ea5cb3f0345a166d1f
[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                 // Needs the flag as it ignores a plain /rehash
153                 OnModuleRehash(NULL,"ssl");
154
155                 // Void return, guess we assume success
156                 gnutls_certificate_set_dh_params(x509_cred, dh_params);
157                 Implementation eventlist[] = { I_On005Numeric, I_OnRehash, I_OnModuleRehash, I_OnUserConnect,
158                         I_OnEvent, I_OnHookIO };
159                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
160
161                 ServerInstance->Modules->AddService(iohook);
162                 ServerInstance->AddCommand(&starttls);
163         }
164
165         void OnRehash(User* user)
166         {
167                 ConfigReader Conf;
168
169                 sslports.clear();
170
171                 for (size_t i = 0; i < ServerInstance->ports.size(); i++)
172                 {
173                         ListenSocket* port = ServerInstance->ports[i];
174                         if (port->bind_tag->getString("ssl") != "gnutls")
175                                 continue;
176
177                         const std::string& portid = port->bind_desc;
178                         ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portid.c_str());
179
180                         if (port->bind_tag->getString("type", "clients") == "clients" && port->bind_addr != "127.0.0.1")
181                                 sslports.append(portid).append(";");
182                 }
183
184                 if (!sslports.empty())
185                         sslports.erase(sslports.end() - 1);
186         }
187
188         void OnModuleRehash(User* user, const std::string &param)
189         {
190                 if(param != "ssl")
191                         return;
192
193                 OnRehash(user);
194
195                 ConfigReader Conf;
196
197                 cafile = Conf.ReadValue("gnutls", "cafile", 0);
198                 crlfile = Conf.ReadValue("gnutls", "crlfile", 0);
199                 certfile = Conf.ReadValue("gnutls", "certfile", 0);
200                 keyfile = Conf.ReadValue("gnutls", "keyfile", 0);
201                 dh_bits = Conf.ReadInteger("gnutls", "dhbits", 0, false);
202
203                 // Set all the default values needed.
204                 if (cafile.empty())
205                         cafile = "conf/ca.pem";
206
207                 if (crlfile.empty())
208                         crlfile = "conf/crl.pem";
209
210                 if (certfile.empty())
211                         certfile = "conf/cert.pem";
212
213                 if (keyfile.empty())
214                         keyfile = "conf/key.pem";
215
216                 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
217                         dh_bits = 1024;
218
219                 int ret;
220
221                 if (cred_alloc)
222                 {
223                         // Deallocate the old credentials
224                         gnutls_dh_params_deinit(dh_params);
225                         gnutls_certificate_free_credentials(x509_cred);
226                 }
227                 else
228                         cred_alloc = true;
229
230                 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
231                         ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
232
233                 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
234                         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));
235
236                 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
237                         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));
238
239                 FileReader reader;
240
241                 reader.LoadFile(certfile);
242                 std::string cert_string = reader.Contents();
243                 gnutls_datum_t cert_datum = { (unsigned char*)cert_string.data(), cert_string.length() };
244
245                 reader.LoadFile(keyfile);
246                 std::string key_string = reader.Contents();
247                 gnutls_datum_t key_datum = { (unsigned char*)key_string.data(), key_string.length() };
248
249                 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
250                 if((ret = gnutls_x509_crt_import(x509_cert, &cert_datum, GNUTLS_X509_FMT_PEM)) < 0)
251                         throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + "): " + std::string(gnutls_strerror(ret)));
252
253                 if((ret = gnutls_x509_privkey_import(x509_key, &key_datum, GNUTLS_X509_FMT_PEM)) < 0)
254                         throw ModuleException("Unable to load GnuTLS server private key (" + keyfile + "): " + std::string(gnutls_strerror(ret)));
255
256                 if((ret = gnutls_certificate_set_x509_key(x509_cred, &x509_cert, 1, x509_key)) < 0)
257                         throw ModuleException("Unable to set GnuTLS cert/key pair: " + std::string(gnutls_strerror(ret)));
258
259                 gnutls_certificate_client_set_retrieve_function (x509_cred, cert_callback);
260
261                 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
262                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
263
264                 // This may be on a large (once a day or week) timer eventually.
265                 GenerateDHParams();
266         }
267
268         void GenerateDHParams()
269         {
270                 // Generate Diffie Hellman parameters - for use with DHE
271                 // kx algorithms. These should be discarded and regenerated
272                 // once a day, once a week or once a month. Depending on the
273                 // security requirements.
274
275                 int ret;
276
277                 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
278                         ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
279         }
280
281         ~ModuleSSLGnuTLS()
282         {
283                 gnutls_x509_crt_deinit(x509_cert);
284                 gnutls_x509_privkey_deinit(x509_key);
285                 gnutls_dh_params_deinit(dh_params);
286                 gnutls_certificate_free_credentials(x509_cred);
287                 gnutls_global_deinit();
288                 delete[] sessions;
289         }
290
291         void OnCleanup(int target_type, void* item)
292         {
293                 if(target_type == TYPE_USER)
294                 {
295                         LocalUser* user = IS_LOCAL(static_cast<User*>(item));
296
297                         if (user && user->eh.GetIOHook() == this)
298                         {
299                                 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
300                                 // Potentially there could be multiple SSL modules loaded at once on different ports.
301                                 ServerInstance->Users->QuitUser(user, "SSL module unloading");
302                         }
303                 }
304         }
305
306         Version GetVersion()
307         {
308                 return Version("Provides SSL support for clients", VF_VENDOR);
309         }
310
311
312         void On005Numeric(std::string &output)
313         {
314                 if (!sslports.empty())
315                         output.append(" SSL=" + sslports);
316                 output.append(" STARTTLS");
317         }
318
319         void OnHookIO(StreamSocket* user, ListenSocket* lsb)
320         {
321                 if (!user->GetIOHook() && lsb->bind_tag->getString("ssl") == "gnutls")
322                 {
323                         /* Hook the user with our module */
324                         user->AddIOHook(this);
325                 }
326         }
327
328         void OnRequest(Request& request)
329         {
330                 if (strcmp("GET_SSL_CERT", request.id) == 0)
331                 {
332                         SocketCertificateRequest& req = static_cast<SocketCertificateRequest&>(request);
333                         int fd = req.sock->GetFd();
334                         issl_session* session = &sessions[fd];
335
336                         req.cert = session->cert;
337                 }
338         }
339
340         void OnStreamSocketAccept(StreamSocket* user, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
341         {
342                 int fd = user->GetFd();
343                 issl_session* session = &sessions[fd];
344
345                 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
346                 if (session->sess)
347                         return;
348
349                 gnutls_init(&session->sess, GNUTLS_SERVER);
350
351                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
352                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
353                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
354
355                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
356                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
357                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
358
359                 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
360
361                 Handshake(session, user);
362         }
363
364         void OnStreamSocketConnect(StreamSocket* user)
365         {
366                 issl_session* session = &sessions[user->GetFd()];
367
368                 gnutls_init(&session->sess, GNUTLS_CLIENT);
369
370                 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
371                 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
372                 gnutls_dh_set_prime_bits(session->sess, dh_bits);
373                 gnutls_transport_set_ptr(session->sess, reinterpret_cast<gnutls_transport_ptr_t>(user));
374                 gnutls_transport_set_push_function(session->sess, gnutls_push_wrapper);
375                 gnutls_transport_set_pull_function(session->sess, gnutls_pull_wrapper);
376
377                 Handshake(session, user);
378         }
379
380         void OnStreamSocketClose(StreamSocket* user)
381         {
382                 CloseSession(&sessions[user->GetFd()]);
383         }
384
385         int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
386         {
387                 issl_session* session = &sessions[user->GetFd()];
388
389                 if (!session->sess)
390                 {
391                         CloseSession(session);
392                         user->SetError("No SSL session");
393                         return -1;
394                 }
395
396                 if (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE)
397                 {
398                         // The handshake isn't finished, try to finish it.
399
400                         if(!Handshake(session, user))
401                         {
402                                 if (session->status != ISSL_CLOSING)
403                                         return 0;
404                                 return -1;
405                         }
406                 }
407
408                 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
409
410                 if (session->status == ISSL_HANDSHAKEN)
411                 {
412                         char* buffer = ServerInstance->GetReadBuffer();
413                         size_t bufsiz = ServerInstance->Config->NetBufferSize;
414                         int ret = gnutls_record_recv(session->sess, buffer, bufsiz);
415                         if (ret > 0)
416                         {
417                                 recvq.append(buffer, ret);
418                                 return 1;
419                         }
420                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
421                         {
422                                 return 0;
423                         }
424                         else if (ret == 0)
425                         {
426                                 user->SetError("SSL Connection closed");
427                                 CloseSession(session);
428                                 return -1;
429                         }
430                         else
431                         {
432                                 user->SetError(gnutls_strerror(ret));
433                                 CloseSession(session);
434                                 return -1;
435                         }
436                 }
437                 else if (session->status == ISSL_CLOSING)
438                         return -1;
439
440                 return 0;
441         }
442
443         int OnStreamSocketWrite(StreamSocket* user, std::string& sendq)
444         {
445                 issl_session* session = &sessions[user->GetFd()];
446
447                 if (!session->sess)
448                 {
449                         CloseSession(session);
450                         user->SetError("No SSL session");
451                         return -1;
452                 }
453
454                 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
455                 {
456                         // The handshake isn't finished, try to finish it.
457                         Handshake(session, user);
458                         if (session->status != ISSL_CLOSING)
459                                 return 0;
460                         return -1;
461                 }
462
463                 int ret = 0;
464
465                 if (session->status == ISSL_HANDSHAKEN)
466                 {
467                         ret = gnutls_record_send(session->sess, sendq.data(), sendq.length());
468
469                         if (ret == (int)sendq.length())
470                         {
471                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_WRITE);
472                                 return 1;
473                         }
474                         else if (ret > 0)
475                         {
476                                 sendq = sendq.substr(ret);
477                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
478                                 return 0;
479                         }
480                         else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
481                         {
482                                 ServerInstance->SE->ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
483                                 return 0;
484                         }
485                         else if (ret == 0)
486                         {
487                                 CloseSession(session);
488                                 user->SetError("SSL Connection closed");
489                                 return -1;
490                         }
491                         else // (ret < 0)
492                         {
493                                 user->SetError(gnutls_strerror(ret));
494                                 CloseSession(session);
495                                 return -1;
496                         }
497                 }
498
499                 return 0;
500         }
501
502         bool Handshake(issl_session* session, StreamSocket* user)
503         {
504                 int ret = gnutls_handshake(session->sess);
505
506                 if (ret < 0)
507                 {
508                         if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
509                         {
510                                 // Handshake needs resuming later, read() or write() would have blocked.
511
512                                 if(gnutls_record_get_direction(session->sess) == 0)
513                                 {
514                                         // gnutls_handshake() wants to read() again.
515                                         session->status = ISSL_HANDSHAKING_READ;
516                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
517                                 }
518                                 else
519                                 {
520                                         // gnutls_handshake() wants to write() again.
521                                         session->status = ISSL_HANDSHAKING_WRITE;
522                                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE);
523                                 }
524                         }
525                         else
526                         {
527                                 user->SetError(std::string("Handshake Failed - ") + gnutls_strerror(ret));
528                                 CloseSession(session);
529                                 session->status = ISSL_CLOSING;
530                         }
531
532                         return false;
533                 }
534                 else
535                 {
536                         // Change the seesion state
537                         session->status = ISSL_HANDSHAKEN;
538
539                         VerifyCertificate(session,user);
540
541                         // Finish writing, if any left
542                         ServerInstance->SE->ChangeEventMask(user, FD_WANT_POLL_READ | FD_WANT_NO_WRITE | FD_ADD_TRIAL_WRITE);
543
544                         return true;
545                 }
546         }
547
548         void OnUserConnect(LocalUser* user)
549         {
550                 if (user->eh.GetIOHook() == this)
551                 {
552                         if (sessions[user->GetFd()].sess)
553                         {
554                                 SSLCertSubmission(user, this, ServerInstance->Modules->Find("m_sslinfo.so"), sessions[user->GetFd()].cert);
555                                 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
556                                 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
557                                 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
558                                 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
559                         }
560                 }
561         }
562
563         void CloseSession(issl_session* session)
564         {
565                 if (session->sess)
566                 {
567                         gnutls_bye(session->sess, GNUTLS_SHUT_WR);
568                         gnutls_deinit(session->sess);
569                 }
570                 session->sess = NULL;
571                 session->cert = NULL;
572                 session->status = ISSL_NONE;
573         }
574
575         void VerifyCertificate(issl_session* session, StreamSocket* user)
576         {
577                 if (!session->sess || !user || session->cert)
578                         return;
579
580                 unsigned int status;
581                 const gnutls_datum_t* cert_list;
582                 int ret;
583                 unsigned int cert_list_size;
584                 gnutls_x509_crt_t cert;
585                 char name[MAXBUF];
586                 unsigned char digest[MAXBUF];
587                 size_t digest_size = sizeof(digest);
588                 size_t name_size = sizeof(name);
589                 ssl_cert* certinfo = new ssl_cert;
590                 session->cert = certinfo;
591
592                 /* This verification function uses the trusted CAs in the credentials
593                  * structure. So you must have installed one or more CA certificates.
594                  */
595                 ret = gnutls_certificate_verify_peers2(session->sess, &status);
596
597                 if (ret < 0)
598                 {
599                         certinfo->error = std::string(gnutls_strerror(ret));
600                         return;
601                 }
602
603                 certinfo->invalid = (status & GNUTLS_CERT_INVALID);
604                 certinfo->unknownsigner = (status & GNUTLS_CERT_SIGNER_NOT_FOUND);
605                 certinfo->revoked = (status & GNUTLS_CERT_REVOKED);
606                 certinfo->trusted = !(status & GNUTLS_CERT_SIGNER_NOT_CA);
607
608                 /* Up to here the process is the same for X.509 certificates and
609                  * OpenPGP keys. From now on X.509 certificates are assumed. This can
610                  * be easily extended to work with openpgp keys as well.
611                  */
612                 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
613                 {
614                         certinfo->error = "No X509 keys sent";
615                         return;
616                 }
617
618                 ret = gnutls_x509_crt_init(&cert);
619                 if (ret < 0)
620                 {
621                         certinfo->error = gnutls_strerror(ret);
622                         return;
623                 }
624
625                 cert_list_size = 0;
626                 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
627                 if (cert_list == NULL)
628                 {
629                         certinfo->error = "No certificate was found";
630                         goto info_done_dealloc;
631                 }
632
633                 /* This is not a real world example, since we only check the first
634                  * certificate in the given chain.
635                  */
636
637                 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
638                 if (ret < 0)
639                 {
640                         certinfo->error = gnutls_strerror(ret);
641                         goto info_done_dealloc;
642                 }
643
644                 gnutls_x509_crt_get_dn(cert, name, &name_size);
645                 certinfo->dn = name;
646
647                 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
648                 certinfo->issuer = name;
649
650                 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
651                 {
652                         certinfo->error = gnutls_strerror(ret);
653                 }
654                 else
655                 {
656                         certinfo->fingerprint = irc::hex(digest, digest_size);
657                 }
658
659                 /* Beware here we do not check for errors.
660                  */
661                 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
662                 {
663                         certinfo->error = "Not activated, or expired certificate";
664                 }
665
666 info_done_dealloc:
667                 gnutls_x509_crt_deinit(cert);
668         }
669
670         void OnEvent(Event& ev)
671         {
672                 capHandler.HandleEvent(ev);
673         }
674 };
675
676 MODULE_INIT(ModuleSSLGnuTLS)