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