#include "openssl\applink.c"
#include "openssl\bio.h"
#include "openssl\ssl.h"
#include "openssl\err.h"
#include"main.h"
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
main()
{
WSADATA wsa;
SOCKET sock;
struct sockaddr_in server_addr;
char Send_BUFFER[2000]="",Recv_BUFFER[2000]="";
FILE *fp;
X509 *cert;
EVP_PKEY *pkey;
SSL_METHOD *meth;
SSL_CTX *ctx=NULL;
SSL *ssl;
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
if(WSAStartup(MAKEWORD(2,2),&wsa)!= 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
if((sock =socket(AF_INET,SOCK_STREAM,0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(host);
if(connect(sock,(struct sockaddr*)&server_addr,sizeof(server_addr))==SOCKET_ERROR)
{
printf("Connect error");
}
ssl = SSL_new(ctx);
SSL_set_fd(ssl, sock);
fp=fopen(CLIENT_CERT, "r");
if(!fp)
{
fprintf(stderr, "unable to open: %s\n", CLIENT_CERT);
return EXIT_FAILURE;
}
cert = PEM_read_X509(fp, NULL, NULL, NULL);
if(!cert)
{
fprintf(stderr, "unable to parse certificate in: %s\n", CLIENT_CERT);
fclose(fp);
return EXIT_FAILURE;
}
SSL_use_certificate(ssl, cert);
pkey = X509_get_pubkey(cert);
SSL_CTX_use_PrivateKey(ctx, pkey);
if( !SSL_CTX_check_private_key( ctx ) )
{
ERR_print_errors_fp(stderr);
exit(1);
}
if(SSL_connect(ssl)<=0)
{
puts("SSL Connect error");
}
SSL_write(ssl, Send_BUFFER,sizeof(Send_BUFFER));
SSL_get_error(ssl, status);
SSL_read(ssl, Recv_BUFFER,2000);
puts(Recv_BUFFER);
closesocket(sock);
}