5 #include <openssl/ssl.h>
6 #include <openssl/err.h>
7 #include <openssl/rand.h>
14 #include "client_crypto.h"
19 int ssl_is_connected = 0;
20 #ifdef THREADED_CLIENT
21 pthread_mutex_t **Critters; /* Things that need locking */
22 #endif /* THREADED_CLIENT */
25 extern int server_is_local;
26 #endif /* HAVE_OPENSSL */
31 * input binary data from encrypted connection
33 void serv_read_ssl(char *buf, int bytes)
40 if (SSL_want_read(ssl)) {
41 if ((SSL_write(ssl, junk, 0)) < 1) {
42 fprintf(stderr, "SSL_write in serv_read:\n");
43 ERR_print_errors_fp(stderr);
46 rlen = SSL_read(ssl, &buf[len], bytes - len);
50 errval = SSL_get_error(ssl, rlen);
51 if (errval == SSL_ERROR_WANT_READ ||
52 errval == SSL_ERROR_WANT_WRITE) {
56 if (errval == SSL_ERROR_ZERO_RETURN ||
57 errval == SSL_ERROR_SSL) {
59 serv_read(&buf[len], bytes - len);
62 fprintf(stderr, "SSL_read in serv_read:\n");
63 ERR_print_errors_fp(stderr);
73 * send binary to server encrypted
75 void serv_write_ssl(char *buf, int nbytes)
77 int bytes_written = 0;
81 while (bytes_written < nbytes) {
82 if (SSL_want_write(ssl)) {
83 if ((SSL_read(ssl, junk, 0)) < 1) {
84 fprintf(stderr, "SSL_read in serv_write:\n");
85 ERR_print_errors_fp(stderr);
88 retval = SSL_write(ssl, &buf[bytes_written],
89 nbytes - bytes_written);
93 errval = SSL_get_error(ssl, retval);
94 if (errval == SSL_ERROR_WANT_READ ||
95 errval == SSL_ERROR_WANT_WRITE) {
99 if (errval == SSL_ERROR_ZERO_RETURN ||
100 errval == SSL_ERROR_SSL) {
102 serv_write(&buf[bytes_written],
103 nbytes - bytes_written);
106 fprintf(stderr, "SSL_write in serv_write:\n");
107 ERR_print_errors_fp(stderr);
111 bytes_written += retval;
116 void ssl_lock(int mode, int n, const char *file, int line)
118 #ifdef THREADED_CLIENT
119 if (mode & CRYPTO_LOCK)
120 pthread_mutex_lock(Critters[n]);
122 pthread_mutex_unlock(Critters[n]);
123 #endif /* THREADED_CLIENT */
125 #endif /* HAVE_OPENSSL */
129 * starttls() starts SSL/TLS if possible
130 * Returns 1 if the session is encrypted, 0 otherwise
137 SSL_METHOD *ssl_method;
140 /* TLS is pointless when server is local */
141 if (server_is_local) {
149 SSL_load_error_strings();
150 SSLeay_add_ssl_algorithms();
152 /* Set up the SSL context in which we will oeprate */
153 ssl_method = SSLv23_client_method();
154 ssl_ctx = SSL_CTX_new(ssl_method);
156 fprintf(stderr, "SSL_CTX_new failed: %s\n",
157 ERR_reason_error_string(ERR_get_error()));
160 /* Any reasonable cipher we can get */
161 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
162 fprintf(stderr, "No ciphers available for encryption\n");
163 SSL_CTX_free(ssl_ctx);
167 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
169 /* Load DH parameters into the context */
172 fprintf(stderr, "Can't allocate a DH object: %s\n",
173 ERR_reason_error_string(ERR_get_error()));
176 if (!(BN_hex2bn(&(dh->p), DH_P))) {
177 fprintf(stderr, "Can't assign DH_P: %s\n",
178 ERR_reason_error_string(ERR_get_error()));
181 if (!(BN_hex2bn(&(dh->g), DH_G))) {
182 fprintf(stderr, "Can't assign DH_G: %s\n",
183 ERR_reason_error_string(ERR_get_error()));
187 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
190 #ifdef THREADED_CLIENT
191 /* OpenSSL requires callbacks for threaded clients */
192 CRYPTO_set_locking_callback(ssl_lock);
193 CRYPTO_set_id_callback(pthread_self);
195 /* OpenSSL requires us to do semaphores for threaded clients */
196 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
198 perror("malloc failed");
201 for (a = 0; a < CRYPTO_num_locks(); a++) {
202 Critters[a] = malloc(sizeof (pthread_mutex_t));
204 perror("malloc failed");
207 pthread_mutex_init(Critters[a], NULL);
210 #endif /* THREADED_CLIENT */
213 ssl = SSL_new(ssl_ctx);
215 fprintf(stderr, "SSL_new failed: %s\n",
216 ERR_reason_error_string(ERR_get_error()));
217 SSL_CTX_free(ssl_ctx);
221 /* Pointless flag waving */
222 #if SSLEAY_VERSION_NUMBER >= 0x0922
223 SSL_set_session_id_context(ssl, "Citadel/UX SID", 14);
225 if (!RAND_status()) {
226 fprintf(stderr, "PRNG not properly seeded\n");
230 /* Associate network connection with SSL object */
231 if (SSL_set_fd(ssl, serv_sock) < 1) {
232 fprintf(stderr, "SSL_set_fd failed: %s\n",
233 ERR_reason_error_string(ERR_get_error()));
234 SSL_CTX_free(ssl_ctx);
241 printf("Requesting encryption...\r");
244 /* Ready to start SSL/TLS */
248 fprintf(stderr, "Server can't start TLS: %s\n", &buf[4]);
252 /* Do SSL/TLS handshake */
253 if ((a = SSL_connect(ssl)) < 1) {
254 fprintf(stderr, "SSL_connect failed: %s\n",
255 ERR_reason_error_string(ERR_get_error()));
256 SSL_CTX_free(ssl_ctx);
262 BIO_set_close(ssl->rbio, BIO_NOCLOSE);
266 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &alg_bits);
267 printf("Encrypting with %s cipher %s (%d of %d bits)\n",
268 SSL_CIPHER_get_version(SSL_get_current_cipher(ssl)),
269 SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)),
272 ssl_is_connected = 1;
276 #endif /* HAVE_OPENSSL */
281 * void endtls() - end SSL/TLS session
291 ssl_is_connected = 0;
293 SSL_CTX_free(ssl_ctx);