"Fossies" - the Fresh Open Source Software Archive

Member "wrk-4.2.0/src/ssl.c" (7 Feb 2021, 1960 Bytes) of package /linux/www/wrk-4.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ssl.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.0.2_vs_4.1.0.

    1 // Copyright (C) 2013 - Will Glozer.  All rights reserved.
    2 
    3 #include <pthread.h>
    4 
    5 #include <openssl/evp.h>
    6 #include <openssl/err.h>
    7 #include <openssl/ssl.h>
    8 
    9 #include "ssl.h"
   10 
   11 SSL_CTX *ssl_init() {
   12     SSL_CTX *ctx = NULL;
   13 
   14     SSL_load_error_strings();
   15     SSL_library_init();
   16     OpenSSL_add_all_algorithms();
   17 
   18     if ((ctx = SSL_CTX_new(SSLv23_client_method()))) {
   19         SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
   20         SSL_CTX_set_verify_depth(ctx, 0);
   21         SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
   22         SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT);
   23     }
   24 
   25     return ctx;
   26 }
   27 
   28 status ssl_connect(connection *c, char *host) {
   29     int r;
   30     SSL_set_fd(c->ssl, c->fd);
   31     SSL_set_tlsext_host_name(c->ssl, host);
   32     if ((r = SSL_connect(c->ssl)) != 1) {
   33         switch (SSL_get_error(c->ssl, r)) {
   34             case SSL_ERROR_WANT_READ:  return RETRY;
   35             case SSL_ERROR_WANT_WRITE: return RETRY;
   36             default:                   return ERROR;
   37         }
   38     }
   39     return OK;
   40 }
   41 
   42 status ssl_close(connection *c) {
   43     SSL_shutdown(c->ssl);
   44     SSL_clear(c->ssl);
   45     return OK;
   46 }
   47 
   48 status ssl_read(connection *c, size_t *n) {
   49     int r;
   50     if ((r = SSL_read(c->ssl, c->buf, sizeof(c->buf))) <= 0) {
   51         switch (SSL_get_error(c->ssl, r)) {
   52             case SSL_ERROR_WANT_READ:  return RETRY;
   53             case SSL_ERROR_WANT_WRITE: return RETRY;
   54             default:                   return ERROR;
   55         }
   56     }
   57     *n = (size_t) r;
   58     return OK;
   59 }
   60 
   61 status ssl_write(connection *c, char *buf, size_t len, size_t *n) {
   62     int r;
   63     if ((r = SSL_write(c->ssl, buf, len)) <= 0) {
   64         switch (SSL_get_error(c->ssl, r)) {
   65             case SSL_ERROR_WANT_READ:  return RETRY;
   66             case SSL_ERROR_WANT_WRITE: return RETRY;
   67             default:                   return ERROR;
   68         }
   69     }
   70     *n = (size_t) r;
   71     return OK;
   72 }
   73 
   74 size_t ssl_readable(connection *c) {
   75     return SSL_pending(c->ssl);
   76 }