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