Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Forked from rashchupkinr/SSLClient.cpp
Last active July 2, 2023 05:14
Show Gist options
  • Save AhnMo/d288652b13cec77bf89b39186d07bf28 to your computer and use it in GitHub Desktop.
Save AhnMo/d288652b13cec77bf89b39186d07bf28 to your computer and use it in GitHub Desktop.
SSLClient for Windows
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
// include order is important.
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "crypt32")
#pragma comment (lib, "libcryptoMT.lib")
#pragma comment (lib, "libsslMT.lib")
// https://www.npcglib.org/~stathis/blog/precompiled-openssl/ this link might be helpful
SSL *ssl;
int sock;
int RecvPacket()
{
int len=100;
char buf[1000000];
do {
len=SSL_read(ssl, buf, 100);
buf[len]=0;
printf(buf);
} while (len > 0);
if (len < 0) {
int err = SSL_get_error(ssl, len);
if (err == SSL_ERROR_WANT_READ)
return 0;
if (err == SSL_ERROR_WANT_WRITE)
return 0;
if (err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL)
return -1;
}
}
int SendPacket(const char *buf)
{
int len = SSL_write(ssl, buf, strlen(buf));
if (len < 0) {
int err = SSL_get_error(ssl, len);
switch (err) {
case SSL_ERROR_WANT_WRITE:
return 0;
case SSL_ERROR_WANT_READ:
return 0;
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
case SSL_ERROR_SSL:
default:
return -1;
}
}
}
void log_ssl()
{
int err;
while (err = ERR_get_error()) {
char *str = ERR_error_string(err, 0);
if (!str)
return;
printf(str);
printf("\n");
fflush(stdout);
}
}
int main(int argc, char *argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("error\n");
return 0;
}
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (!s) {
printf("Error creating socket.\n");
return -1;
}
struct sockaddr_in sa;
memset (&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("172.217.27.132"); // address of www.google.com
sa.sin_port = htons (443);
socklen_t socklen = sizeof(sa);
if (connect(s, (struct sockaddr *)&sa, socklen)) {
printf("Error connecting to server.\n");
return -1;
}
SSL_library_init();
SSLeay_add_ssl_algorithms();
SSL_load_error_strings();
const SSL_METHOD *meth = TLSv1_2_client_method();
SSL_CTX *ctx = SSL_CTX_new (meth);
ssl = SSL_new (ctx);
if (!ssl) {
printf("Error creating SSL.\n");
log_ssl();
return -1;
}
sock = SSL_get_fd(ssl);
SSL_set_fd(ssl, s);
int err = SSL_connect(ssl);
if (err <= 0) {
printf("Error creating SSL connection. err=%x\n", err);
log_ssl();
fflush(stdout);
return -1;
}
printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
char *request = "GET / HTTP/1.0\r\n\r\n";
SendPacket(request);
RecvPacket();
WSACleanup();
return 0;
}
@RealTrisT
Copy link

RealTrisT commented Jan 14, 2020

good shit 👌
tho you can use TLS_client_method instead of TLSv1_2_client_method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment