Switch out the key/cert in a critical section (mutex wrapped). This will prevent...
[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
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         if (!(new_ctx = SSL_CTX_new(TLS_server_method()))) {
220                 syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
221                 return;
222         }
223
224         syslog(LOG_DEBUG, "crypto: using certificate chain %s", file_crpt_file_cer);
225         SSL_CTX_use_certificate_chain_file(new_ctx, file_crpt_file_cer);
226
227         syslog(LOG_DEBUG, "crypto: using private key %s", file_crpt_file_key);
228         SSL_CTX_use_PrivateKey_file(new_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
229         if ( !SSL_CTX_check_private_key(new_ctx) ) {
230                 syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
231         }
232
233         old_ctx = ssl_ctx;
234         ssl_ctx = new_ctx;              // All future binds will use the new certificate
235
236         if (old_ctx != NULL) {
237                 sleep(1);               // Hopefully wait until all in-progress binds to the old certificate have completed
238                 SSL_CTX_free(old_ctx);
239         }
240 }
241
242
243 // Check the modification time of the key and certificate files.  Reload if either one changed.
244 void update_key_and_cert_if_needed(void) {
245         static time_t previous_mtime = 0;
246         struct stat keystat;
247         struct stat certstat;
248
249         if (stat(file_crpt_file_key, &keystat) != 0) {
250                 syslog(LOG_ERR, "%s: %s", file_crpt_file_key, strerror(errno));
251                 return;
252         }
253         if (stat(file_crpt_file_cer, &certstat) != 0) {
254                 syslog(LOG_ERR, "%s: %s", file_crpt_file_cer, strerror(errno));
255                 return;
256         }
257
258         if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
259                 begin_critical_section(S_OPENSSL);
260                 bind_to_key_and_certificate();
261                 end_critical_section(S_OPENSSL);
262                 previous_mtime = keystat.st_mtime + certstat.st_mtime;
263         }
264 }
265
266
267 // Initialize the TLS subsystem.
268 void init_ssl(void) {
269
270         // Initialize the OpenSSL library
271         SSL_load_error_strings();
272         ERR_load_crypto_strings();
273         OpenSSL_add_all_algorithms();
274         SSL_library_init();
275
276         // Load (or generate) a key and certificate
277         mkdir(ctdl_key_dir, 0700);                                      // If the keys directory does not exist, create it
278         generate_key(file_crpt_file_key);                               // If a private key does not exist, create it
279         generate_certificate(file_crpt_file_key, file_crpt_file_cer);   // If a certificate does not exist, create it
280         bind_to_key_and_certificate();                                  // Load key and cert from disk, and bind to them.
281
282         // Register some Citadel protocol commands for dealing with encrypted sessions
283         CtdlRegisterProtoHook(cmd_stls, "STLS", "Start TLS session");
284         CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get TLS session status");
285         CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
286 }
287
288
289 // client_write_ssl() Send binary data to the client encrypted.
290 void client_write_ssl(const char *buf, int nbytes) {
291         int retval;
292         int nremain;
293         char junk[1];
294
295         nremain = nbytes;
296
297         while (nremain > 0) {
298                 if (SSL_want_write(CC->ssl)) {
299                         if ((SSL_read(CC->ssl, junk, 0)) < 1) {
300                                 syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
301                         }
302                 }
303                 retval =
304                     SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
305                 if (retval < 1) {
306                         long errval;
307
308                         errval = SSL_get_error(CC->ssl, retval);
309                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
310                                 sleep(1);
311                                 continue;
312                         }
313                         syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
314                         if (retval == -1) {
315                                 syslog(LOG_DEBUG, "crypto: errno is %d", errno);
316                         }
317                         endtls();
318                         client_write(&buf[nbytes - nremain], nremain);
319                         return;
320                 }
321                 nremain -= retval;
322         }
323 }
324
325
326 // read data from the encrypted layer.
327 int client_read_sslbuffer(StrBuf *buf, int timeout) {
328         char sbuf[16384];       // OpenSSL communicates in 16k blocks, so let's speak its native tongue.
329         int rlen;
330         char junk[1];
331         SSL *pssl = CC->ssl;
332
333         if (pssl == NULL) return(-1);
334
335         while (1) {
336                 if (SSL_want_read(pssl)) {
337                         if ((SSL_write(pssl, junk, 0)) < 1) {
338                                 syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
339                         }
340                 }
341                 rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
342                 if (rlen < 1) {
343                         long errval;
344
345                         errval = SSL_get_error(pssl, rlen);
346                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
347                                 sleep(1);
348                                 continue;
349                         }
350                         syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
351                         endtls();
352                         return (-1);
353                 }
354                 StrBufAppendBufPlain(buf, sbuf, rlen, 0);
355                 return rlen;
356         }
357         return (0);
358 }
359
360
361 int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout) {
362         const char *pos = NULL;
363         const char *pLF;
364         int len, rlen;
365         int nSuccessLess = 0;
366         const char *pch = NULL;
367         
368         if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL)) {
369                 if (Pos != NULL) {
370                         *Pos = NULL;
371                 }
372                 return -1;
373         }
374
375         pos = *Pos;
376         if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) {
377                 pch = pos;
378                 pch = strchr(pch, '\n');
379                 
380                 if (pch == NULL) {
381                         StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
382                         FlushStrBuf(IOBuf);
383                         *Pos = NULL;
384                 }
385                 else {
386                         int n = 0;
387                         if ((pch > ChrPtr(IOBuf)) && (*(pch - 1) == '\r')) {
388                                 n = 1;
389                         }
390                         StrBufAppendBufPlain(Line, pos, (pch - pos - n), 0);
391
392                         if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
393                                 FlushStrBuf(IOBuf);
394                                 *Pos = NULL;
395                         }
396                         else {
397                                 *Pos = pch + 1;
398                         }
399                         return StrLength(Line);
400                 }
401         }
402
403         pLF = NULL;
404         while ((nSuccessLess < timeout) && (pLF == NULL) && (CC->ssl != NULL)) {
405
406                 rlen = client_read_sslbuffer(IOBuf, timeout);
407                 if (rlen < 1) {
408                         return -1;
409                 }
410                 else if (rlen > 0) {
411                         pLF = strchr(ChrPtr(IOBuf), '\n');
412                 }
413         }
414         *Pos = NULL;
415         if (pLF != NULL) {
416                 pos = ChrPtr(IOBuf);
417                 len = pLF - pos;
418                 if (len > 0 && (*(pLF - 1) == '\r') )
419                         len --;
420                 StrBufAppendBufPlain(Line, pos, len, 0);
421                 if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf)) {
422                         FlushStrBuf(IOBuf);
423                 }
424                 else  {
425                         *Pos = pLF + 1;
426                 }
427                 return StrLength(Line);
428         }
429         return -1;
430 }
431
432
433 int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
434         long baselen;
435         long RemainRead;
436         int retval = 0;
437
438         baselen = StrLength(Target);
439
440         if (StrLength(CC->RecvBuf.Buf) > 0) {
441                 long RemainLen;
442                 long TotalLen;
443                 const char *pchs;
444
445                 if (CC->RecvBuf.ReadWritePointer == NULL) {
446                         CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf);
447                 }
448                 pchs = ChrPtr(CC->RecvBuf.Buf);
449                 TotalLen = StrLength(CC->RecvBuf.Buf);
450                 RemainLen = TotalLen - (pchs - CC->RecvBuf.ReadWritePointer);
451                 if (RemainLen > bytes) {
452                         RemainLen = bytes;
453                 }
454                 if (RemainLen > 0) {
455                         StrBufAppendBufPlain(Target, CC->RecvBuf.ReadWritePointer, RemainLen, 0);
456                         CC->RecvBuf.ReadWritePointer += RemainLen;
457                 }
458                 if ((ChrPtr(CC->RecvBuf.Buf) + StrLength(CC->RecvBuf.Buf)) <= CC->RecvBuf.ReadWritePointer) {
459                         CC->RecvBuf.ReadWritePointer = NULL;
460                         FlushStrBuf(CC->RecvBuf.Buf);
461                 }
462         }
463
464         if (StrLength(Target) >= bytes + baselen) {
465                 return 1;
466         }
467
468         CC->RecvBuf.ReadWritePointer = NULL;
469
470         while ((StrLength(Target) < bytes + baselen) && (retval >= 0)) {
471                 retval = client_read_sslbuffer(CC->RecvBuf.Buf, timeout);
472                 if (retval >= 0) {
473                         RemainRead = bytes - (StrLength (Target) - baselen);
474                         if (RemainRead < StrLength(CC->RecvBuf.Buf)) {
475                                 StrBufAppendBufPlain(
476                                         Target, 
477                                         ChrPtr(CC->RecvBuf.Buf), 
478                                         RemainRead, 0);
479                                 CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf) + RemainRead;
480                                 break;
481                         }
482                         StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0);
483                         FlushStrBuf(CC->RecvBuf.Buf);
484                 }
485                 else {
486                         FlushStrBuf(CC->RecvBuf.Buf);
487                         return -1;
488         
489                 }
490         }
491         return 1;
492 }
493
494
495 // CtdlStartTLS() starts TLS encryption for the current session.  It
496 // must be supplied with pre-generated strings for responses of "ok," "no
497 // support for TLS," and "error" so that we can use this in any protocol.
498 void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response) {
499         int retval, bits, alg_bits;
500
501         if (CC->redirect_ssl) {
502                 syslog(LOG_ERR, "crypto: attempt to begin SSL on an already encrypted connection");
503                 if (error_response != NULL) {
504                         cprintf("%s", error_response);
505                 }
506                 return;
507         }
508
509         if (!ssl_ctx) {
510                 syslog(LOG_ERR, "crypto: SSL failed: context has not been initialized");
511                 if (nosup_response != NULL) {
512                         cprintf("%s", nosup_response);
513                 }
514                 return;
515         }
516
517         update_key_and_cert_if_needed();                // did someone update the key or cert?  if so, re-bind them
518
519         if (!(CC->ssl = SSL_new(ssl_ctx))) {
520                 syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
521                 if (error_response != NULL) {
522                         cprintf("%s", error_response);
523                 }
524                 return;
525         }
526         if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
527                 syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
528                 SSL_free(CC->ssl);
529                 CC->ssl = NULL;
530                 if (error_response != NULL) {
531                         cprintf("%s", error_response);
532                 }
533                 return;
534         }
535         if (ok_response != NULL) {
536                 cprintf("%s", ok_response);
537         }
538         retval = SSL_accept(CC->ssl);
539         if (retval < 1) {
540                 // Can't notify the client of an error here; they will
541                 // discover the problem at the SSL layer and should
542                 // revert to unencrypted communications.
543                 long errval;
544                 char error_string[128];
545
546                 errval = SSL_get_error(CC->ssl, retval);
547                 syslog(LOG_ERR, "crypto: SSL_accept failed: retval=%d, errval=%ld, err=%s",
548                         retval,
549                         errval,
550                         ERR_error_string(errval, error_string)
551                 );
552                 SSL_free(CC->ssl);
553                 CC->ssl = NULL;
554                 return;
555         }
556         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
557         syslog(LOG_INFO, "crypto: TLS using %s on %s (%d of %d bits)",
558                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
559                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
560                 bits, alg_bits
561         );
562         CC->redirect_ssl = 1;
563 }
564
565
566 // cmd_stls() starts TLS encryption for the current session
567 void cmd_stls(char *params) {
568         char ok_response[SIZ];
569         char nosup_response[SIZ];
570         char error_response[SIZ];
571
572         unbuffer_output();
573
574         sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
575         sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
576         sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
577
578         CtdlStartTLS(ok_response, nosup_response, error_response);
579 }
580
581
582 // cmd_gtls() returns status info about the TLS connection
583 void cmd_gtls(char *params) {
584         int bits, alg_bits;
585
586         if (!CC->ssl || !CC->redirect_ssl) {
587                 cprintf("%d Session is not encrypted.\n", ERROR);
588                 return;
589         }
590         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
591         cprintf("%d %s|%s|%d|%d\n", CIT_OK,
592                 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
593                 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
594                 alg_bits, bits);
595 }
596
597
598 // endtls() shuts down the TLS connection
599 void endtls(void) {
600         if (!CC->ssl) {
601                 CC->redirect_ssl = 0;
602                 return;
603         }
604
605         syslog(LOG_INFO, "crypto: ending TLS on this session");
606         SSL_shutdown(CC->ssl);
607         SSL_free(CC->ssl);
608         CC->ssl = NULL;
609         CC->redirect_ssl = 0;
610 }
611
612 #endif  // HAVE_OPENSSL