C hiredis无法使用TLS
在C hiredis无法使用TLS,c,linux,redis,hiredis,C,Linux,Redis,Hiredis,在C中使用Redis有以下代码。 基于hiredis #include <stdio.h> #include <stdlib.h> #include <string.h> #include <hiredis.h> int main(int argc, char **argv) { unsigned int j; redisContext *c; redisReply *reply; const char *ho
C
中使用Redis
有以下代码。
基于hiredis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
int main(int argc, char **argv) {
unsigned int j;
redisContext *c;
redisReply *reply;
const char *hostname = "MY-HOSTNAME";
int port = 6379;
const char *cert = NULL;
const char *key = NULL;
const char *ca = "MY-CA";
struct timeval tv = { 1, 500000 }; // 1.5 seconds
redisOptions options = {0};
REDIS_OPTIONS_SET_TCP(&options, hostname, port);
options.timeout = &tv;
c = redisConnectWithOptions(&options);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
if (redisSecureConnection(c, ca, cert, key, "sni") != REDIS_OK) {
printf("Couldn't initialize SSL!\n");
printf("Error: %s\n", c->errstr);
redisFree(c);
exit(1);
}
reply = redisCommand(c,"SELECT 0");
printf("SELECT DB: %s\n", reply->str);
freeReplyObject(reply);
redisFree(c);
return 0;
}
错误为空,我无法控制Redis
服务器
如何调试
redisSecureConnection(c、ca、证书、密钥,“sni”)
似乎返回-1
Wireshark
输出以下内容:
1027 26.554662 192.168.20.228 → 179.11.21.99 TCP 64 55480 → 30642 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=64 TSval=197044507 TSecr=0 SACK_PERM=1
1028 26.589376 179.11.21.99 → 192.168.20.228 TCP 60 30642 → 55480 [SYN, ACK] Seq=0 Ack=1 Win=28560 Len=0 MSS=1440 SACK_PERM=1 TSval=136342937 TSecr=197044507 WS=512
1029 26.589430 192.168.20.228 → 179.11.21.99 TCP 52 55480 → 30642 [ACK] Seq=1 Ack=1 Win=131328 Len=0 TSval=197044541 TSecr=136342937
1030 26.589625 192.168.20.228 → 179.11.21.99 TCP 52 55480 → 30642 [FIN, ACK] Seq=1 Ack=1 Win=131328 Len=0 TSval=197044541 TSecr=136342937
1033 26.625423 179.11.21.99 → 192.168.20.228 TCP 52 30642 → 55480 [FIN, ACK] Seq=1 Ack=2 Win=28672 Len=0 TSval=136342946 TSecr=197044541
1034 26.625499 192.168.20.228 → 179.11.21.99 TCP 52 55480 → 30642 [ACK] Seq=2 Ack=2 Win=131328 Len=0 TSval=197044577 TSecr=136342946
这表示从未尝试过任何SSL
/TLS
,没有客户端Hello
等
如果我尝试使用NodeJS
client,我会得到这样的结果,并且一切正常:
902034 1181.706157 192.168.20.228 → 179.11.21.99 TCP 64 56765 → 30642 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=64 TSval=198187591 TSecr=0 SACK_PERM=1
902035 1181.742757 179.11.21.99 → 192.168.20.228 TCP 60 30642 → 56765 [SYN, ACK] Seq=0 Ack=1 Win=28560 Len=0 MSS=1440 SACK_PERM=1 TSval=106784830 TSecr=198187591 WS=512
902036 1181.742823 192.168.20.228 → 179.11.21.99 TCP 52 56765 → 30642 [ACK] Seq=1 Ack=1 Win=131328 Len=0 TSval=198187627 TSecr=106784830
902037 1181.744130 192.168.20.228 → 179.11.21.99 TLSv1 272 Client Hello
902039 1181.779023 179.11.21.99 → 192.168.20.228 TLSv1.2 1480 Server Hello
902040 1181.779026 179.11.21.99 → 192.168.20.228 TLSv1.2 912 Certificate, Server Key Exchange, Server Hello Done
您提供的示例代码是正确的,应该可以工作,或者至少在所有错误配置的情况下生成有意义的错误消息 如果在没有任何错误消息的情况下获得
REDIS\u ERR
,则表明您的hiredis版本未使用SSL/TLS支持进行编译
如果您自己构建hiredis,请使用
make use_SSL=1
来构建SSL/TLS支持,因为默认情况下,它当前未启用。然后,您可以使用类似于gcc example.c libhiredis.a-o example-lssl-lcrypto
(在Linux上)的东西重新编译和重新链接示例。您提供的示例代码是正确的,应该可以工作,或者至少在所有配置错误的情况下都会产生有意义的错误消息
如果在没有任何错误消息的情况下获得REDIS\u ERR
,则表明您的hiredis版本未使用SSL/TLS支持进行编译
如果您自己构建hiredis,请使用
make use_SSL=1
来构建SSL/TLS支持,因为默认情况下,它当前未启用。然后,您可以使用类似于gcc example.c libhiredis.a-o example-lssl-lcrypto
(在Linux上)的东西重新编译和重新链接示例。您在Mac上吗?当前是,但在CentOS上出现相同的“缺失”错误。CentOS上出现错误。为问题添加了更多信息…我运行了make USE\u SSL=1
然后make install
。使用gcc示例ssl.c-o示例ssl-lhiredis-I/Users/alfredball/hiredis/-lssl-lcrypto
编译失败,因为库未找到-lssl
。使用“-lcrypto-L/usr/local/local/lib-L/usr/local/opt/openssl/lib/`I运行使用\u ssl=1
,然后进行安装。使用gcc示例ssl.c-o示例ssl-lhiredis-I/Users/alfredballe/hiredis/-lssl-lcrypto
编译失败,因为库未找到-lssl
。使用“--lcrypto-L/usr/local/lib-L/usr/local/opt/openssl/lib”/`
902034 1181.706157 192.168.20.228 → 179.11.21.99 TCP 64 56765 → 30642 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=64 TSval=198187591 TSecr=0 SACK_PERM=1
902035 1181.742757 179.11.21.99 → 192.168.20.228 TCP 60 30642 → 56765 [SYN, ACK] Seq=0 Ack=1 Win=28560 Len=0 MSS=1440 SACK_PERM=1 TSval=106784830 TSecr=198187591 WS=512
902036 1181.742823 192.168.20.228 → 179.11.21.99 TCP 52 56765 → 30642 [ACK] Seq=1 Ack=1 Win=131328 Len=0 TSval=198187627 TSecr=106784830
902037 1181.744130 192.168.20.228 → 179.11.21.99 TLSv1 272 Client Hello
902039 1181.779023 179.11.21.99 → 192.168.20.228 TLSv1.2 1480 Server Hello
902040 1181.779026 179.11.21.99 → 192.168.20.228 TLSv1.2 912 Certificate, Server Key Exchange, Server Hello Done