Jine_GG
2021-05-27 15:48
采纳率: 100%
浏览 93

linux C 使用websocket SSL 连接服务器 提示HTTP/1.1 401?

我发送的内容:
GET /ocpp/SINEXCEL HTTP/1.1
Host: everon.io
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Origin: https://everon.io/ocpp
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13


服务器应答的内容:
HTTP/1.1 401 Unauthorized
Date: Thu, 27 May 2021 06:25:14 GMT
Content-Type: text/html;charset=iso-8859-1
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: ISESSIONID=node01sy61o916n1apadkozky7l2w52060936.node0; Path=/
Cache-Control: must-revalidate,no-cache,no-store
Via: 1.1 google
CF-Cache-Status: DYNAMIC
cf-request-id: 0a4e18b15700003666c90b6000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 655d2a2eefaf3666-LAX


源码如下:
secureSocket (char* host, const int& port, char* path)
{
long res = 1;
long flags = 0;
X509* cert = NULL;
std::stringstream line;
int len = 0;
char buff[1536] = {};
int status = 0;
int min_version = 0, max_version = 0;
min_version = TLS1_2_VERSION;
max_version = TLS1_2_VERSION;
unsigned char shakeKey[128] = {0};
const std::string PREFERRED_CIPHERS =
"ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-ECDSA-AES128-SHA "
"ECDHE-ECDSA-AES256-SHA ECDHE-ECDSA-AES128-SHA256 ECDHE-ECDSA-AES256-SHA384 "
"ECDHE-RSA-AES128-GCM-SHA256 ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-AES128-SHA "
"ECDHE-RSA-AES256-SHA ECDHE-RSA-AES128-SHA256 ECDHE-RSA-AES256-SHA384 "
"DHE-RSA-AES128-GCM-SHA256 DHE-RSA-AES256-GCM-SHA384 DHE-RSA-AES128-SHA "
"DHE-RSA-AES256-SHA DHE-RSA-AES128-SHA256 DHE-RSA-AES256-SHA256 AES128-SHA";
std::string hostname(host);
OPENSSL_init_ssl(0, NULL);
const SSL_METHOD* method = SSLv23_method();
if(!(NULL != method)) {errorcode = 1;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure1\n");}
ctx = SSL_CTX_new(method);
if(!(ctx != NULL)) {errorcode = 2;
goto ERRORCODE;};// {throw std::invalid_argument("Error, handle failure2\n");}

/* 制定证书验证方式 */
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);

/* Cannot fail ??? */
SSL_CTX_set_verify_depth(ctx, 4);

/* Cannot fail ??? */
flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;

SSL_CTX_set_options(ctx, (const long )flags);

web = BIO_new_ssl_connect(ctx);

if(!(web != NULL)) {errorcode = 4;
goto ERRORCODE;};// {throw std::invalid_argument("Error, handle failure\n");}


hostname += ":";
hostname += std::to_string(port);


res = BIO_set_conn_hostname(web, hostname.c_str());


if(!(1 == res)) {errorcode = 5;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}

BIO_get_ssl(web, &ssl);

if(!(ssl != NULL)) {errorcode = 6;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}

res = SSL_set_cipher_list(ssl, PREFERRED_CIPHERS.c_str());
if(!(1 == res)) {errorcode = 7;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}

res = SSL_set_tlsext_host_name(ssl, host);
if(!(1 == res)) {errorcode = 8;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}

out = BIO_new_fp(stdout, BIO_NOCLOSE);
if(!(NULL != out)) {errorcode = 9;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}
res = BIO_do_connect(web);
if(!(1 == res)) {errorcode = 10;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}
res = BIO_do_handshake(web);
if(!(1 == res)) {errorcode = 11;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}
/* Step 1: verify a server certificate was presented during the negotiation */
cert = SSL_get_peer_certificate(ssl);
if(cert) { X509_free(cert); } /* Free immediately */
if(NULL == cert) {errorcode = 12;
goto ERRORCODE;};//{throw std::invalid_argument("Error, handle failure\n");}




memset(shakeKey,0, sizeof(shakeKey));
webSocket_buildShakeKey(shakeKey);
line << "GET /" << path << " HTTP/1.1\r\n";
line << "Host: " << host <<" \r\n";
line << "Upgrade: websocket\r\n";
line << "Connection: Upgrade\r\n";
line << "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n";
line << "Origin: https://everon.io/ocpp\r\n";
line << "Sec-WebSocket-Protocol: chat, superchat\r\n";
line << "Sec-WebSocket-Version: 13\r\n\n";

BIO_puts(web, line.str().c_str());
BIO_puts(out, "\n");
printf("Returning pointer to function\n");

len = BIO_read(web, buff, sizeof(buff));
if(len > 0)
BIO_write(out, buff, len);
printf("**line.str().c_str() = %s**\n",line.str().c_str());
printf("**buff = %s**\n",buff);
if(sscanf(buff, "HTTP/1.1 %d", &status) == 1)
{
if(status != 101)
{
errorcode = 99;
printf("**status = %d**\n",status);
goto ERRORCODE;
}
}


printf("**LEAVING BUFFER**\n");


ERRORCODE:
if(errorcode != 0)
{
printf("**ERROR %d**\n",errorcode);
}
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • hufan531 2021-06-02 17:38
    已采纳

    401状态码:请求要求身份验证。 对于需要登录的网页,服务器可能返回此响应。

    打赏 评论
  • 401状态码:请求要求身份验证。 对于需要登录的网页,服务器可能返回此响应。

    打赏 评论
  • Jine_GG 2021-05-27 15:57

    抓包数据如下:

    与我查找的资料点不一致,好像缺少server hello done ,但是我不确定是哪里出现了问题。

    打赏 评论
  • Jine_GG 2021-05-27 17:35

    球球了! 

    打赏 评论
  • 有问必答小助手 2021-05-27 18:11

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    打赏 评论

相关推荐 更多相似问题