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