From b1bce6e6d5ac6bde933bc50d5df141b1e6239ed7 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 17 Jan 2024 01:35:25 +0000 Subject: [PATCH] ssl git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@5 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/fetch.c | 2 +- Library/Core.c | 8 +++++++- Library/DNS.c | 33 ++++++++++++++++++++++++++++++++- Library/W3DNS.h | 8 +++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Example/fetch.c b/Example/fetch.c index afd1134..c95bdf8 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -15,5 +15,5 @@ int main(int argc, char** argv){ return 1; } W3_Library_Init(); - struct W3* w3 = W3_Create(false, argv[1], 80); + struct W3* w3 = W3_Create(true, argv[1], 443); } diff --git a/Library/Core.c b/Library/Core.c index 2710c63..3d6d6e7 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -31,6 +31,12 @@ int W3_Library_Init(void){ struct W3* W3_Create(bool ssl, const char* hostname, int port){ __W3_Debug("Create", "Creating a struct"); struct W3* w3 = malloc(sizeof(*w3)); - w3->sock = __W3_DNS_Connect(hostname, ssl, port); + w3->sock = __W3_DNS_Connect(hostname, ssl, port +#ifdef SSL_SUPPORT + , + &w3->ssl, + &w3->ssl_ctx +#endif + ); return w3; } diff --git a/Library/DNS.c b/Library/DNS.c index ef48222..e4d73fc 100644 --- a/Library/DNS.c +++ b/Library/DNS.c @@ -12,7 +12,17 @@ #include #include -int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port){ +#ifdef SSL_SUPPORT +#include +#endif + +int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port +#ifdef SSL_SUPPORT + , + void** o_ssl, + void** o_ctx +#endif +){ __W3_Debug("DNS-Connect", "Resolving"); struct addrinfo hints; struct addrinfo* result; @@ -46,4 +56,25 @@ int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port){ return -1; /* Failed to connect */ } __W3_Debug("Connect", "Conencted"); +#ifdef SSL_SUPPORT + if(ssl){ + __W3_Debug("SSL", "Initializing"); + const SSL_METHOD* method = TLSv1_2_client_method(); + *o_ctx = SSL_CTX_new(method); + *o_ssl = SSL_new(*o_ctx); + SSL_set_fd(*o_ssl, sock); + if(SSL_connect(*o_ssl) != 1){ + SSL_CTX_free(*o_ctx); + SSL_free(*o_ssl); + *o_ctx = NULL; + *o_ssl = NULL; + close(sock); + sock = -1; + + }else{ + __W3_Debug("SSL", "Connected"); + } + } +#endif + return sock; } diff --git a/Library/W3DNS.h b/Library/W3DNS.h index ee09e96..c34852a 100644 --- a/Library/W3DNS.h +++ b/Library/W3DNS.h @@ -5,6 +5,12 @@ #include #include -int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port); +int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port +#ifdef SSL_SUPPORT + , + void** o_ssl, + void** o_ctx +#endif +); #endif -- 2.43.0