OpenVPN 3 Core Library
Loading...
Searching...
No Matches
x509track.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_PKI_X509TRACK_H
13#define OPENVPN_PKI_X509TRACK_H
14
15#include <string>
16#include <vector>
17
23
25
41
42static const char *const names[] = {
43 // CONST GLOBAL
44 "SERIAL",
45 "SERIAL_HEX",
46 "SHA1",
47 "CN",
48 "C",
49 "L",
50 "ST",
51 "O",
52 "OU",
53 "emailAddress",
54};
55
56OPENVPN_EXCEPTION(x509_track_error);
57
58inline const char *name(const Type type)
59{
60 static_assert(N_TYPES == array_size(names), "x509 names array inconsistency");
61 if (type >= 0 && type < N_TYPES)
62 return names[type];
63 else
64 return "UNDEF";
65}
66
67inline Type parse_type(const std::string &name)
68{
69 for (size_t i = 0; i < N_TYPES; ++i)
70 if (name == names[i])
71 return Type(i);
72 return UNDEF;
73}
74
75struct Config
76{
77 Config(const Type type_arg, const bool full_chain_arg)
78 : type(type_arg),
79 full_chain(full_chain_arg)
80 {
81 }
82
83 Config(const std::string &spec)
84 {
85 full_chain = (spec.length() > 0 && spec[0] == '+');
86 type = parse_type(spec.substr(full_chain ? 1 : 0));
87 if (type == UNDEF)
88 throw Exception("cannot parse attribute '" + spec + "'");
89 }
90
91 std::string to_string() const
92 {
93 std::string ret;
94 if (full_chain)
95 ret += '+';
96 ret += name(type);
97 return ret;
98 }
99
100 bool depth_match(const int depth) const
101 {
102 return !depth || full_chain;
103 }
104
107};
108
109struct ConfigSet : public std::vector<Config>
110{
112 {
113 }
114
116 const bool include_serial,
117 const bool include_serial_hex)
118 {
119 const auto *xt = opt.get_index_ptr("x509-track");
120 if (xt)
121 {
122 for (const auto &i : *xt)
123 {
124 try
125 {
126 const Option &o = opt[i];
127 o.touch();
128 emplace_back(o.get(1, 64));
129 }
130 catch (const std::exception &e)
131 {
132 throw x509_track_error(e.what());
133 }
134 }
135 }
136
137 if (include_serial && !exists(SERIAL))
138 emplace_back(SERIAL, true);
139 if (include_serial_hex && !exists(SERIAL_HEX))
140 emplace_back(SERIAL_HEX, true);
141 }
142
143 bool exists(const Type t) const
144 {
145 for (auto &c : *this)
146 if (c.type == t)
147 return true;
148 return false;
149 }
150
151 std::string to_string() const
152 {
153 std::string ret;
154 for (auto &c : *this)
155 {
156 ret += c.to_string();
157 ret += '\n';
158 }
159 return ret;
160 }
161};
162
164{
165 KeyValue(const Type type_arg,
166 const int depth_arg,
167 std::string value_arg)
168 : type(type_arg),
169 depth(depth_arg),
170 value(std::move(value_arg))
171 {
172 }
173
174 std::string to_string(const bool omi_form) const
175 {
176 std::string ret;
177 ret.reserve(128);
178 if (omi_form)
179 ret += ">CLIENT:ENV,";
180 ret += key_name();
181 ret += '=';
183 return ret;
184 }
185
186 std::string key_name() const
187 {
188 switch (type)
189 {
190 case SERIAL:
191 return "tls_serial_" + openvpn::to_string(depth);
192 case SERIAL_HEX:
193 return "tls_serial_hex_" + openvpn::to_string(depth);
194 default:
195 return "X509_" + openvpn::to_string(depth) + '_' + name(type);
196 }
197 }
198
200 int depth = 0;
201 std::string value;
202};
203
204struct Set : public std::vector<KeyValue>
205{
206 std::string to_string(const bool omi_form) const
207 {
208 std::string ret;
209 ret.reserve(512);
210 for (auto &kv : *this)
211 {
212 ret += kv.to_string(omi_form);
213 if (omi_form)
214 ret += '\r';
215 ret += '\n';
216 }
217 return ret;
218 }
219};
220
221} // namespace openvpn::X509Track
222
223#endif
const IndexList * get_index_ptr(const std::string &name) const
Definition options.hpp:1276
void touch(bool lightly=false) const
Definition options.hpp:385
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
#define OPENVPN_EXCEPTION(C)
static const char *const names[]
Definition x509track.hpp:42
const char * name(const Type type)
Definition x509track.hpp:58
Type parse_type(const std::string &name)
Definition x509track.hpp:67
std::string reduce_spaces(const std::string &str, const char rep)
Definition string.hpp:385
constexpr std::size_t array_size(T(&)[N])
Definition arraysize.hpp:19
std::string to_string(const T &t)
Convert a value to a string.
Definition to_string.hpp:45
std::string to_string() const
bool exists(const Type t) const
ConfigSet(const OptionList &opt, const bool include_serial, const bool include_serial_hex)
bool depth_match(const int depth) const
Config(const Type type_arg, const bool full_chain_arg)
Definition x509track.hpp:77
std::string to_string() const
Definition x509track.hpp:91
Config(const std::string &spec)
Definition x509track.hpp:83
std::string key_name() const
std::string to_string(const bool omi_form) const
KeyValue(const Type type_arg, const int depth_arg, std::string value_arg)
std::string to_string(const bool omi_form) const
std::string ret