problem with my code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with my code

Attila Csosz
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);
}



Reply | Threaded
Open this post in threaded view
|

Re: problem with my code

Viktor Dukhovni
On Thu, Jun 18, 2020 at 12:00:33PM +0200, Attila Csosz wrote:

>  err = connect(sd, (struct sockaddr*) &sa, sizeof(sa));
>
>  // Create SSL context
>  meth = SSLv23_server_method();

Have you tried SSLv23_client_method()?  Your application is a TLS
client, not a TLS server...

>  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");

--
    Viktor.