File: pem.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (93 lines) | stat: -rw-r--r-- 3,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2017-2018 OpenVPN Technologies, Inc.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License Version 3
//    as published by the Free Software Foundation.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program in the COPYING file.
//    If not, see <http://www.gnu.org/licenses/>.

// Wrap the mbedTLS PEM API defined in <mbedtls/pem.h> so
// that it can be used as part of the crypto layer of the OpenVPN core.

#ifndef OPENVPN_MBEDTLS_UTIL_PEM_H
#define OPENVPN_MBEDTLS_UTIL_PEM_H

#include <mbedtls/pem.h>

namespace openvpn {
class MbedTLSPEM
{
  public:
    static bool pem_encode(BufferAllocated &dst,
                           const unsigned char *src,
                           size_t src_len,
                           const std::string &key_name)
    {
        std::string header = "-----BEGIN " + key_name + "-----\n";
        std::string footer = "-----END " + key_name + "-----\n";
        size_t out_len = 0;

        int ret = mbedtls_pem_write_buffer(header.c_str(),
                                           footer.c_str(),
                                           src,
                                           src_len,
                                           dst.data(),
                                           dst.max_size(),
                                           &out_len);
        if (ret == 0)
            dst.set_size(out_len);
        else
        {
            char buf[128];
            mbedtls_strerror(ret, buf, 128);
            OPENVPN_LOG("mbedtls_pem_write_buffer error: " << buf);
        }

        return (ret == 0);
    }

    static bool pem_decode(BufferAllocated &dst,
                           const char *src,
                           size_t src_len,
                           const std::string &key_name)
    {
        std::string header = "-----BEGIN " + key_name + "-----";
        std::string footer = "-----END " + key_name + "-----";
        mbedtls_pem_context ctx = {};
        size_t out_len = 0;

        int ret = mbedtls_pem_read_buffer(&ctx,
                                          header.c_str(),
                                          footer.c_str(),
                                          (unsigned char *)src,
                                          nullptr,
                                          0,
                                          &out_len);
        if (ret == 0)
        {
            size_t buflen = 0;
            const uint8_t *bufptr = mbedtls_pem_get_buffer(&ctx, &buflen);
            dst.init(bufptr, buflen, BufAllocFlags::DESTRUCT_ZERO);
        }

        mbedtls_pem_free(&ctx);

        return (ret == 0);
    }
};
}; // namespace openvpn

#endif /* OPENVPN_MBEDTLS_UTIL_PEM_H */