oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_tls.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_tls.h>
17#include <ove/net.hpp>
18#include <ove/types.hpp>
19#include <ove/error.hpp>
20
21#ifdef CONFIG_OVE_NET_TLS
22
23namespace ove::tls
24{
25
37struct Config {
38 const unsigned char *ca_cert{};
39 size_t ca_cert_len{};
40 const char *hostname{};
41 const unsigned char *client_cert{};
42 size_t client_cert_len{};
43 const unsigned char *client_key{};
44 size_t client_key_len{};
45};
46
55{
56 public:
57 Session()
58 {
59#ifdef CONFIG_OVE_ZERO_HEAP
60 int err = ove_tls_init(&handle_, &storage_);
61#else
62 int err = ove_tls_create(&handle_);
63#endif
64 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
65 }
66
67 ~Session() noexcept
68 {
69 if (!handle_)
70 return;
71#ifdef CONFIG_OVE_ZERO_HEAP
72 ove_tls_deinit(handle_);
73#else
74 ove_tls_destroy(handle_);
75#endif
76 }
77
78 Session(const Session &) = delete;
79 Session &operator=(const Session &) = delete;
80 Session(Session &&) = delete;
81 Session &operator=(Session &&) = delete;
82
90 [[nodiscard]] Result<void> handshake(ove_socket_t sock, const Config &cfg = {}) noexcept
91 {
92 ove_tls_config_t c{cfg.ca_cert, cfg.ca_cert_len, cfg.hostname,
93 cfg.client_cert, cfg.client_cert_len, cfg.client_key,
94 cfg.client_key_len};
95 return from_rc(ove_tls_handshake(handle_, sock, &c));
96 }
97
106 [[nodiscard]] Result<size_t> send(const void *data, size_t len) noexcept
107 {
108 size_t sent = 0;
109 const int rc = ove_tls_send(handle_, data, len, &sent);
110 return from_rc(rc, sent);
111 }
112
122 [[nodiscard]] Result<size_t> recv(void *buf, size_t len) noexcept
123 {
124 size_t received = 0;
125 const int rc = ove_tls_recv(handle_, buf, len, &received);
126 return from_rc(rc, received);
127 }
128
130 void close()
131 {
132 ove_tls_close(handle_);
133 }
134
135 private:
136 ove_tls_t handle_{};
137#ifdef CONFIG_OVE_ZERO_HEAP
138 ove_tls_storage_t storage_{};
139#endif
140};
141
142} // namespace ove::tls
143
144#endif /* CONFIG_OVE_NET_TLS */
RAII wrapper around an oveRTOS TLS session.
Definition net_tls.hpp:55
Result< size_t > send(const void *data, size_t len) noexcept
Send encrypted bytes over the TLS session.
Definition net_tls.hpp:106
void close()
Close the TLS session (sends close_notify and tears down state).
Definition net_tls.hpp:130
Result< size_t > recv(void *buf, size_t len) noexcept
Receive decrypted bytes from the TLS session.
Definition net_tls.hpp:122
Result< void > handshake(ove_socket_t sock, const Config &cfg={}) noexcept
Perform TLS handshake over an established TCP socket.
Definition net_tls.hpp:90
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
TLS session wrapper over mbedTLS. Provides handshake, send, recv, and close on top of a connected ove...
Definition net_tls.hpp:24
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
RAII networking: sockets, network interface, DNS.
TLS session configuration.
Definition net_tls.hpp:37
size_t client_cert_len
Definition net_tls.hpp:42
const unsigned char * client_key
Definition net_tls.hpp:43
const unsigned char * ca_cert
Definition net_tls.hpp:38
size_t client_key_len
Definition net_tls.hpp:44
const char * hostname
Definition net_tls.hpp:40
const unsigned char * client_cert
Definition net_tls.hpp:41
size_t ca_cert_len
Definition net_tls.hpp:39
Common type definitions and concepts for the C++ wrapper layer.