695ae4209740e3eda46ec1675b9e6837a2197a14
[citadel.git] / citadel / modules / crypto / serv_crypto.c
1 // Copyright (c) 1987-2022 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
44 SSL_CTX *ssl_ctx = NULL;                // This SSL context is used for all sessions.
45 char *ssl_cipher_list = CIT_CIPHERS;
46
47 // If a private key does not exist, generate one now.
48 void generate_key(char *keyfilename) {
49         int ret = 0;
50         RSA *rsa = NULL;
51         BIGNUM *bne = NULL;
52         int bits = 2048;
53         unsigned long e = RSA_F4;
54         FILE *fp;
55
56         if (access(keyfilename, R_OK) == 0) {   // Already have one.
57                 return;
58         }
59
60         syslog(LOG_INFO, "crypto: generating RSA key pair");
61  
62         // generate rsa key
63         bne = BN_new();
64         ret = BN_set_word(bne,e);
65         if (ret != 1) {
66                 goto free_all;
67         }
68  
69         rsa = RSA_new();
70         ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
71         if (ret != 1) {
72                 goto free_all;
73         }
74
75         // write the key file
76         fp = fopen(keyfilename, "w");
77         if (fp != NULL) {
78                 chmod(keyfilename, 0600);
79                 if (PEM_write_RSAPrivateKey(fp, // the file
80                                         rsa,    // the key
81                                         NULL,   // no enc
82                                         NULL,   // no passphrase
83                                         0,      // no passphrase
84                                         NULL,   // no callback
85                                         NULL    // no callback
86                 ) != 1) {
87                         syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
88                         unlink(keyfilename);
89                 }
90                 fclose(fp);
91         }
92
93         // free the memory we used
94 free_all:
95         RSA_free(rsa);
96         BN_free(bne);
97 }
98
99
100 // If a certificate does not exist, generate a self-signed one now.
101 void generate_certificate(char *keyfilename, char *certfilename) {
102         RSA *private_key = NULL;
103         EVP_PKEY *public_key = NULL;
104         X509_REQ *certificate_signing_request = NULL;
105         X509_NAME *name = NULL;
106         X509 *certificate = NULL;
107         FILE *fp;
108
109         if (access(certfilename, R_OK) == 0) {                  // already have one.
110                 return;
111         }
112
113         syslog(LOG_INFO, "crypto: generating a self-signed certificate");
114
115         // Read in our private key.
116         fp = fopen(keyfilename, "r");
117         if (fp) {
118                 private_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
119                 fclose(fp);
120         }
121
122         if (!private_key) {
123                 syslog(LOG_ERR, "crypto: cannot read the private key");
124                 return;
125         }
126
127         // Create a public key from the private key
128         public_key = EVP_PKEY_new();
129         if (!public_key) {
130                 syslog(LOG_ERR, "crypto: cannot allocate public key");
131                 RSA_free(private_key);
132                 return;
133         }
134         EVP_PKEY_assign_RSA(public_key, private_key);
135
136         // Create a certificate signing request
137         certificate_signing_request = X509_REQ_new();
138         if (!certificate_signing_request) {
139                 syslog(LOG_ERR, "crypto: cannot allocate certificate signing request");
140                 RSA_free(private_key);
141                 EVP_PKEY_free(public_key);
142                 return;
143         }
144
145         // Assign the public key to the certificate signing request
146         X509_REQ_set_pubkey(certificate_signing_request, public_key);
147         X509_REQ_set_version(certificate_signing_request, 0L);
148
149         // Tell it who we are
150         name = X509_REQ_get_subject_name(certificate_signing_request);
151         X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned const char *)"ZZ", -1, -1, 0);
152         X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, (unsigned const char *)"The World", -1, -1, 0);
153         X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, (unsigned const char *)"My Location", -1, -1, 0);
154         X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned const char *)"Generic certificate", -1, -1, 0);
155         X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned const char *)"Citadel server", -1, -1, 0);
156         X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned const char *)"*", -1, -1, 0);
157         X509_REQ_set_subject_name(certificate_signing_request, name);
158
159         // Sign the CSR
160         if (!X509_REQ_sign(certificate_signing_request, public_key, EVP_md5())) {
161                 syslog(LOG_ERR, "crypto: X509_REQ_sign(): error");
162                 X509_REQ_free(certificate_signing_request);
163                 RSA_free(private_key);
164                 EVP_PKEY_free(public_key);
165                 return;
166         }
167
168         // Generate the self-signed certificate
169         certificate = X509_new();
170         if (!certificate) {
171                 syslog(LOG_ERR, "crypto: cannot allocate X.509 certificate");
172                 X509_REQ_free(certificate_signing_request);
173                 RSA_free(private_key);
174                 EVP_PKEY_free(public_key);
175                 return;
176         }
177
178         ASN1_INTEGER_set(X509_get_serialNumber(certificate), 0);
179         X509_set_issuer_name(certificate, X509_REQ_get_subject_name(certificate_signing_request));
180         X509_set_subject_name(certificate, X509_REQ_get_subject_name(certificate_signing_request));
181         X509_gmtime_adj(X509_get_notBefore(certificate), 0);
182         X509_gmtime_adj(X509_get_notAfter(certificate), (long)60*60*24*SIGN_DAYS);
183         X509_set_pubkey(certificate, public_key);
184         X509_REQ_free(certificate_signing_request);             // We're done with the CSR; free it
185
186         // Finally, sign the certificate with our private key.
187         if (!X509_sign(certificate, public_key, EVP_md5())) {
188                 syslog(LOG_ERR, "crypto: X509_sign() error");
189                 X509_free(certificate);
190                 RSA_free(private_key);
191                 EVP_PKEY_free(public_key);
192                 return;
193         }
194
195         // Write the certificate to disk
196         fp = fopen(certfilename, "w");
197         if (fp != NULL) {
198                 chmod(certfilename, 0600);
199                 PEM_write_X509(fp, certificate);
200                 fclose(fp);
201         }
202         else {
203                 syslog(LOG_ERR, "crypto: %s: %m", certfilename);
204         }
205
206         X509_free(certificate);
207         EVP_PKEY_free(public_key);
208         // RSA_free(private_key);                               // private_key is freed by EVP_PKEY_free() above
209 }
210
211
212 // Set the private key and certificate chain for the global SSL Context.
213 // This is called during initialization, and can be called again later if the certificate changes.
214 void bind_to_key_and_certificate(void) {
215
216         SSL_CTX *old_ctx = NULL;
217         SSL_CTX *new_ctx = NULL;
218
219         const SSL_METHOD *method = TLS_server_method();
220         if (!method) {
221                 syslog(LOG_ERR, "crypto: TLS_server_method() failed: %s", ERR_reason_error_string(ERR_get_error()));
222                 return;
223         }
224
225         new_ctx = SSL_CTX_new(method);
226         if (!new_ctx) {
227                 syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
228                 return;
229         }
230
231         if (!(SSL_CTX_set_cipher_list(new_ctx, ssl_cipher_list))) {
232                 syslog(LOG_ERR, "crypto: SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
233                 return;
234         }
235
236         syslog(LOG_DEBUG, "crypto: using certificate chain %s", file_crpt_file_cer);
237         SSL_CTX_use_certificate_chain_file(new_ctx, file_crpt_file_cer);
238
239         syslog(LOG_DEBUG, "crypto: using private key %s", file_crpt_file_key);
240         SSL_CTX_use_PrivateKey_file(new_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
241
242         old_ctx = ssl_ctx;
243         ssl_ctx = new_ctx;              // All future binds will use the new certificate
244
245         if (old_ctx != NULL) {
246                 sleep(1);               // Hopefully wait until all in-progress binds to the old certificate have completed
247                 SSL_CTX_free(old_ctx);
248         }
249 }
250
251
252 // Check the modification time of the key and certificate files.  Reload if either one changed.
253 void update_key_and_cert_if_needed(void) {
254         static time_t previous_mtime = 0;
255         struct stat keystat;
256         struct stat certstat;
257
258         if (stat(file_crpt_file_key, &keystat) != 0) {
259                 syslog(LOG_ERR, "%s: %s", file_crpt_file_key, strerror(errno));
260                 return;
261         }
262         if (stat(file_crpt_file_cer, &certstat) != 0) {
263                 syslog(LOG_ERR, "%s: %s", file_crpt_file_cer, strerror(errno));
264                 return;
265         }
266
267         if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
268                 begin_critical_section(S_OPENSSL);
269                 bind_to_key_and_certificate();
270                 end_critical_section(S_OPENSSL);
271                 previous_mtime = keystat.st_mtime + certstat.st_mtime;
272         }
273 }
274
275
276 // Initialize the TLS subsystem.
277 void init_ssl(void) {
278
279         // Initialize the OpenSSL library
280         SSL_load_error_strings();
281         ERR_load_crypto_strings();
282         OpenSSL_add_all_algorithms();
283         OpenSSL_add_all_ciphers();
284         SSL_library_init();
285
286         // Load (or generate) a key and certificate
287         mkdir(ctdl_key_dir, 0700);                                      // If the keys directory does not exist, create it
288         generate_key(file_crpt_file_key);                               // If a private key does not exist, create it
289         generate_certificate(file_crpt_file_key, file_crpt_file_cer);   // If a certificate does not exist, create it
290         bind_to_key_and_certificate();                                  // Load key and cert from disk, and bind to them.
291
292         // Register some Citadel protocol commands for dealing with encrypted sessions
293         CtdlRegisterProtoHook(cmd_stls, "STLS", "Start TLS session");
294         CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get TLS session status");
295         CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
296 }
297
298
299 // client_write_ssl() Send binary data to the client encrypted.
300 void client_write_ssl(const char *buf, int nbytes) {
301         int retval;
302         int nremain;
303         char junk[1];
304
305         nremain = nbytes;
306
307         while (nremain > 0) {
308                 if (SSL_want_write(CC->ssl)) {
309                         if ((SSL_read(CC->ssl, junk, 0)) < 1) {
310                                 syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
311                         }
312                 }
313                 retval =
314                     SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
315                 if (retval < 1) {
316                         long errval;
317
318                         errval = SSL_get_error(CC->ssl, retval);
319                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
320                                 sleep(1);
321                                 continue;
322                         }
323                         syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
324                         if (retval == -1) {
325                                 syslog(LOG_DEBUG, "crypto: errno is %d", errno);
326                         }
327                         endtls();
328                         client_write(&buf[nbytes - nremain], nremain);
329                         return;
330                 }
331                 nremain -= retval;
332         }
333 }
334
335
336 // read data from the encrypted layer.
337 int client_read_sslbuffer(StrBuf *buf, int timeout) {
338         char sbuf[16384];       // OpenSSL communicates in 16k blocks, so let's speak its native tongue.
339         int rlen;
340         char junk[1];
341         SSL *pssl = CC->ssl;
342
343         if (pssl == NULL) return(-1);
344
345         while (1) {
346                 if (SSL_want_read(pssl)) {
347                         if ((SSL_write(pssl, junk, 0)) < 1) {
348                                 syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
349                         }
350                 }
351                 rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
352                 if (rlen < 1) {
353                         long errval;
354
355                         errval = SSL_get_error(pssl, rlen);
356                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
357                                 sleep(1);
358                                 continue;
359                         }
360                         syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
361                         endtls();
362                         return (-1);
363                 }
364                 StrBufAppendBufPlain(buf, sbuf, rlen, 0);
365                 return rlen;
366         }
367         return (0);
368 }
369
370
371 int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout) {
372         const char *pos = NULL;
373         const char *pLF;
374         int len, rlen;
375         int nSuccessLess = 0;
376         const char *pch = NULL;
377         
378         if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL)) {
379                 if (Pos != NULL) {
380                         *Pos = NULL;
381                 }
382                 return -1;
383         }
384
385         pos = *Pos;
386         if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) {
387                 pch = pos;
388                 pch = strchr(pch, '\n');
389                 
390                 if (pch == NULL) {
391                         StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
392                         FlushStrBuf(IOBuf);
393                         *Pos = NULL;
394                 }
395                 else {
396                         int n = 0;
397                         if ((pch > ChrPtr(IOBuf)) && (*(pch - 1) == '\r')) {
398                                 n = 1;
399                         }
400                         StrBufAppendBufPlain(Line, pos, (pch - pos - n), 0);
401
402                         if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
403                                 FlushStrBuf(IOBuf);
404                                 *Pos = NULL;
405                         }
406                         else {
407                                 *Pos = pch + 1;
408                         }
409                         return StrLength(Line);
410                 }
411         }
412
413         pLF = NULL;
414         while ((nSuccessLess < timeout) && (pLF == NULL) && (CC->ssl != NULL)) {
415
416                 rlen = client_read_sslbuffer(IOBuf, timeout);
417                 if (rlen < 1) {
418                         return -1;
419                 }
420                 else if (rlen > 0) {
421                         pLF = strchr(ChrPtr(IOBuf), '\n');
422                 }
423         }
424         *Pos = NULL;
425         if (pLF != NULL) {
426                 pos = ChrPtr(IOBuf);
427                 len = pLF - pos;
428                 if (len > 0 && (*(pLF - 1) == '\r') )
429                         len --;
430                 StrBufAppendBufPlain(Line, pos, len, 0);
431                 if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf)) {
432                         FlushStrBuf(IOBuf);
433                 }
434                 else  {
435                         *Pos = pLF + 1;
436                 }
437                 return StrLength(Line);
438         }
439         return -1;
440 }
441
442
443 int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
444         long baselen;
445         long RemainRead;
446         int retval = 0;
447
448         baselen = StrLength(Target);
449
450         if (StrLength(CC->RecvBuf.Buf) > 0) {
451                 long RemainLen;
452                 long TotalLen;
453                 const char *pchs;
454
455                 if (CC->RecvBuf.ReadWritePointer == NULL) {
456                         CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf);
457                 }
458                 pchs = ChrPtr(CC->RecvBuf.Buf);
459                 TotalLen = StrLength(CC->RecvBuf.Buf);
460                 RemainLen = TotalLen - (pchs - CC->RecvBuf.ReadWritePointer);
461                 if (RemainLen > bytes) {
462                         RemainLen = bytes;
463                 }
464                 if (RemainLen > 0) {
465                         StrBufAppendBufPlain(Target, CC->RecvBuf.ReadWritePointer, RemainLen, 0);
466                         CC->RecvBuf.ReadWritePointer += RemainLen;
467                 }
468                 if ((ChrPtr(CC->RecvBuf.Buf) + StrLength(CC->RecvBuf.Buf)) <= CC->RecvBuf.ReadWritePointer) {
469                         CC->RecvBuf.ReadWritePointer = NULL;
470                         FlushStrBuf(CC->RecvBuf.Buf);
471                 }
472         }
473
474         if (StrLength(Target) >= bytes + baselen) {
475                 return 1;
476         }
477
478         CC->RecvBuf.ReadWritePointer = NULL;
479
480         while ((StrLength(Target) < bytes + baselen) && (retval >= 0)) {
481                 retval = client_read_sslbuffer(CC->RecvBuf.Buf, timeout);
482                 if (retval >= 0) {
483                         RemainRead = bytes - (StrLength (Target) - baselen);
484                         if (RemainRead < StrLength(CC->RecvBuf.Buf)) {
485                                 StrBufAppendBufPlain(
486                                         Target, 
487                                         ChrPtr(CC->RecvBuf.Buf), 
488                                         RemainRead, 0);
489                                 CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf) + RemainRead;
490                                 break;
491                         }
492                         StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0);
493                         FlushStrBuf(CC->RecvBuf.Buf);
494                 }
495                 else {
496                         FlushStrBuf(CC->RecvBuf.Buf);
497                         return -1;
498         
499                 }
500         }
501         return 1;
502 }
503
504
505 // CtdlStartTLS() starts TLS encryption for the current session.  It
506 // must be supplied with pre-generated strings for responses of "ok," "no
507 // support for TLS," and "error" so that we can use this in any protocol.
508 void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response) {
509         int retval, bits, alg_bits;
510
511         if (CC->redirect_ssl) {
512                 syslog(LOG_ERR, "crypto: attempt to begin SSL on an already encrypted connection");
513                 if (error_response != NULL) {
514                         cprintf("%s", error_response);
515                 }
516                 return;
517         }
518
519         if (!ssl_ctx) {
520                 syslog(LOG_ERR, "crypto: SSL failed: context has not been initialized");
521                 if (nosup_response != NULL) {
522                         cprintf("%s", nosup_response);
523                 }
524                 return;
525         }
526
527         update_key_and_cert_if_needed();                // did someone update the key or cert?  if so, re-bind them
528
529         if (!(CC->ssl = SSL_new(ssl_ctx))) {
530                 syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
531                 if (error_response != NULL) {
532                         cprintf("%s", error_response);
533                 }
534                 return;
535         }
536         if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
537                 syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
538                 SSL_free(CC->ssl);
539                 CC->ssl = NULL;
540                 if (error_response != NULL) {
541                         cprintf("%s", error_response);
542                 }
543                 return;
544         }
545         if (ok_response != NULL) {
546                 cprintf("%s", ok_response);
547         }
548         retval = SSL_accept(CC->ssl);
549         if (retval < 1) {
550                 // Can't notify the client of an error here; they will
551                 // discover the problem at the SSL layer and should
552                 // revert to unencrypted communications.
553                 syslog(LOG_ERR, "crypto: SSL_accept failed: %s", ERR_reason_error_string(ERR_get_error()));
554                 SSL_free(CC->ssl);
555                 CC->ssl = NULL;
556                 return;
557         }
558         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
559         syslog(LOG_INFO, "crypto: TLS using %s on %s (%d of %d bits)",
560                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
561                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
562                 bits, alg_bits
563         );
564         CC->redirect_ssl = 1;
565 }
566
567
568 // cmd_stls() starts TLS encryption for the current session
569 void cmd_stls(char *params) {
570         char ok_response[SIZ];
571         char nosup_response[SIZ];
572         char error_response[SIZ];
573
574         unbuffer_output();
575
576         sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
577         sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
578         sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
579
580         CtdlStartTLS(ok_response, nosup_response, error_response);
581 }
582
583
584 // cmd_gtls() returns status info about the TLS connection
585 void cmd_gtls(char *params) {
586         int bits, alg_bits;
587
588         if (!CC->ssl || !CC->redirect_ssl) {
589                 cprintf("%d Session is not encrypted.\n", ERROR);
590                 return;
591         }
592         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
593         cprintf("%d %s|%s|%d|%d\n", CIT_OK,
594                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
595                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
596                 alg_bits, bits);
597 }
598
599
600 // endtls() shuts down the TLS connection
601 void endtls(void) {
602         if (!CC->ssl) {
603                 CC->redirect_ssl = 0;
604                 return;
605         }
606
607         syslog(LOG_INFO, "crypto: ending TLS on this session");
608         SSL_shutdown(CC->ssl);
609         SSL_free(CC->ssl);
610         CC->ssl = NULL;
611         CC->redirect_ssl = 0;
612 }
613
614 #endif  // HAVE_OPENSSL