WOLFSSL *ssl_cmd=NULL, *ssl_data=NULL;
WOLFSSL_CTX* ctx=NULL, *ctx1=NULL;
int sock1, sock3;
void exitApp(WOLFSSL_CTX* ctx)
{
if (ctx != NULL)
{
wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup();
}
// BIOS_exit(-1);
}
int start_TLS( WOLFSSL **ssl)
{
int ret, valread;
// WOLFSSL *ssl;
Error_Block eb;
int nbytes;
char *buffer;
char msg[50] = { };
char buff[512];
memset(buff, 0, 512);
// fdOpenSession(TaskSelf());
Error_init(&eb);
wolfSSL_Init();
ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
if (ctx == 0)
{
logg("****Error****: WolfSSL_CTX error", "");
exitApp(ctx);
return -1;
}
// cert
uint8_t *der = NULL;
uint32_t len, ret1;
ret1 = CertConv_pem2der(ca_cert, sizeof_ca_cert, &der, &len);
if (ret1 != 0)
{
logg("***Error***: cert conversion to .der fail", "");
return -1;
}
int status = wolfSSL_CTX_load_verify_buffer(ctx, der, len,
SSL_FILETYPE_ASN1);
if (status != SSL_SUCCESS)
{
logg("tcpHandler: Error loading ca_cert_der_2048\n", "");
exitApp(ctx);
return -1;
}
strcpy(msg, "AUTH TLS\r\n");
ret = send(sock1, msg, strlen(msg), 0);
valread = recv(sock1, buff, sizeof(buff) - 1, 0);
*ssl = wolfSSL_new(ctx);
if (*ssl == NULL)
{
logg("tcpHandler: wolfSSL_new error.\n", "");
exitApp(ctx);
return -1;
}
wolfSSL_set_fd(*ssl, sock1);
ret = wolfSSL_connect(*ssl);
int err;
char err_buffer[80];
err = wolfSSL_get_error(*ssl, 0);
if (ret == SSL_SUCCESS)
{
logg("TLS successful", "");
sock1 = wolfSSL_get_fd(*ssl);
/* Get a buffer to receive incoming packets. Use the default heap. */
buffer = Memory_alloc(NULL, 512, 0, &eb);
if (buffer == NULL)
{
logg("tcpWorker: failed to alloc memory\n", "");
exitApp(ctx);
return -1;
}
strcpy(msg, "PBSZ 0\r\n");
if (wolfSSL_send(*ssl, msg, strlen(msg), 0) != strlen(msg))
{
ret = wolfSSL_get_error(*ssl, 0);
logInt("Write error: %i.\n", "", ret);
}
nbytes = wolfSSL_recv(*ssl, (char *) buffer, 512, 0);
if (nbytes <= 0)
{
logg("***error: Reading failed", "");
return -1;
}
else
logStr("PBSZ 0 is successfull %s", "", buffer);
strcpy(msg, "PROT P\r\n");
if (wolfSSL_write(*ssl, msg, strlen(msg)) != strlen(msg))
{
ret = wolfSSL_get_error(*ssl, 0);
logInt("Write error: %i.\n", "", ret);
return -1;
}
nbytes = wolfSSL_read(*ssl, (char *) buffer, 512);
if (nbytes <= 0)
{
logg("***error: Reading failed", "");
return -1;
}
else
logStr("PROT P is successfull %s", "", buffer);
// ssl_cmd = ssl;
logInt("local ssl for command is %p ", *ssl,"");
logInt("ssl_data is %p ", ssl_cmd,"");
logInt("ctx is %p ", ctx,"");
Memory_free(NULL, buffer, 512);
}
// wolfSSL_free(ssl_cmd);
// fdClose((SOCKET) sockfd);
// flag = false;
else
{
wolfSSL_free(*ssl);
// fdClose((SOCKET) sockfd);
logg("wolfSSL_connect failed.\n", "");
// fdCloseSession(TaskSelf());
close(sock1);
exitApp(ctx);
return -1;
}
return 0;
}
int check = 0;
int createFtpDataSocket(WOLFSSL **ssl, char *ipAddress, int portNr)
{
int sockfd;
struct sockaddr_in servAddr;
int ret, valread;
unsigned char cert[sizeof_ca_cert];
strcpy(cert, ca_cert);
// WOLFSSL *ssl;
Error_Block eb;
bool flag = true;
bool internal_flag = true;
int nbytes;
char *buffer;
char msg[50] = { };
char buff[512];
memset(buff, 0, 512);
// fdOpenSession(TaskSelf());
Error_init(&eb);
wolfSSL_Init();
//WOLFSSL_CTX* ctx = NULL;
ctx1 = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
if (ctx1 == 0)
{
logg("****Error****: WolfSSL_CTX error", "");
exitApp(ctx1);
return -1;
}
//SSL_FILETYPE_PEM
uint8_t *der = NULL;
uint32_t len, ret1;
ret1 = CertConv_pem2der(ca_cert, sizeof_ca_cert, &der, &len);
if (ret1 != 0)
{
logg("***Error***: cert conversion to .der fail", "");
return -1;
}
int status = wolfSSL_CTX_load_verify_buffer(ctx1, der, len,
SSL_FILETYPE_ASN1);
if (status != SSL_SUCCESS)
{
logg("tcpHandler: Error loading ca_cert_der_2048\n", "");
exitApp(ctx1);
return -1;
}
*ssl = wolfSSL_new(ctx1);
if (*ssl == NULL)
{
logg("tcpHandler: wolfSSL_new error.\n", "");
exitApp(ctx1);
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
logInt("***Error***: ftp socket creation failed val is %d", "", sockfd);
}
memset((char *) &servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(portNr);
if (HTTPCli_initSockAddr((struct sockaddr *) &servAddr, ipAddress, 0) < 0)
{
logg("ftp: ***ERROR*** - address not resolved.", "");
sockfd = 0;
return -1;
}
// if(cmd_channel)
ret = connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr));
wolfSSL_set_fd(*ssl, sockfd);
ret = wolfSSL_connect(*ssl);
int err;
char err_buffer[80];
err = wolfSSL_get_error(*ssl, 0);
if (ret == SSL_SUCCESS)
{
logg("TLS successful", "");
sockfd = wolfSSL_get_fd(*ssl);
/* Get a buffer to receive incoming packets. Use the default heap. */
buffer = Memory_alloc(NULL, 512, 0, &eb);
if (buffer == NULL)
{
logg("tcpWorker: failed to alloc memory\n", "");
exitApp(ctx1);
return -1;
}
// ssl_data = ssl;
sock3 = sockfd;
logInt("local *ssl for data is %p ", *ssl,"");
logInt("ssl_data is %p ", ssl_data,"");
logInt("ctx1 is %p ", ctx1,"");
Memory_free(NULL, buffer, 512);
}
else
{
wolfSSL_free(*ssl);
close(sock3);
// fdClose((SOCKET) sockfd);
logg("***Error***: Data connection failed failed.\n", "");
// fdCloseSession(TaskSelf());
exitApp(ctx1);
return -1;
}
return 0;
}
int createFtpSocket(char *ipAddress, int portNr)
{
int sockfd;
struct sockaddr_in servAddr;
int ret, valread;
Error_Block eb;
char buff[512];
memset(buff, 0, 512);
// fdOpenSession(TaskSelf());
Error_init(&eb);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
logInt("***Error***: ftp socket creation failed val is %d", "", sockfd);
}
memset((char *) &servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(portNr);
if (HTTPCli_initSockAddr((struct sockaddr *) &servAddr, ipAddress, 0) < 0)
{
logg("ftp: ***ERROR*** - address not resolved.", "");
sockfd = 0;
return -1;
}
// if(cmd_channel)
ret = connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if (ret < 0)
{
logStrInt("ftp: ***ERROR*** - connect failed ip %s port %d - quitting.",
"", ipAddress, portNr);
logInt(" \nreturn value is %d ", "", ret);
return ret;
}
else
{
//if(cmd_channel)
valread = recv(sockfd, buff, sizeof(buff) - 1, 0);
logStr("received buffer : %s", "", buff);
sock1 = sockfd;
memset(buff, 0, 512);
}
/////////////////////////////////
if (g_ftps)
{
int status = start_TLS(&ssl_cmd);
if (status)
{
logg("***error*** tls failed:", "");
return -1;
}
}
return 0;
}
bool executeFtpCmd(char *ftpCmd, char* result, int size)
{
//logStr("executing ftp cmd %s","",ftpCmd);
bool answer = true;
// strcpy(ftpCmd,ftpCmd1);
int sent = wolfSSL_write(ssl_cmd, ftpCmd, strlen(ftpCmd));
int ret = wolfSSL_get_error(ssl_cmd, 0);
//send(sock, ftpCmd, strlen(ftpCmd), 0);
int valread = wolfSSL_read(ssl_cmd, (char *) result, size);
//recv(sock, result, size, 0);
if (valread > 0)
{
result[valread - 2] = 0;
}
else
{
result[0] = 0;
answer = false;
char cmd[5] = { 0 };
strncat(cmd, ftpCmd, 4);
logStr("***ERROR*** - ftp: cmd %s failed - continuing", "", cmd);
}
return answer;
}
//
bool processFtpData(char* inFtpCmd, char* outBuff, bool push)
{
/ * //////////// some code *//////////////
strcpy(ftpCmd, "PASV\r\n");
if (!executeFtpCmd(ftpCmd, ftpResp, sizeof(ftpResp) - 1))
{
return false;
}
/////////////// some code to extract ip and port no ///////////////////////
int sock2 = createFtpDataSocket(&ssl_data, ftpDataIpAdr, ftpDataPortNr); ////////////////// data connection
///// do some processing related to data connection /////////////////////
wolfSSL_free(ssl_data); ///////// freeing data socket
close(sock3); //////////////closing data socket
exitApp(ctx1);//////////////ctx1 related to data connection
return answer;
}
bool pmoPosFtpProc(char* result)
{
int sock = createFtpSocket(g_ftpHost, 21);
if (sock < 0)
return false;
//////////////////////////// do login and other commands////////////////
processFtpData("LIST /abc.txt\r\n", result, false);////////////////////////////////
ftpCleanup: strcpy(ftpCmd, "QUIT\r\n");
logg(" ******Closing socket******",__FUNCTION__);
executeFtpCmd(ftpCmd, ftpResp, sizeof(ftpResp) - 1);
logg("before freeing cmd pointers:","");
logInt("ssl_cmd is %p", ssl_cmd,"");
logInt("ctx is %p ", ctx,"");
wolfSSL_free(ssl_cmd); ///////////// closing command channel related stuffs
close(sock1);
exitApp(ctx);
return (answer);
}
I am getting the same behavior, different pointer values.
Thanks