OpenVPN 3 Core Library
Loading...
Searching...
No Matches
pkctx.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 pk_context object.
13
14#ifndef OPENVPN_MBEDTLS_PKI_PKCTX_H
15#define OPENVPN_MBEDTLS_PKI_PKCTX_H
16
17#include <string>
18#include <sstream>
19#include <cstring>
20
21#include <mbedtls/pk.h>
22
25#include <openvpn/common/rc.hpp>
28
29namespace openvpn::MbedTLSPKI {
30
31class PKContext : public RC<thread_unsafe_refcount>
32{
33 public:
35
37 : ctx(nullptr)
38 {
39 }
40
41 PKContext(const std::string &key_txt, const std::string &title, const std::string &priv_key_pwd, MbedTLSRandom &rand)
42 : ctx(nullptr)
43 {
44 try
45 {
46 parse(key_txt, title, priv_key_pwd, rand);
47 }
48 catch (...)
49 {
50 dealloc();
51 throw;
52 }
53 }
54
55 bool defined() const
56 {
57 return ctx != nullptr;
58 }
59
61 {
62 switch (mbedtls_pk_get_type(ctx))
63 {
64 case MBEDTLS_PK_RSA:
65 case MBEDTLS_PK_RSA_ALT:
66 case MBEDTLS_PK_RSASSA_PSS:
67 return PKType::PK_RSA;
68 case MBEDTLS_PK_ECKEY:
69 case MBEDTLS_PK_ECKEY_DH:
70 return PKType::PK_EC;
71 case MBEDTLS_PK_ECDSA:
72 return PKType::PK_ECDSA;
73 case MBEDTLS_PK_NONE:
74 return PKType::PK_NONE;
75 default:
76 return PKType::PK_UNKNOWN;
77 }
78 }
79
80 size_t key_length() const
81 {
82 return mbedtls_pk_get_bitlen(ctx);
83 }
84
85 void parse(const std::string &key_txt, const std::string &title, const std::string &priv_key_pwd, MbedTLSRandom &rand)
86 {
87 alloc();
88 // key_txt.length() is increased by 1 as it does not include the NULL-terminator
89 // which mbedtls_pk_parse_key() expects to see.
90 const int status = mbedtls_pk_parse_key(ctx,
91 (const unsigned char *)key_txt.c_str(),
92 key_txt.length() + 1,
93 (const unsigned char *)priv_key_pwd.c_str(),
94 priv_key_pwd.length()
95#if MBEDTLS_VERSION_NUMBER > 0x03000000
96 ,
97 mbedtls_ctr_drbg_random,
98 rand.get_ctr_drbg_ctx()
99#endif
100 );
101 if (status < 0)
102 throw MbedTLSException("error parsing " + title + " private key", status);
103 }
104
105 std::string extract() const
106 {
107 // maximum size of the PEM data is not available at this point
108 BufferAllocated buff(16000);
109
110 int ret = mbedtls_pk_write_key_pem(ctx, buff.data(), buff.max_size());
111 if (ret < 0)
112 throw MbedTLSException("extract priv_key: can't write to buffer", ret);
113
114 return std::string((const char *)buff.data());
115 }
116
117 std::string render_pem() const
118 {
119 return extract();
120 }
121
122 void epki_enable(void *arg,
123 mbedtls_pk_rsa_alt_decrypt_func epki_decrypt,
124 mbedtls_pk_rsa_alt_sign_func epki_sign,
125 mbedtls_pk_rsa_alt_key_len_func epki_key_len)
126 {
127 alloc();
128 const int status = mbedtls_pk_setup_rsa_alt(ctx, arg, epki_decrypt, epki_sign, epki_key_len);
129 if (status < 0)
130 throw MbedTLSException("error in mbedtls_pk_setup_rsa_alt", status);
131 }
132
133 mbedtls_pk_context *get() const
134 {
135 return ctx;
136 }
137
139 {
140 dealloc();
141 }
142
143 private:
144 void alloc()
145 {
146 if (!ctx)
147 {
148 ctx = new mbedtls_pk_context;
149 mbedtls_pk_init(ctx);
150 }
151 }
152
153 void dealloc()
154 {
155 if (ctx)
156 {
157 mbedtls_pk_free(ctx);
158 delete ctx;
159 ctx = nullptr;
160 }
161 }
162
163 mbedtls_pk_context *ctx;
164};
165
166} // namespace openvpn::MbedTLSPKI
167#endif
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 epki_enable(void *arg, mbedtls_pk_rsa_alt_decrypt_func epki_decrypt, mbedtls_pk_rsa_alt_sign_func epki_sign, mbedtls_pk_rsa_alt_key_len_func epki_key_len)
Definition pkctx.hpp:122
mbedtls_pk_context * ctx
Definition pkctx.hpp:163
void parse(const std::string &key_txt, const std::string &title, const std::string &priv_key_pwd, MbedTLSRandom &rand)
Definition pkctx.hpp:85
mbedtls_pk_context * get() const
Definition pkctx.hpp:133
std::string render_pem() const
Definition pkctx.hpp:117
PKType::Type key_type() const
Definition pkctx.hpp:60
size_t key_length() const
Definition pkctx.hpp:80
std::string extract() const
Definition pkctx.hpp:105
PKContext(const std::string &key_txt, const std::string &title, const std::string &priv_key_pwd, MbedTLSRandom &rand)
Definition pkctx.hpp:41
RCPtr< PKContext > Ptr
Definition pkctx.hpp:34
mbedtls_ctr_drbg_context * get_ctr_drbg_ctx()
Definition rand.hpp:92
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
std::string ret