| 1 |
#ifndef OPENSSL_HH |
| 2 |
#define OPENSSL_HH |
| 3 |
|
| 4 |
#include "zstring_view.hh" |
| 5 |
|
| 6 |
#include <cstddef> |
| 7 |
#include <cstdint> |
| 8 |
#include <expected> |
| 9 |
#include <memory> |
| 10 |
#include <optional> |
| 11 |
#include <span> |
| 12 |
#include <string> |
| 13 |
|
| 14 |
namespace openmsx::OpenSSL { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct Ssl; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
struct OpenSSLApi; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct ErrorCode { |
| 46 |
unsigned long value = 0; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
[[nodiscard]] std::string to_string(ErrorCode ec); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
enum class IoResult { |
| 57 |
WouldBlock, |
| 58 |
Closed |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
using IoError = std::expected<IoResult, ErrorCode>; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
using IoReturn = std::expected<size_t, IoError>; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
enum class HandshakeType { |
| 77 |
NeedRead = 0, |
| 78 |
Done = 1, |
| 79 |
NeedWrite = 2, |
| 80 |
}; |
| 81 |
using HandshakeResult = std::expected<HandshakeType, ErrorCode>; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct SessionHandle { |
| 87 |
SessionHandle(const SessionHandle&) = delete; |
| 88 |
SessionHandle& operator=(const SessionHandle&) = delete; |
| 89 |
SessionHandle(SessionHandle&& other) noexcept; |
| 90 |
SessionHandle& operator=(SessionHandle&& other) noexcept; |
| 91 |
~SessionHandle(); |
| 92 |
|
| 93 |
|
| 94 |
[[nodiscard]] HandshakeResult handshake() const; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
[[nodiscard]] int verifyResult() const; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
[[nodiscard]] std::string verifyErrorDescription() const; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
[[nodiscard]] IoReturn read(std::span<uint8_t> buf) const; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
[[nodiscard]] IoReturn write(std::span<const uint8_t> buf) const; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
[[nodiscard]] size_t pending() const; |
| 123 |
|
| 124 |
private: |
| 125 |
friend struct LibHandle; |
| 126 |
SessionHandle(Ssl* ssl_, const OpenSSLApi* api_); |
| 127 |
void release() noexcept; |
| 128 |
Ssl* ssl; |
| 129 |
const OpenSSLApi* api; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
struct LibHandle { |
| 135 |
|
| 136 |
[[nodiscard]] zstring_view version() const; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
[[nodiscard]] std::optional<SessionHandle> createClientSession( |
| 147 |
bool verify, zstring_view hostname, int fd) const; |
| 148 |
|
| 149 |
private: |
| 150 |
friend LibHandle* load(); |
| 151 |
friend std::string to_string(ErrorCode ec); |
| 152 |
LibHandle(); |
| 153 |
~LibHandle(); |
| 154 |
std::unique_ptr<OpenSSLApi> api; |
| 155 |
void* libHandle = nullptr; |
| 156 |
void* cryptoHandle = nullptr; |
| 157 |
bool loaded = false; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
LibHandle* load(); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
#endif |