Compare commits
No commits in common. "cba0ae1da350208cce9daa825aa88f79aa98ad57" and "803154306243573493694f4d8ce89397373ce808" have entirely different histories.
cba0ae1da3
...
8031543062
2 changed files with 20 additions and 30 deletions
|
@ -31,19 +31,19 @@ int main() {
|
||||||
// Create new SSL context
|
// Create new SSL context
|
||||||
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
|
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
cout << "Creation of SSL context failed" << endl;
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the server's certificate into context
|
// Load the server's certificate into context
|
||||||
if (SSL_CTX_load_verify_locations(
|
if (SSL_CTX_load_verify_locations(
|
||||||
ctx, "./ssl/server.crt", nullptr) <= 0) {
|
ctx, "~/git/mogens_og_karen/ssl/server.crt", nullptr) <= 0) {
|
||||||
cout << "SSL load failed" << endl;
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create SSL object
|
// Create SSL object
|
||||||
SSL *ssl = SSL_new(ctx);
|
SSL *ssl = SSL_new(ctx);
|
||||||
if (!ssl) {
|
if (!ssl) {
|
||||||
cout << "Failed to create SSL object" << endl;
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -65,7 +65,7 @@ int main() {
|
||||||
|
|
||||||
// Perform SSL handshake
|
// Perform SSL handshake
|
||||||
if (SSL_connect(ssl) != 1) {
|
if (SSL_connect(ssl) != 1) {
|
||||||
cout << "SSL Handshake error" << endl;
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -81,7 +81,7 @@ int main() {
|
||||||
cout << endl;
|
cout << endl;
|
||||||
message.username = "Client 1";
|
message.username = "Client 1";
|
||||||
|
|
||||||
SSL_write(ssl, message.toString().data(), strlen(message.toString().data()));
|
SSL_write(ssl, message.toString().data(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
|
@ -20,31 +21,26 @@ int main() {
|
||||||
|
|
||||||
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
|
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
cout << "Creation of SSL context failed" << endl;
|
// TODO Handle error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load certificate
|
// Load certificate
|
||||||
if (SSL_CTX_use_certificate_file(ctx, "./ssl/server.crt",
|
if (SSL_CTX_use_certificate_file(ctx,
|
||||||
|
"~/git/mogens_og_karen/ssl/server.crt",
|
||||||
SSL_FILETYPE_PEM) <= 0) {
|
SSL_FILETYPE_PEM) <= 0) {
|
||||||
unsigned long errCode = ERR_get_error();
|
// TODO Handle error
|
||||||
char errBuffer[128];
|
|
||||||
ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer));
|
|
||||||
cout << "Certificate load failed: " << errBuffer << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load certificate private key
|
// Load certificate private key
|
||||||
if (SSL_CTX_use_PrivateKey_file(ctx, "./ssl/server.key",
|
if (SSL_CTX_use_PrivateKey_file(ctx, "~/git/mogens_og_karen/ssl/server.key",
|
||||||
SSL_FILETYPE_PEM) <= 0) {
|
SSL_FILETYPE_PEM) <= 0) {
|
||||||
unsigned long errCode = ERR_get_error();
|
// TODO Handle error
|
||||||
char errBuffer[128];
|
|
||||||
ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer));
|
|
||||||
cout << "Private key load failed: " << errBuffer << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Initializing server" << endl;
|
cout << "Initializing server" << endl;
|
||||||
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (serverSocket == -1) {
|
if (serverSocket == -1) {
|
||||||
cout << "Server socket failed" << endl;
|
// TODO error
|
||||||
}
|
}
|
||||||
|
|
||||||
sockaddr_in serverAddress;
|
sockaddr_in serverAddress;
|
||||||
|
@ -55,36 +51,30 @@ int main() {
|
||||||
int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress,
|
int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress,
|
||||||
sizeof(serverAddress));
|
sizeof(serverAddress));
|
||||||
if (bindStatus == -1) {
|
if (bindStatus == -1) {
|
||||||
cout << "Bind failed" << endl;
|
// TODO error
|
||||||
}
|
}
|
||||||
|
|
||||||
int listenStatus = listen(serverSocket, 5);
|
int listenStatus = listen(serverSocket, 5);
|
||||||
if (listenStatus == -1) {
|
if (listenStatus == -1) {
|
||||||
cout << "Listen failed" << endl;
|
// TODO error
|
||||||
}
|
}
|
||||||
|
|
||||||
int clientSocket = accept(serverSocket, nullptr, nullptr);
|
int clientSocket = accept(serverSocket, nullptr, nullptr);
|
||||||
if (clientSocket == -1) {
|
if (clientSocket == -1) {
|
||||||
cout << "Client socket failed" << endl;
|
// TODO error
|
||||||
}
|
}
|
||||||
|
|
||||||
SSL *ssl = SSL_new(ctx);
|
SSL *ssl = SSL_new(ctx);
|
||||||
SSL_set_fd(ssl, clientSocket);
|
SSL_set_fd(ssl, clientSocket);
|
||||||
|
|
||||||
// Perform the SSL negotiation
|
// Perform the SSL negotiation
|
||||||
int sslAcceptCode = SSL_accept(ssl);
|
if (SSL_accept(ssl)) {
|
||||||
if (sslAcceptCode <= 0) {
|
// TODO Handle error
|
||||||
int sslError = SSL_get_error(ssl, sslAcceptCode);
|
|
||||||
char *errStr = ERR_error_string(ERR_get_error(), nullptr);
|
|
||||||
cout << "SSL_accept failed with SSL error: " << sslError << endl;
|
|
||||||
cout << "OpenSSL error: " << errStr << endl;
|
|
||||||
} else {
|
|
||||||
cout << "negotiated SSL" << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
SSL_read(ssl, buffer, sizeof(buffer) - 1);
|
SSL_read(ssl, buffer, 0);
|
||||||
|
|
||||||
if (strlen(buffer) <= 0) {
|
if (strlen(buffer) <= 0) {
|
||||||
cout << "Client seems to have just straight up left :(" << endl;
|
cout << "Client seems to have just straight up left :(" << endl;
|
||||||
|
|
Reference in a new issue