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