Compare commits
No commits in common. "803154306243573493694f4d8ce89397373ce808" and "f42008af5a265cf76bb8622736ea56a87a22a0ca" have entirely different histories.
8031543062
...
f42008af5a
2 changed files with 4 additions and 78 deletions
|
@ -1,18 +1,13 @@
|
||||||
#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/ssl.h>
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
using std::cout, std::cin, std::endl, std::string;
|
using std::cout, std::cin, std::endl, std::string;
|
||||||
|
|
||||||
/*
|
|
||||||
* Remember to build with the flags "-L/usr/lib -lssl -lcrypto"
|
|
||||||
*/
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
struct Message {
|
struct Message {
|
||||||
|
@ -24,30 +19,7 @@ int main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize OpenSSL
|
|
||||||
SSL_library_init();
|
|
||||||
SSL_load_error_strings();
|
|
||||||
|
|
||||||
// Create new SSL context
|
|
||||||
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
|
|
||||||
if (!ctx) {
|
|
||||||
// TODO Handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the server's certificate into context
|
|
||||||
if (SSL_CTX_load_verify_locations(
|
|
||||||
ctx, "~/git/mogens_og_karen/ssl/server.crt", nullptr) <= 0) {
|
|
||||||
// TODO Handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create SSL object
|
|
||||||
SSL *ssl = SSL_new(ctx);
|
|
||||||
if (!ssl) {
|
|
||||||
// TODO Handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
SSL_set_fd(ssl, clientSocket);
|
|
||||||
|
|
||||||
sockaddr_in serverAdress;
|
sockaddr_in serverAdress;
|
||||||
serverAdress.sin_family = AF_INET;
|
serverAdress.sin_family = AF_INET;
|
||||||
|
@ -63,11 +35,6 @@ int main() {
|
||||||
cout << "CONNECTED!" << endl;
|
cout << "CONNECTED!" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform SSL handshake
|
|
||||||
if (SSL_connect(ssl) != 1) {
|
|
||||||
// TODO Handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Message message;
|
Message message;
|
||||||
cout << "Type message to server: ";
|
cout << "Type message to server: ";
|
||||||
|
@ -81,13 +48,10 @@ int main() {
|
||||||
cout << endl;
|
cout << endl;
|
||||||
message.username = "Client 1";
|
message.username = "Client 1";
|
||||||
|
|
||||||
SSL_write(ssl, message.toString().data(), 0);
|
send(clientSocket, message.toString().data(),
|
||||||
|
strlen(message.toString().data()), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
|
||||||
SSL_free(ssl);
|
|
||||||
SSL_CTX_free(ctx);
|
|
||||||
ERR_free_strings();
|
|
||||||
close(clientSocket);
|
close(clientSocket);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -2,41 +2,14 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <openssl/err.h>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
using std::cout, std::endl, std::string;
|
using std::cout, std::endl, std::string;
|
||||||
|
|
||||||
/*
|
|
||||||
* Remember to build with the flags "-L/usr/lib -lssl -lcrypto"
|
|
||||||
*/
|
|
||||||
|
|
||||||
int main() {
|
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;
|
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) {
|
||||||
|
@ -64,17 +37,9 @@ int main() {
|
||||||
// TODO error
|
// 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) {
|
while (true) {
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
SSL_read(ssl, buffer, 0);
|
recv(clientSocket, buffer, sizeof(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;
|
||||||
|
@ -84,9 +49,6 @@ int main() {
|
||||||
cout << buffer << endl;
|
cout << buffer << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
SSL_free(ssl);
|
|
||||||
SSL_CTX_free(ctx);
|
|
||||||
ERR_free_strings();
|
|
||||||
close(serverSocket);
|
close(serverSocket);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Reference in a new issue