2019-08-29 19:08:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
|
|
|
* Copyright (c) 2019, Redis Labs
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-11-27 13:10:21 +00:00
|
|
|
#include "hiredis.h"
|
2019-08-29 19:08:54 +00:00
|
|
|
#include "async.h"
|
2017-11-27 13:10:21 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2019-04-11 19:08:27 +00:00
|
|
|
#include <errno.h>
|
2019-08-22 13:19:05 +00:00
|
|
|
#include <string.h>
|
2020-07-09 00:44:30 +00:00
|
|
|
#ifdef _WIN32
|
2020-04-09 15:05:14 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2019-08-22 13:19:05 +00:00
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
#include <openssl/ssl.h>
|
2019-08-22 13:19:05 +00:00
|
|
|
#include <openssl/err.h>
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-04-09 15:05:14 +00:00
|
|
|
#include "win32.h"
|
2019-08-29 19:08:54 +00:00
|
|
|
#include "async_private.h"
|
2020-05-22 11:10:08 +00:00
|
|
|
#include "hiredis_ssl.h"
|
2019-08-29 19:08:54 +00:00
|
|
|
|
2017-11-27 13:10:21 +00:00
|
|
|
void __redisSetError(redisContext *c, int type, const char *str);
|
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
struct redisSSLContext {
|
|
|
|
/* Associated OpenSSL SSL_CTX as created by redisCreateSSLContext() */
|
2019-08-29 19:08:54 +00:00
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
/* Requested SNI, or NULL */
|
|
|
|
char *server_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The SSL connection context is attached to SSL/TLS connections as a privdata. */
|
|
|
|
typedef struct redisSSL {
|
2019-08-29 19:08:54 +00:00
|
|
|
/**
|
|
|
|
* OpenSSL SSL object.
|
|
|
|
*/
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SSL_write() requires to be called again with the same arguments it was
|
|
|
|
* previously called with in the event of an SSL_read/SSL_write situation
|
|
|
|
*/
|
|
|
|
size_t lastLen;
|
|
|
|
|
|
|
|
/** Whether the SSL layer requires read (possibly before a write) */
|
|
|
|
int wantRead;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether a write was requested prior to a read. If set, the write()
|
|
|
|
* should resume whenever a read takes place, if possible
|
|
|
|
*/
|
|
|
|
int pendingWrite;
|
2020-05-22 11:10:08 +00:00
|
|
|
} redisSSL;
|
2019-08-29 19:08:54 +00:00
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
redisContextFuncs redisContextSSLFuncs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenSSL global initialization and locking handling callbacks.
|
|
|
|
* Note that this is only required for OpenSSL < 1.1.0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
|
|
|
#define HIREDIS_USE_CRYPTO_LOCKS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HIREDIS_USE_CRYPTO_LOCKS
|
2020-07-09 00:44:30 +00:00
|
|
|
#ifdef _WIN32
|
2020-04-09 15:05:14 +00:00
|
|
|
typedef CRITICAL_SECTION sslLockType;
|
|
|
|
static void sslLockInit(sslLockType* l) {
|
|
|
|
InitializeCriticalSection(l);
|
|
|
|
}
|
|
|
|
static void sslLockAcquire(sslLockType* l) {
|
|
|
|
EnterCriticalSection(l);
|
|
|
|
}
|
|
|
|
static void sslLockRelease(sslLockType* l) {
|
|
|
|
LeaveCriticalSection(l);
|
|
|
|
}
|
|
|
|
#else
|
2017-11-27 13:10:21 +00:00
|
|
|
typedef pthread_mutex_t sslLockType;
|
|
|
|
static void sslLockInit(sslLockType *l) {
|
|
|
|
pthread_mutex_init(l, NULL);
|
|
|
|
}
|
|
|
|
static void sslLockAcquire(sslLockType *l) {
|
|
|
|
pthread_mutex_lock(l);
|
|
|
|
}
|
|
|
|
static void sslLockRelease(sslLockType *l) {
|
|
|
|
pthread_mutex_unlock(l);
|
|
|
|
}
|
2020-04-09 15:05:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static sslLockType* ossl_locks;
|
2017-11-27 13:10:21 +00:00
|
|
|
|
|
|
|
static void opensslDoLock(int mode, int lkid, const char *f, int line) {
|
|
|
|
sslLockType *l = ossl_locks + lkid;
|
|
|
|
|
|
|
|
if (mode & CRYPTO_LOCK) {
|
|
|
|
sslLockAcquire(l);
|
|
|
|
} else {
|
|
|
|
sslLockRelease(l);
|
|
|
|
}
|
2018-01-08 21:09:53 +00:00
|
|
|
|
2017-11-27 13:10:21 +00:00
|
|
|
(void)f;
|
|
|
|
(void)line;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:27:49 +00:00
|
|
|
static int initOpensslLocks(void) {
|
2017-11-27 13:10:21 +00:00
|
|
|
unsigned ii, nlocks;
|
|
|
|
if (CRYPTO_get_locking_callback() != NULL) {
|
|
|
|
/* Someone already set the callback before us. Don't destroy it! */
|
2020-05-22 16:27:49 +00:00
|
|
|
return REDIS_OK;
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
|
|
|
nlocks = CRYPTO_num_locks();
|
2020-01-28 20:13:05 +00:00
|
|
|
ossl_locks = hi_malloc(sizeof(*ossl_locks) * nlocks);
|
2020-05-22 16:27:49 +00:00
|
|
|
if (ossl_locks == NULL)
|
|
|
|
return REDIS_ERR;
|
|
|
|
|
2017-11-27 13:10:21 +00:00
|
|
|
for (ii = 0; ii < nlocks; ii++) {
|
|
|
|
sslLockInit(ossl_locks + ii);
|
|
|
|
}
|
|
|
|
CRYPTO_set_locking_callback(opensslDoLock);
|
2020-05-22 16:27:49 +00:00
|
|
|
return REDIS_OK;
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
2019-08-29 19:08:54 +00:00
|
|
|
#endif /* HIREDIS_USE_CRYPTO_LOCKS */
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
int redisInitOpenSSL(void)
|
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
#ifdef HIREDIS_USE_CRYPTO_LOCKS
|
|
|
|
initOpensslLocks();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* redisSSLContext helper context destruction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *redisSSLContextGetError(redisSSLContextError error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case REDIS_SSL_CTX_NONE:
|
|
|
|
return "No Error";
|
|
|
|
case REDIS_SSL_CTX_CREATE_FAILED:
|
|
|
|
return "Failed to create OpenSSL SSL_CTX";
|
|
|
|
case REDIS_SSL_CTX_CERT_KEY_REQUIRED:
|
|
|
|
return "Client cert and key must both be specified or skipped";
|
|
|
|
case REDIS_SSL_CTX_CA_CERT_LOAD_FAILED:
|
|
|
|
return "Failed to load CA Certificate or CA Path";
|
|
|
|
case REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED:
|
|
|
|
return "Failed to load client certificate";
|
|
|
|
case REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED:
|
|
|
|
return "Failed to load private key";
|
|
|
|
default:
|
|
|
|
return "Unknown error code";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisFreeSSLContext(redisSSLContext *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctx->server_name) {
|
|
|
|
hi_free(ctx->server_name);
|
|
|
|
ctx->server_name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->ssl_ctx) {
|
|
|
|
SSL_CTX_free(ctx->ssl_ctx);
|
|
|
|
ctx->ssl_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hi_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* redisSSLContext helper context initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *capath,
|
|
|
|
const char *cert_filename, const char *private_key_filename,
|
|
|
|
const char *server_name, redisSSLContextError *error)
|
|
|
|
{
|
|
|
|
redisSSLContext *ctx = hi_calloc(1, sizeof(redisSSLContext));
|
2020-05-30 18:03:45 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
goto error;
|
2020-05-22 11:10:08 +00:00
|
|
|
|
|
|
|
ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
|
|
|
if (!ctx->ssl_ctx) {
|
|
|
|
if (error) *error = REDIS_SSL_CTX_CREATE_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
|
|
|
|
SSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_PEER, NULL);
|
|
|
|
|
|
|
|
if ((cert_filename != NULL && private_key_filename == NULL) ||
|
|
|
|
(private_key_filename != NULL && cert_filename == NULL)) {
|
|
|
|
if (error) *error = REDIS_SSL_CTX_CERT_KEY_REQUIRED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capath || cacert_filename) {
|
|
|
|
if (!SSL_CTX_load_verify_locations(ctx->ssl_ctx, cacert_filename, capath)) {
|
|
|
|
if (error) *error = REDIS_SSL_CTX_CA_CERT_LOAD_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cert_filename) {
|
|
|
|
if (!SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, cert_filename)) {
|
|
|
|
if (error) *error = REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, private_key_filename, SSL_FILETYPE_PEM)) {
|
|
|
|
if (error) *error = REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server_name)
|
|
|
|
ctx->server_name = hi_strdup(server_name);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
error:
|
|
|
|
redisFreeSSLContext(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
/**
|
|
|
|
* SSL Connection initialization.
|
|
|
|
*/
|
|
|
|
|
2020-05-22 16:27:49 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
static int redisSSLConnect(redisContext *c, SSL *ssl) {
|
2020-07-29 18:53:03 +00:00
|
|
|
if (c->privctx) {
|
2019-08-29 19:08:54 +00:00
|
|
|
__redisSetError(c, REDIS_ERR_OTHER, "redisContext was already associated");
|
|
|
|
return REDIS_ERR;
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
2019-08-29 19:08:54 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
redisSSL *rssl = hi_calloc(1, sizeof(redisSSL));
|
2020-05-22 16:27:49 +00:00
|
|
|
if (rssl == NULL) {
|
|
|
|
__redisSetError(c, REDIS_ERR_OOM, "Out of memory");
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
2019-08-29 19:08:54 +00:00
|
|
|
|
2020-05-22 16:27:49 +00:00
|
|
|
c->funcs = &redisContextSSLFuncs;
|
2019-08-29 19:08:54 +00:00
|
|
|
rssl->ssl = ssl;
|
|
|
|
|
|
|
|
SSL_set_mode(rssl->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
|
|
|
SSL_set_fd(rssl->ssl, c->fd);
|
|
|
|
SSL_set_connect_state(rssl->ssl);
|
|
|
|
|
|
|
|
ERR_clear_error();
|
|
|
|
int rv = SSL_connect(rssl->ssl);
|
|
|
|
if (rv == 1) {
|
2020-07-29 18:53:03 +00:00
|
|
|
c->privctx = rssl;
|
2019-08-29 19:08:54 +00:00
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = SSL_get_error(rssl->ssl, rv);
|
|
|
|
if (((c->flags & REDIS_BLOCK) == 0) &&
|
|
|
|
(rv == SSL_ERROR_WANT_READ || rv == SSL_ERROR_WANT_WRITE)) {
|
2020-07-29 18:53:03 +00:00
|
|
|
c->privctx = rssl;
|
2019-08-29 19:08:54 +00:00
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->err == 0) {
|
|
|
|
char err[512];
|
|
|
|
if (rv == SSL_ERROR_SYSCALL)
|
|
|
|
snprintf(err,sizeof(err)-1,"SSL_connect failed: %s",strerror(errno));
|
|
|
|
else {
|
|
|
|
unsigned long e = ERR_peek_last_error();
|
|
|
|
snprintf(err,sizeof(err)-1,"SSL_connect failed: %s",
|
|
|
|
ERR_reason_error_string(e));
|
|
|
|
}
|
|
|
|
__redisSetError(c, REDIS_ERR_IO, err);
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
2020-05-22 16:27:49 +00:00
|
|
|
|
|
|
|
hi_free(rssl);
|
2019-08-29 19:08:54 +00:00
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
/**
|
|
|
|
* A wrapper around redisSSLConnect() for users who manage their own context and
|
|
|
|
* create their own SSL object.
|
|
|
|
*/
|
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
int redisInitiateSSL(redisContext *c, SSL *ssl) {
|
2020-05-22 11:10:08 +00:00
|
|
|
return redisSSLConnect(c, ssl);
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
/**
|
|
|
|
* A wrapper around redisSSLConnect() for users who use redisSSLContext and don't
|
|
|
|
* manage their own SSL objects.
|
|
|
|
*/
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
int redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx)
|
|
|
|
{
|
|
|
|
if (!c || !redis_ssl_ctx)
|
|
|
|
return REDIS_ERR;
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
/* We want to verify that redisSSLConnect() won't fail on this, as it will
|
|
|
|
* not own the SSL object in that case and we'll end up leaking.
|
|
|
|
*/
|
2020-07-29 18:53:03 +00:00
|
|
|
if (c->privctx)
|
2020-05-22 11:10:08 +00:00
|
|
|
return REDIS_ERR;
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
SSL *ssl = SSL_new(redis_ssl_ctx->ssl_ctx);
|
2019-08-29 19:08:54 +00:00
|
|
|
if (!ssl) {
|
|
|
|
__redisSetError(c, REDIS_ERR_OTHER, "Couldn't create new SSL instance");
|
|
|
|
goto error;
|
2017-11-27 13:10:21 +00:00
|
|
|
}
|
2020-05-22 11:10:08 +00:00
|
|
|
|
|
|
|
if (redis_ssl_ctx->server_name) {
|
|
|
|
if (!SSL_set_tlsext_host_name(ssl, redis_ssl_ctx->server_name)) {
|
|
|
|
__redisSetError(c, REDIS_ERR_OTHER, "Failed to set server_name/SNI");
|
2019-08-29 19:08:54 +00:00
|
|
|
goto error;
|
2018-12-20 14:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
return redisSSLConnect(c, ssl);
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
error:
|
2020-05-22 11:10:08 +00:00
|
|
|
if (ssl)
|
|
|
|
SSL_free(ssl);
|
2017-11-27 13:10:21 +00:00
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:10:08 +00:00
|
|
|
static int maybeCheckWant(redisSSL *rssl, int rv) {
|
2017-11-27 20:49:28 +00:00
|
|
|
/**
|
|
|
|
* If the error is WANT_READ or WANT_WRITE, the appropriate flags are set
|
|
|
|
* and true is returned. False is returned otherwise
|
|
|
|
*/
|
|
|
|
if (rv == SSL_ERROR_WANT_READ) {
|
|
|
|
rssl->wantRead = 1;
|
|
|
|
return 1;
|
|
|
|
} else if (rv == SSL_ERROR_WANT_WRITE) {
|
|
|
|
rssl->pendingWrite = 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
/**
|
|
|
|
* Implementation of redisContextFuncs for SSL connections.
|
|
|
|
*/
|
|
|
|
|
2020-07-29 18:53:03 +00:00
|
|
|
static void redisSSLFree(void *privctx){
|
|
|
|
redisSSL *rsc = privctx;
|
2019-08-29 19:08:54 +00:00
|
|
|
|
|
|
|
if (!rsc) return;
|
|
|
|
if (rsc->ssl) {
|
|
|
|
SSL_free(rsc->ssl);
|
|
|
|
rsc->ssl = NULL;
|
|
|
|
}
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(rsc);
|
2019-08-29 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:38:16 +00:00
|
|
|
static ssize_t redisSSLRead(redisContext *c, char *buf, size_t bufcap) {
|
2020-07-29 18:53:03 +00:00
|
|
|
redisSSL *rssl = c->privctx;
|
2019-08-29 19:08:54 +00:00
|
|
|
|
|
|
|
int nread = SSL_read(rssl->ssl, buf, bufcap);
|
2017-11-27 13:10:21 +00:00
|
|
|
if (nread > 0) {
|
|
|
|
return nread;
|
|
|
|
} else if (nread == 0) {
|
|
|
|
__redisSetError(c, REDIS_ERR_EOF, "Server closed the connection");
|
|
|
|
return -1;
|
|
|
|
} else {
|
2019-08-29 19:08:54 +00:00
|
|
|
int err = SSL_get_error(rssl->ssl, nread);
|
2019-04-11 19:08:27 +00:00
|
|
|
if (c->flags & REDIS_BLOCK) {
|
|
|
|
/**
|
|
|
|
* In blocking mode, we should never end up in a situation where
|
|
|
|
* we get an error without it being an actual error, except
|
|
|
|
* in the case of EINTR, which can be spuriously received from
|
|
|
|
* debuggers or whatever.
|
|
|
|
*/
|
|
|
|
if (errno == EINTR) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
const char *msg = NULL;
|
|
|
|
if (errno == EAGAIN) {
|
2019-09-16 14:16:44 +00:00
|
|
|
msg = "Resource temporarily unavailable";
|
2019-04-11 19:08:27 +00:00
|
|
|
}
|
|
|
|
__redisSetError(c, REDIS_ERR_IO, msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We can very well get an EWOULDBLOCK/EAGAIN, however
|
|
|
|
*/
|
2019-08-29 19:08:54 +00:00
|
|
|
if (maybeCheckWant(rssl, err)) {
|
2017-11-27 13:10:21 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
__redisSetError(c, REDIS_ERR_IO, NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:38:16 +00:00
|
|
|
static ssize_t redisSSLWrite(redisContext *c) {
|
2020-07-29 18:53:03 +00:00
|
|
|
redisSSL *rssl = c->privctx;
|
2019-08-29 19:08:54 +00:00
|
|
|
|
|
|
|
size_t len = rssl->lastLen ? rssl->lastLen : sdslen(c->obuf);
|
|
|
|
int rv = SSL_write(rssl->ssl, c->obuf, len);
|
2017-11-27 13:10:21 +00:00
|
|
|
|
|
|
|
if (rv > 0) {
|
2019-08-29 19:08:54 +00:00
|
|
|
rssl->lastLen = 0;
|
2017-11-27 13:10:21 +00:00
|
|
|
} else if (rv < 0) {
|
2019-08-29 19:08:54 +00:00
|
|
|
rssl->lastLen = len;
|
2017-11-27 13:10:21 +00:00
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
int err = SSL_get_error(rssl->ssl, rv);
|
|
|
|
if ((c->flags & REDIS_BLOCK) == 0 && maybeCheckWant(rssl, err)) {
|
2017-11-27 13:10:21 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
__redisSetError(c, REDIS_ERR_IO, NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-08-29 19:08:54 +00:00
|
|
|
static void redisSSLAsyncRead(redisAsyncContext *ac) {
|
|
|
|
int rv;
|
2020-07-29 18:53:03 +00:00
|
|
|
redisSSL *rssl = ac->c.privctx;
|
2019-08-29 19:08:54 +00:00
|
|
|
redisContext *c = &ac->c;
|
|
|
|
|
|
|
|
rssl->wantRead = 0;
|
|
|
|
|
|
|
|
if (rssl->pendingWrite) {
|
|
|
|
int done;
|
|
|
|
|
|
|
|
/* This is probably just a write event */
|
|
|
|
rssl->pendingWrite = 0;
|
|
|
|
rv = redisBufferWrite(c, &done);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
} else if (!done) {
|
|
|
|
_EL_ADD_WRITE(ac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = redisBufferRead(c);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
} else {
|
|
|
|
_EL_ADD_READ(ac);
|
|
|
|
redisProcessCallbacks(ac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void redisSSLAsyncWrite(redisAsyncContext *ac) {
|
|
|
|
int rv, done = 0;
|
2020-07-29 18:53:03 +00:00
|
|
|
redisSSL *rssl = ac->c.privctx;
|
2019-08-29 19:08:54 +00:00
|
|
|
redisContext *c = &ac->c;
|
|
|
|
|
|
|
|
rssl->pendingWrite = 0;
|
|
|
|
rv = redisBufferWrite(c, &done);
|
|
|
|
if (rv == REDIS_ERR) {
|
|
|
|
__redisAsyncDisconnect(ac);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
if (rssl->wantRead) {
|
|
|
|
/* Need to read-before-write */
|
|
|
|
rssl->pendingWrite = 1;
|
|
|
|
_EL_DEL_WRITE(ac);
|
|
|
|
} else {
|
|
|
|
/* No extra reads needed, just need to write more */
|
|
|
|
_EL_ADD_WRITE(ac);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Already done! */
|
|
|
|
_EL_DEL_WRITE(ac);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always reschedule a read */
|
|
|
|
_EL_ADD_READ(ac);
|
|
|
|
}
|
|
|
|
|
|
|
|
redisContextFuncs redisContextSSLFuncs = {
|
2020-07-29 18:53:03 +00:00
|
|
|
.free_privctx = redisSSLFree,
|
2019-08-29 19:08:54 +00:00
|
|
|
.async_read = redisSSLAsyncRead,
|
|
|
|
.async_write = redisSSLAsyncWrite,
|
|
|
|
.read = redisSSLRead,
|
|
|
|
.write = redisSSLWrite
|
|
|
|
};
|
|
|
|
|