From c6ea3d784ee920f51de3088437b471c8dd6d70e2 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 18 Nov 2021 15:54:46 +0300 Subject: Add openssl::info() overloads --- libbutl/openssl.txx | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'libbutl/openssl.txx') diff --git a/libbutl/openssl.txx b/libbutl/openssl.txx index f198c22..01e854c 100644 --- a/libbutl/openssl.txx +++ b/libbutl/openssl.txx @@ -1,6 +1,7 @@ // file : libbutl/openssl.txx -*- C++ -*- // license : MIT; see accompanying LICENSE file +#include // size_t #include // forward() namespace butl @@ -49,4 +50,66 @@ namespace butl // Note: leaving this scope closes any open ends of the pipes in io_data. } + + template + optional openssl:: + info (const C& cmdc, E&& err, const process_env& env) + { + using namespace std; + + // Run the `openssl version` command. + // + openssl os (cmdc, + nullfd, fdstream_mode::text, forward (err), + env, + "version"); + + // Read the command's stdout and wait for its completion. Bail out if the + // command didn't terminate successfully or stdout contains no data. + // + string s; + if (!getline (os.in, s)) + return nullopt; + + os.in.close (); + + if (!os.wait ()) + return nullopt; + + // Parse the version string. + // + // Note that there is some variety in the version representations: + // + // OpenSSL 3.0.0 7 sep 2021 (Library: OpenSSL 3.0.0 7 sep 2021) + // OpenSSL 1.1.1l FIPS 24 Aug 2021 + // LibreSSL 2.8.3 + // + // We will only consider the first two space separated components as the + // program name and version. We will also assume that there are no leading + // spaces and the version is delimited from the program name with a single + // space character. + // + size_t e (s.find (' ')); + + // Bail out if there is no version present in the string or the program + // name is empty. + // + if (e == string::npos || e == 0) + return nullopt; + + string nm (s, 0, e); + + size_t b (e + 1); // The beginning of the version. + e = s.find (' ', b); // The end of the version. + + optional ver ( + parse_semantic_version (string (s, b, e != string::npos ? e - b : e), + "" /* build_separators */)); + + if (!ver) + return nullopt; + + return openssl_info {move (nm), move (*ver)}; + } } -- cgit v1.1