sslcon ported to both windows and linux

This commit is contained in:
2022-01-04 12:42:48 -05:00
parent 18bccc0c25
commit 6b23651606
3 changed files with 48 additions and 14 deletions

View File

@@ -4,6 +4,10 @@
*.jpg binary
*.gif binary
*.png binary
*.a binary
*.lib binary
*.o binary
*.obj binary
*.bat text eol=crlf
*.a filter=lfs diff=lfs merge=lfs -text
*.lib filter=lfs diff=lfs merge=lfs -text

View File

@@ -0,0 +1 @@
gcc -o sslcon sslcon.c -lssl -lcrypto

View File

@@ -6,14 +6,16 @@
#include <openssl/err.h>
#include <string.h>
#ifdef WIN64
#ifdef _WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#include <synchapi.h>
#include <sysinfoapi.h>
#include <wincrypt.h>
#include <cryptuiapi.h>
#else
#endif
#ifdef __linux__
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
@@ -25,7 +27,7 @@
typedef int SOCKET;
#endif
#ifdef _WIN32
void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket
int status = ioctlsocket(sock, FIONBIO, &mode);
@@ -33,7 +35,6 @@ void set_nonblocking(SOCKET sock) {
}
void load_root_certs(SSL_CTX *ctx) {
// SSL_CTX_set_default_verify_paths(ssl_ctx);
HCERTSTORE hStore = CertOpenSystemStoreW(0, L"ROOT");
PCCERT_CONTEXT pContext = NULL;
@@ -60,18 +61,51 @@ void load_root_certs(SSL_CTX *ctx) {
CertCloseStore(hStore, 0);
}
void init_libraries() {
WSADATA whocares;
assert(WSAStartup(MAKEWORD(2,2), &whocares) == 0);
}
void print_error_and_exit() {
int err = WSAGetLastError();
fprintf(stderr, "err #%d\n", err);
exit(1);
}
#endif
#ifdef __linux__
void set_nonblocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
assert(flags != -1);
int status = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
assert(status != -1);
}
void load_root_certs(SSL_CTX *ssl_ctx) {
SSL_CTX_set_default_verify_paths(ssl_ctx);
}
void init_libraries() {
}
void print_error_and_exit() {
fprintf(stderr, "error=%d\n", errno);
exit(1);
}
#endif
void main(int argc, char **argv) {
/* OPENSSL_init_ssl(0, NULL); */
init_libraries();
SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method());
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
load_root_certs(ssl_ctx);
WSADATA whocares;
assert(WSAStartup(MAKEWORD(2,2), &whocares) == 0);
SOCKET sock_fd = socket(AF_INET, SOCK_STREAM, 0);
/* this is mit.edu */
@@ -87,12 +121,7 @@ void main(int argc, char **argv) {
addr.sin_addr.s_addr = htonl(ip);
int status = connect(sock_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (status != 0) {
int err = WSAGetLastError();
fprintf(stderr, "err #%d\n", err);
exit(1);
}
assert(status == 0);
if (status != 0) print_error_and_exit();
fprintf(stderr, "Connect successful.\n");
set_nonblocking(sock_fd);