OpenSSL SSL_read malfunction
OpenSSL SSL_read malfunction
我正在尝试编写一个连接到具有OpenSSL的服务器的c ++应用程序。我可以发送数据,这些数据完好无损地到达服务器,但读取操作只读取 1 个字节。
代码 :
char* dest_url = "147.87.116.74";
X509 *cert = NULL;
X509_name_st *certname = NULL;
const SSL_METHOD *method;
SSL_CTX *ctx;
SSL *ssl;
int server = 0;
int ret, i;
/* ---------------------------------------------------------- *
* These function calls initialize openssl for correct work. *
* ---------------------------------------------------------- */
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();
/* ---------------------------------------------------------- *
* initialize SSL library and register algorithms *
* ---------------------------------------------------------- */
SSL_library_init();
/* ---------------------------------------------------------- *
* Set SSLv2 client hello, also announce SSLv3 and TLSv1 *
* ---------------------------------------------------------- */
method = TLSv1_2_client_method();
/* ---------------------------------------------------------- *
* Try to create a new SSL context *
* ---------------------------------------------------------- */
ctx = SSL_CTX_new(method);
/* ---------------------------------------------------------- *
* SSL certificate checking AND MY STUFF
* thanks guys http://h71000.www7.hp.com/doc/83final/ba554_90007/ch05s03.html
* ---------------------------------------------------------- */
SSL_CTX_load_verify_locations(ctx, "ca.crt", nullptr);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
SSL_CTX_set_verify_depth(ctx, 1);
/* ---------------------------------------------------------- *
* Create new SSL connection state object *
* ---------------------------------------------------------- */
ssl = SSL_new(ctx);
/* ---------------------------------------------------------- *
* Make the underlying TCP socket connection *
* ---------------------------------------------------------- */
server = create_socket(dest_url);
/* ---------------------------------------------------------- *
* Attach the SSL session to the socket descriptor *
* ---------------------------------------------------------- */
SSL_set_fd(ssl, server);
/* ---------------------------------------------------------- *
* Try to SSL-connect here, returns 1 for success *
* ---------------------------------------------------------- */
SSL_connect(ssl);
/* ---------------------------------------------------------- *
* send some text *
* -----------------------------------------------------------*/
char* tSend = "testdata";
int sendSize = strlen(tSend);
int net_tSend = htonl(sendSize);
SSL_write(ssl, &net_tSend, 4);
SSL_write(ssl, tSend, sendSize);
long size = 0L;
int bytesread = SSL_read(ssl, &size, 4);
我的问题 :是否必须使用BIO对象?为什么 read() 函数读取只有 1 个字节?如何检索/读取错误?
读取的字节数是任意的。如果您确切地知道应该接收多少字节,则可以使用准确读取该数字的函数。
此函数是SSL_read
的替代品,除非出现错误,否则始终准确读取指定的字节数。它在错误时返回 <0(调用 SSL_get_error
)。如果连接关闭,则返回 0。成功后,它返回读取的字节数,该字节数将始终与请求的数字相同。
int SSL_read_all(SSL *ssl, void* buf, int num)
{
char* ptr = reinterpret_cast<char*>(buf);
int read_bytes = 0;
while (read_bytes < num)
{
int r = SSL_read(ssl, ptr + read_bytes, num - read_bytes);
if (r <= 0)
return r;
read_bytes += r;
}
return read_bytes;
}
如果OpenSSL直接与套接字通信,则不需要显式使用BIO。
相关文章:
- 使用 ImageIO.read() 生成的图像是否仍然使用传递给它的相同内存?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- write() 和 read() 中几乎没有混淆
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 对同一文件使用 .read 的次数是否有限制?
- Win32 API - HWND "{unused = ???} Unable to read memory"错误
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- QDataStream read to QVector
- asio::read() 需要很长时间,使用 asio::write 没有问题
- Read OpenCV Mat 16bit to QImage 8bit Greyscale
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 如何将 (serverClients[i].read() 传递给 ESP8266 上的字节数组
- 将 Lora.read() 转储到数组
- 无法通过 FUSE 文件系统"read"任何内容
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- 封送指向结构异常"cannot Read or Write protected memory"的指针数组的指针数组
- C++HTTP客户端在GET请求后挂起read()调用
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- Arduino Uno - serial.read to a struct of bits