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