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 CtdlRegisterSessionHook(endtls, EVT_STOP);
133 * client_write_ssl() Send binary data to the client encrypted.
135 void client_write_ssl(char *buf, int nbytes)
143 while (nremain > 0) {
144 if (SSL_want_write(CC->ssl)) {
145 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
146 lprintf(9, "SSL_read in client_write:\n");
147 ERR_print_errors_fp(stderr);
150 retval = SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
154 errval = SSL_get_error(CC->ssl, retval);
155 if (errval == SSL_ERROR_WANT_READ ||
156 errval == SSL_ERROR_WANT_WRITE) {
160 lprintf(9, "SSL_write got error %ld\n", errval);
162 client_write(&buf[nbytes - nremain], nremain);
171 * client_read_ssl() - read data from the encrypted layer.
173 int client_read_ssl(char *buf, int bytes, int timeout)
185 s = BIO_get_fd(CC->ssl->rbio, NULL);
190 retval = select(s+1, &rfds, NULL, NULL, &tv);
192 if (FD_ISSET(s, &rfds) == 0) {
196 if (SSL_want_read(CC->ssl)) {
197 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
198 lprintf(9, "SSL_write in client_read:\n");
199 ERR_print_errors_fp(stderr);
202 rlen = SSL_read(CC->ssl, &buf[len], bytes-len);
206 errval = SSL_get_error(CC->ssl, rlen);
207 if (errval == SSL_ERROR_WANT_READ ||
208 errval == SSL_ERROR_WANT_WRITE) {
212 lprintf(9, "SSL_read got error %ld\n", errval);
214 return (client_read_to(&buf[len], bytes - len, timeout));
223 * cmd_stls() starts SSL/TLS encryption for the current session
225 void cmd_stls(char *params)
227 int retval, bits, alg_bits;
230 cprintf("%d No SSL_CTX available\n", ERROR);
233 if (!(CC->ssl = SSL_new(ssl_ctx))) {
234 lprintf(2, "SSL_new failed: %s\n",
235 ERR_reason_error_string(ERR_peek_error()));
236 cprintf("%d SSL_new: %s\n", ERROR,
237 ERR_reason_error_string(ERR_get_error()));
240 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
241 lprintf(2, "SSL_set_fd failed: %s\n",
242 ERR_reason_error_string(ERR_peek_error()));
245 cprintf("%d SSL_set_fd: %s\n", ERROR,
246 ERR_reason_error_string(ERR_get_error()));
249 cprintf("%d \n", OK);
250 retval = SSL_accept(CC->ssl);
253 * Can't notify the client of an error here; they will
254 * discover the problem at the SSL layer and should
255 * revert to unencrypted communications.
259 errval = SSL_get_error(CC->ssl, retval);
260 lprintf(2, "SSL_accept failed: %s\n",
261 ERR_reason_error_string(ERR_get_error()));
266 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
267 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
268 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
269 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
270 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
272 CC->redirect_ssl = 1;
277 * cmd_gtls() returns status info about the TLS connection
279 void cmd_gtls(char *params)
283 if (!CC->ssl || !CC->redirect_ssl) {
284 cprintf("%d Session is not encrypted.\n", ERROR);
287 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
288 cprintf("%d %s|%s|%d|%d\n", OK,
289 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
290 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
296 * endtls() shuts down the TLS connection
298 * WARNING: This may make your session vulnerable to a known plaintext
299 * attack in the current implmentation.
303 lprintf(7, "Ending SSL/TLS%s\n");
306 CC->redirect_ssl = 0;
310 SSL_shutdown(CC->ssl);
313 CC->redirect_ssl = 0;
318 * ssl_lock() callback for OpenSSL mutex locks
320 void ssl_lock(int mode, int n, const char *file, int line)
322 if (mode & CRYPTO_LOCK)
323 pthread_mutex_lock(SSLCritters[n]);
325 pthread_mutex_unlock(SSLCritters[n]);
327 #endif /* HAVE_OPENSSL */