Skip to content

Commit

Permalink
feat(ssl): add SSLContext object and wire up with main
Browse files Browse the repository at this point in the history
  • Loading branch information
552020 committed May 23, 2024
1 parent ef792d8 commit 8424b18
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Compiler and Flags
CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98 -I. -Iinclude -Isrc -Isrc/events -g
CXXFLAGS = -Wall -Wextra -Werror -std=c++98 -I. -Iinclude -Isrc -Isrc/events -Isrc/ssl -g
DEPFLAGS = -MMD -MP
LDFLAGS =

UNAME_S := $(shell uname -s)
# Additional Flags for macOS
Expand All @@ -27,6 +28,11 @@ else
USE_LOCAL_OPENSSL := 1
endif

# Paths for local OpenSSL installation
LOCAL_OPENSSL_DIR := $(CURDIR)/local/openssl
LOCAL_INCLUDE := $(LOCAL_OPENSSL_DIR)/include
LOCAL_LIB := $(LOCAL_OPENSSL_DIR)/lib

# Source and Object Files
SRCS = src/main.cpp \
src/Parser.cpp \
Expand All @@ -48,7 +54,9 @@ SRCS = src/main.cpp \
src/ServerSocket.cpp \
src/Listen.cpp \
src/events/EventManager.cpp \
src/events/ServerEventListener.cpp
src/events/ServerEventListener.cpp \
src/ssl/SSLManager.cpp \
src/ssl/SSLContext.cpp
OBJDIR = obj
OBJS = $(SRCS:%.cpp=$(OBJDIR)/%.o)

Expand Down Expand Up @@ -87,7 +95,7 @@ $(OBJDIR)/%.o: %.cpp

# Linking the main target
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)

# Cleaning up the build
clean:
Expand Down
2 changes: 2 additions & 0 deletions include/webserv.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef WEBSERV_H
#define WEBSERV_H

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string>
#include <sstream>
#include <cstdlib>
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "webserv.hpp"
#include "EventManager.hpp"
#include "ServerEventListener.hpp"
#include "SSLManager.hpp"
#include "SSLContext.hpp"

int main(int argc, char **argv)
{
Expand All @@ -29,6 +31,12 @@ int main(int argc, char **argv)

std::cout << &webserv.getEventManager() << std::endl;

// Initialize SSLManager and SSLContext
SSLManager *sslManager = SSLManager::getInstance();
(void)sslManager;
SSLContext sslContext;
(void)sslContext;

webserv.startListening();
webserv.startPollEventLoop();

Expand Down
24 changes: 24 additions & 0 deletions src/ssl/SSLContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "SSLContext.hpp"

SSLContext::SSLContext()
{
ctx = SSL_CTX_new(SSLv23_client_method());
if (!ctx)
{
throw std::runtime_error("Unable to create SSL context");
}
SSL_CTX_set_ecdh_auto(ctx, 1);
}

SSLContext::~SSLContext()
{
if (ctx)
{
SSL_CTX_free(ctx);
}
}

SSL_CTX *SSLContext::getContext()
{
return ctx;
}
20 changes: 20 additions & 0 deletions src/ssl/SSLContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SSL_CONTEXT_HPP
#define SSL_CONTEXT_HPP

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <stdexcept>

class SSLContext
{
public:
SSLContext();
~SSLContext();

SSL_CTX *getContext();

private:
SSL_CTX *ctx;
};

#endif // SSL_CONTEXT_HPP
23 changes: 23 additions & 0 deletions src/ssl/SSLManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "SSLManager.hpp"

SSLManager *SSLManager::getInstance()
{
static SSLManager instance;
return &instance;
}

SSLManager::SSLManager()
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}

SSLManager::~SSLManager()
{
ERR_free_strings();
EVP_cleanup();
// Additional cleanup can be added here
// CRYPTO_cleanup_all_ex_data();
// SSL_COMP_free_compression_methods();
}
19 changes: 19 additions & 0 deletions src/ssl/SSLManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SSL_MANAGER_HPP
#define SSL_MANAGER_HPP

#include <openssl/ssl.h>
#include <openssl/err.h>

class SSLManager
{
public:
static SSLManager *getInstance();

private:
SSLManager();
~SSLManager();
SSLManager(const SSLManager &other);
SSLManager &operator=(const SSLManager &other);
};

#endif // SSL_MANAGER_HPP

0 comments on commit 8424b18

Please sign in to comment.