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 pthread_self();
50 SSL_METHOD *ssl_method;
54 lprintf(2, "PRNG not adequately seeded, won't do SSL/TLS\n");
57 SSLCritters = mallok(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
59 lprintf(1, "citserver: can't allocate memory!!\n");
60 /* Nothing's been initialized, just die */
65 for (a=0; a<CRYPTO_num_locks(); a++) {
66 SSLCritters[a] = mallok(sizeof (pthread_mutex_t));
67 if (!SSLCritters[a]) {
68 lprintf(1, "citserver: can't allocate memory!!\n");
69 /* Nothing's been initialized, just die */
72 pthread_mutex_init(SSLCritters[a], NULL);
77 * Initialize SSL transport layer
80 SSL_load_error_strings();
81 ssl_method = SSLv23_server_method();
82 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
83 lprintf(2, "SSL_CTX_new failed: %s\n",
84 ERR_reason_error_string(ERR_get_error()));
87 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
88 lprintf(2, "SSL: No ciphers available\n");
89 SSL_CTX_free(ssl_ctx);
94 #if SSLEAY_VERSION_NUMBER >= 0x00906000L
95 SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
99 CRYPTO_set_locking_callback(ssl_lock);
100 CRYPTO_set_id_callback(id_callback);
102 /* Load DH parameters into the context */
105 lprintf(2, "init_ssl() can't allocate a DH object: %s\n",
106 ERR_reason_error_string(ERR_get_error()));
107 SSL_CTX_free(ssl_ctx);
111 if (!(BN_hex2bn(&(dh->p), DH_P))) {
112 lprintf(2, "init_ssl() can't assign DH_P: %s\n",
113 ERR_reason_error_string(ERR_get_error()));
114 SSL_CTX_free(ssl_ctx);
118 if (!(BN_hex2bn(&(dh->g), DH_G))) {
119 lprintf(2, "init_ssl() can't assign DH_G: %s\n",
120 ERR_reason_error_string(ERR_get_error()));
121 SSL_CTX_free(ssl_ctx);
126 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
129 /* Finally let the server know we're here */
130 CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
131 CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
132 CtdlRegisterSessionHook(endtls, EVT_STOP);
137 * client_write_ssl() Send binary data to the client encrypted.
139 void client_write_ssl(char *buf, int nbytes)
147 while (nremain > 0) {
148 if (SSL_want_write(CC->ssl)) {
149 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
150 lprintf(9, "SSL_read in client_write:\n");
151 ERR_print_errors_fp(stderr);
154 retval = SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
158 errval = SSL_get_error(CC->ssl, retval);
159 if (errval == SSL_ERROR_WANT_READ ||
160 errval == SSL_ERROR_WANT_WRITE) {
164 lprintf(9, "SSL_write got error %ld\n", errval);
166 client_write(&buf[nbytes - nremain], nremain);
175 * client_read_ssl() - read data from the encrypted layer.
177 int client_read_ssl(char *buf, int bytes, int timeout)
189 s = BIO_get_fd(CC->ssl->rbio, NULL);
194 retval = select(s+1, &rfds, NULL, NULL, &tv);
196 if (FD_ISSET(s, &rfds) == 0) {
200 if (SSL_want_read(CC->ssl)) {
201 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
202 lprintf(9, "SSL_write in client_read:\n");
203 ERR_print_errors_fp(stderr);
206 rlen = SSL_read(CC->ssl, &buf[len], bytes-len);
210 errval = SSL_get_error(CC->ssl, rlen);
211 if (errval == SSL_ERROR_WANT_READ ||
212 errval == SSL_ERROR_WANT_WRITE) {
216 lprintf(9, "SSL_read got error %ld\n", errval);
218 return (client_read_to(&buf[len], bytes - len, timeout));
227 * cmd_stls() starts SSL/TLS encryption for the current session
229 void cmd_stls(char *params)
231 int retval, bits, alg_bits;
234 cprintf("%d No SSL_CTX available\n", ERROR);
237 if (!(CC->ssl = SSL_new(ssl_ctx))) {
238 lprintf(2, "SSL_new failed: %s\n",
239 ERR_reason_error_string(ERR_peek_error()));
240 cprintf("%d SSL_new: %s\n", ERROR,
241 ERR_reason_error_string(ERR_get_error()));
244 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
245 lprintf(2, "SSL_set_fd failed: %s\n",
246 ERR_reason_error_string(ERR_peek_error()));
249 cprintf("%d SSL_set_fd: %s\n", ERROR,
250 ERR_reason_error_string(ERR_get_error()));
253 cprintf("%d \n", OK);
254 retval = SSL_accept(CC->ssl);
257 * Can't notify the client of an error here; they will
258 * discover the problem at the SSL layer and should
259 * revert to unencrypted communications.
263 errval = SSL_get_error(CC->ssl, retval);
264 lprintf(2, "SSL_accept failed: %s\n",
265 ERR_reason_error_string(ERR_get_error()));
270 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
271 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
272 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
273 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
274 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
276 CC->redirect_ssl = 1;
281 * cmd_gtls() returns status info about the TLS connection
283 void cmd_gtls(char *params)
287 if (!CC->ssl || !CC->redirect_ssl) {
288 cprintf("%d Session is not encrypted.\n", ERROR);
291 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
292 cprintf("%d %s|%s|%d|%d\n", OK,
293 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
294 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
300 * endtls() shuts down the TLS connection
302 * WARNING: This may make your session vulnerable to a known plaintext
303 * attack in the current implmentation.
307 lprintf(7, "Ending SSL/TLS\n");
310 CC->redirect_ssl = 0;
314 SSL_shutdown(CC->ssl);
317 CC->redirect_ssl = 0;
322 * ssl_lock() callback for OpenSSL mutex locks
324 void ssl_lock(int mode, int n, const char *file, int line)
326 if (mode & CRYPTO_LOCK)
327 pthread_mutex_lock(SSLCritters[n]);
329 pthread_mutex_unlock(SSLCritters[n]);
331 #endif /* HAVE_OPENSSL */