🚧 Add SSL stuff to client
This commit is contained in:
parent
2089be370e
commit
8031543062
1 changed files with 39 additions and 3 deletions
|
@ -1,13 +1,18 @@
|
|||
#include <arpa/inet.h>
|
||||
#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::cin, std::endl, std::string;
|
||||
|
||||
/*
|
||||
* Remember to build with the flags "-L/usr/lib -lssl -lcrypto"
|
||||
*/
|
||||
|
||||
int main() {
|
||||
|
||||
struct Message {
|
||||
|
@ -19,7 +24,30 @@ 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);
|
||||
SSL_set_fd(ssl, clientSocket);
|
||||
|
||||
sockaddr_in serverAdress;
|
||||
serverAdress.sin_family = AF_INET;
|
||||
|
@ -35,6 +63,11 @@ int main() {
|
|||
cout << "CONNECTED!" << endl;
|
||||
}
|
||||
|
||||
// Perform SSL handshake
|
||||
if (SSL_connect(ssl) != 1) {
|
||||
// TODO Handle error
|
||||
}
|
||||
|
||||
while (true) {
|
||||
Message message;
|
||||
cout << "Type message to server: ";
|
||||
|
@ -48,10 +81,13 @@ int main() {
|
|||
cout << endl;
|
||||
message.username = "Client 1";
|
||||
|
||||
send(clientSocket, message.toString().data(),
|
||||
strlen(message.toString().data()), 0);
|
||||
SSL_write(ssl, message.toString().data(), 0);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ctx);
|
||||
ERR_free_strings();
|
||||
close(clientSocket);
|
||||
|
||||
return 0;
|
||||
|
|
Reference in a new issue