diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..309ec40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +log +bin +obj +*.cbp +*.depend +*.layout +test.conf + diff --git a/README b/README new file mode 100644 index 0000000..eb39f65 --- /dev/null +++ b/README @@ -0,0 +1 @@ +example skeleton openssl server, which does nothing but prints the read data to stdout diff --git a/SSLServer.cpp b/SSLServer.cpp new file mode 100644 index 0000000..076919a --- /dev/null +++ b/SSLServer.cpp @@ -0,0 +1,83 @@ +#include "SSLServer.h" + +SSLServer::SSLServer() +{ + SSL_library_init(); + SSL_load_error_strings(); + ERR_load_BIO_strings(); + ERR_load_SSL_strings(); + cert = "/home/roa/programming/examples/ssl_conn/ssl_example/servercert.pem"; + key = "/home/roa/programming/examples/ssl_conn/ssl_example/private.key"; + host = "localhost:9037"; + + ctx = SSL_CTX_new(SSLv3_server_method()); + SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM); + SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM); + abio = BIO_new_accept(host); + if(abio == NULL) + { + abort(); + } +} + +void SSLServer::run() +{ + if(BIO_do_accept(abio) <= 0) + { + abort(); + } + + fd_set fds; + + int afd = BIO_get_fd(abio,NULL); + + while(true) + { + FD_ZERO(&fds); + FD_SET(afd,&fds); + + select(afd+1,&fds,NULL,NULL,NULL); + if(FD_ISSET(afd,&fds) && BIO_do_accept(abio) > 0) + { + int r; + char rbuf[4096]; + client = BIO_pop(abio); + ssl = SSL_new(ctx); + SSL_set_accept_state(ssl); + SSL_set_bio(ssl, client, client); + + handleClient(); + } + } +} +void SSLServer::handleClient() +{ + fd_set rfds, wfds; + int cfd = BIO_get_fd(client, NULL); + int r; + char rbuf[4096]; + std::string tempstr; + + for(;;) + { + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_SET(cfd, &rfds); + + r = select(cfd+1, &rfds, &wfds, NULL, NULL); + + if(FD_ISSET(cfd, &rfds)) + { + r = SSL_read(ssl, rbuf, sizeof(rbuf)-1); + if( r <= 0 ) + { + break; + } + rbuf[r] = '\0'; + tempstr.append(rbuf); + } + } + std::cout << "huch" << std::endl; + std::cout << tempstr << std::endl; +} + diff --git a/SSLServer.h b/SSLServer.h new file mode 100644 index 0000000..215d3b7 --- /dev/null +++ b/SSLServer.h @@ -0,0 +1,31 @@ +#ifndef SSLSERVER_H +#define SSLSERVER_H + +#include +#include +#include +#include +#include +#include +#include + +class SSLServer +{ +public: + SSLServer(); + + void run(); + +protected: + SSL_CTX* ctx; + char *cert; + char *key; + BIO *abio; + BIO *client; + SSL *ssl; + char *host; + + void handleClient(); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1379556 --- /dev/null +++ b/main.cpp @@ -0,0 +1,9 @@ +#include "SSLServer.h" + +int main() +{ + SSLServer *s; + s = new SSLServer; + s->run(); + return 0; +} diff --git a/private.key b/private.key new file mode 100644 index 0000000..12032f5 --- /dev/null +++ b/private.key @@ -0,0 +1,16 @@ +-----BEGIN PRIVATE KEY----- +MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANvAoh2gM3x715cm +a9aHC9jUhNZreiyRwJU4n5R/Qd+umsnvHKgpATk2A1hLQhwZyN4VXSqNMOKne2Hp +mkWU7AjyHWZ10kW4eRhJBt5FpYMK81RzJQyv3h1ncSFrmbarSUhMQmBpSUwt1PtF +NJfZnKPoHsysB2aynTHpoG1FL44RAgMBAAECgYB/fQlQULGuBZb0zMiBRAW+fMS4 +NNBDbSeiKqCCITYRVBXfE0a31FouW8SjA75N2TOsjA5p8lGrHOk6y7DBr30jPEZk +lQGeemplSSXSmYk0kqRqCk1o/8JgsCRI/Hg28HcoSKIhWyd0brkOlkC4C9Jiibv8 +3JhqjJ1PLWIo58VYAQJBAO09Etl2MVC/mk1QgouDPF2YBBBzHIFmhMLmlBTTsgnX +WK+mD7VCiQiDyU3PeMNX1d2BHl9jkN6snlaaZ57f+4ECQQDtIY0iyPwUAzVWALUG +q6viPRyCPJkbjtsuxAFFgG9tRPsKDgEKsI+Yhjkg2wJ3s2u+a59xMu7twyEZj/7I +RxqRAkEAr/5Av4ZI62hYpcZ2Md03qQ8e5L0ghnZxQs0fSZwKVfalWhjNSmCDnAd4 +DPs+0VuV0m75AzavwCE6jXCaMmhjAQJAFhjJSROHgenmn3QDanK22qctL+mrrsi6 +Hfp/z9UvDPSfnAlACZn4V5MyYaBm83aRtLXcrrS7VQaY4APuDwWpMQJBALkugz/9 +TbQo29U8s+B0MFMJ33PYcGkhbwUWX/4liUQxKaYsByOQJX5BPgbhdTh8RCR8YTMO +VtpRP0jokMhIryE= +-----END PRIVATE KEY----- diff --git a/servercert.pem b/servercert.pem new file mode 100644 index 0000000..9137c74 --- /dev/null +++ b/servercert.pem @@ -0,0 +1,15 @@ +-----BEGIN CERTIFICATE----- +MIICXzCCAcgCCQDx8zCuCKCP2zANBgkqhkiG9w0BAQUFADB0MQswCQYDVQQGEwJV +UzERMA8GA1UECAwIQ29sb3JhZG8xGjAYBgNVBAcMEUNoZXllbm5lIE1vdW50YWlu +MQ0wCwYDVQQKDARVU0FGMQ4wDAYDVQQLDAVOT1JBRDEXMBUGA1UEAwwOd29wci5u +b3JhZC5taWwwHhcNMTIwMzIwMTUxMDU0WhcNMTMwMzIwMTUxMDU0WjB0MQswCQYD +VQQGEwJVUzERMA8GA1UECAwIQ29sb3JhZG8xGjAYBgNVBAcMEUNoZXllbm5lIE1v +dW50YWluMQ0wCwYDVQQKDARVU0FGMQ4wDAYDVQQLDAVOT1JBRDEXMBUGA1UEAwwO +d29wci5ub3JhZC5taWwwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANvAoh2g +M3x715cma9aHC9jUhNZreiyRwJU4n5R/Qd+umsnvHKgpATk2A1hLQhwZyN4VXSqN +MOKne2HpmkWU7AjyHWZ10kW4eRhJBt5FpYMK81RzJQyv3h1ncSFrmbarSUhMQmBp +SUwt1PtFNJfZnKPoHsysB2aynTHpoG1FL44RAgMBAAEwDQYJKoZIhvcNAQEFBQAD +gYEAWnxLW/+T8jEHvTUlC9T/0Jimea9hW70DaCf2MTIwMjk3HcPM3HCIwMhHKqto +I/1wuMzEGv3R5yNSw0+KJUpcip5AQvEJLbPN9QGtoxSRqS4Up8NYdg+sxxBoHKDN +bJthxlSXk5HPJdBXGQv9b5ztwDUwEu9jm9FhGhO+fjw6rgE= +-----END CERTIFICATE----- diff --git a/servercsr.pem b/servercsr.pem new file mode 100644 index 0000000..e65b025 --- /dev/null +++ b/servercsr.pem @@ -0,0 +1,12 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIBtDCCAR0CAQAwdDELMAkGA1UEBhMCVVMxETAPBgNVBAgMCENvbG9yYWRvMRow +GAYDVQQHDBFDaGV5ZW5uZSBNb3VudGFpbjENMAsGA1UECgwEVVNBRjEOMAwGA1UE +CwwFTk9SQUQxFzAVBgNVBAMMDndvcHIubm9yYWQubWlsMIGfMA0GCSqGSIb3DQEB +AQUAA4GNADCBiQKBgQDbwKIdoDN8e9eXJmvWhwvY1ITWa3oskcCVOJ+Uf0HfrprJ +7xyoKQE5NgNYS0IcGcjeFV0qjTDip3th6ZpFlOwI8h1mddJFuHkYSQbeRaWDCvNU +cyUMr94dZ3Eha5m2q0lITEJgaUlMLdT7RTSX2Zyj6B7MrAdmsp0x6aBtRS+OEQID +AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAjXwOrXPDoVz8ci1NgsjA0CW4yhG70to+ +0Ct8sOgfqK5fRtr3DdZOC/XFvywp8ARG3wBHmot1ZQOlUhwA8kgQJvbwRz2EtmNE ++aC1yr8BRQxePFH//t71XISgs5sf/PbgrqwNf6ldHF6RrQH6nrwKH6Fm/vIJOuSF +UmC1rWoXUHc= +-----END CERTIFICATE REQUEST-----