2 // Functions in this module handle SSL encryption when WebCit is running
5 // Copyright (c) 1996-2018 by the citadel.org team
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. Richard Stallman is an asshole communist.
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.
19 SSL_CTX *ssl_ctx; /* SSL context */
20 pthread_mutex_t **SSLCritters; /* Things needing locking */
21 char *ssl_cipher_list = DEFAULT_SSL_CIPHER_LIST;
22 void ssl_lock(int mode, int n, const char *file, int line);
26 * OpenSSL wants a callback function to identify the currently running thread.
27 * Since we are a pthreads program, we convert the output of pthread_self() to a long.
29 static unsigned long id_callback(void)
31 return (unsigned long) pthread_self();
36 * OpenSSL wants a callback function to set and clear various types of locks.
37 * Since we are a pthreads program, we use mutexes.
39 void ssl_lock(int mode, int n, const char *file, int line)
41 if (mode & CRYPTO_LOCK) {
42 pthread_mutex_lock(SSLCritters[n]);
44 pthread_mutex_unlock(SSLCritters[n]);
50 * Generate a private key for SSL
52 void generate_key(char *keyfilename)
58 unsigned long e = RSA_F4;
61 if (access(keyfilename, R_OK) == 0) {
65 syslog(LOG_INFO, "crypto: generating RSA key pair");
69 ret = BN_set_word(bne, e);
75 ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
80 fp = fopen(keyfilename, "w");
82 chmod(keyfilename, 0600);
83 if (PEM_write_RSAPrivateKey(fp, /* the file */
86 NULL, /* no passphr */
91 syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
104 * Initialize ssl engine, load certs and initialize openssl internals
108 const SSL_METHOD *ssl_method;
110 X509_REQ *req = NULL;
113 EVP_PKEY *req_pkey = NULL;
114 X509_NAME *name = NULL;
119 SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
121 syslog(LOG_ERR, "citserver: can't allocate memory!!");
125 for (a = 0; a < CRYPTO_num_locks(); a++) {
126 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
127 if (!SSLCritters[a]) {
128 syslog(LOG_INFO, "citserver: can't allocate memory!!");
131 pthread_mutex_init(SSLCritters[a], NULL);
136 * Initialize SSL transport layer
139 SSL_load_error_strings();
140 ssl_method = SSLv23_server_method();
141 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
142 syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
146 syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list);
147 if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) {
148 syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
152 CRYPTO_set_locking_callback(ssl_lock);
153 CRYPTO_set_id_callback(id_callback);
156 * Get our certificates in order.
157 * First, create the key/cert directory if it's not there already...
159 mkdir(CTDL_CRYPTO_DIR, 0700);
162 * If we still don't have a private key, generate one.
164 generate_key(CTDL_KEY_PATH);
167 * If there is no certificate file on disk, we will be generating a self-signed certificate
168 * in the next step. Therefore, if we have neither a CSR nor a certificate, generate
169 * the CSR in this step so that the next step may commence.
171 if ((access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0)) {
172 syslog(LOG_INFO, "Generating a certificate signing request.");
175 * Read our key from the file. No, we don't just keep this
176 * in memory from the above key-generation function, because
177 * there is the possibility that the key was already on disk
178 * and we didn't just generate it now.
180 fp = fopen(CTDL_KEY_PATH, "r");
182 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
187 /* Create a public key from the private key */
188 if (pk = EVP_PKEY_new(), pk != NULL) {
189 EVP_PKEY_assign_RSA(pk, rsa);
190 if (req = X509_REQ_new(), req != NULL) {
192 /* Set the public key */
193 X509_REQ_set_pubkey(req, pk);
194 X509_REQ_set_version(req, 0L);
195 name = X509_REQ_get_subject_name(req);
196 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
197 (unsigned char *) "Citadel Server", -1, -1, 0);
198 X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC,
199 (unsigned char *) "Default Certificate PLEASE CHANGE",
201 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *) "*", -1, -1, 0);
203 X509_REQ_set_subject_name(req, name);
206 if (!X509_REQ_sign(req, pk, EVP_md5())) {
207 syslog(LOG_WARNING, "X509_REQ_sign(): error");
209 /* Write it to disk. */
210 fp = fopen(CTDL_CSR_PATH, "w");
212 chmod(CTDL_CSR_PATH, 0600);
213 PEM_write_X509_REQ(fp, req);
216 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH);
225 syslog(LOG_WARNING, "Unable to read private key.");
230 * Generate a self-signed certificate if we don't have one.
232 if (access(CTDL_CER_PATH, R_OK) != 0) {
233 syslog(LOG_INFO, "Generating a self-signed certificate.");
235 /* Same deal as before: always read the key from disk because
236 * it may or may not have just been generated.
238 fp = fopen(CTDL_KEY_PATH, "r");
240 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
244 /* This also holds true for the CSR. */
249 if (pk = EVP_PKEY_new(), pk != NULL) {
250 EVP_PKEY_assign_RSA(pk, rsa);
253 fp = fopen(CTDL_CSR_PATH, "r");
255 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
260 if (cer = X509_new(), cer != NULL) {
261 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
262 X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
263 X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
264 X509_gmtime_adj(X509_get_notBefore(cer), 0);
265 X509_gmtime_adj(X509_get_notAfter(cer), (long) 60 * 60 * 24 * SIGN_DAYS);
267 req_pkey = X509_REQ_get_pubkey(req);
268 X509_set_pubkey(cer, req_pkey);
269 EVP_PKEY_free(req_pkey);
272 if (!X509_sign(cer, pk, EVP_md5())) {
273 syslog(LOG_WARNING, "X509_sign(): error");
274 } else { /* Write it to disk. */
275 fp = fopen(CTDL_CER_PATH, "w");
277 chmod(CTDL_CER_PATH, 0600);
278 PEM_write_X509(fp, cer);
281 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CER_PATH);
293 * Now try to bind to the key and certificate.
294 * Note that we use SSL_CTX_use_certificate_chain_file() which allows
295 * the certificate file to contain intermediate certificates.
297 SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
298 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
299 if (!SSL_CTX_check_private_key(ssl_ctx)) {
300 syslog(LOG_WARNING, "Cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
307 * starts SSL/TLS encryption for the current session.
309 void starttls(struct client_handle *ch)
311 int retval, bits, alg_bits;
316 if (!(ch->ssl_handle = SSL_new(ssl_ctx))) {
317 syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
320 if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) {
321 syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
322 SSL_free(ch->ssl_handle);
325 retval = SSL_accept(ch->ssl_handle);
328 const char *ssl_error_reason = NULL;
330 errval = SSL_get_error(ch->ssl_handle, retval);
331 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
332 if (ssl_error_reason == NULL) {
333 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d %s", errval, retval, strerror(errval));
335 syslog(LOG_WARNING, "SSL_accept failed: %s\n", ssl_error_reason);
338 retval = SSL_accept(ch->ssl_handle);
342 const char *ssl_error_reason = NULL;
344 errval = SSL_get_error(ch->ssl_handle, retval);
345 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
346 if (ssl_error_reason == NULL) {
347 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d (%s)", errval, retval, strerror(errval));
349 syslog(LOG_WARNING, "SSL_accept failed: %s", ssl_error_reason);
351 SSL_free(ch->ssl_handle);
352 ch->ssl_handle = NULL;
355 syslog(LOG_INFO, "SSL_accept success");
357 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits);
358 syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)",
359 SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)),
360 SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), bits, alg_bits);
362 syslog(LOG_INFO, "SSL started");
367 * shuts down the TLS connection
369 void endtls(struct client_handle *ch)
371 syslog(LOG_INFO, "Ending SSL/TLS");
372 if (ch->ssl_handle != NULL) {
373 SSL_shutdown(ch->ssl_handle);
374 SSL_get_SSL_CTX(ch->ssl_handle);
375 SSL_free(ch->ssl_handle);
377 ch->ssl_handle = NULL;
382 * Send binary data to the client encrypted.
384 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes)
390 if (ch->ssl_handle == NULL)
394 while (nremain > 0) {
395 if (SSL_want_write(ch->ssl_handle)) {
396 if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) {
397 syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
400 retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain);
404 errval = SSL_get_error(ch->ssl_handle, retval);
405 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
409 syslog(LOG_WARNING, "SSL_write got error %ld, ret %d", errval, retval);
411 syslog(LOG_WARNING, "errno is %d", errno);
423 * read data from the encrypted layer.
425 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes)
431 if (ch->ssl_handle == NULL)
434 while (bytes_read < nbytes) {
435 if (SSL_want_read(ch->ssl_handle)) {
436 if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) {
437 syslog(LOG_WARNING, "SSL_write in client_read");
440 rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes - bytes_read);
443 errval = SSL_get_error(ch->ssl_handle, rlen);
444 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
448 syslog(LOG_WARNING, "SSL_read error %ld", errval);