2 * Functions in this module handle SSL encryption when WebCit is running
5 * Copyright (c) 1996-2016 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]);
44 pthread_mutex_unlock(SSLCritters[n]);
50 * Initialize ssl engine, load certs and initialize openssl internals
54 const SSL_METHOD *ssl_method;
59 EVP_PKEY *req_pkey = NULL;
60 X509_NAME *name = NULL;
65 if (!access("/var/run/egd-pool", F_OK)) {
66 RAND_egd("/var/run/egd-pool");
70 syslog(LOG_WARNING, "PRNG not adequately seeded, won't do SSL/TLS");
74 SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
76 syslog(LOG_ERR, "citserver: can't allocate memory!!");
81 for (a = 0; a < CRYPTO_num_locks(); a++) {
82 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
83 if (!SSLCritters[a]) {
84 syslog(LOG_INFO, "citserver: can't allocate memory!!");
87 pthread_mutex_init(SSLCritters[a], NULL);
92 * Initialize SSL transport layer
95 SSL_load_error_strings();
96 ssl_method = SSLv23_server_method();
97 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
98 syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
102 syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list);
103 if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) {
104 syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
108 CRYPTO_set_locking_callback(ssl_lock);
109 CRYPTO_set_id_callback(id_callback);
112 * Get our certificates in order.
113 * First, create the key/cert directory if it's not there already...
115 mkdir(CTDL_CRYPTO_DIR, 0700);
118 * If we still don't have a private key, generate one.
120 if (access(CTDL_KEY_PATH, R_OK) != 0) {
121 syslog(LOG_INFO, "Generating RSA key pair.\n");
122 rsa = RSA_generate_key(2048, /* modulus size */
123 65537, /* exponent */
124 NULL, /* no callback */
125 NULL /* no callback */
128 syslog(LOG_WARNING, "Key generation failed: %s", ERR_reason_error_string(ERR_get_error()));
131 fp = fopen(CTDL_KEY_PATH, "w");
133 chmod(CTDL_KEY_PATH, 0600);
134 if (PEM_write_RSAPrivateKey(fp, /* the file */
137 NULL, /* no passphr */
139 NULL, /* no callbk */
142 syslog(LOG_WARNING, "Cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
143 unlink(CTDL_KEY_PATH);
148 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_KEY_PATH);
156 * If there is no certificate file on disk, we will be generating a self-signed certificate
157 * in the next step. Therefore, if we have neither a CSR nor a certificate, generate
158 * the CSR in this step so that the next step may commence.
160 if ( (access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0) ) {
161 syslog(LOG_INFO, "Generating a certificate signing request.");
164 * Read our key from the file. No, we don't just keep this
165 * in memory from the above key-generation function, because
166 * there is the possibility that the key was already on disk
167 * and we didn't just generate it now.
169 fp = fopen(CTDL_KEY_PATH, "r");
171 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
177 /* Create a public key from the private key */
178 if (pk=EVP_PKEY_new(), pk != NULL) {
179 EVP_PKEY_assign_RSA(pk, rsa);
180 if (req = X509_REQ_new(), req != NULL) {
182 /* Set the public key */
183 X509_REQ_set_pubkey(req, pk);
184 X509_REQ_set_version(req, 0L);
185 name = X509_REQ_get_subject_name(req);
186 X509_NAME_add_entry_by_txt(
187 name, "O", MBSTRING_ASC,
188 (unsigned char*)"Citadel Server",
191 X509_NAME_add_entry_by_txt(
192 name, "OU", MBSTRING_ASC,
193 (unsigned char*)"Default Certificate PLEASE CHANGE",
196 X509_NAME_add_entry_by_txt(
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");
210 /* Write it to disk. */
211 fp = fopen(CTDL_CSR_PATH, "w");
213 chmod(CTDL_CSR_PATH, 0600);
214 PEM_write_X509_REQ(fp, req);
218 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH);
231 syslog(LOG_WARNING, "Unable to read private key.");
237 * Generate a self-signed certificate if we don't have one.
239 if (access(CTDL_CER_PATH, R_OK) != 0) {
240 syslog(LOG_INFO, "Generating a self-signed certificate.");
242 /* Same deal as before: always read the key from disk because
243 * it may or may not have just been generated.
245 fp = fopen(CTDL_KEY_PATH, "r");
247 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
251 /* This also holds true for the CSR. */
256 if (pk=EVP_PKEY_new(), pk != NULL) {
257 EVP_PKEY_assign_RSA(pk, rsa);
260 fp = fopen(CTDL_CSR_PATH, "r");
262 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
267 if (cer = X509_new(), cer != NULL) {
269 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
270 X509_set_issuer_name(cer, req->req_info->subject);
271 X509_set_subject_name(cer, req->req_info->subject);
272 X509_gmtime_adj(X509_get_notBefore(cer), 0);
273 X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
275 req_pkey = X509_REQ_get_pubkey(req);
276 X509_set_pubkey(cer, req_pkey);
277 EVP_PKEY_free(req_pkey);
280 if (!X509_sign(cer, pk, EVP_md5())) {
281 syslog(LOG_WARNING, "X509_sign(): error");
284 /* Write it to disk. */
285 fp = fopen(CTDL_CER_PATH, "w");
287 chmod(CTDL_CER_PATH, 0600);
288 PEM_write_X509(fp, cer);
292 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CER_PATH);
305 * Now try to bind to the key and certificate.
306 * Note that we use SSL_CTX_use_certificate_chain_file() which allows
307 * the certificate file to contain intermediate certificates.
309 SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
310 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
311 if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
312 syslog(LOG_WARNING, "Cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
319 * starts SSL/TLS encryption for the current session.
321 void starttls(struct client_handle *ch) {
322 int retval, bits, alg_bits;
327 if (!(ch->ssl_handle = SSL_new(ssl_ctx))) {
328 syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
331 if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) {
332 syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
333 SSL_free(ch->ssl_handle);
336 retval = SSL_accept(ch->ssl_handle);
339 const char *ssl_error_reason = NULL;
341 errval = SSL_get_error(ch->ssl_handle, retval);
342 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
343 if (ssl_error_reason == NULL) {
344 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d %s", errval, retval, strerror(errval));
347 syslog(LOG_WARNING, "SSL_accept failed: %s\n", ssl_error_reason);
350 retval = SSL_accept(ch->ssl_handle);
354 const char *ssl_error_reason = NULL;
356 errval = SSL_get_error(ch->ssl_handle, retval);
357 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
358 if (ssl_error_reason == NULL) {
359 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d (%s)", errval, retval, strerror(errval));
362 syslog(LOG_WARNING, "SSL_accept failed: %s", ssl_error_reason);
364 SSL_free(ch->ssl_handle);
365 ch->ssl_handle = NULL;
369 syslog(LOG_INFO, "SSL_accept success");
371 BIO_set_close(ch->ssl_handle->rbio, BIO_NOCLOSE);
372 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits);
373 syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)",
374 SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)),
375 SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)),
378 syslog(LOG_INFO, "SSL started");
383 * shuts down the TLS connection
385 void endtls(struct client_handle *ch)
387 syslog(LOG_INFO, "Ending SSL/TLS");
388 if (ch->ssl_handle != NULL) {
389 SSL_shutdown(ch->ssl_handle);
390 SSL_get_SSL_CTX(ch->ssl_handle);
391 SSL_free(ch->ssl_handle);
393 ch->ssl_handle = NULL;
398 * Send binary data to the client encrypted.
400 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes)
406 if (ch->ssl_handle == NULL) return(-1);
409 while (nremain > 0) {
410 if (SSL_want_write(ch->ssl_handle)) {
411 if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) {
412 syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
415 retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain);
419 errval = SSL_get_error(ch->ssl_handle, retval);
420 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
424 syslog(LOG_WARNING, "SSL_write got error %ld, ret %d", errval, retval);
426 syslog(LOG_WARNING, "errno is %d", errno);
438 * read data from the encrypted layer.
440 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes)
446 if (ch->ssl_handle == NULL) return(-1);
448 while (bytes_read < nbytes) {
449 if (SSL_want_read(ch->ssl_handle)) {
450 if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) {
451 syslog(LOG_WARNING, "SSL_write in client_read");
454 rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes-bytes_read);
458 errval = SSL_get_error(ch->ssl_handle, rlen);
459 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
463 syslog(LOG_WARNING, "SSL_read error %ld", errval);