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