diff options
author | Nick White <git@njw.name> | 2015-07-28 16:49:34 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2015-07-28 16:49:34 +0100 |
commit | 9c40d77b5e04bbcae170dcd425a25cd857dddc82 (patch) | |
tree | 5a9241403553673b382b487382db500052c79d6e /util.c | |
parent | 85adba49c4fbf14ac3e17c42cab0d9c0c4bbcccd (diff) |
Clean up SSL usage
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 38 |
1 files changed, 17 insertions, 21 deletions
@@ -19,6 +19,7 @@ conn *dial(char *host, char *port, int ssl) static struct addrinfo hints; int srv; struct addrinfo *res, *r; + SSL_CTX *sslcontext; conn *c; c = malloc(sizeof(conn)); c->fd = -1; @@ -52,17 +53,15 @@ conn *dial(char *host, char *port, int ssl) } c->fd = srv; - c->sslcontext = NULL; c->sslhandle = NULL; if(ssl) { SSL_load_error_strings(); SSL_library_init(); - if((c->sslcontext = SSL_CTX_new(SSLv23_client_method())) == NULL) { - /*if((c->sslcontext = SSL_CTX_new(TLS_client_method())) == NULL) {*/ + if((sslcontext = SSL_CTX_new(TLSv1_2_client_method())) == NULL) { ERR_print_errors_fp(stderr); } - if((c->sslhandle = SSL_new(c->sslcontext)) == NULL) { + if((c->sslhandle = SSL_new(sslcontext)) == NULL) { ERR_print_errors_fp(stderr); } if(SSL_set_fd(c->sslhandle, c->fd) != 1) { @@ -99,28 +98,18 @@ int request(char *host, int ssl, char *request, char *savecookie, char **body, i return 0; } - if(ssl) { - if(!SSL_write(c->sslhandle, request, strlen(request))) { - ERR_print_errors_fp(stderr); - return 0; - } - } else { - if(!write(c->fd, request, strlen(request))) { - return 0; - } + if(ssl ? !SSL_write(c->sslhandle, request, strlen(request)) : + !write(c->fd, request, strlen(request))) { + return 0; } /* download everything into buf */ l = 0; buf = malloc(sizeof(char *) * BUFSIZ); - /* TODO: rewrite this so it's clear that only the read call differs, e.g. with - * macros as ii does it, or maybe as a function pointer or something */ - if(ssl) { - for(; buf != NULL && (res = SSL_read(c->sslhandle, buf+l, BUFSIZ)) > 0; l+=res) - buf = realloc(buf, sizeof(char *) * (l+BUFSIZ)); - } else { - for(; buf != NULL && (res = read(c->fd, buf+l, BUFSIZ)) > 0; l+=res) - buf = realloc(buf, sizeof(char *) * (l+BUFSIZ)); + for(; buf != NULL && + (res = ssl ? SSL_read(c->sslhandle, buf+l, BUFSIZ) : read(c->fd, buf+l, BUFSIZ)) > 0; + l+=res) { + buf = realloc(buf, sizeof(char *) * (l+BUFSIZ)); } /* strstr to find end of header */ @@ -168,6 +157,13 @@ int request(char *host, int ssl, char *request, char *savecookie, char **body, i free(buf); + if(ssl) { + SSL_shutdown(c->sslhandle); + SSL_free(c->sslhandle); + } + close(c->fd); + free(c); + return l; } |