OpenVPN 3 Core Library
Loading...
Searching...
No Matches
pem.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) 2017-2018 OpenVPN Technologies, Inc.
8//
9// This program is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License Version 3
11// as published by the Free Software Foundation.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program in the COPYING file.
20// If not, see <http://www.gnu.org/licenses/>.
21
22// Wrap the mbedTLS PEM API defined in <mbedtls/pem.h> so
23// that it can be used as part of the crypto layer of the OpenVPN core.
24
25#ifndef OPENVPN_MBEDTLS_UTIL_PEM_H
26#define OPENVPN_MBEDTLS_UTIL_PEM_H
27
28#include <mbedtls/pem.h>
29
30namespace openvpn {
32{
33 public:
34 static bool pem_encode(BufferAllocated &dst,
35 const unsigned char *src,
36 size_t src_len,
37 const std::string &key_name)
38 {
39 std::string header = "-----BEGIN " + key_name + "-----\n";
40 std::string footer = "-----END " + key_name + "-----\n";
41 size_t out_len = 0;
42
43 int ret = mbedtls_pem_write_buffer(header.c_str(),
44 footer.c_str(),
45 src,
46 src_len,
47 dst.data(),
48 dst.max_size(),
49 &out_len);
50 if (ret == 0)
51 dst.set_size(out_len);
52 else
53 {
54 char buf[128];
55 mbedtls_strerror(ret, buf, 128);
56 OPENVPN_LOG("mbedtls_pem_write_buffer error: " << buf);
57 }
58
59 return (ret == 0);
60 }
61
62 static bool pem_decode(BufferAllocated &dst,
63 const char *src,
64 size_t src_len,
65 const std::string &key_name)
66 {
67 std::string header = "-----BEGIN " + key_name + "-----";
68 std::string footer = "-----END " + key_name + "-----";
69 mbedtls_pem_context ctx = {};
70 size_t out_len = 0;
71
72 int ret = mbedtls_pem_read_buffer(&ctx,
73 header.c_str(),
74 footer.c_str(),
75 (unsigned char *)src,
76 nullptr,
77 0,
78 &out_len);
79 if (ret == 0)
80 {
81 size_t buflen = 0;
82 const uint8_t *bufptr = mbedtls_pem_get_buffer(&ctx, &buflen);
83 dst.init(bufptr, buflen, BufAllocFlags::DESTRUCT_ZERO);
84 }
85
86 mbedtls_pem_free(&ctx);
87
88 return (ret == 0);
89 }
90};
91}; // namespace openvpn
92
93#endif /* OPENVPN_MBEDTLS_UTIL_PEM_H */
void init(const size_t capacity, const BufferFlags flags=BufAllocFlags::NO_FLAGS)
Initializes the buffer with the specified capacity and flags.
Definition buffer.hpp:1739
size_t max_size() const
Return the maximum allowable size value in T objects given the current offset (without considering re...
Definition buffer.hpp:1377
T * data()
Get a mutable pointer to the start of the array.
Definition buffer.hpp:1450
void set_size(const size_t size)
After an external method, operating on the array as a mutable unsigned char buffer,...
Definition buffer.hpp:1384
static bool pem_encode(BufferAllocated &dst, const unsigned char *src, size_t src_len, const std::string &key_name)
Definition pem.hpp:34
static bool pem_decode(BufferAllocated &dst, const char *src, size_t src_len, const std::string &key_name)
Definition pem.hpp:62
#define OPENVPN_LOG(args)
static const unsigned char * mbedtls_pem_get_buffer(const mbedtls_pem_context *ctx, size_t *buf_size)
constexpr BufferFlags DESTRUCT_ZERO(1u<< 1)
if enabled, destructor will zero data before deletion
std::string ret