5 #include <openssl/ssl.h>
6 #include <openssl/err.h>
7 #include <openssl/rand.h>
13 #include <sys/types.h>
15 #include "client_crypto.h"
21 int ssl_is_connected = 0;
24 #ifdef THREADED_CLIENT
25 pthread_mutex_t **Critters; /* Things that need locking */
26 #endif /* THREADED_CLIENT */
29 extern int server_is_local;
30 #endif /* HAVE_OPENSSL */
33 static void (*status_hook)(char *s) = NULL;
35 void setCryptoStatusHook(void (*hook)(char *s)) {
42 * input binary data from encrypted connection
44 void serv_read_ssl(char *buf, int bytes)
51 if (SSL_want_read(ssl)) {
52 if ((SSL_write(ssl, junk, 0)) < 1) {
53 error_printf("SSL_write in serv_read:\n");
54 ERR_print_errors_fp(stderr);
57 rlen = SSL_read(ssl, &buf[len], bytes - len);
61 errval = SSL_get_error(ssl, rlen);
62 if (errval == SSL_ERROR_WANT_READ ||
63 errval == SSL_ERROR_WANT_WRITE) {
67 if (errval == SSL_ERROR_ZERO_RETURN ||
68 errval == SSL_ERROR_SSL) {
70 serv_read(&buf[len], bytes - len);
73 error_printf("SSL_read in serv_read:\n");
74 ERR_print_errors_fp(stderr);
84 * send binary to server encrypted
86 void serv_write_ssl(char *buf, int nbytes)
88 int bytes_written = 0;
92 while (bytes_written < nbytes) {
93 if (SSL_want_write(ssl)) {
94 if ((SSL_read(ssl, junk, 0)) < 1) {
95 error_printf("SSL_read in serv_write:\n");
96 ERR_print_errors_fp(stderr);
99 retval = SSL_write(ssl, &buf[bytes_written],
100 nbytes - bytes_written);
104 errval = SSL_get_error(ssl, retval);
105 if (errval == SSL_ERROR_WANT_READ ||
106 errval == SSL_ERROR_WANT_WRITE) {
110 if (errval == SSL_ERROR_ZERO_RETURN ||
111 errval == SSL_ERROR_SSL) {
113 serv_write(&buf[bytes_written],
114 nbytes - bytes_written);
117 error_printf("SSL_write in serv_write:\n");
118 ERR_print_errors_fp(stderr);
122 bytes_written += retval;
127 void ssl_lock(int mode, int n, const char *file, int line)
129 #ifdef THREADED_CLIENT
130 if (mode & CRYPTO_LOCK)
131 pthread_mutex_lock(Critters[n]);
133 pthread_mutex_unlock(Critters[n]);
134 #endif /* THREADED_CLIENT */
136 #endif /* HAVE_OPENSSL */
138 #if defined(THREADED_CLIENT) && defined(HAVE_OPENSSL)
139 static unsigned long id_callback(void) {
140 return (unsigned long)pthread_self();
145 * starttls() starts SSL/TLS if possible
146 * Returns 1 if the session is encrypted, 0 otherwise
153 SSL_METHOD *ssl_method;
156 /* Figure out whether to encrypt the session based on user options */
157 /* User request to disable encryption */
158 if (arg_encrypt == RC_NO || rc_encrypt == RC_NO) {
161 /* User expressed no preference */
162 else if (rc_encrypt == RC_DEFAULT && arg_encrypt == RC_DEFAULT &&
171 SSL_load_error_strings();
172 SSLeay_add_ssl_algorithms();
174 /* Set up the SSL context in which we will oeprate */
175 ssl_method = SSLv23_client_method();
176 ssl_ctx = SSL_CTX_new(ssl_method);
178 error_printf("SSL_CTX_new failed: %s\n",
179 ERR_reason_error_string(ERR_get_error()));
182 /* Any reasonable cipher we can get */
183 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
184 error_printf("No ciphers available for encryption\n");
185 SSL_CTX_free(ssl_ctx);
189 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
191 /* Load DH parameters into the context */
194 error_printf("Can't allocate a DH object: %s\n",
195 ERR_reason_error_string(ERR_get_error()));
198 if (!(BN_hex2bn(&(dh->p), DH_P))) {
199 error_printf("Can't assign DH_P: %s\n",
200 ERR_reason_error_string(ERR_get_error()));
203 if (!(BN_hex2bn(&(dh->g), DH_G))) {
204 error_printf("Can't assign DH_G: %s\n",
205 ERR_reason_error_string(ERR_get_error()));
209 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
212 #ifdef THREADED_CLIENT
213 /* OpenSSL requires callbacks for threaded clients */
214 CRYPTO_set_locking_callback(ssl_lock);
215 CRYPTO_set_id_callback(id_callback);
217 /* OpenSSL requires us to do semaphores for threaded clients */
218 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
220 perror("malloc failed");
223 for (a = 0; a < CRYPTO_num_locks(); a++) {
224 Critters[a] = malloc(sizeof (pthread_mutex_t));
226 perror("malloc failed");
229 pthread_mutex_init(Critters[a], NULL);
232 #endif /* THREADED_CLIENT */
235 ssl = SSL_new(ssl_ctx);
237 error_printf("SSL_new failed: %s\n",
238 ERR_reason_error_string(ERR_get_error()));
239 SSL_CTX_free(ssl_ctx);
243 /* Pointless flag waving */
244 #if SSLEAY_VERSION_NUMBER >= 0x0922
245 SSL_set_session_id_context(ssl, "Citadel/UX SID", 14);
248 if (!access("/var/run/egd-pool", F_OK))
249 RAND_egd("/var/run/egd-pool");
251 if (!RAND_status()) {
252 error_printf("PRNG not properly seeded\n");
256 /* Associate network connection with SSL object */
257 if (SSL_set_fd(ssl, serv_sock) < 1) {
258 error_printf("SSL_set_fd failed: %s\n",
259 ERR_reason_error_string(ERR_get_error()));
260 SSL_CTX_free(ssl_ctx);
267 if (status_hook != NULL)
268 status_hook("Requesting encryption...\r");
270 /* Ready to start SSL/TLS */
274 error_printf("Server can't start TLS: %s\n", &buf[4]);
278 /* Do SSL/TLS handshake */
279 if ((a = SSL_connect(ssl)) < 1) {
280 error_printf("SSL_connect failed: %s\n",
281 ERR_reason_error_string(ERR_get_error()));
282 SSL_CTX_free(ssl_ctx);
288 BIO_set_close(ssl->rbio, BIO_NOCLOSE);
292 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &alg_bits);
293 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
294 SSL_CIPHER_get_version(SSL_get_current_cipher(ssl)),
295 SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)),
298 ssl_is_connected = 1;
302 #endif /* HAVE_OPENSSL */
307 * void endtls() - end SSL/TLS session
317 ssl_is_connected = 0;
319 SSL_CTX_free(ssl_ctx);