Redis-Fast
view release on metacpan or search on metacpan
deps/hiredis/README.md view on Meta::CPAN
This requires OpenSSL development package (e.g. including header files to be
available.
When enabled, SSL/TLS support is built into extra `libhiredis_ssl.a` and
`libhiredis_ssl.so` static/dynamic libraries. This leaves the original libraries
unaffected so no additional dependencies are introduced.
### Using it
First, you'll need to make sure you include the SSL header file:
```c
#include <hiredis/hiredis.h>
#include <hiredis/hiredis_ssl.h>
```
You will also need to link against `libhiredis_ssl`, **in addition** to
`libhiredis` and add `-lssl -lcrypto` to satisfy its dependencies.
Hiredis implements SSL/TLS on top of its normal `redisContext` or
`redisAsyncContext`, so you will need to establish a connection first and then
initiate an SSL/TLS handshake.
#### Hiredis OpenSSL Wrappers
Before Hiredis can negotiate an SSL/TLS connection, it is necessary to
initialize OpenSSL and create a context. You can do that in two ways:
1. Work directly with the OpenSSL API to initialize the library's global context
and create `SSL_CTX *` and `SSL *` contexts. With an `SSL *` object you can
call `redisInitiateSSL()`. In this case Hiredis does not configure
certificate verification for you: set the expected peer name on your own
`SSL *` object (e.g. with `SSL_set1_host()`) before handing it over.
2. Work with a set of Hiredis-provided wrappers around OpenSSL, create a
`redisSSLContext` object to hold configuration and use
`redisInitiateSSLWithContext()` to initiate the SSL/TLS handshake.
```c
/* An Hiredis SSL context. It holds SSL configuration and can be reused across
* many contexts.
*/
redisSSLContext *ssl_context;
/* An error variable to indicate what went wrong, if the context fails to
* initialize.
*/
redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
/* Initialize global OpenSSL state.
*
* You should call this only once when your app initializes, and only if
* you don't explicitly or implicitly initialize OpenSSL it elsewhere.
*/
redisInitOpenSSL();
/* Create SSL context */
redisSSLOptions ssl_options = {
.cacert_filename = "cacertbundle.crt", /* File name of trusted CA/ca bundle file, optional */
.capath = "/path/to/certs", /* Path of trusted certificates, optional */
.cert_filename = "client_cert.pem", /* File name of client certificate file, optional */
.private_key_filename = "client_key.pem", /* File name of client private key, optional */
.server_name = "redis.mydomain.com", /* Server name to request (SNI), optional */
.verify_mode = REDIS_SSL_VERIFY_PEER, /* Verify the peer certificate */
.verify_name = "redis.mydomain.com", /* DNS name or IP the peer certificate must carry */
};
ssl_context = redisCreateSSLContextWithOptions(&ssl_options, &ssl_error);
if(ssl_context == NULL || ssl_error != REDIS_SSL_CTX_NONE) {
/* Handle error and abort... */
/* e.g.
printf("SSL error: %s\n",
(ssl_error != REDIS_SSL_CTX_NONE) ?
redisSSLContextGetError(ssl_error) : "Unknown error");
// Abort
*/
}
/* Create Redis context and establish connection */
c = redisConnect("redis.mydomain.com", 6443);
if (c == NULL || c->err) {
/* Handle error and abort... */
}
/* Negotiate SSL/TLS */
if (redisInitiateSSLWithContext(c, ssl_context) != REDIS_OK) {
/* Handle error, in c->err / c->errstr */
}
```
#### Verifying the server's identity
Set `verify_name` to the DNS name or IP address you are connecting to. The
handshake then fails unless the server certificate carries that identity, in
addition to chaining to a trusted CA.
* Without `verify_name`, **any** certificate issued by a trusted CA is
accepted, whatever name it carries. This is also what the older
`redisCreateSSLContext()` wrapper does: it has no way to set `verify_name`.
* `server_name` only selects which certificate the server presents (SNI). It
is not checked against that certificate.
* `verify_name` requires `verify_mode` to include `REDIS_SSL_VERIFY_PEER`, and
context creation fails with `REDIS_SSL_CTX_VERIFY_NAME_FAILED` otherwise.
* The expected name belongs to the `redisSSLContext`, so use one context per
server name (for example, one per node when connecting to several hosts).
## RESP3 PUSH replies
Redis 6.0 introduced PUSH replies with the reply-type `>`. These messages are generated spontaneously and can arrive at any time, so must be handled using callbacks.
### Default behavior
Hiredis installs handlers on `redisContext` and `redisAsyncContext` by default, which will intercept and free any PUSH replies detected. This means existing code will work as-is after upgrading to Redis 6 and switching to `RESP3`.
### Custom PUSH handler prototypes
The callback prototypes differ between `redisContext` and `redisAsyncContext`.
#### redisContext
```c
void my_push_handler(void *privdata, void *reply) {
/* Handle the reply */
/* Note: We need to free the reply in our custom handler for
blocking contexts. This lets us keep the reply if
( run in 0.699 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )