OpenVPN 3 Core Library
Loading...
Searching...
No Matches
crl.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// Wrap an OpenSSL X509_CRL object
13
14#pragma once
15
16#include <string>
17#include <vector>
18
19#include <openssl/ssl.h>
20#include <openssl/bio.h>
21
25
27
28class CRL
29{
30 public:
32 : crl_(nullptr)
33 {
34 }
35
36 explicit CRL(const std::string &crl_txt)
37 : crl_(nullptr)
38 {
39 parse_pem(crl_txt);
40 }
41
42 CRL(const CRL &other)
43 : crl_(dup(other.crl_))
44 {
45 }
46
47 CRL(CRL &&other) noexcept
48 : crl_(other.crl_)
49 {
50 other.crl_ = nullptr;
51 }
52
53 CRL &operator=(const CRL &other)
54 {
55 if (this != &other)
56 {
57 erase();
58 crl_ = dup(other.crl_);
59 }
60 return *this;
61 }
62
63 CRL &operator=(CRL &&other) noexcept
64 {
65 if (this != &other)
66 {
67 erase();
68 crl_ = other.crl_;
69 other.crl_ = nullptr;
70 }
71 return *this;
72 }
73
74 bool defined() const
75 {
76 return crl_ != nullptr;
77 }
78 ::X509_CRL *obj() const
79 {
80 return crl_;
81 }
82
83 void parse_pem(const std::string &crl_txt)
84 {
85 BIO *bio = ::BIO_new_mem_buf(const_cast<char *>(crl_txt.c_str()), numeric_cast<int>(crl_txt.length()));
86 if (!bio)
87 throw OpenSSLException();
88
89 ::X509_CRL *crl = ::PEM_read_bio_X509_CRL(bio, nullptr, nullptr, nullptr);
90 ::BIO_free(bio);
91 if (!crl)
92 throw OpenSSLException("CRL::parse_pem");
93
94 erase();
95 crl_ = crl;
96 }
97
98 std::string render_pem() const
99 {
100 if (crl_)
101 {
102 BIO *bio = ::BIO_new(BIO_s_mem());
103 const int ret = ::PEM_write_bio_X509_CRL(bio, crl_);
104 if (ret == 0)
105 {
106 ::BIO_free(bio);
107 throw OpenSSLException("CRL::render_pem");
108 }
109
110 {
111 char *temp;
112 const auto buf_len = ::BIO_get_mem_data(bio, &temp);
113 std::string ret = std::string(temp, buf_len);
114 ::BIO_free(bio);
115 return ret;
116 }
117 }
118 else
119 return "";
120 }
121
123 {
124 erase();
125 }
126
127 private:
128 void erase()
129 {
130 if (crl_)
131 ::X509_CRL_free(crl_);
132 }
133
134 static X509_CRL *dup(const X509_CRL *crl)
135 {
136 if (crl)
137 return ::X509_CRL_dup(const_cast<X509_CRL *>(crl));
138 return nullptr;
139 }
140
141 ::X509_CRL *crl_;
142};
143
144class CRLList : public std::vector<CRL>
145{
146 public:
147 using CRL = X509;
148
149 bool defined() const
150 {
151 return !empty();
152 }
153
154 std::string render_pem() const
155 {
156 std::string ret;
157 for (const auto &e : *this)
158 ret += e.render_pem();
159 return ret;
160 }
161};
162} // namespace openvpn::OpenSSLPKI
std::string render_pem() const
Definition crl.hpp:154
::X509_CRL * obj() const
Definition crl.hpp:78
CRL(CRL &&other) noexcept
Definition crl.hpp:47
CRL & operator=(CRL &&other) noexcept
Definition crl.hpp:63
::X509_CRL * crl_
Definition crl.hpp:141
void parse_pem(const std::string &crl_txt)
Definition crl.hpp:83
CRL(const std::string &crl_txt)
Definition crl.hpp:36
static X509_CRL * dup(const X509_CRL *crl)
Definition crl.hpp:134
bool defined() const
Definition crl.hpp:74
CRL & operator=(const CRL &other)
Definition crl.hpp:53
std::string render_pem() const
Definition crl.hpp:98
CRL(const CRL &other)
Definition crl.hpp:42
std::string ret