Skeleton code for filters.
[citadel.git] / webcit-ng / server / tls.c
1 // Functions in this module handle SSL encryption when WebCit is running
2 // as an HTTPS server.
3 //
4 // Copyright (c) 1996-2022 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10 SSL_CTX *ssl_ctx;               // global SSL context
11 char key_file[PATH_MAX] = "";
12 char cert_file[PATH_MAX] = "";
13 char *ssl_cipher_list = DEFAULT_SSL_CIPHER_LIST;
14
15
16 // Set the private key and certificate chain for the global SSL Context.
17 // This is called during initialization, and can be called again later if the certificate changes.
18 void bind_to_key_and_certificate(void) {
19         SSL_CTX *old_ctx, *new_ctx;
20
21         if (!(new_ctx = SSL_CTX_new(SSLv23_server_method()))) {
22                 syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
23                 return;
24         }
25
26         syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list);
27         if (!(SSL_CTX_set_cipher_list(new_ctx, ssl_cipher_list))) {
28                 syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
29                 return;
30         }
31
32         if (IsEmptyStr(key_file)) {
33                 snprintf(key_file, sizeof key_file, "%s/keys/citadel.key", ctdl_dir);
34         }
35         if (IsEmptyStr(cert_file)) {
36                 snprintf(cert_file, sizeof key_file, "%s/keys/citadel.cer", ctdl_dir);
37         }
38
39         syslog(LOG_DEBUG, "crypto: [re]installing key \"%s\" and certificate \"%s\"", key_file, cert_file);
40
41         SSL_CTX_use_certificate_chain_file(new_ctx, cert_file);
42         SSL_CTX_use_PrivateKey_file(new_ctx, key_file, SSL_FILETYPE_PEM);
43
44         if ( !SSL_CTX_check_private_key(new_ctx) ) {
45                 syslog(LOG_WARNING, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
46         }
47
48         old_ctx = ssl_ctx;
49         ssl_ctx = new_ctx;
50         sleep(1);
51         SSL_CTX_free(old_ctx);
52 }
53
54
55 // Initialize ssl engine, load certs and initialize openssl internals
56 void init_ssl(void) {
57
58         // Initialize the OpenSSL library
59         SSL_load_error_strings();
60         ERR_load_crypto_strings();
61         OpenSSL_add_all_algorithms();
62         SSL_library_init();
63
64         // Now try to bind to the key and certificate.
65         bind_to_key_and_certificate();
66 }
67
68
69 // Check the modification time of the key and certificate -- reload if they changed
70 void update_key_and_cert_if_needed(void) {
71         static time_t previous_mtime = 0;
72         struct stat keystat;
73         struct stat certstat;
74
75         if (stat(key_file, &keystat) != 0) {
76                 syslog(LOG_ERR, "%s: %s", key_file, strerror(errno));
77                 return;
78         }
79         if (stat(cert_file, &certstat) != 0) {
80                 syslog(LOG_ERR, "%s: %s", cert_file, strerror(errno));
81                 return;
82         }
83
84         if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
85                 bind_to_key_and_certificate();
86                 previous_mtime = keystat.st_mtime + certstat.st_mtime;
87         }
88 }
89
90
91 // starts SSL/TLS encryption for the current session.
92 void starttls(struct client_handle *ch) {
93         int retval, bits, alg_bits;
94
95         if (!ssl_ctx) {
96                 return;
97         }
98
99         // Check the modification time of the key and certificate -- reload if they changed
100         update_key_and_cert_if_needed();
101
102         if (!(ch->ssl_handle = SSL_new(ssl_ctx))) {
103                 syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
104                 return;
105         }
106         if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) {
107                 syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
108                 SSL_free(ch->ssl_handle);
109                 return;
110         }
111         retval = SSL_accept(ch->ssl_handle);
112         if (retval < 1) {
113                 syslog(LOG_WARNING, "SSL_accept failed: %s", ERR_reason_error_string(ERR_get_error()));
114         }
115         else {
116                 syslog(LOG_INFO, "SSL_accept success");
117         }
118         bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits);
119         syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)",
120                SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)),
121                SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), bits, alg_bits);
122         syslog(LOG_INFO, "SSL started");
123 }
124
125
126 // shuts down the TLS connection
127 void endtls(struct client_handle *ch) {
128         syslog(LOG_INFO, "Ending SSL/TLS");
129         if (ch->ssl_handle != NULL) {
130                 SSL_shutdown(ch->ssl_handle);
131                 SSL_get_SSL_CTX(ch->ssl_handle);
132                 SSL_free(ch->ssl_handle);
133         }
134         ch->ssl_handle = NULL;
135 }
136
137
138 // Send binary data to the client encrypted.
139 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes) {
140         int retval;
141         int nremain;
142         char junk[1];
143
144         if (ch->ssl_handle == NULL)
145                 return (-1);
146
147         nremain = nbytes;
148         while (nremain > 0) {
149                 if (SSL_want_write(ch->ssl_handle)) {
150                         if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) {
151                                 syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
152                         }
153                 }
154                 retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain);
155                 if (retval < 1) {
156                         long errval;
157
158                         errval = SSL_get_error(ch->ssl_handle, retval);
159                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
160                                 sleep(1);
161                                 continue;
162                         }
163                         syslog(LOG_WARNING, "SSL_write: %s", ERR_reason_error_string(ERR_get_error()));
164                         if (retval == -1) {
165                                 syslog(LOG_WARNING, "errno is %d", errno);
166                                 endtls(ch);
167                         }
168                         return -1;
169                 }
170                 nremain -= retval;
171         }
172         return 0;
173 }
174
175
176 // read data from the encrypted layer
177 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes) {
178         int bytes_read = 0;
179         int rlen = 0;
180         char junk[1];
181
182         if (ch->ssl_handle == NULL)
183                 return (-1);
184
185         while (bytes_read < nbytes) {
186                 if (SSL_want_read(ch->ssl_handle)) {
187                         if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) {
188                                 syslog(LOG_WARNING, "SSL_write in client_read");
189                         }
190                 }
191                 rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes - bytes_read);
192                 if (rlen < 1) {
193                         long errval;
194                         errval = SSL_get_error(ch->ssl_handle, rlen);
195                         if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
196                                 sleep(1);
197                                 continue;
198                         }
199                         syslog(LOG_WARNING, "SSL_read error %ld", errval);
200                         endtls(ch);
201                         return (-1);
202                 }
203                 bytes_read += rlen;
204         }
205         return (bytes_read);
206 }