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