aboutsummaryrefslogtreecommitdiff
path: root/libbutl/openssl.txx
blob: f55432dee77f7b2f6481067535f8344e9aade1a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// file      : libbutl/openssl.txx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cstddef> // size_t
#include <utility> // forward()

namespace butl
{
  template <typename I>
  typename std::enable_if<openssl::is_other<I>::value, I>::type openssl::
  map_in (I&& in, io_data&)
  {
    return std::forward<I> (in);
  }

  template <typename O>
  typename std::enable_if<openssl::is_other<O>::value, O>::type openssl::
  map_out (O&& out, io_data&)
  {
    return std::forward<O> (out);
  }

  template <typename C,
            typename I,
            typename O,
            typename E,
            typename... A>
  openssl::
  openssl (const C& cmdc,
           I&& in,
           O&& out,
           E&& err,
           const process_env& env,
           const std::string& command,
           A&&... options)
  {
    io_data in_data;
    io_data out_data;

    process& p (*this);
    p = process_start_callback (cmdc,
                                map_in  (std::forward<I> (in),  in_data),
                                map_out (std::forward<O> (out), out_data),
                                std::forward<E> (err),
                                env,
                                command,
                                in_data.options,
                                out_data.options,
                                std::forward<A> (options)...);

    // Note: leaving this scope closes any open ends of the pipes in io_data.
  }

  template <typename C,
            typename E>
  optional<openssl_info> 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<E> (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<semantic_version> ver (
      parse_semantic_version (string (s, b, e != string::npos ? e - b : e),
                              semantic_version::allow_build,
                              "" /* build_separators */));

    if (!ver)
      return nullopt;

    return openssl_info {move (nm), move (*ver)};
  }
}