I am looking to download or read a file from a server over HTTPS, and I'm using openSSL for it.
I see the connection succeeds, however SSL_read()
returns 0. SSL_get_error()
returns 6 which refers to SSL_ERROR_ZERO_RETURN
macro and seems to be a normal behavior but I'm not sure why was the connection shutdown while something was being read? And this might be why it's reading 0 bytes?
#define CHECK_NULL(x) if ((x)==NULL) exit (1)
#define CHECK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
#define CHECK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
void ServerConnectAndRcv(uint8_t *str)
{
int err;
int sd;
struct sockaddr_in sa;
SSL_CTX* ctx;
SSL* ssl;
char* str;
char buf [4096];
const SSL_METHOD *meth;
SSLeay_add_ssl_algorithms();
meth = TLSv1_2_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new (meth);
SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
// create a socket and connect to the server
sd = socket (AF_INET, SOCK_STREAM, 0);
CHK_ERR(sd, "socket");
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr (<SERVER-IP>); /* Server IP */
sa.sin_port = htons (<SERVER-PORT>); /* Server Port number */
ssl = SSL_new (ctx);
CHECK_NULL(ssl);
SSL_set_fd (ssl, sd);
err = SSL_connect (ssl);
CHECK_SSL(err);
// read from the server
err = SSL_read (ssl, buf, sizeof(buf) - 1);
CHECK_SSL(err);
if (err <= 0)
{
printf ("Error reading: %d\n", SSL_get_error(ssl, err));
}
buf[err] = '\0';
printf ("Received % bytes\n", err); // Received 0 bytes
SSL_shutdown (ssl);
}