sslcon ported to both windows and linux
This commit is contained in:
4
luprex/.gitattributes
vendored
4
luprex/.gitattributes
vendored
@@ -4,6 +4,10 @@
|
|||||||
*.jpg binary
|
*.jpg binary
|
||||||
*.gif binary
|
*.gif binary
|
||||||
*.png binary
|
*.png binary
|
||||||
|
*.a binary
|
||||||
|
*.lib binary
|
||||||
|
*.o binary
|
||||||
|
*.obj binary
|
||||||
*.bat text eol=crlf
|
*.bat text eol=crlf
|
||||||
*.a filter=lfs diff=lfs merge=lfs -text
|
*.a filter=lfs diff=lfs merge=lfs -text
|
||||||
*.lib filter=lfs diff=lfs merge=lfs -text
|
*.lib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
|||||||
1
luprex/experiments/build-sslcon.sh
Executable file
1
luprex/experiments/build-sslcon.sh
Executable file
@@ -0,0 +1 @@
|
|||||||
|
gcc -o sslcon sslcon.c -lssl -lcrypto
|
||||||
@@ -6,14 +6,16 @@
|
|||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef WIN64
|
#ifdef _WIN32
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <synchapi.h>
|
#include <synchapi.h>
|
||||||
#include <sysinfoapi.h>
|
#include <sysinfoapi.h>
|
||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#include <cryptuiapi.h>
|
#include <cryptuiapi.h>
|
||||||
#else
|
#endif
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -25,7 +27,7 @@
|
|||||||
typedef int SOCKET;
|
typedef int SOCKET;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
void set_nonblocking(SOCKET sock) {
|
void set_nonblocking(SOCKET sock) {
|
||||||
u_long mode = 1; // 1 to enable non-blocking socket
|
u_long mode = 1; // 1 to enable non-blocking socket
|
||||||
int status = ioctlsocket(sock, FIONBIO, &mode);
|
int status = ioctlsocket(sock, FIONBIO, &mode);
|
||||||
@@ -33,7 +35,6 @@ void set_nonblocking(SOCKET sock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void load_root_certs(SSL_CTX *ctx) {
|
void load_root_certs(SSL_CTX *ctx) {
|
||||||
// SSL_CTX_set_default_verify_paths(ssl_ctx);
|
|
||||||
|
|
||||||
HCERTSTORE hStore = CertOpenSystemStoreW(0, L"ROOT");
|
HCERTSTORE hStore = CertOpenSystemStoreW(0, L"ROOT");
|
||||||
PCCERT_CONTEXT pContext = NULL;
|
PCCERT_CONTEXT pContext = NULL;
|
||||||
@@ -60,18 +61,51 @@ void load_root_certs(SSL_CTX *ctx) {
|
|||||||
CertCloseStore(hStore, 0);
|
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) {
|
void main(int argc, char **argv) {
|
||||||
|
|
||||||
/* OPENSSL_init_ssl(0, NULL); */
|
init_libraries();
|
||||||
|
|
||||||
SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method());
|
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_ACCEPT_MOVING_WRITE_BUFFER);
|
||||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
|
||||||
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
|
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
|
||||||
load_root_certs(ssl_ctx);
|
load_root_certs(ssl_ctx);
|
||||||
|
|
||||||
WSADATA whocares;
|
|
||||||
assert(WSAStartup(MAKEWORD(2,2), &whocares) == 0);
|
|
||||||
SOCKET sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
SOCKET sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
/* this is mit.edu */
|
/* this is mit.edu */
|
||||||
@@ -87,12 +121,7 @@ void main(int argc, char **argv) {
|
|||||||
addr.sin_addr.s_addr = htonl(ip);
|
addr.sin_addr.s_addr = htonl(ip);
|
||||||
|
|
||||||
int status = connect(sock_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
|
int status = connect(sock_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
|
||||||
if (status != 0) {
|
if (status != 0) print_error_and_exit();
|
||||||
int err = WSAGetLastError();
|
|
||||||
fprintf(stderr, "err #%d\n", err);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
assert(status == 0);
|
|
||||||
fprintf(stderr, "Connect successful.\n");
|
fprintf(stderr, "Connect successful.\n");
|
||||||
|
|
||||||
set_nonblocking(sock_fd);
|
set_nonblocking(sock_fd);
|
||||||
|
|||||||
Reference in New Issue
Block a user