🚧 Add SSL stuff to server
This commit is contained in:
parent
f42008af5a
commit
2089be370e
1 changed files with 39 additions and 1 deletions
|
@ -2,14 +2,41 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <ostream>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using std::cout, std::endl, std::string;
|
||||
|
||||
/*
|
||||
* Remember to build with the flags "-L/usr/lib -lssl -lcrypto"
|
||||
*/
|
||||
|
||||
int main() {
|
||||
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
|
||||
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
|
||||
if (!ctx) {
|
||||
// TODO Handle error
|
||||
}
|
||||
|
||||
// Load certificate
|
||||
if (SSL_CTX_use_certificate_file(ctx,
|
||||
"~/git/mogens_og_karen/ssl/server.crt",
|
||||
SSL_FILETYPE_PEM) <= 0) {
|
||||
// TODO Handle error
|
||||
}
|
||||
|
||||
// Load certificate private key
|
||||
if (SSL_CTX_use_PrivateKey_file(ctx, "~/git/mogens_og_karen/ssl/server.key",
|
||||
SSL_FILETYPE_PEM) <= 0) {
|
||||
// TODO Handle error
|
||||
}
|
||||
|
||||
cout << "Initializing server" << endl;
|
||||
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverSocket == -1) {
|
||||
|
@ -37,9 +64,17 @@ int main() {
|
|||
// TODO error
|
||||
}
|
||||
|
||||
SSL *ssl = SSL_new(ctx);
|
||||
SSL_set_fd(ssl, clientSocket);
|
||||
|
||||
// Perform the SSL negotiation
|
||||
if (SSL_accept(ssl)) {
|
||||
// TODO Handle error
|
||||
}
|
||||
|
||||
while (true) {
|
||||
char buffer[1024] = {0};
|
||||
recv(clientSocket, buffer, sizeof(buffer), 0);
|
||||
SSL_read(ssl, buffer, 0);
|
||||
|
||||
if (strlen(buffer) <= 0) {
|
||||
cout << "Client seems to have just straight up left :(" << endl;
|
||||
|
@ -49,6 +84,9 @@ int main() {
|
|||
cout << buffer << endl;
|
||||
}
|
||||
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ctx);
|
||||
ERR_free_strings();
|
||||
close(serverSocket);
|
||||
|
||||
return 0;
|
||||
|
|
Reference in a new issue