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. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 SSL_CTX *ssl_ctx; /* SSL context */
19 pthread_mutex_t **SSLCritters; /* Things needing locking */
20 char *ssl_cipher_list = DEFAULT_SSL_CIPHER_LIST;
21 void ssl_lock(int mode, int n, const char *file, int line);
25 * OpenSSL wants a callback function to identify the currently running thread.
26 * Since we are a pthreads program, we convert the output of pthread_self() to a long.
28 static unsigned long id_callback(void)
30 return (unsigned long) pthread_self();
35 * OpenSSL wants a callback function to set and clear various types of locks.
36 * Since we are a pthreads program, we use mutexes.
38 void ssl_lock(int mode, int n, const char *file, int line)
40 if (mode & CRYPTO_LOCK) {
41 pthread_mutex_lock(SSLCritters[n]);
43 pthread_mutex_unlock(SSLCritters[n]);
49 * Generate a private key for SSL
51 void generate_key(char *keyfilename)
57 unsigned long e = RSA_F4;
60 if (access(keyfilename, R_OK) == 0) {
64 syslog(LOG_INFO, "crypto: generating RSA key pair");
68 ret = BN_set_word(bne, e);
74 ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
79 fp = fopen(keyfilename, "w");
81 chmod(keyfilename, 0600);
82 if (PEM_write_RSAPrivateKey(fp, /* the file */
85 NULL, /* no passphr */
90 syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
103 * Initialize ssl engine, load certs and initialize openssl internals
107 const SSL_METHOD *ssl_method;
109 X509_REQ *req = NULL;
112 EVP_PKEY *req_pkey = NULL;
113 X509_NAME *name = NULL;
118 SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
120 syslog(LOG_ERR, "citserver: can't allocate memory!!");
124 for (a = 0; a < CRYPTO_num_locks(); a++) {
125 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
126 if (!SSLCritters[a]) {
127 syslog(LOG_INFO, "citserver: can't allocate memory!!");
130 pthread_mutex_init(SSLCritters[a], NULL);
135 * Initialize SSL transport layer
138 SSL_load_error_strings();
139 ssl_method = SSLv23_server_method();
140 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
141 syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
145 syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list);
146 if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) {
147 syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
151 CRYPTO_set_locking_callback(ssl_lock);
152 CRYPTO_set_id_callback(id_callback);
155 * Get our certificates in order.
156 * First, create the key/cert directory if it's not there already...
158 mkdir(CTDL_CRYPTO_DIR, 0700);
161 * If we still don't have a private key, generate one.
163 generate_key(CTDL_KEY_PATH);
166 * If there is no certificate file on disk, we will be generating a self-signed certificate
167 * in the next step. Therefore, if we have neither a CSR nor a certificate, generate
168 * the CSR in this step so that the next step may commence.
170 if ((access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0)) {
171 syslog(LOG_INFO, "Generating a certificate signing request.");
174 * Read our key from the file. No, we don't just keep this
175 * in memory from the above key-generation function, because
176 * there is the possibility that the key was already on disk
177 * and we didn't just generate it now.
179 fp = fopen(CTDL_KEY_PATH, "r");
181 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
186 /* Create a public key from the private key */
187 if (pk = EVP_PKEY_new(), pk != NULL) {
188 EVP_PKEY_assign_RSA(pk, rsa);
189 if (req = X509_REQ_new(), req != NULL) {
191 /* Set the public key */
192 X509_REQ_set_pubkey(req, pk);
193 X509_REQ_set_version(req, 0L);
194 name = X509_REQ_get_subject_name(req);
195 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
196 (unsigned char *) "Citadel Server", -1, -1, 0);
197 X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC,
198 (unsigned char *) "Default Certificate PLEASE CHANGE",
200 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *) "*", -1, -1, 0);
202 X509_REQ_set_subject_name(req, name);
205 if (!X509_REQ_sign(req, pk, EVP_md5())) {
206 syslog(LOG_WARNING, "X509_REQ_sign(): error");
208 /* Write it to disk. */
209 fp = fopen(CTDL_CSR_PATH, "w");
211 chmod(CTDL_CSR_PATH, 0600);
212 PEM_write_X509_REQ(fp, req);
215 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH);
224 syslog(LOG_WARNING, "Unable to read private key.");
229 * Generate a self-signed certificate if we don't have one.
231 if (access(CTDL_CER_PATH, R_OK) != 0) {
232 syslog(LOG_INFO, "Generating a self-signed certificate.");
234 /* Same deal as before: always read the key from disk because
235 * it may or may not have just been generated.
237 fp = fopen(CTDL_KEY_PATH, "r");
239 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
243 /* This also holds true for the CSR. */
248 if (pk = EVP_PKEY_new(), pk != NULL) {
249 EVP_PKEY_assign_RSA(pk, rsa);
252 fp = fopen(CTDL_CSR_PATH, "r");
254 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
259 if (cer = X509_new(), cer != NULL) {
260 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
261 X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
262 X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
263 X509_gmtime_adj(X509_get_notBefore(cer), 0);
264 X509_gmtime_adj(X509_get_notAfter(cer), (long) 60 * 60 * 24 * SIGN_DAYS);
266 req_pkey = X509_REQ_get_pubkey(req);
267 X509_set_pubkey(cer, req_pkey);
268 EVP_PKEY_free(req_pkey);
271 if (!X509_sign(cer, pk, EVP_md5())) {
272 syslog(LOG_WARNING, "X509_sign(): error");
273 } else { /* Write it to disk. */
274 fp = fopen(CTDL_CER_PATH, "w");
276 chmod(CTDL_CER_PATH, 0600);
277 PEM_write_X509(fp, cer);
280 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CER_PATH);
292 * Now try to bind to the key and certificate.
293 * Note that we use SSL_CTX_use_certificate_chain_file() which allows
294 * the certificate file to contain intermediate certificates.
296 SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
297 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
298 if (!SSL_CTX_check_private_key(ssl_ctx)) {
299 syslog(LOG_WARNING, "Cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
306 * starts SSL/TLS encryption for the current session.
308 void starttls(struct client_handle *ch)
310 int retval, bits, alg_bits;
315 if (!(ch->ssl_handle = SSL_new(ssl_ctx))) {
316 syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
319 if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) {
320 syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
321 SSL_free(ch->ssl_handle);
324 retval = SSL_accept(ch->ssl_handle);
327 const char *ssl_error_reason = NULL;
329 errval = SSL_get_error(ch->ssl_handle, retval);
330 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
331 if (ssl_error_reason == NULL) {
332 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d %s", errval, retval, strerror(errval));
334 syslog(LOG_WARNING, "SSL_accept failed: %s\n", ssl_error_reason);
337 retval = SSL_accept(ch->ssl_handle);
341 const char *ssl_error_reason = NULL;
343 errval = SSL_get_error(ch->ssl_handle, retval);
344 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
345 if (ssl_error_reason == NULL) {
346 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d (%s)", errval, retval, strerror(errval));
348 syslog(LOG_WARNING, "SSL_accept failed: %s", ssl_error_reason);
350 SSL_free(ch->ssl_handle);
351 ch->ssl_handle = NULL;
354 syslog(LOG_INFO, "SSL_accept success");
356 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits);
357 syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)",
358 SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)),
359 SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), bits, alg_bits);
361 syslog(LOG_INFO, "SSL started");
366 * shuts down the TLS connection
368 void endtls(struct client_handle *ch)
370 syslog(LOG_INFO, "Ending SSL/TLS");
371 if (ch->ssl_handle != NULL) {
372 SSL_shutdown(ch->ssl_handle);
373 SSL_get_SSL_CTX(ch->ssl_handle);
374 SSL_free(ch->ssl_handle);
376 ch->ssl_handle = NULL;
381 * Send binary data to the client encrypted.
383 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes)
389 if (ch->ssl_handle == NULL)
393 while (nremain > 0) {
394 if (SSL_want_write(ch->ssl_handle)) {
395 if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) {
396 syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
399 retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain);
403 errval = SSL_get_error(ch->ssl_handle, retval);
404 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
408 syslog(LOG_WARNING, "SSL_write got error %ld, ret %d", errval, retval);
410 syslog(LOG_WARNING, "errno is %d", errno);
422 * read data from the encrypted layer.
424 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes)
430 if (ch->ssl_handle == NULL)
433 while (bytes_read < nbytes) {
434 if (SSL_want_read(ch->ssl_handle)) {
435 if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) {
436 syslog(LOG_WARNING, "SSL_write in client_read");
439 rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes - bytes_read);
442 errval = SSL_get_error(ch->ssl_handle, rlen);
443 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
447 syslog(LOG_WARNING, "SSL_read error %ld", errval);