SSL_WRITE is merging messages when sent in different functions for TLS over TCP [duplicate]

我们两清 提交于 2020-12-27 06:09:50

问题


I am writing a client to send data to remote server. I am 3 different messages (hello, hello1, hello2) using SSL_WRITE, but in server I can see only single message is sent like this: hellohello1hello2. Can someone please help what am I missing here, why separate hello, hello1 and hello2 messages are not sent but a string hellohello1hello2 is sent. I am new to openSSL, could you please suggest any tutorial to learn openSSL for TLS over TCP. I need to send logs from my client machine to remote syslog server using TLS over TCP.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <Wspiapi.h>  // This is for pre XP machines to work with new APIs. See MSDN

#include "openssl/sha.h"
#include "openssl/rsa.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/x509v3.h"
#include "openssl/rand.h"
#include "openssl/crypto.h"
#include <errno.h>
#include <iostream>

#include <thread>
using namespace std;
#include <string>

SSL_CTX *InitSSL_CTX(void)
{
    
    const SSL_METHOD *method = TLSv1_2_method(); // Create new client-method instance 
    SSL_CTX *ctx = SSL_CTX_new(method);


    // Trusted root CA certificate bundle
    int iRetVal = SSL_CTX_load_verify_locations(ctx, "ca.pem", NULL);
    if (iRetVal != ERR_LIB_NONE)
    {
        cout<<"Failed to load trusted CA certificates bundle";
        exit(EXIT_FAILURE);
    }

// Callback gets invoked for each certificate in the certificate chain; we just log certificate information in there

    if (ctx == nullptr)
    {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }
    return ctx;
}


int OpenConnection(const char *hostname, const char *port)
{
    SOCKET ConnectSocket;
    struct addrinfo *result, *ptr, hints;
    int iResult;
    char portStr[1024];


/*
Initialize Winsock.
WSAStartup allows to specify what version of WinSock you want to use.
It sets up all the "behind the scenes stuff" that any process needs to use sockets.
winSockDLL is loaded into the process and it sets up many internal structures.
Each process must call WSAStartup to initialize the structures within its own memory space and WSACleanup to tear them down again when it is finished using sockets.
*/
    WSADATA wsaData;
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        cout << "WSAStartup failed with error: " << iResult << endl;
        return 1;
    }


    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(hostname, port, &hints, &result);
    if (iResult != 0)
    {
        cout << "getaddrinfo failed with error: " << iResult;
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address
    for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
    {
        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
        ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET)
        {
            cout << "socket failed with error: " << WSAGetLastError();
            break;
        }

        // Connect to server.
        iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            cout << "connect failed" << endl;
            continue;
        }
        break;
    }

    freeaddrinfo(result);
    if (ConnectSocket == INVALID_SOCKET) {
        cout << "Unable to connect to server!" << endl;
        WSACleanup();
        return 1;
    }
    return ConnectSocket;
}

int main()
{
     SSL_CTX *ctx = InitSSL_CTX();
     SSL *ssl = SSL_new(ctx);
     if (ssl == nullptr)
     {
       cout << "SSL_new() failed\n";
       exit(EXIT_FAILURE);
     }

     //Host is hardcoded to localhost for testing purposes
     const int sfd = OpenConnection("172.16.74.4", "6514");
     SSL_set_fd(ssl, sfd);
 
     const int status = SSL_connect(ssl);
     if (status != 1)
     {
         SSL_get_error(ssl, status);
         ERR_print_errors_fp(stderr); //High probability this doesn't do anything
         cout << "SSL_connect failed with SSL_get_error code :" << status << endl;
         exit(EXIT_FAILURE);
     }

     cout << "Connected with %s encryption\n" << SSL_get_cipher(ssl) << endl;
     DisplayCerts(ssl);

      SSL_write(ssl, "hello", strlen("hello"));
      SSL_write(ssl, "hello1", strlen("hello1"));
      SSL_write(ssl, "hello2", strlen("hello2"));

      SSL_free(ssl);

      SSL_CTX_free(ctx);

      return 0;

}

来源:https://stackoverflow.com/questions/64853870/ssl-write-is-merging-messages-when-sent-in-different-functions-for-tls-over-tcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!