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 "serv_extensions.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);
158 lprintf(9, "SSL_write(%d) returned %d\n", nremain, retval);
162 errval = SSL_get_error(CC->ssl, retval);
163 if (errval == SSL_ERROR_WANT_READ ||
164 errval == SSL_ERROR_WANT_WRITE) {
168 lprintf(9, "SSL_write: error %ld: %s\n",
170 ERR_error_string(errval, NULL)
173 client_write(&buf[nbytes - nremain], nremain);
182 * client_read_ssl() - read data from the encrypted layer.
184 int client_read_ssl(char *buf, int bytes, int timeout)
196 s = BIO_get_fd(CC->ssl->rbio, NULL);
201 retval = select(s+1, &rfds, NULL, NULL, &tv);
203 if (FD_ISSET(s, &rfds) == 0) {
207 if (SSL_want_read(CC->ssl)) {
208 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
209 lprintf(9, "SSL_write in client_read:\n");
210 ERR_print_errors_fp(stderr);
213 rlen = SSL_read(CC->ssl, &buf[len], bytes-len);
214 lprintf(9, "SSL_read(%d) returned %d\n", bytes-len, rlen);
218 errval = SSL_get_error(CC->ssl, rlen);
219 if (errval == SSL_ERROR_WANT_READ ||
220 errval == SSL_ERROR_WANT_WRITE) {
224 lprintf(9, "SSL_read: error %ld: %s\n",
226 ERR_error_string(errval, NULL)
229 return (client_read_to(&buf[len], bytes - len, timeout));
238 * cmd_stls() starts SSL/TLS encryption for the current session
240 void cmd_stls(char *params)
242 int retval, bits, alg_bits;
245 cprintf("%d No SSL_CTX available\n", ERROR);
248 if (!(CC->ssl = SSL_new(ssl_ctx))) {
249 lprintf(2, "SSL_new failed: %s\n",
250 ERR_reason_error_string(ERR_peek_error()));
251 cprintf("%d SSL_new: %s\n", ERROR,
252 ERR_reason_error_string(ERR_get_error()));
255 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
256 lprintf(2, "SSL_set_fd failed: %s\n",
257 ERR_reason_error_string(ERR_peek_error()));
260 cprintf("%d SSL_set_fd: %s\n", ERROR,
261 ERR_reason_error_string(ERR_get_error()));
264 cprintf("%d \n", CIT_OK);
265 retval = SSL_accept(CC->ssl);
268 * Can't notify the client of an error here; they will
269 * discover the problem at the SSL layer and should
270 * revert to unencrypted communications.
274 errval = SSL_get_error(CC->ssl, retval);
275 lprintf(2, "SSL_accept failed: %s\n",
276 ERR_reason_error_string(ERR_get_error()));
281 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
282 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
283 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
284 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
285 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
287 CC->redirect_ssl = 1;
292 * cmd_gtls() returns status info about the TLS connection
294 void cmd_gtls(char *params)
298 if (!CC->ssl || !CC->redirect_ssl) {
299 cprintf("%d Session is not encrypted.\n", ERROR);
302 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
303 cprintf("%d %s|%s|%d|%d\n", CIT_OK,
304 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
305 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
311 * endtls() shuts down the TLS connection
313 * WARNING: This may make your session vulnerable to a known plaintext
314 * attack in the current implmentation.
318 lprintf(7, "Ending SSL/TLS\n");
321 CC->redirect_ssl = 0;
325 SSL_shutdown(CC->ssl);
328 CC->redirect_ssl = 0;
333 * ssl_lock() callback for OpenSSL mutex locks
335 void ssl_lock(int mode, int n, const char *file, int line)
337 if (mode & CRYPTO_LOCK)
338 pthread_mutex_lock(SSLCritters[n]);
340 pthread_mutex_unlock(SSLCritters[n]);
342 #endif /* HAVE_OPENSSL */