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)
46 return (unsigned long) pthread_self();
50 * Set up the cert things on the server side. We do need both the
51 * private key (in key_file) and the cert (in cert_file).
52 * Both files may be identical.
54 * This function is taken from OpenSSL apps/s_cb.c
57 static int set_cert_stuff(SSL_CTX * ctx,
58 const char *cert_file, const char *key_file)
60 if (cert_file != NULL) {
61 if (SSL_CTX_use_certificate_file(ctx, cert_file,
62 SSL_FILETYPE_PEM) <= 0) {
63 lprintf(3, "unable to get certificate from '%s'",
69 if (SSL_CTX_use_PrivateKey_file(ctx, key_file,
70 SSL_FILETYPE_PEM) <= 0) {
71 lprintf(3, "unable to get private key from '%s'",
75 /* Now we know that a key and cert have been set against
77 if (!SSL_CTX_check_private_key(ctx)) {
79 "Private key does not match the certificate public key");
89 SSL_METHOD *ssl_method;
92 if (!access("/var/run/egd-pool", F_OK))
93 RAND_egd("/var/run/egd-pool");
97 "PRNG not adequately seeded, won't do SSL/TLS\n");
101 mallok(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
103 lprintf(1, "citserver: can't allocate memory!!\n");
104 /* Nothing's been initialized, just die */
109 for (a = 0; a < CRYPTO_num_locks(); a++) {
110 SSLCritters[a] = mallok(sizeof(pthread_mutex_t));
111 if (!SSLCritters[a]) {
113 "citserver: can't allocate memory!!\n");
114 /* Nothing's been initialized, just die */
117 pthread_mutex_init(SSLCritters[a], NULL);
122 * Initialize SSL transport layer
125 SSL_load_error_strings();
126 ssl_method = SSLv23_server_method();
127 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
128 lprintf(2, "SSL_CTX_new failed: %s\n",
129 ERR_reason_error_string(ERR_get_error()));
132 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
133 lprintf(2, "SSL: No ciphers available\n");
134 SSL_CTX_free(ssl_ctx);
139 #if SSLEAY_VERSION_NUMBER >= 0x00906000L
140 SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
141 SSL_MODE_AUTO_RETRY);
144 SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
145 SSL_MODE_AUTO_RETRY);
147 CRYPTO_set_locking_callback(ssl_lock);
148 CRYPTO_set_id_callback(id_callback);
150 /* Load DH parameters into the context */
153 lprintf(2, "init_ssl() can't allocate a DH object: %s\n",
154 ERR_reason_error_string(ERR_get_error()));
155 SSL_CTX_free(ssl_ctx);
159 if (!(BN_hex2bn(&(dh->p), DH_P))) {
160 lprintf(2, "init_ssl() can't assign DH_P: %s\n",
161 ERR_reason_error_string(ERR_get_error()));
162 SSL_CTX_free(ssl_ctx);
166 if (!(BN_hex2bn(&(dh->g), DH_G))) {
167 lprintf(2, "init_ssl() can't assign DH_G: %s\n",
168 ERR_reason_error_string(ERR_get_error()));
169 SSL_CTX_free(ssl_ctx);
174 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
177 /* Get our certificates in order */
178 if (set_cert_stuff(ssl_ctx,
179 BBSDIR "/keys/citadel.cer",
180 BBSDIR "/keys/citadel.key") != 1) {
182 lprintf(3, "SSL ERROR: cert is bad!\n");
186 /* Finally let the server know we're here */
187 CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
188 CtdlRegisterProtoHook(cmd_gtls, "GTLS",
189 "Get SSL/TLS session status");
190 CtdlRegisterSessionHook(endtls, EVT_STOP);
195 * client_write_ssl() Send binary data to the client encrypted.
197 void client_write_ssl(char *buf, int nbytes)
205 while (nremain > 0) {
206 if (SSL_want_write(CC->ssl)) {
207 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
208 lprintf(9, "SSL_read in client_write:\n");
209 ERR_print_errors_fp(stderr);
213 SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
217 errval = SSL_get_error(CC->ssl, retval);
218 if (errval == SSL_ERROR_WANT_READ ||
219 errval == SSL_ERROR_WANT_WRITE) {
223 lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
225 lprintf(9, "errno is %d\n", errno);
227 client_write(&buf[nbytes - nremain], nremain);
236 * client_read_ssl() - read data from the encrypted layer.
238 int client_read_ssl(char *buf, int bytes, int timeout)
250 while (len < bytes) {
253 * This code is disabled because we don't need it when
254 * using blocking reads (which we are). -IO
257 s = BIO_get_fd(CC->ssl->rbio, NULL);
262 retval = select(s + 1, &rfds, NULL, NULL, &tv);
264 if (FD_ISSET(s, &rfds) == 0) {
269 if (SSL_want_read(CC->ssl)) {
270 if ((SSL_write(CC->ssl, junk, 0)) < 1) {
271 lprintf(9, "SSL_write in client_read:\n");
272 ERR_print_errors_fp(stderr);
275 rlen = SSL_read(CC->ssl, &buf[len], bytes - len);
279 errval = SSL_get_error(CC->ssl, rlen);
280 if (errval == SSL_ERROR_WANT_READ ||
281 errval == SSL_ERROR_WANT_WRITE) {
285 lprintf(9, "SSL_read got error %ld\n", errval);
287 return (client_read_to
288 (&buf[len], bytes - len, timeout));
297 * cmd_stls() starts SSL/TLS encryption for the current session
299 void cmd_stls(char *params)
301 int retval, bits, alg_bits;
304 cprintf("%d No SSL_CTX available\n", ERROR + CMD_NOT_SUPPORTED);
307 if (!(CC->ssl = SSL_new(ssl_ctx))) {
308 lprintf(2, "SSL_new failed: %s\n",
309 ERR_reason_error_string(ERR_peek_error()));
310 cprintf("%d SSL_new: %s\n", ERROR + INTERNAL_ERROR,
311 ERR_reason_error_string(ERR_get_error()));
314 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
315 lprintf(2, "SSL_set_fd failed: %s\n",
316 ERR_reason_error_string(ERR_peek_error()));
319 cprintf("%d SSL_set_fd: %s\n", ERROR + INTERNAL_ERROR,
320 ERR_reason_error_string(ERR_get_error()));
323 cprintf("%d \n", CIT_OK);
324 retval = SSL_accept(CC->ssl);
327 * Can't notify the client of an error here; they will
328 * discover the problem at the SSL layer and should
329 * revert to unencrypted communications.
333 errval = SSL_get_error(CC->ssl, retval);
334 lprintf(2, "SSL_accept failed: %s\n",
335 ERR_reason_error_string(ERR_get_error()));
340 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
342 SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
344 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
345 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
346 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
348 CC->redirect_ssl = 1;
353 * cmd_gtls() returns status info about the TLS connection
355 void cmd_gtls(char *params)
359 if (!CC->ssl || !CC->redirect_ssl) {
360 cprintf("%d Session is not encrypted.\n", ERROR);
364 SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
366 cprintf("%d %s|%s|%d|%d\n", CIT_OK,
367 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
368 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
374 * endtls() shuts down the TLS connection
376 * WARNING: This may make your session vulnerable to a known plaintext
377 * attack in the current implmentation.
381 lprintf(7, "Ending SSL/TLS\n");
384 CC->redirect_ssl = 0;
388 SSL_shutdown(CC->ssl);
391 CC->redirect_ssl = 0;
396 * ssl_lock() callback for OpenSSL mutex locks
398 void ssl_lock(int mode, int n, const char *file, int line)
400 if (mode & CRYPTO_LOCK)
401 pthread_mutex_lock(SSLCritters[n]);
403 pthread_mutex_unlock(SSLCritters[n]);
405 #endif /* HAVE_OPENSSL */