]> Nishi Git Mirror - libw3.git/commitdiff
ssl
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 17 Jan 2024 01:35:25 +0000 (01:35 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 17 Jan 2024 01:35:25 +0000 (01:35 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@5 d27a3e52-49c5-7645-884c-6793ebffc270

Example/fetch.c
Library/Core.c
Library/DNS.c
Library/W3DNS.h

index afd1134f8e48178d117b5649439b54fd4b3c86ce..c95bdf8aecefd4d94a467240be8e2896f194ccac 100644 (file)
@@ -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);
 }
index 2710c63bb939ca1c7c03806b9da0bc635b0fc78b..3d6d6e7b6c881b09ec50b6fdfe351e544df9bc56 100644 (file)
@@ -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;
 }
index ef48222853522b00a0bb4fe36392501ab3b4840d..e4d73fca244d6ee06dd8af7da19efd3bbb35416f 100644 (file)
 #include <stdbool.h>
 #include <unistd.h>
 
-int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port){
+#ifdef SSL_SUPPORT
+#include <openssl/ssl.h>
+#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;
 }
index ee09e9662c88e570cc1e55d1bd65fc1fb1bda070..c34852a48e8f331c2200bf34807dd24b0f381ac4 100644 (file)
@@ -5,6 +5,12 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-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