5830da909d63475dc5373a65e256d3478be3674c
[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
281                                         ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
282                                         X509_set_issuer_name(cer, req->req_info->subject);
283                                         X509_set_subject_name(cer, req->req_info->subject);
284                                         X509_gmtime_adj(X509_get_notBefore(cer),0);
285                                         X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
286                                         req_pkey = X509_REQ_get_pubkey(req);
287                                         X509_set_pubkey(cer, req_pkey);
288                                         EVP_PKEY_free(req_pkey);
289                                         
290                                         /* Sign the cert */
291                                         if (!X509_sign(cer, pk, EVP_md5())) {
292                                                 syslog(LOG_ERR, "crypto: X509_sign() error");
293                                         }
294                                         else {
295                                                 /* Write it to disk. */ 
296                                                 fp = fopen(file_crpt_file_cer, "w");
297                                                 if (fp != NULL) {
298                                                         chmod(file_crpt_file_cer, 0600);
299                                                         PEM_write_X509(fp, cer);
300                                                         fclose(fp);
301                                                 }
302                                         }
303                                         X509_free(cer);
304                                 }
305                         }
306
307                         RSA_free(rsa);
308                 }
309         }
310
311
312         /*
313          * Now try to bind to the key and certificate.
314          */
315         SSL_CTX_use_certificate_chain_file(ssl_ctx, file_crpt_file_cer);
316         SSL_CTX_use_PrivateKey_file(ssl_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
317         if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
318                 syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
319         }
320
321         /* Finally let the server know we're here */
322         CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
323         CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
324         CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
325 }
326
327
328 /*
329  * client_write_ssl() Send binary data to the client encrypted.
330  */
331 void client_write_ssl(const char *buf, int nbytes)
332 {
333         int retval;
334         int nremain;
335         char junk[1];
336
337         nremain = nbytes;
338
339         while (nremain > 0) {
340                 if (SSL_want_write(CC->ssl)) {
341                         if ((SSL_read(CC->ssl, junk, 0)) < 1) {
342                                 syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
343                         }
344                 }
345                 retval =
346                     SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
347                 if (retval < 1) {
348                         long errval;
349
350                         errval = SSL_get_error(CC->ssl, retval);
351                         if (errval == SSL_ERROR_WANT_READ ||
352                             errval == SSL_ERROR_WANT_WRITE) {
353                                 sleep(1);
354                                 continue;
355                         }
356                         syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
357                         if (retval == -1) {
358                                 syslog(LOG_DEBUG, "crypto: errno is %d", errno);
359                         }
360                         endtls();
361                         client_write(&buf[nbytes - nremain], nremain);
362                         return;
363                 }
364                 nremain -= retval;
365         }
366 }
367
368
369 /*
370  * read data from the encrypted layer.
371  */
372 int client_read_sslbuffer(StrBuf *buf, int timeout)
373 {
374         char sbuf[16384]; /* OpenSSL communicates in 16k blocks, so let's speak its native tongue. */
375         int rlen;
376         char junk[1];
377         SSL *pssl = CC->ssl;
378
379         if (pssl == NULL) return(-1);
380
381         while (1) {
382                 if (SSL_want_read(pssl)) {
383                         if ((SSL_write(pssl, junk, 0)) < 1) {
384                                 syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
385                         }
386                 }
387                 rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
388                 if (rlen < 1) {
389                         long errval;
390
391                         errval = SSL_get_error(pssl, rlen);
392                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
393                                 sleep(1);
394                                 continue;
395                         }
396                         syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
397                         endtls();
398                         return (-1);
399                 }
400                 StrBufAppendBufPlain(buf, sbuf, rlen, 0);
401                 return rlen;
402         }
403         return (0);
404 }
405
406 int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout)
407 {
408         CitContext *CCC = CC;
409         const char *pos = NULL;
410         const char *pLF;
411         int len, rlen;
412         int nSuccessLess = 0;
413         const char *pch = NULL;
414         
415         if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL))
416         {
417                 if (Pos != NULL)
418                 {
419                         *Pos = NULL;
420                 }
421                 return -1;
422         }
423
424         pos = *Pos;
425         if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) 
426         {
427                 pch = pos;
428                 pch = strchr(pch, '\n');
429                 
430                 if (pch == NULL) {
431                         StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
432                         FlushStrBuf(IOBuf);
433                         *Pos = NULL;
434                 }
435                 else {
436                         int n = 0;
437                         if ((pch > ChrPtr(IOBuf)) && 
438                             (*(pch - 1) == '\r')) {
439                                 n = 1;
440                         }
441                         StrBufAppendBufPlain(Line, pos, 
442                                              (pch - pos - n), 0);
443
444                         if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
445                                 FlushStrBuf(IOBuf);
446                                 *Pos = NULL;
447                         }
448                         else 
449                                 *Pos = pch + 1;
450                         return StrLength(Line);
451                 }
452         }
453
454         pLF = NULL;
455         while ((nSuccessLess < timeout) && 
456                (pLF == NULL) &&
457                (CCC->ssl != NULL)) {
458
459                 rlen = client_read_sslbuffer(IOBuf, timeout);
460                 if (rlen < 1) {
461                         return -1;
462                 }
463                 else if (rlen > 0) {
464                         pLF = strchr(ChrPtr(IOBuf), '\n');
465                 }
466         }
467         *Pos = NULL;
468         if (pLF != NULL) {
469                 pos = ChrPtr(IOBuf);
470                 len = pLF - pos;
471                 if (len > 0 && (*(pLF - 1) == '\r') )
472                         len --;
473                 StrBufAppendBufPlain(Line, pos, len, 0);
474                 if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf))
475                 {
476                         FlushStrBuf(IOBuf);
477                 }
478                 else 
479                         *Pos = pLF + 1;
480                 return StrLength(Line);
481         }
482         return -1;
483 }
484
485
486 int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
487 {
488         long baselen;
489         long RemainRead;
490         int retval = 0;
491         CitContext *CCC = CC;
492
493         baselen = StrLength(Target);
494
495         if (StrLength(CCC->RecvBuf.Buf) > 0)
496         {
497                 long RemainLen;
498                 long TotalLen;
499                 const char *pchs;
500
501                 if (CCC->RecvBuf.ReadWritePointer == NULL)
502                 {
503                         CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf);
504                 }
505                 pchs = ChrPtr(CCC->RecvBuf.Buf);
506                 TotalLen = StrLength(CCC->RecvBuf.Buf);
507                 RemainLen = TotalLen - (pchs - CCC->RecvBuf.ReadWritePointer);
508                 if (RemainLen > bytes)
509                 {
510                         RemainLen = bytes;
511                 }
512                 if (RemainLen > 0)
513                 {
514                         StrBufAppendBufPlain(Target, CCC->RecvBuf.ReadWritePointer, RemainLen, 0);
515                         CCC->RecvBuf.ReadWritePointer += RemainLen;
516                 }
517                 if ((ChrPtr(CCC->RecvBuf.Buf) + StrLength(CCC->RecvBuf.Buf)) <= CCC->RecvBuf.ReadWritePointer)
518                 {
519                         CCC->RecvBuf.ReadWritePointer = NULL;
520                         FlushStrBuf(CCC->RecvBuf.Buf);
521                 }
522         }
523
524         if (StrLength(Target) >= bytes + baselen)
525         {
526                 return 1;
527         }
528
529         CCC->RecvBuf.ReadWritePointer = NULL;
530
531         while ((StrLength(Target) < bytes + baselen) && (retval >= 0))
532         {
533                 retval = client_read_sslbuffer(CCC->RecvBuf.Buf, timeout);
534                 if (retval >= 0) {
535                         RemainRead = bytes - (StrLength (Target) - baselen);
536                         if (RemainRead < StrLength(CCC->RecvBuf.Buf))
537                         {
538                                 StrBufAppendBufPlain(
539                                         Target, 
540                                         ChrPtr(CCC->RecvBuf.Buf), 
541                                         RemainRead, 0);
542                                 CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf) + RemainRead;
543                                 break;
544                         }
545                         StrBufAppendBuf(Target, CCC->RecvBuf.Buf, 0); /* todo: Buf > bytes? */
546                         FlushStrBuf(CCC->RecvBuf.Buf);
547                 }
548                 else 
549                 {
550                         FlushStrBuf(CCC->RecvBuf.Buf);
551                         return -1;
552         
553                 }
554         }
555         return 1;
556 }
557
558
559 /*
560  * CtdlStartTLS() starts SSL/TLS encryption for the current session.  It
561  * must be supplied with pre-generated strings for responses of "ok," "no
562  * support for TLS," and "error" so that we can use this in any protocol.
563  */
564 void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
565 {
566         int retval, bits, alg_bits;
567
568         if (!ssl_ctx) {
569                 syslog(LOG_ERR, "crypto: SSL failed: no ssl_ctx exists?");
570                 if (nosup_response != NULL) cprintf("%s", nosup_response);
571                 return;
572         }
573         if (!(CC->ssl = SSL_new(ssl_ctx))) {
574                 syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
575                 if (error_response != NULL) {
576                         cprintf("%s", error_response);
577                 }
578                 return;
579         }
580         if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
581                 syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
582                 SSL_free(CC->ssl);
583                 CC->ssl = NULL;
584                 if (error_response != NULL) cprintf("%s", error_response);
585                 return;
586         }
587         if (ok_response != NULL) cprintf("%s", ok_response);
588         retval = SSL_accept(CC->ssl);
589         if (retval < 1) {
590                 /*
591                  * Can't notify the client of an error here; they will
592                  * discover the problem at the SSL layer and should
593                  * revert to unencrypted communications.
594                  */
595                 long errval;
596                 char error_string[128];
597
598                 errval = SSL_get_error(CC->ssl, retval);
599                 syslog(LOG_ERR, "crypto: SSL_accept failed: retval=%d, errval=%ld, err=%s",
600                         retval,
601                         errval,
602                         ERR_error_string(errval, error_string)
603                 );
604                 SSL_free(CC->ssl);
605                 CC->ssl = NULL;
606                 return;
607         }
608         BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
609         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
610         syslog(LOG_INFO, "crypto: SSL/TLS using %s on %s (%d of %d bits)",
611                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
612                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
613                 bits, alg_bits
614         );
615         CC->redirect_ssl = 1;
616 }
617
618
619 /*
620  * cmd_stls() starts SSL/TLS encryption for the current session
621  */
622 void cmd_stls(char *params)
623 {
624         char ok_response[SIZ];
625         char nosup_response[SIZ];
626         char error_response[SIZ];
627
628         unbuffer_output();
629
630         sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
631         sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
632         sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
633
634         CtdlStartTLS(ok_response, nosup_response, error_response);
635 }
636
637
638 /*
639  * cmd_gtls() returns status info about the TLS connection
640  */
641 void cmd_gtls(char *params)
642 {
643         int bits, alg_bits;
644
645         if (!CC->ssl || !CC->redirect_ssl) {
646                 cprintf("%d Session is not encrypted.\n", ERROR);
647                 return;
648         }
649         bits =
650             SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
651                                 &alg_bits);
652         cprintf("%d %s|%s|%d|%d\n", CIT_OK,
653                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
654                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
655                 alg_bits, bits);
656 }
657
658
659 /*
660  * endtls() shuts down the TLS connection
661  *
662  * WARNING:  This may make your session vulnerable to a known plaintext
663  * attack in the current implmentation.
664  */
665 void endtls(void)
666 {
667         if (!CC->ssl) {
668                 CC->redirect_ssl = 0;
669                 return;
670         }
671
672         syslog(LOG_INFO, "crypto: ending SSL/TLS");
673         SSL_shutdown(CC->ssl);
674         SSL_free(CC->ssl);
675         CC->ssl = NULL;
676         CC->redirect_ssl = 0;
677 }
678
679
680 /*
681  * ssl_lock() callback for OpenSSL mutex locks
682  */
683 void ssl_lock(int mode, int n, const char *file, int line)
684 {
685         if (mode & CRYPTO_LOCK)
686         {
687                 pthread_mutex_lock(SSLCritters[n]);
688         }
689         else {
690                 pthread_mutex_unlock(SSLCritters[n]);
691         }
692 }
693 #endif                          /* HAVE_OPENSSL */