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