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 OpenSSL PEM API defined in <openssl/pem.h> so
23// that it can be used as part of the crypto layer of the OpenVPN core.
24
25#ifndef OPENVPN_OPENSSL_UTIL_PEM_H
26#define OPENVPN_OPENSSL_UTIL_PEM_H
27
30
31#include <openssl/pem.h>
32
33namespace openvpn {
35{
36 public:
37 static bool pem_encode(BufferAllocated &dst,
38 const unsigned char *src,
39 size_t src_len,
40 const std::string &key_name)
41 {
42 bool ret = false;
43 if (!is_safe_conversion<int>(src_len))
44 return false;
45 BIO *bio = BIO_new(BIO_s_mem());
46 if (!bio)
47 return false;
48
49 if (!PEM_write_bio(bio, key_name.c_str(), "", src, static_cast<int>(src_len)))
50 goto out;
51
52 BUF_MEM *bptr;
53 BIO_get_mem_ptr(bio, &bptr);
54 dst.write((unsigned char *)bptr->data, bptr->length);
55
56 ret = true;
57
58 out:
59 if (!BIO_free(bio))
60 ret = false;
61
62 return ret;
63 }
64
65 static bool pem_decode(BufferAllocated &dst,
66 const char *src,
67 size_t src_len,
68 const std::string &key_name)
69 {
70 bool ret = false;
71 BIO *bio;
72
73 if (!(bio = BIO_new_mem_buf(src, numeric_cast<int>(src_len))))
74 throw OpenSSLException("Cannot open memory BIO for PEM decode");
75
76 char *name_read = NULL;
77 char *header_read = NULL;
78 uint8_t *data_read = NULL;
79 long data_read_len = 0;
80 if (!PEM_read_bio(bio,
81 &name_read,
82 &header_read,
83 &data_read,
84 &data_read_len))
85 {
86 OPENVPN_LOG("PEM decode failed");
87 goto out;
88 }
89
90 if (key_name.compare(std::string(name_read)))
91 {
92 OPENVPN_LOG("unexpected PEM name (got '" << name_read << "', expected '" << key_name << "')");
93 goto out;
94 }
95
96 dst.write(data_read, data_read_len);
97
98 ret = true;
99 out:
100 OPENSSL_free(name_read);
101 OPENSSL_free(header_read);
102 OPENSSL_free(data_read);
103
104 if (!BIO_free(bio))
105 ret = false;
106
107 return ret;
108 }
109};
110}; // namespace openvpn
111
112#endif /* OPENVPN_OPENSSL_UTIL_PEM_H */
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1563
static bool pem_encode(BufferAllocated &dst, const unsigned char *src, size_t src_len, const std::string &key_name)
Definition pem.hpp:37
static bool pem_decode(BufferAllocated &dst, const char *src, size_t src_len, const std::string &key_name)
Definition pem.hpp:65
#define OPENVPN_LOG(args)
std::string ret
static std::stringstream out
Definition test_path.cpp:10