ba9a6d03c2b4575f8c43cf46e730d32618cd0e17
[citadel.git] / citadel / modules / crypto / serv_crypto.c
1 /*
2  * Copyright (c) 1987-2018 by the citadel.org team
3  *
4  * This program is open source software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include "sysdep.h"
18
19 #ifdef HAVE_OPENSSL
20 #include <openssl/ssl.h>
21 #include <openssl/err.h>
22 #include <openssl/rand.h>
23 #endif
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #ifdef HAVE_PTHREAD_H
37 #include <pthread.h>
38 #endif
39
40 #ifdef HAVE_SYS_SELECT_H
41 #include <sys/select.h>
42 #endif
43
44 #include <stdio.h>
45 #include <libcitadel.h>
46 #include "server.h"
47 #include "serv_crypto.h"
48 #include "sysdep_decls.h"
49 #include "citadel.h"
50 #include "config.h"
51
52
53 #include "ctdl_module.h"
54
55 #ifdef HAVE_OPENSSL
56 SSL_CTX *ssl_ctx;               /* SSL context */
57 pthread_mutex_t **SSLCritters;  /* Things needing locking */
58
59 static unsigned long id_callback(void)
60 {
61         return (unsigned long) pthread_self();
62 }
63
64 void destruct_ssl(void)
65 {
66         int a;
67         for (a = 0; a < CRYPTO_num_locks(); a++) 
68                 free(SSLCritters[a]);
69         free (SSLCritters);
70 }
71
72
73 void generate_key(char *keyfilename)
74 {
75         int ret = 0;
76         RSA *rsa = NULL;
77         BIGNUM *bne = NULL;
78         int bits = 2048;
79         unsigned long e = RSA_F4;
80         FILE *fp;
81
82         if (access(keyfilename, R_OK) == 0) {
83                 return;
84         }
85
86         syslog(LOG_INFO, "crypto: generating RSA key pair");
87  
88         // generate rsa key
89         bne = BN_new();
90         ret = BN_set_word(bne,e);
91         if (ret != 1) {
92                 goto free_all;
93         }
94  
95         rsa = RSA_new();
96         ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
97         if (ret != 1) {
98                 goto free_all;
99         }
100
101         // write the key file
102         fp = fopen(keyfilename, "w");
103         if (fp != NULL) {
104                 chmod(keyfilename, 0600);
105                 if (PEM_write_RSAPrivateKey(fp, /* the file */
106                                         rsa,    /* the key */
107                                         NULL,   /* no enc */
108                                         NULL,   /* no passphr */
109                                         0,      /* no passphr */
110                                         NULL,   /* no callbk */
111                                         NULL    /* no callbk */
112                 ) != 1) {
113                         syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
114                         unlink(keyfilename);
115                 }
116                 fclose(fp);
117         }
118
119     // 4. free
120 free_all:
121     RSA_free(rsa);
122     BN_free(bne);
123 }
124
125
126 void init_ssl(void)
127 {
128         const SSL_METHOD *ssl_method;
129         RSA *rsa = NULL;
130         X509_REQ *req = NULL;
131         X509 *cer = NULL;
132         EVP_PKEY *pk = NULL;
133         EVP_PKEY *req_pkey = NULL;
134         X509_NAME *name = NULL;
135         FILE *fp;
136
137         SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
138         if (!SSLCritters) {
139                 syslog(LOG_ERR, "crypto: can't allocate memory!");
140                 exit(CTDLEXIT_CRYPTO);
141         } else {
142                 int a;
143
144                 for (a = 0; a < CRYPTO_num_locks(); a++) {
145                         SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
146                         if (!SSLCritters[a]) {
147                                 syslog(LOG_ERR, "crypto: can't allocate memory!!");
148                                 exit(CTDLEXIT_CRYPTO);
149                         }
150                         pthread_mutex_init(SSLCritters[a], NULL);
151                 }
152         }
153
154         /*
155          * Initialize SSL transport layer
156          */
157         SSL_library_init();
158         SSL_load_error_strings();
159         ssl_method = SSLv23_server_method();
160         if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
161                 syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
162                 return;
163         }
164         if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
165                 syslog(LOG_ERR, "crypto: No ciphers available");
166                 SSL_CTX_free(ssl_ctx);
167                 ssl_ctx = NULL;
168                 return;
169         }
170
171         CRYPTO_set_locking_callback(ssl_lock);
172         CRYPTO_set_id_callback(id_callback);
173
174         mkdir(ctdl_key_dir, 0700);              // If the keys directory does not exist, create it
175         generate_key(file_crpt_file_key);       // If a private key does not exist, create it
176
177         /*
178          * If there is no certificate file on disk, we will be generating a self-signed certificate
179          * in the next step.  Therefore, if we have neither a CSR nor a certificate, generate
180          * the CSR in this step so that the next step may commence.
181          */
182         if ( (access(file_crpt_file_cer, R_OK) != 0) && (access(file_crpt_file_csr, R_OK) != 0) ) {
183                 syslog(LOG_INFO, "crypto: generating a generic certificate signing request.");
184
185                 /*
186                  * Read our key from the file.  No, we don't just keep this
187                  * in memory from the above key-generation function, because
188                  * there is the possibility that the key was already on disk
189                  * and we didn't just generate it now.
190                  */
191                 fp = fopen(file_crpt_file_key, "r");
192                 if (fp) {
193                         rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
194                         fclose(fp);
195                 }
196
197                 if (rsa) {
198
199                         /* Create a public key from the private key */
200                         if (pk=EVP_PKEY_new(), pk != NULL) {
201                                 EVP_PKEY_assign_RSA(pk, rsa);
202                                 if (req = X509_REQ_new(), req != NULL) {
203
204                                         /* Set the public key */
205                                         X509_REQ_set_pubkey(req, pk);
206                                         X509_REQ_set_version(req, 0L);
207
208                                         name = X509_REQ_get_subject_name(req);
209
210                                         /* Tell it who we are */
211                                         X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned const char *)"ZZ", -1, -1, 0);
212                                         X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, (unsigned const char *)"The World", -1, -1, 0);
213                                         X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, (unsigned const char *)"My Location", -1, -1, 0);
214                                         X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned const char *)"Generic certificate", -1, -1, 0);
215                                         X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned const char *)"Citadel server", -1, -1, 0);
216                                         X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned const char *)"*", -1, -1, 0);
217                                         X509_REQ_set_subject_name(req, name);
218
219                                         /* Sign the CSR */
220                                         if (!X509_REQ_sign(req, pk, EVP_md5())) {
221                                                 syslog(LOG_ERR, "crypto: X509_REQ_sign(): error");
222                                         }
223                                         else {
224                                                 /* Write it to disk. */ 
225                                                 fp = fopen(file_crpt_file_csr, "w");
226                                                 if (fp != NULL) {
227                                                         chmod(file_crpt_file_csr, 0600);
228                                                         PEM_write_X509_REQ(fp, req);
229                                                         fclose(fp);
230                                                 }
231                                         }
232
233                                         X509_REQ_free(req);
234                                 }
235                         }
236
237                         RSA_free(rsa);
238                 }
239
240                 else {
241                         syslog(LOG_ERR, "crypto: unable to read private key.");
242                 }
243         }
244
245
246         /*
247          * Generate a self-signed certificate if we don't have one.
248          */
249         if (access(file_crpt_file_cer, R_OK) != 0) {
250                 syslog(LOG_INFO, "crypto: generating a generic self-signed certificate.");
251
252                 /* Same deal as before: always read the key from disk because
253                  * it may or may not have just been generated.
254                  */
255                 fp = fopen(file_crpt_file_key, "r");
256                 if (fp) {
257                         rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
258                         fclose(fp);
259                 }
260
261                 /* This also holds true for the CSR. */
262                 req = NULL;
263                 cer = NULL;
264                 pk = NULL;
265                 if (rsa) {
266                         if (pk=EVP_PKEY_new(), pk != NULL) {
267                                 EVP_PKEY_assign_RSA(pk, rsa);
268                         }
269
270                         fp = fopen(file_crpt_file_csr, "r");
271                         if (fp) {
272                                 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
273                                 fclose(fp);
274                         }
275
276                         if (req) {
277                                 if (cer = X509_new(), cer != NULL) {
278                                         ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
279                                         X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
280                                         X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
281                                         X509_gmtime_adj(X509_get_notBefore(cer),0);
282                                         X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
283                                         req_pkey = X509_REQ_get_pubkey(req);
284                                         X509_set_pubkey(cer, req_pkey);
285                                         EVP_PKEY_free(req_pkey);
286                                         
287                                         /* Sign the cert */
288                                         if (!X509_sign(cer, pk, EVP_md5())) {
289                                                 syslog(LOG_ERR, "crypto: X509_sign() error");
290                                         }
291                                         else {
292                                                 /* Write it to disk. */ 
293                                                 fp = fopen(file_crpt_file_cer, "w");
294                                                 if (fp != NULL) {
295                                                         chmod(file_crpt_file_cer, 0600);
296                                                         PEM_write_X509(fp, cer);
297                                                         fclose(fp);
298                                                 }
299                                         }
300                                         X509_free(cer);
301                                 }
302                         }
303
304                         RSA_free(rsa);
305                 }
306         }
307
308
309         /*
310          * Now try to bind to the key and certificate.
311          */
312         SSL_CTX_use_certificate_chain_file(ssl_ctx, file_crpt_file_cer);
313         SSL_CTX_use_PrivateKey_file(ssl_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
314         if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
315                 syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
316         }
317
318         /* Finally let the server know we're here */
319         CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
320         CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
321         CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
322 }
323
324
325 /*
326  * client_write_ssl() Send binary data to the client encrypted.
327  */
328 void client_write_ssl(const char *buf, int nbytes)
329 {
330         int retval;
331         int nremain;
332         char junk[1];
333
334         nremain = nbytes;
335
336         while (nremain > 0) {
337                 if (SSL_want_write(CC->ssl)) {
338                         if ((SSL_read(CC->ssl, junk, 0)) < 1) {
339                                 syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
340                         }
341                 }
342                 retval =
343                     SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
344                 if (retval < 1) {
345                         long errval;
346
347                         errval = SSL_get_error(CC->ssl, retval);
348                         if (errval == SSL_ERROR_WANT_READ ||
349                             errval == SSL_ERROR_WANT_WRITE) {
350                                 sleep(1);
351                                 continue;
352                         }
353                         syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
354                         if (retval == -1) {
355                                 syslog(LOG_DEBUG, "crypto: errno is %d", errno);
356                         }
357                         endtls();
358                         client_write(&buf[nbytes - nremain], nremain);
359                         return;
360                 }
361                 nremain -= retval;
362         }
363 }
364
365
366 /*
367  * read data from the encrypted layer.
368  */
369 int client_read_sslbuffer(StrBuf *buf, int timeout)
370 {
371         char sbuf[16384]; /* OpenSSL communicates in 16k blocks, so let's speak its native tongue. */
372         int rlen;
373         char junk[1];
374         SSL *pssl = CC->ssl;
375
376         if (pssl == NULL) return(-1);
377
378         while (1) {
379                 if (SSL_want_read(pssl)) {
380                         if ((SSL_write(pssl, junk, 0)) < 1) {
381                                 syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
382                         }
383                 }
384                 rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
385                 if (rlen < 1) {
386                         long errval;
387
388                         errval = SSL_get_error(pssl, rlen);
389                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
390                                 sleep(1);
391                                 continue;
392                         }
393                         syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
394                         endtls();
395                         return (-1);
396                 }
397                 StrBufAppendBufPlain(buf, sbuf, rlen, 0);
398                 return rlen;
399         }
400         return (0);
401 }
402
403 int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout)
404 {
405         CitContext *CCC = CC;
406         const char *pos = NULL;
407         const char *pLF;
408         int len, rlen;
409         int nSuccessLess = 0;
410         const char *pch = NULL;
411         
412         if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL))
413         {
414                 if (Pos != NULL)
415                 {
416                         *Pos = NULL;
417                 }
418                 return -1;
419         }
420
421         pos = *Pos;
422         if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) 
423         {
424                 pch = pos;
425                 pch = strchr(pch, '\n');
426                 
427                 if (pch == NULL) {
428                         StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
429                         FlushStrBuf(IOBuf);
430                         *Pos = NULL;
431                 }
432                 else {
433                         int n = 0;
434                         if ((pch > ChrPtr(IOBuf)) && 
435                             (*(pch - 1) == '\r')) {
436                                 n = 1;
437                         }
438                         StrBufAppendBufPlain(Line, pos, 
439                                              (pch - pos - n), 0);
440
441                         if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
442                                 FlushStrBuf(IOBuf);
443                                 *Pos = NULL;
444                         }
445                         else 
446                                 *Pos = pch + 1;
447                         return StrLength(Line);
448                 }
449         }
450
451         pLF = NULL;
452         while ((nSuccessLess < timeout) && 
453                (pLF == NULL) &&
454                (CCC->ssl != NULL)) {
455
456                 rlen = client_read_sslbuffer(IOBuf, timeout);
457                 if (rlen < 1) {
458                         return -1;
459                 }
460                 else if (rlen > 0) {
461                         pLF = strchr(ChrPtr(IOBuf), '\n');
462                 }
463         }
464         *Pos = NULL;
465         if (pLF != NULL) {
466                 pos = ChrPtr(IOBuf);
467                 len = pLF - pos;
468                 if (len > 0 && (*(pLF - 1) == '\r') )
469                         len --;
470                 StrBufAppendBufPlain(Line, pos, len, 0);
471                 if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf))
472                 {
473                         FlushStrBuf(IOBuf);
474                 }
475                 else 
476                         *Pos = pLF + 1;
477                 return StrLength(Line);
478         }
479         return -1;
480 }
481
482
483 int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
484 {
485         long baselen;
486         long RemainRead;
487         int retval = 0;
488         CitContext *CCC = CC;
489
490         baselen = StrLength(Target);
491
492         if (StrLength(CCC->RecvBuf.Buf) > 0)
493         {
494                 long RemainLen;
495                 long TotalLen;
496                 const char *pchs;
497
498                 if (CCC->RecvBuf.ReadWritePointer == NULL)
499                 {
500                         CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf);
501                 }
502                 pchs = ChrPtr(CCC->RecvBuf.Buf);
503                 TotalLen = StrLength(CCC->RecvBuf.Buf);
504                 RemainLen = TotalLen - (pchs - CCC->RecvBuf.ReadWritePointer);
505                 if (RemainLen > bytes)
506                 {
507                         RemainLen = bytes;
508                 }
509                 if (RemainLen > 0)
510                 {
511                         StrBufAppendBufPlain(Target, CCC->RecvBuf.ReadWritePointer, RemainLen, 0);
512                         CCC->RecvBuf.ReadWritePointer += RemainLen;
513                 }
514                 if ((ChrPtr(CCC->RecvBuf.Buf) + StrLength(CCC->RecvBuf.Buf)) <= CCC->RecvBuf.ReadWritePointer)
515                 {
516                         CCC->RecvBuf.ReadWritePointer = NULL;
517                         FlushStrBuf(CCC->RecvBuf.Buf);
518                 }
519         }
520
521         if (StrLength(Target) >= bytes + baselen)
522         {
523                 return 1;
524         }
525
526         CCC->RecvBuf.ReadWritePointer = NULL;
527
528         while ((StrLength(Target) < bytes + baselen) && (retval >= 0))
529         {
530                 retval = client_read_sslbuffer(CCC->RecvBuf.Buf, timeout);
531                 if (retval >= 0) {
532                         RemainRead = bytes - (StrLength (Target) - baselen);
533                         if (RemainRead < StrLength(CCC->RecvBuf.Buf))
534                         {
535                                 StrBufAppendBufPlain(
536                                         Target, 
537                                         ChrPtr(CCC->RecvBuf.Buf), 
538                                         RemainRead, 0);
539                                 CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf) + RemainRead;
540                                 break;
541                         }
542                         StrBufAppendBuf(Target, CCC->RecvBuf.Buf, 0); /* todo: Buf > bytes? */
543                         FlushStrBuf(CCC->RecvBuf.Buf);
544                 }
545                 else 
546                 {
547                         FlushStrBuf(CCC->RecvBuf.Buf);
548                         return -1;
549         
550                 }
551         }
552         return 1;
553 }
554
555
556 /*
557  * CtdlStartTLS() starts SSL/TLS encryption for the current session.  It
558  * must be supplied with pre-generated strings for responses of "ok," "no
559  * support for TLS," and "error" so that we can use this in any protocol.
560  */
561 void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
562 {
563         int retval, bits, alg_bits;
564
565         if (!ssl_ctx) {
566                 syslog(LOG_ERR, "crypto: SSL failed: no ssl_ctx exists?");
567                 if (nosup_response != NULL) cprintf("%s", nosup_response);
568                 return;
569         }
570         if (!(CC->ssl = SSL_new(ssl_ctx))) {
571                 syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
572                 if (error_response != NULL) {
573                         cprintf("%s", error_response);
574                 }
575                 return;
576         }
577         if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
578                 syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
579                 SSL_free(CC->ssl);
580                 CC->ssl = NULL;
581                 if (error_response != NULL) cprintf("%s", error_response);
582                 return;
583         }
584         if (ok_response != NULL) cprintf("%s", ok_response);
585         retval = SSL_accept(CC->ssl);
586         if (retval < 1) {
587                 /*
588                  * Can't notify the client of an error here; they will
589                  * discover the problem at the SSL layer and should
590                  * revert to unencrypted communications.
591                  */
592                 long errval;
593                 char error_string[128];
594
595                 errval = SSL_get_error(CC->ssl, retval);
596                 syslog(LOG_ERR, "crypto: SSL_accept failed: retval=%d, errval=%ld, err=%s",
597                         retval,
598                         errval,
599                         ERR_error_string(errval, error_string)
600                 );
601                 SSL_free(CC->ssl);
602                 CC->ssl = NULL;
603                 return;
604         }
605         // BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE); not needed anymore in openssl 1.1 ?
606         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
607         syslog(LOG_INFO, "crypto: SSL/TLS using %s on %s (%d of %d bits)",
608                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
609                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
610                 bits, alg_bits
611         );
612         CC->redirect_ssl = 1;
613 }
614
615
616 /*
617  * cmd_stls() starts SSL/TLS encryption for the current session
618  */
619 void cmd_stls(char *params)
620 {
621         char ok_response[SIZ];
622         char nosup_response[SIZ];
623         char error_response[SIZ];
624
625         unbuffer_output();
626
627         sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
628         sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
629         sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
630
631         CtdlStartTLS(ok_response, nosup_response, error_response);
632 }
633
634
635 /*
636  * cmd_gtls() returns status info about the TLS connection
637  */
638 void cmd_gtls(char *params)
639 {
640         int bits, alg_bits;
641
642         if (!CC->ssl || !CC->redirect_ssl) {
643                 cprintf("%d Session is not encrypted.\n", ERROR);
644                 return;
645         }
646         bits =
647             SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
648                                 &alg_bits);
649         cprintf("%d %s|%s|%d|%d\n", CIT_OK,
650                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
651                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
652                 alg_bits, bits);
653 }
654
655
656 /*
657  * endtls() shuts down the TLS connection
658  *
659  * WARNING:  This may make your session vulnerable to a known plaintext
660  * attack in the current implmentation.
661  */
662 void endtls(void)
663 {
664         if (!CC->ssl) {
665                 CC->redirect_ssl = 0;
666                 return;
667         }
668
669         syslog(LOG_INFO, "crypto: ending SSL/TLS");
670         SSL_shutdown(CC->ssl);
671         SSL_free(CC->ssl);
672         CC->ssl = NULL;
673         CC->redirect_ssl = 0;
674 }
675
676
677 /*
678  * ssl_lock() callback for OpenSSL mutex locks
679  */
680 void ssl_lock(int mode, int n, const char *file, int line)
681 {
682         if (mode & CRYPTO_LOCK)
683         {
684                 pthread_mutex_lock(SSLCritters[n]);
685         }
686         else {
687                 pthread_mutex_unlock(SSLCritters[n]);
688         }
689 }
690 #endif                          /* HAVE_OPENSSL */