Archived
1
0
Fork 0

🚧 Print errors

This commit is contained in:
Simon V. Lejel 2024-02-11 22:37:23 +01:00
parent 23093b1505
commit cba0ae1da3
Signed by: sl
GPG key ID: 6544A0430A2CFFAD
2 changed files with 24 additions and 12 deletions

View file

@ -31,7 +31,7 @@ int main() {
// Create new SSL context
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) {
// TODO Handle error
cout << "Creation of SSL context failed" << endl;
}
// Load the server's certificate into context
@ -43,7 +43,7 @@ int main() {
// Create SSL object
SSL *ssl = SSL_new(ctx);
if (!ssl) {
// TODO Handle error
cout << "Failed to create SSL object" << endl;
}
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
@ -65,7 +65,7 @@ int main() {
// Perform SSL handshake
if (SSL_connect(ssl) != 1) {
// TODO Handle error
cout << "SSL Handshake error" << endl;
}
while (true) {

View file

@ -20,25 +20,31 @@ int main() {
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
if (!ctx) {
// TODO Handle error
cout << "Creation of SSL context failed" << endl;
}
// Load certificate
if (SSL_CTX_use_certificate_file(ctx, "./ssl/server.crt",
SSL_FILETYPE_PEM) <= 0) {
// TODO Handle error
unsigned long errCode = ERR_get_error();
char errBuffer[128];
ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer));
cout << "Certificate load failed: " << errBuffer << endl;
}
// Load certificate private key
if (SSL_CTX_use_PrivateKey_file(ctx, "./ssl/server.key",
SSL_FILETYPE_PEM) <= 0) {
// TODO Handle error
unsigned long errCode = ERR_get_error();
char errBuffer[128];
ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer));
cout << "Private key load failed: " << errBuffer << endl;
}
cout << "Initializing server" << endl;
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
// TODO error
cout << "Server socket failed" << endl;
}
sockaddr_in serverAddress;
@ -49,25 +55,31 @@ int main() {
int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress,
sizeof(serverAddress));
if (bindStatus == -1) {
// TODO error
cout << "Bind failed" << endl;
}
int listenStatus = listen(serverSocket, 5);
if (listenStatus == -1) {
// TODO error
cout << "Listen failed" << endl;
}
int clientSocket = accept(serverSocket, nullptr, nullptr);
if (clientSocket == -1) {
// TODO error
cout << "Client socket failed" << endl;
}
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, clientSocket);
// Perform the SSL negotiation
if (SSL_accept(ssl)) {
// TODO Handle error
int sslAcceptCode = SSL_accept(ssl);
if (sslAcceptCode <= 0) {
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) {