problem with my code

Attila Csosz csosz77 at gmail.com
Thu Jun 18 10:00:33 UTC 2020


Hi,

I'm trying to connect to www.google.com on 443 port with SSL but my code
shows an error at ssl_write.
This code is written in C++ Builder on Windows.
Everything is good before this point
 err = SSL_write(ssl, buf, strlen(buf));

But after that err=-1 and code=1 (means SSL_ERROR_SSL )

What may the problem?

My question also: I have found that maybe I have to use ERR_get_error but
this function is in an another dll (libeay32) how will this function
connect to the ssl instance?

Thanks for your help


void init_openssl()
{
    SSL_load_error_strings();
    SSL_library_init();
}

AnsiString s, HEADERS = "GET /search?q=test HTTP/1.1\r\nHost:
www.google.com\r\nUser-Agent:
OpenSSL\r\n\r\n";

void TestProc()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 struct sockaddr_in sa;
 PSSL_METHOD meth;
 u_long iMode = 1;
 char buf[1024];

 strcpy(buf, HEADERS.c_str());

 wVersionRequested = MAKEWORD(2, 2);
 err = WSAStartup(wVersionRequested, &wsaData);
 if (err != 0) throw Exception("WSAStartup");

 init_openssl();

 // Create socket
 sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP  );
 memset(&sa, 0, sizeof(sa));
 sa.sin_family      = AF_INET;
 sa.sin_addr.s_addr = inet_addr("216.58.213.4");   /* Server IP : this
google.com */
 sa.sin_port        = htons(443);                  /* Server Port number */
 err = connect(sd, (struct sockaddr*) &sa, sizeof(sa));
 if (err == -1) throw Exception("SOCKET: connect");
 err = ioctlsocket(sd, FIONBIO, &iMode);
 if (err == -1) throw Exception("SOCKET: set blocking mode");

 // Create SSL context
 meth = SSLv23_server_method();
 if (!meth) throw Exception("SSL: method");
 ctx = SSL_CTX_new(meth);
 if (!meth) throw Exception("SSL: context");
 ssl = SSL_new(ctx);
 if (!ssl) throw Exception("SSL: ssl");
 SSL_set_fd(ssl, sd);

 // Connect
 err = SSL_connect(ssl);
 if (err == -1) throw Exception("SSL: SSL_connect");

 // Send
 s = HEADERS;
 err = SSL_write(ssl, buf, strlen(buf));   /* error at this point, code =
SSL_ERROR_SSL */
 code = SSL_get_error(ssl, err);
 if (err == -1) throw Exception("SSL: send: "+s);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mta.openssl.org/pipermail/openssl-users/attachments/20200618/c1508eab/attachment.html>


More information about the openssl-users mailing list