OpenVPN 3 Core Library
Loading...
Searching...
No Matches
x509crl.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 a mbed TLS x509_crl object
13
14#ifndef OPENVPN_MBEDTLS_PKI_X509CRL_H
15#define OPENVPN_MBEDTLS_PKI_X509CRL_H
16
17#include <string>
18#include <sstream>
19#include <cstring>
20
21#include <mbedtls/x509_crl.h>
22
25#include <openvpn/common/rc.hpp>
27
28namespace openvpn::MbedTLSPKI {
29
30class X509CRL : public RC<thread_unsafe_refcount>
31{
32 public:
34
36 : chain(nullptr)
37 {
38 }
39
40 X509CRL(const std::string &crl_txt)
41 : chain(nullptr)
42 {
43 try
44 {
45 parse(crl_txt);
46 }
47 catch (...)
48 {
49 dealloc();
50 throw;
51 }
52 }
53
54 void parse(const std::string &crl_txt)
55 {
56 alloc();
57
58 // crl_txt.length() is increased by 1 as it does not include the NULL-terminator
59 // which mbedtls_x509_crl_parse() expects to see.
60 const int status = mbedtls_x509_crl_parse(chain,
61 (const unsigned char *)crl_txt.c_str(),
62 crl_txt.length() + 1);
63 if (status < 0)
64 {
65 throw MbedTLSException("error parsing CRL", status);
66 }
67
68 pem_chain = crl_txt;
69 }
70
71 std::string extract() const
72 {
73 return std::string(pem_chain);
74 }
75
76 mbedtls_x509_crl *get() const
77 {
78 return chain;
79 }
80
82 {
83 dealloc();
84 }
85
86 private:
87 void alloc()
88 {
89 if (!chain)
90 {
91 chain = new mbedtls_x509_crl;
92 std::memset(chain, 0, sizeof(mbedtls_x509_crl));
93 }
94 }
95
96 void dealloc()
97 {
98 if (chain)
99 {
100 mbedtls_x509_crl_free(chain);
101 delete chain;
102 chain = nullptr;
103 }
104 }
105
106 mbedtls_x509_crl *chain;
107 std::string pem_chain;
108};
109} // namespace openvpn::MbedTLSPKI
110
111#endif
X509CRL(const std::string &crl_txt)
Definition x509crl.hpp:40
mbedtls_x509_crl * chain
Definition x509crl.hpp:106
std::string extract() const
Definition x509crl.hpp:71
void parse(const std::string &crl_txt)
Definition x509crl.hpp:54
mbedtls_x509_crl * get() const
Definition x509crl.hpp:76
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912