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