OpenSSL: Misc code cleanups

Make use of C99 for loop initialisers, declarations with immediate
rvalues, etc.
This commit is contained in:
Aaron Jones 2016-09-15 16:54:06 +00:00
parent a61e06e1d1
commit e569720fe1
No known key found for this signature in database
GPG key ID: EC6F86EE9CD840B5

View file

@ -56,13 +56,10 @@ static void rb_ssl_tryaccept(rb_fde_t *, void *);
static unsigned long static unsigned long
get_last_err(void) get_last_err(void)
{ {
unsigned long t_err, err = 0; unsigned long err_saved, err = 0;
err = ERR_get_error();
if(err == 0)
return 0;
while((t_err = ERR_get_error()) > 0) while((err_saved = ERR_get_error()) != 0)
err = t_err; err = err_saved;
return err; return err;
} }
@ -70,10 +67,11 @@ get_last_err(void)
static void static void
rb_ssl_accept_common(rb_fde_t *const new_F) rb_ssl_accept_common(rb_fde_t *const new_F)
{ {
int ssl_err; int ssl_err = SSL_accept((SSL *) new_F->ssl);
if((ssl_err = SSL_accept((SSL *) new_F->ssl)) <= 0)
if(ssl_err <= 0)
{ {
switch (ssl_err = SSL_get_error((SSL *) new_F->ssl, ssl_err)) switch((ssl_err = SSL_get_error((SSL *) new_F->ssl, ssl_err)))
{ {
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
if(rb_ignore_errno(errno)) if(rb_ignore_errno(errno))
@ -101,13 +99,14 @@ rb_ssl_accept_common(rb_fde_t *const new_F)
static void static void
rb_ssl_tryaccept(rb_fde_t *const F, void *const data) rb_ssl_tryaccept(rb_fde_t *const F, void *const data)
{ {
int ssl_err;
lrb_assert(F->accept != NULL); lrb_assert(F->accept != NULL);
int flags;
if(! SSL_is_init_finished((SSL *) F->ssl)) if(! SSL_is_init_finished((SSL *) F->ssl))
{ {
if((ssl_err = SSL_accept((SSL *) F->ssl)) <= 0) int flags;
int ssl_err = SSL_accept((SSL *) F->ssl);
if(ssl_err <= 0)
{ {
switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
{ {
@ -133,6 +132,7 @@ rb_ssl_tryaccept(rb_fde_t *const F, void *const data)
else else
F->handshake_count++; F->handshake_count++;
} }
rb_settimeout(F, 0, NULL, NULL); rb_settimeout(F, 0, NULL, NULL);
rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
@ -145,11 +145,12 @@ rb_ssl_tryaccept(rb_fde_t *const F, void *const data)
static void static void
rb_ssl_tryconn_cb(rb_fde_t *const F, void *const data) rb_ssl_tryconn_cb(rb_fde_t *const F, void *const data)
{ {
struct ssl_connect *const sconn = data;
int ssl_err;
if(! SSL_is_init_finished((SSL *) F->ssl)) if(! SSL_is_init_finished((SSL *) F->ssl))
{ {
if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) struct ssl_connect *const sconn = data;
int ssl_err = SSL_connect((SSL *) F->ssl);
if(ssl_err <= 0)
{ {
switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
{ {
@ -181,7 +182,7 @@ static void
rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data)
{ {
struct ssl_connect *const sconn = data; struct ssl_connect *const sconn = data;
int ssl_err;
if(status != RB_OK) if(status != RB_OK)
{ {
rb_ssl_connect_realcb(F, status, sconn); rb_ssl_connect_realcb(F, status, sconn);
@ -189,11 +190,16 @@ rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data)
} }
F->type |= RB_FD_SSL; F->type |= RB_FD_SSL;
F->ssl = SSL_new(ssl_ctx); F->ssl = SSL_new(ssl_ctx);
SSL_set_fd((SSL *) F->ssl, F->fd); SSL_set_fd((SSL *) F->ssl, F->fd);
SSL_set_connect_state((SSL *) F->ssl); SSL_set_connect_state((SSL *) F->ssl);
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0)
int ssl_err = SSL_connect((SSL *) F->ssl);
if(ssl_err <= 0)
{ {
switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
{ {
@ -226,6 +232,7 @@ get_ssl_error(unsigned long err)
static char buf[512]; static char buf[512];
ERR_error_string_n(err, buf, sizeof buf); ERR_error_string_n(err, buf, sizeof buf);
return buf; return buf;
} }
@ -271,6 +278,7 @@ rb_ssl_read_or_write(const int r_or_w, rb_fde_t *const F, void *const rbuf, cons
err = get_last_err(); err = get_last_err();
break; break;
} }
F->ssl_errno = err; F->ssl_errno = err;
if(err > 0) if(err > 0)
{ {
@ -291,16 +299,17 @@ rb_ssl_read_or_write(const int r_or_w, rb_fde_t *const F, void *const rbuf, cons
void void
rb_ssl_shutdown(rb_fde_t *const F) rb_ssl_shutdown(rb_fde_t *const F)
{ {
int i;
if(F == NULL || F->ssl == NULL) if(F == NULL || F->ssl == NULL)
return; return;
SSL_set_shutdown((SSL *) F->ssl, SSL_RECEIVED_SHUTDOWN); SSL_set_shutdown((SSL *) F->ssl, SSL_RECEIVED_SHUTDOWN);
for(i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
{ {
if(SSL_shutdown((SSL *) F->ssl)) if(SSL_shutdown((SSL *) F->ssl))
break; break;
} }
get_last_err(); get_last_err();
SSL_free((SSL *) F->ssl); SSL_free((SSL *) F->ssl);
} }
@ -308,8 +317,6 @@ rb_ssl_shutdown(rb_fde_t *const F)
int int
rb_init_ssl(void) rb_init_ssl(void)
{ {
char libratbox_data[] = "libratbox data";
/* /*
* OpenSSL 1.1.0 and above automatically initialises itself with sane defaults * OpenSSL 1.1.0 and above automatically initialises itself with sane defaults
*/ */
@ -500,13 +507,12 @@ rb_get_ssl_strerror(rb_fde_t *const F)
int int
rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
{ {
const EVP_MD *evp;
unsigned int len;
int res;
if(F->ssl == NULL) if(F->ssl == NULL)
return 0; return 0;
const EVP_MD *evp;
unsigned int len;
switch(method) switch(method)
{ {
case RB_SSL_CERTFP_METH_SHA1: case RB_SSL_CERTFP_METH_SHA1:
@ -529,7 +535,7 @@ rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], co
if(cert == NULL) if(cert == NULL)
return 0; return 0;
res = SSL_get_verify_result((SSL *) F->ssl); int res = SSL_get_verify_result((SSL *) F->ssl);
switch(res) switch(res)
{ {
case X509_V_OK: case X509_V_OK:
@ -555,14 +561,14 @@ rb_get_ssl_info(char *const buf, const size_t len)
{ {
#ifdef LRB_SSL_FULL_VERSION_INFO #ifdef LRB_SSL_FULL_VERSION_INFO
if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME) if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME)
rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", (void) rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s",
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME); LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME);
else else
rb_snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)", (void) rb_snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)",
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME, LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME,
LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME); LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME);
#else #else
rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", (void) rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s",
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME); LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME);
#endif #endif
} }
@ -596,38 +602,40 @@ rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count)
} }
void void
rb_ssl_start_accepted(rb_fde_t *const new_F, ACCB *const cb, void *const data, const int timeout) rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout)
{ {
new_F->type |= RB_FD_SSL; F->type |= RB_FD_SSL;
new_F->ssl = SSL_new(ssl_ctx);
new_F->accept = rb_malloc(sizeof(struct acceptdata));
new_F->accept->callback = cb; F->accept = rb_malloc(sizeof(struct acceptdata));
new_F->accept->data = data; F->accept->callback = cb;
rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL); F->accept->data = data;
F->accept->addrlen = 0;
(void) memset(&F->accept->S, 0x00, sizeof F->accept->S);
new_F->accept->addrlen = 0; rb_settimeout(F, timeout, rb_ssl_timeout, NULL);
SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F)); F->ssl = SSL_new(ssl_ctx);
SSL_set_accept_state((SSL *) new_F->ssl); SSL_set_fd((SSL *) F->ssl, rb_get_fd(F));
rb_ssl_accept_common(new_F); SSL_set_accept_state((SSL *) F->ssl);
rb_ssl_accept_common(F);
} }
void void
rb_ssl_accept_setup(rb_fde_t *const F, rb_fde_t *const new_F, struct sockaddr *const st, const int addrlen) rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen)
{ {
new_F->type |= RB_FD_SSL; cli_F->type |= RB_FD_SSL;
new_F->ssl = SSL_new(ssl_ctx);
new_F->accept = rb_malloc(sizeof(struct acceptdata));
new_F->accept->callback = F->accept->callback; cli_F->accept = rb_malloc(sizeof(struct acceptdata));
new_F->accept->data = F->accept->data; cli_F->accept->callback = srv_F->accept->callback;
rb_settimeout(new_F, 10, rb_ssl_timeout, NULL); cli_F->accept->data = srv_F->accept->data;
memcpy(&new_F->accept->S, st, addrlen); cli_F->accept->addrlen = addrlen;
new_F->accept->addrlen = addrlen; (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S);
(void) memcpy(&cli_F->accept->S, st, addrlen);
SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F)); rb_settimeout(cli_F, 10, rb_ssl_timeout, NULL);
SSL_set_accept_state((SSL *) new_F->ssl); cli_F->ssl = SSL_new(ssl_ctx);
rb_ssl_accept_common(new_F); SSL_set_fd((SSL *) cli_F->ssl, rb_get_fd(cli_F));
SSL_set_accept_state((SSL *) cli_F->ssl);
rb_ssl_accept_common(cli_F);
} }
void void
@ -636,8 +644,6 @@ rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data
if(F == NULL) if(F == NULL)
return; return;
int ssl_err;
struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
sconn->data = data; sconn->data = data;
sconn->callback = callback; sconn->callback = callback;
@ -647,12 +653,15 @@ rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data
F->connect->callback = callback; F->connect->callback = callback;
F->connect->data = data; F->connect->data = data;
F->type |= RB_FD_SSL; F->type |= RB_FD_SSL;
F->ssl = SSL_new(ssl_ctx);
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
F->ssl = SSL_new(ssl_ctx);
SSL_set_fd((SSL *) F->ssl, F->fd); SSL_set_fd((SSL *) F->ssl, F->fd);
SSL_set_connect_state((SSL *) F->ssl); SSL_set_connect_state((SSL *) F->ssl);
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) int ssl_err = SSL_connect((SSL *) F->ssl);
if(ssl_err <= 0)
{ {
switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) switch(ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
{ {
@ -733,9 +742,8 @@ rb_ssl_clear_handshake_count(rb_fde_t *const F)
int int
rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept)
{ {
int result; int result = rb_listen(F, backlog, defer_accept);
result = rb_listen(F, backlog, defer_accept);
F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
return result; return result;