Async 1.8.0
AsyncSslX509Extensions.h
Go to the documentation of this file.
1
31#ifndef ASYNC_SSL_X509_EXTENSIONS_INCLUDED
32#define ASYNC_SSL_X509_EXTENSIONS_INCLUDED
33
34
35/****************************************************************************
36 *
37 * System Includes
38 *
39 ****************************************************************************/
40
41#include <openssl/x509v3.h>
42#include <cstring>
43#include <sstream>
44
45
46/****************************************************************************
47 *
48 * Project Includes
49 *
50 ****************************************************************************/
51
52#include <AsyncIpAddress.h>
54
55
56/****************************************************************************
57 *
58 * Local Includes
59 *
60 ****************************************************************************/
61
62
63
64/****************************************************************************
65 *
66 * Forward declarations
67 *
68 ****************************************************************************/
69
70
71
72/****************************************************************************
73 *
74 * Namespace
75 *
76 ****************************************************************************/
77
78namespace Async
79{
80
81
82/****************************************************************************
83 *
84 * Forward declarations of classes inside of the declared namespace
85 *
86 ****************************************************************************/
87
88
89
90/****************************************************************************
91 *
92 * Defines & typedefs
93 *
94 ****************************************************************************/
95
96
97
98/****************************************************************************
99 *
100 * Exported Global Variables
101 *
102 ****************************************************************************/
103
104
105
106/****************************************************************************
107 *
108 * Class definitions
109 *
110 ****************************************************************************/
111
120{
121 public:
126 {
127 m_exts = sk_X509_EXTENSION_new_null();
128 }
129
134 explicit SslX509Extensions(STACK_OF(X509_EXTENSION)* exts)
135 : m_exts(exts)
136 {
137 }
138
144 {
145 m_exts = other.m_exts;
146 other.m_exts = nullptr;
147 }
148
154 {
155 //std::cout << "### SslX509Extensions copy constructor" << std::endl;
156 for (int i=0; i<X509v3_get_ext_count(other.m_exts); ++i)
157 {
158 const auto ext = X509v3_get_ext(other.m_exts, i);
159 X509v3_add_ext(&m_exts, ext, -1);
160 }
161 }
162
167
172 {
173 if (m_exts != nullptr)
174 {
175 sk_X509_EXTENSION_pop_free(m_exts, X509_EXTENSION_free);
176 m_exts = nullptr;
177 }
178 }
179
187 bool addBasicConstraints(const std::string& bc)
188 {
189 return addExt(NID_basic_constraints, bc);
190 }
191
201 bool addKeyUsage(const std::string& ku)
202 {
203 return addExt(NID_key_usage, ku);
204 }
205
213 bool addExtKeyUsage(const std::string& eku)
214 {
215 return addExt(NID_ext_key_usage, eku);
216 }
217
225 bool addSubjectAltNames(const std::string& san)
226 {
227 return addExt(NID_subject_alt_name, san);
228 }
229
235 {
236 int ext_idx = X509v3_get_ext_by_NID(m_exts, NID_subject_alt_name, -1);
237 if (ext_idx < 0)
238 {
239 return nullptr;
240 }
241 auto ext = X509v3_get_ext(m_exts, ext_idx);
242 return ext;
243 }
244
250 {
251 const X509_EXTENSION* other_ext = san;
252#if OPENSSL_VERSION_MAJOR >= 3
253 auto ext = X509_EXTENSION_dup(other_ext);
254#else
255 auto ext = X509_EXTENSION_dup(const_cast<X509_EXTENSION*>(other_ext));
256#endif
257 return (sk_X509_EXTENSION_push(m_exts, ext) > 0);
258 }
259
263 operator const STACK_OF(X509_EXTENSION)*() const { return m_exts; }
264
265 private:
266 STACK_OF(X509_EXTENSION)* m_exts = nullptr;
267
268 bool addExt(int nid, const std::string& value)
269 {
270 auto ex = X509V3_EXT_conf_nid(NULL, NULL, nid, value.c_str());
271 if (ex == nullptr)
272 {
273 return false;
274 }
275 sk_X509_EXTENSION_push(m_exts, ex);
276 return true;
277 }
278
279 X509_EXTENSION* cloneExtension(int nid) const
280 {
281 int ext_idx = X509v3_get_ext_by_NID(m_exts, nid, -1);
282 if (ext_idx < 0)
283 {
284 return nullptr;
285 }
286 auto ext = X509v3_get_ext(m_exts, ext_idx);
287 return X509_EXTENSION_dup(ext);
288 }
289
290}; /* SslX509Extensions */
291
292
293
294} /* namespace Async */
295
296#endif /* ASYNC_SSL_X509_EXTENSIONS_INCLUDED */
297
298/*
299 * This file has not been truncated
300 */
Platform independent representation of an IP address.
A class representing the X.509 Subject Alternative Name extension.
A class representing X.509 extensions.
bool addExtKeyUsage(const std::string &eku)
Add extended key usage.
SslX509ExtSubjectAltName subjectAltName(void) const
Get the subject alternative names extension.
bool addSubjectAltNames(const std::string &san)
Add subject alternative names.
SslX509Extensions(SslX509Extensions &&other)
Move Constructor.
bool addExtension(const SslX509ExtSubjectAltName &san)
Add a subject alternative names object.
SslX509Extensions(void)
Default constructor.
bool addBasicConstraints(const std::string &bc)
Add basic constraints extension.
bool addKeyUsage(const std::string &ku)
Add key usage.
SslX509Extensions(STACK_OF(X509_EXTENSION) *exts)
Constructor.
SslX509Extensions & operator=(const SslX509Extensions &)=delete
Disallow copy assignment.
SslX509Extensions(const SslX509Extensions &other)
Copy constructor.
Namespace for the asynchronous programming classes.