File: x509cert.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 (170 lines) | stat: -rw-r--r-- 4,713 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//    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) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

// Wrap a mbed TLS x509_crt object

#ifndef OPENVPN_MBEDTLS_PKI_X509CERT_H
#define OPENVPN_MBEDTLS_PKI_X509CERT_H

#include <string>
#include <sstream>
#include <cstring>
#include <iostream>

#include <mbedtls/x509.h>
#include <mbedtls/pem.h>
#include <mbedtls/base64.h>

#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/mbedtls/util/error.hpp>

namespace openvpn::MbedTLSPKI {

class X509Cert : public RC<thread_unsafe_refcount>
{
  public:
    typedef RCPtr<X509Cert> Ptr;

    X509Cert()
        : chain(nullptr)
    {
    }

    X509Cert(const std::string &cert_txt, const std::string &title, const bool strict)
        : chain(nullptr)
    {
        try
        {
            parse(cert_txt, title, strict);
        }
        catch (...)
        {
            dealloc();
            throw;
        }
    }

    void parse(const std::string &cert_txt, const std::string &title, const bool strict)
    {
        alloc();

        if (cert_txt.empty())
            throw MbedTLSException(title + " certificate is undefined");

        // cert_txt.length() is increased by 1 as it does not include the NULL-terminator
        // which mbedtls_x509_crt_parse() expects to see.
        const int status = mbedtls_x509_crt_parse(chain,
                                                  (const unsigned char *)cert_txt.c_str(),
                                                  cert_txt.length() + 1);
        if (status < 0)
        {
            throw MbedTLSException("error parsing " + title + " certificate", status);
        }
        if (status > 0)
        {
            std::ostringstream os;
            os << status << " certificate(s) in " << title << " bundle failed to parse";
            if (strict)
                throw MbedTLSException(os.str());
            else
                OPENVPN_LOG("MBEDTLS: " << os.str());
        }
    }

    static std::string der_to_pem(const unsigned char *der, size_t der_size)
    {
        size_t olen = 0;
        int ret;

        ret = mbedtls_pem_write_buffer(begin_cert,
                                       end_cert,
                                       der,
                                       der_size,
                                       NULL,
                                       0,
                                       &olen);
        if (ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL)
            throw MbedTLSException("X509Cert::extract: can't calculate PEM size");

        BufferAllocated buff(olen, 0);

        ret = mbedtls_pem_write_buffer(begin_cert,
                                       end_cert,
                                       der,
                                       der_size,
                                       buff.data(),
                                       buff.max_size(),
                                       &olen);
        if (ret)
            throw MbedTLSException("X509Cert::extract: can't write PEM buffer");

        return std::string((const char *)buff.data());
    }

    std::string extract() const
    {
        return der_to_pem(chain->raw.p, chain->raw.len);
    }

    std::vector<std::string> extract_extra_certs() const
    {
        std::vector<std::string> extra_certs;

        /* extra certificates are appended to the main one */
        for (mbedtls_x509_crt *cert = chain->next; cert; cert = cert->next)
        {
            extra_certs.push_back(der_to_pem(cert->raw.p, cert->raw.len));
        }
        return extra_certs;
    }

    mbedtls_x509_crt *get() const
    {
        return chain;
    }

    virtual ~X509Cert()
    {
        dealloc();
    }

  protected:
    void alloc()
    {
        if (!chain)
        {
            chain = new mbedtls_x509_crt;
            mbedtls_x509_crt_init(chain);
        }
    }

    mbedtls_x509_crt *chain;

  private:
    void dealloc()
    {
        if (chain)
        {
            mbedtls_x509_crt_free(chain);
            delete chain;
            chain = nullptr;
        }
    }

    constexpr static const char *begin_cert = "-----BEGIN CERTIFICATE-----\n";
    constexpr static const char *end_cert = "-----END CERTIFICATE-----\n";
};
} // namespace openvpn::MbedTLSPKI

#endif