8 #include <openssl/ssl.h>
9 #include <openssl/err.h>
10 #include <openssl/rand.h>
13 #if TIME_WITH_SYS_TIME
14 # include <sys/time.h>
18 # include <sys/time.h>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
34 #include "serv_crypto.h"
35 #include "sysdep_decls.h"
36 #include "dynloader.h"
40 SSL_CTX *ssl_ctx; /* SSL context */
41 pthread_mutex_t **SSLCritters; /* Things needing locking */
46 SSL_METHOD *ssl_method;
50 lprintf(2, "PRNG not adequately seeded, won't do SSL/TLS\n");
53 SSLCritters = mallok(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
55 lprintf(1, "citserver: can't allocate memory!!\n");
56 /* Nothing's been initialized, just die */
61 for (a=0; a<CRYPTO_num_locks(); a++) {
62 SSLCritters[a] = mallok(sizeof (pthread_mutex_t));
63 if (!SSLCritters[a]) {
64 lprintf(1, "citserver: can't allocate memory!!\n");
65 /* Nothing's been initialized, just die */
68 pthread_mutex_init(SSLCritters[a], NULL);
73 * Initialize SSL transport layer
76 SSL_load_error_strings();
77 ssl_method = SSLv23_server_method();
78 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
79 lprintf(2, "SSL_CTX_new failed: %s\n",
80 ERR_reason_error_string(ERR_get_error()));
83 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
84 lprintf(2, "SSL: No ciphers available\n");
85 SSL_CTX_free(ssl_ctx);
90 #if SSLEAY_VERSION_NUMBER >= 0x00906000L
91 SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
95 CRYPTO_set_locking_callback(ssl_lock);
96 CRYPTO_set_id_callback(pthread_self);
98 /* Load DH parameters into the context */
101 lprintf(2, "init_ssl() can't allocate a DH object: %s\n",
102 ERR_reason_error_string(ERR_get_error()));
103 SSL_CTX_free(ssl_ctx);
107 if (!(BN_hex2bn(&(dh->p), DH_P))) {
108 lprintf(2, "init_ssl() can't assign DH_P: %s\n",
109 ERR_reason_error_string(ERR_get_error()));
110 SSL_CTX_free(ssl_ctx);
114 if (!(BN_hex2bn(&(dh->g), DH_G))) {
115 lprintf(2, "init_ssl() can't assign DH_G: %s\n",
116 ERR_reason_error_string(ERR_get_error()));
117 SSL_CTX_free(ssl_ctx);
122 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
125 /* Finally let the server know we're here */
126 CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
127 CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
128 CtdlRegisterProtoHook(cmd_etls, "ETLS", "End SSL/TLS session");
129 CtdlRegisterSessionHook(endtls_atlogout, EVT_STOP);
134 * client_write_ssl() Send binary data to the client encrypted.
136 void client_write_ssl(char *buf, int nbytes)
144 while (nremain > 0) {
145 if (SSL_want_write(CC->ssl)) {
146 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
147 lprintf(9, "SSL_read in client_write:\n");
148 ERR_print_errors_fp(stderr);
151 retval = SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
155 errval = SSL_get_error(CC->ssl, retval);
156 if (errval == SSL_ERROR_WANT_READ ||
157 errval == SSL_ERROR_WANT_WRITE) {
161 lprintf(9, "SSL_write got error %ld\n", errval);
163 client_write(&buf[nbytes - nremain], nremain);
172 * client_read_ssl() - read data from the encrypted layer.
174 int client_read_ssl(char *buf, int bytes, int timeout)
186 s = BIO_get_fd(CC->ssl->rbio, NULL);
191 retval = select(s+1, &rfds, NULL, NULL, &tv);
193 if (FD_ISSET(s, &rfds) == 0) {
197 if (SSL_want_read(CC->ssl)) {
198 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
199 lprintf(9, "SSL_write in client_read:\n");
200 ERR_print_errors_fp(stderr);
203 rlen = SSL_read(CC->ssl, &buf[len], bytes-len);
207 errval = SSL_get_error(CC->ssl, rlen);
208 if (errval == SSL_ERROR_WANT_READ ||
209 errval == SSL_ERROR_WANT_WRITE) {
213 lprintf(9, "SSL_read got error %ld\n", errval);
215 return (client_read_to(&buf[len], bytes - len, timeout));
224 * cmd_stls() starts SSL/TLS encryption for the current session
226 void cmd_stls(char *params)
228 int retval, bits, alg_bits;
231 cprintf("%d No SSL_CTX available\n", ERROR);
234 if (!(CC->ssl = SSL_new(ssl_ctx))) {
235 lprintf(2, "SSL_new failed: %s\n",
236 ERR_reason_error_string(ERR_peek_error()));
237 cprintf("%d SSL_new: %s\n", ERROR,
238 ERR_reason_error_string(ERR_get_error()));
241 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
242 lprintf(2, "SSL_set_fd failed: %s\n",
243 ERR_reason_error_string(ERR_peek_error()));
246 cprintf("%d SSL_set_fd: %s\n", ERROR,
247 ERR_reason_error_string(ERR_get_error()));
250 cprintf("%d \n", OK);
251 retval = SSL_accept(CC->ssl);
254 * Can't notify the client of an error here; they will
255 * discover the problem at the SSL layer and should
256 * revert to unencrypted communications.
260 errval = SSL_get_error(CC->ssl, retval);
261 lprintf(2, "SSL_accept failed: %s\n",
262 ERR_reason_error_string(ERR_get_error()));
267 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
268 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
269 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
270 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
271 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
273 CC->redirect_ssl = 1;
278 * cmd_gtls() returns status info about the TLS connection
280 void cmd_gtls(char *params)
284 if (!CC->ssl || !CC->redirect_ssl) {
285 cprintf("%d Session is not encrypted.\n", ERROR);
288 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
289 cprintf("%d %s|%s|%d|%d\n", OK,
290 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
291 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
296 /* Logout function hook */
297 void endtls_atlogout(void)
303 /* Command function hook */
304 void cmd_etls(char *params)
311 * endtls() shuts down the TLS connection
312 * Parameter is NULL for client request, CitContext * for server request
314 * WARNING: This may make your session vulnerable to a known plaintext
315 * attack in the current implmentation.
319 lprintf(7, "Ending SSL/TLS%s\n",
320 (who) ? "" : " at client request");
324 cprintf("%d Connection is not encrypted.\n", ERROR);
327 cprintf("%d Now stop encryption.\n", OK);
328 } else if (!CC->ssl) {
332 SSL_shutdown(CC->ssl);
335 CC->redirect_ssl = 0;
340 * ssl_lock() callback for OpenSSL mutex locks
342 void ssl_lock(int mode, int n, const char *file, int line)
344 if (mode & CRYPTO_LOCK)
345 pthread_mutex_lock(SSLCritters[n]);
347 pthread_mutex_unlock(SSLCritters[n]);
349 #endif /* HAVE_OPENSSL */