So what I want is simple -I love openSSL api. I found some simple code to begin with for learning it. I am quite new to server creation stuff. I wonder – how to make OpenSSL work with simple http instead of https? I mean I want to provide same service, be capable to jump into https when I need to but have no protection http vercion of it.
I mean It is so grate just to say
SSLServer server("cert", "pkey", 1420);
// Set the thread function.
server.SetPthread_F(conn_thread);
I wish I could do same for not protected http service creation.
After some grate answers I understood I shall edit main question:
How to keep/use only non-blocking TCP server part of OpenSSL library? Main goal would be a crossplatform small and simple in use TCP server on top of which it would be eazy to implement http and http costumized analogs
So If we look onto example:
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "sslserver.h"
#define REPLY "<html><body>Metalshell.com OpenSSL Server</body></html>"
#define MAX_PACKET_SIZE 1024
// Called when a new connection is made.
void *conn_thread(void *ssl) {
int fd = SSL_get_fd((SSL *)ssl);
if(SSL_accept((SSL *)ssl) == -1) {
ERR_print_errors_fp(stderr);
} else {
char cipdesc[128];
SSL_CIPHER *sslciph = SSL_get_current_cipher((SSL *)ssl);
cout << "Encryption Description:\n";
cout << SSL_CIPHER_description(sslciph, cipdesc, sizeof(cipdesc)) << endl;
char buff[MAX_PACKET_SIZE];
// Wait for data to be sent.
int bytes = SSL_read((SSL *)ssl, buff, sizeof(buff));
buff[bytes] = '\0';
// Show the browser request.
cout << "Recieved: \n" << buff << endl;
// Send the html reply.
SSL_write((SSL *)ssl, REPLY, strlen(REPLY));
}
// Tell the client we are closing the connection.
SSL_shutdown((SSL *)ssl);
// We do not wait for a reply, just clear everything.
SSL_free((SSL *)ssl);
close(fd);
cout << "Connection Closed\n";
cout << "---------------------------------------------\n";
pthread_exit(NULL);
}
int main() {
SSLServer server("cert", "pkey", 1420);
// Set the thread function.
server.SetPthread_F(conn_thread);
while(1) {
/* Wait for 10 seconds, and if no one trys
* to connect return back. This allows us to do
* other things while waiting.
*/
server.CheckClients(10);
}
return 0;
}
What shall be changed to our server accept all connections not only ssl ones (cout full request if possible) and send them REPLYs?
HTTPS is simple HTTP with SSL (the implementations of which is the point of OpenSSL). The S in HTTPS stands for secure.
Don’t use the OpenSSL API when you don’t want SSL.