9 #include <openssl/ssl.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
29 #ifdef HAVE_SYS_SELECT_H
30 #include <sys/select.h>
35 #include "serv_crypto.h"
36 #include "sysdep_decls.h"
37 #include "dynloader.h"
41 SSL_CTX *ssl_ctx; /* SSL context */
42 pthread_mutex_t **SSLCritters; /* Things needing locking */
44 static unsigned long id_callback(void) {
45 return (unsigned long)pthread_self();
50 SSL_METHOD *ssl_method;
53 if (!access("/var/run/egd-pool", F_OK))
54 RAND_egd("/var/run/egd-pool");
57 lprintf(2, "PRNG not adequately seeded, won't do SSL/TLS\n");
60 SSLCritters = mallok(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
62 lprintf(1, "citserver: can't allocate memory!!\n");
63 /* Nothing's been initialized, just die */
68 for (a=0; a<CRYPTO_num_locks(); a++) {
69 SSLCritters[a] = mallok(sizeof (pthread_mutex_t));
70 if (!SSLCritters[a]) {
71 lprintf(1, "citserver: can't allocate memory!!\n");
72 /* Nothing's been initialized, just die */
75 pthread_mutex_init(SSLCritters[a], NULL);
80 * Initialize SSL transport layer
83 SSL_load_error_strings();
84 ssl_method = SSLv23_server_method();
85 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
86 lprintf(2, "SSL_CTX_new failed: %s\n",
87 ERR_reason_error_string(ERR_get_error()));
90 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
91 lprintf(2, "SSL: No ciphers available\n");
92 SSL_CTX_free(ssl_ctx);
97 #if SSLEAY_VERSION_NUMBER >= 0x00906000L
98 SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
102 CRYPTO_set_locking_callback(ssl_lock);
103 CRYPTO_set_id_callback(id_callback);
105 /* Load DH parameters into the context */
108 lprintf(2, "init_ssl() can't allocate a DH object: %s\n",
109 ERR_reason_error_string(ERR_get_error()));
110 SSL_CTX_free(ssl_ctx);
114 if (!(BN_hex2bn(&(dh->p), DH_P))) {
115 lprintf(2, "init_ssl() can't assign DH_P: %s\n",
116 ERR_reason_error_string(ERR_get_error()));
117 SSL_CTX_free(ssl_ctx);
121 if (!(BN_hex2bn(&(dh->g), DH_G))) {
122 lprintf(2, "init_ssl() can't assign DH_G: %s\n",
123 ERR_reason_error_string(ERR_get_error()));
124 SSL_CTX_free(ssl_ctx);
129 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
132 /* Finally let the server know we're here */
133 CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
134 CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
135 CtdlRegisterSessionHook(endtls, EVT_STOP);
140 * client_write_ssl() Send binary data to the client encrypted.
142 void client_write_ssl(char *buf, int nbytes)
150 while (nremain > 0) {
151 if (SSL_want_write(CC->ssl)) {
152 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
153 lprintf(9, "SSL_read in client_write:\n");
154 ERR_print_errors_fp(stderr);
157 retval = SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
161 errval = SSL_get_error(CC->ssl, retval);
162 if (errval == SSL_ERROR_WANT_READ ||
163 errval == SSL_ERROR_WANT_WRITE) {
167 lprintf(9, "SSL_write got error %ld\n", errval);
169 client_write(&buf[nbytes - nremain], nremain);
178 * client_read_ssl() - read data from the encrypted layer.
180 int client_read_ssl(char *buf, int bytes, int timeout)
192 s = BIO_get_fd(CC->ssl->rbio, NULL);
197 retval = select(s+1, &rfds, NULL, NULL, &tv);
199 if (FD_ISSET(s, &rfds) == 0) {
203 if (SSL_want_read(CC->ssl)) {
204 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
205 lprintf(9, "SSL_write in client_read:\n");
206 ERR_print_errors_fp(stderr);
209 rlen = SSL_read(CC->ssl, &buf[len], bytes-len);
213 errval = SSL_get_error(CC->ssl, rlen);
214 if (errval == SSL_ERROR_WANT_READ ||
215 errval == SSL_ERROR_WANT_WRITE) {
219 lprintf(9, "SSL_read got error %ld\n", errval);
221 return (client_read_to(&buf[len], bytes - len, timeout));
230 * cmd_stls() starts SSL/TLS encryption for the current session
232 void cmd_stls(char *params)
234 int retval, bits, alg_bits;
237 cprintf("%d No SSL_CTX available\n", ERROR);
240 if (!(CC->ssl = SSL_new(ssl_ctx))) {
241 lprintf(2, "SSL_new failed: %s\n",
242 ERR_reason_error_string(ERR_peek_error()));
243 cprintf("%d SSL_new: %s\n", ERROR,
244 ERR_reason_error_string(ERR_get_error()));
247 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
248 lprintf(2, "SSL_set_fd failed: %s\n",
249 ERR_reason_error_string(ERR_peek_error()));
252 cprintf("%d SSL_set_fd: %s\n", ERROR,
253 ERR_reason_error_string(ERR_get_error()));
256 cprintf("%d \n", CIT_OK);
257 retval = SSL_accept(CC->ssl);
260 * Can't notify the client of an error here; they will
261 * discover the problem at the SSL layer and should
262 * revert to unencrypted communications.
266 errval = SSL_get_error(CC->ssl, retval);
267 lprintf(2, "SSL_accept failed: %s\n",
268 ERR_reason_error_string(ERR_get_error()));
273 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
274 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
275 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
276 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
277 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
279 CC->redirect_ssl = 1;
284 * cmd_gtls() returns status info about the TLS connection
286 void cmd_gtls(char *params)
290 if (!CC->ssl || !CC->redirect_ssl) {
291 cprintf("%d Session is not encrypted.\n", ERROR);
294 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
295 cprintf("%d %s|%s|%d|%d\n", CIT_OK,
296 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
297 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
303 * endtls() shuts down the TLS connection
305 * WARNING: This may make your session vulnerable to a known plaintext
306 * attack in the current implmentation.
310 lprintf(7, "Ending SSL/TLS\n");
313 CC->redirect_ssl = 0;
317 SSL_shutdown(CC->ssl);
320 CC->redirect_ssl = 0;
325 * ssl_lock() callback for OpenSSL mutex locks
327 void ssl_lock(int mode, int n, const char *file, int line)
329 if (mode & CRYPTO_LOCK)
330 pthread_mutex_lock(SSLCritters[n]);
332 pthread_mutex_unlock(SSLCritters[n]);
334 #endif /* HAVE_OPENSSL */