LibreSSL TLS C++ Client

Exported from Notepad++
//clear; rm -frv ssl_client; g++-7 -g -fPIC -Wall -ltls -lssl -lcrypto ssl_client.cpp -o ssl_client -static-libstdc++ //#################################################################### //openssl req -x509 -newkey rsa:4096 -subj “/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=www.example.com” -keyout key.pem -out cert.pem -days 365 //openssl x509 -in cert.pem -text -noout //openssl rsa -in key.pem -out key2.pem #Passwort entfernen //#################################################################### //lynx https://github.com/daniloegea/libressl-tls-api-examples //#################################################################### //################################################################# //TEST //tcpdump -nnvvXSs 1514 -i eth0 port not 22 and not host 172.17.190.1 and not arp and port 9001 //Im vergleich zu Netcat unverschlüsselte Verbindung //Server: “netcat -l -v 9001” //Client: “telnet 172.17.190.84 9001” // //openssl s_client -connect 172.17.190.84:9001 //C_C++ Header #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> #include <stdio.h> #include <string> #include <string.h> #include <cstring> //algorithm fürs string klein machen #include <algorithm> //Zum Casten von Char Hex Array nach string (bei md5sum) #include <iomanip> #include <iostream> #include <vector> #include <sys/time.h> #include <sstream> #include <fstream> #include <pthread.h> #include <thread> #include <time.h> //Network Header #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <netdb.h> #include <string.h> #include <stdlib.h> #include <vector> #include <errno.h> #include <malloc.h> #include <resolv.h> //#include <openssl/ssl.h> //#include <openssl/err.h> //# //#include <poll.h> #include <tls.h> using namespace std; int main(int argc, char *argv[]) { cout<<“SSL CLIENT TEST”<<endl; struct tls *tls = NULL; struct tls_config *config = NULL; //char *msg = “HELLO TLS SERVER!\n”; string msg = “HELLO TLS SERVER”; //ssize_t outlen = 0; //char bufs[1000], bufc[1000]; struct sockaddr_in server; //struct pollfd pfd[2]; int sock; //gnuTLS Initilisieren tls_init(); tls = tls_client(); config = tls_config_new(); tls_config_insecure_noverifycert(config); tls_config_insecure_noverifyname(config); tls_configure(tls, config); //Socket API sock = socket(AF_INET, SOCK_STREAM, 0); //server.sin_port = htons(443); server.sin_port = htons(9001); //server.sin_addr.s_addr = inet_addr(“172.217.22.99”); //www.google.de server.sin_addr.s_addr = inet_addr(“172.17.190.84”); //server.sin_addr.s_addr = inet_addr(“127.0.0.1”); server.sin_family = AF_INET; /* if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) { printf(“erro socket\n”); //cout<<“connect#”<<connect<<endl; exit(1); } */ //Socket Verbindung zum Server aufbauen int connect_status = 666; connect_status = connect(sock,(struct sockaddr*)&server, sizeof(server)); if (connect_status == -1) { cout<<“connect_status# “<<connect_status<<endl; } //Socket TLS verschlüssel /* if(tls_connect_socket(tls, sock, “localhost”) < 0) { printf(“tls_connect error\n”); printf(“%s\n”, tls_error(tls)); exit(1); } */ int tls_connect_socket_status = 666; tls_connect_socket_status = tls_connect_socket(tls, sock, “localhost”); if (tls_connect_socket_status == -1) { cout<<“tls_connect_socket_status#”<<endl; cout<<” tls_error(tls)”<<tls_error(tls)<<endl; } //TLS Scoket senden, analog zu Socket API write tls_write(tls, msg.c_str(), strlen(msg.c_str())); /* pfd[0].fd = 0; pfd[0].events = POLLIN; pfd[1].fd = sock; pfd[1].events = POLLIN; while(bufc[0] != ‘:’ && bufc[1] != ‘q’) { bzero(bufs, 1000); bzero(bufc, 1000); poll(pfd, 2, -1); if(pfd[0].revents & POLLIN) { int q = read(0, bufc, 1000); tls_write(tls, bufc, q); } //if(pfd[1].lrevents & POLLIN) { // if((outlen = tls_read(tls, bufs, 1000)) <= 0) break; // printf(“Message (%lu): %s\n”, outlen, bufs); //} if(outlen = tls_read(tls, bufs, 1000)) { printf(“Message (%lu): %s\n”, outlen, bufs); } } */ //TLS Vebrindung abbauen und speicher free`en tls_close(tls); tls_free(tls); tls_config_free(config); return (0); }

Post Revisions: