ssl functionality wip

This commit is contained in:
vaclavt 2022-01-13 21:56:29 +01:00
parent f78380afd4
commit a274c99835
2 changed files with 32 additions and 8 deletions

View File

@ -19,8 +19,7 @@
HttpClient::HttpClient() {};
std::pair<int, std::string>
HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) {
std::pair<int, std::string> HttpClient::doGetRequest(const std::string &url, const std::unordered_map<std::string, std::string> &headers) {
// https://stackoverflow.com/questions/25896916/parse-http-headers-in-c
std::regex rgx{R"(^(?:((?:https?|s?ftp):)//)([^:/\s]+)(?::(\d*))?(?:/([^\s?#]+)?([?][^?#]*)?(#.*)?)?)"};
@ -125,7 +124,7 @@ std::string HttpClient::inetAddress(std::string hostname) {
int HttpClient::sslRecvPacket() {
ssl_read_packet.resize(4096);
ssl_read_packet.clear();
int len = 16384;
char buf[len + 1];
do {
@ -165,7 +164,7 @@ int HttpClient::sslSendPacket(std::string buf) {
return -1;
}
}
int errr = SSL_get_error(ssl, len);
return buf.length();
}
@ -178,6 +177,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
return -1;
}
// socket address
std::string server_ip = inetAddress(server_name);
struct sockaddr_in sa;
@ -192,7 +192,7 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
printf("sslRequest, error connecting to server.\n");
return -1;
}
SSL_library_init();
SSLeay_add_ssl_algorithms();
SSL_load_error_strings();
@ -218,11 +218,12 @@ int HttpClient::sslRequest(const std::string &server_name, const std::string &re
}
// log cipher
// printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
ShowCerts(ssl);
// send request
//std::err << request << std::endl;
sslSendPacket(request);
// std::out << request << std::endl;
int written_bytes = sslSendPacket(request);
// read response and return its length
return sslRecvPacket();
@ -237,3 +238,25 @@ void HttpClient::log_ssl() {
std::cerr << str << std::endl;
}
}
void HttpClient::ShowCerts(SSL* ssl)
{ X509 *cert;
char *line;
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( cert != NULL )
{
printf("Server certificates:\n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %s\n", line);
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %s\n", line);
free(line); /* free the malloc'ed string */
X509_free(cert); /* free the malloc'ed certificate copy */
}
else
printf("No certificates.\n");
}

View File

@ -34,4 +34,5 @@ private:
int sslRequest(const std::string &server_name, const std::string &request);
void log_ssl();
void ShowCerts(SSL* ssl);
};