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