|
6 | 6 | #include <optional> |
7 | 7 | #include <string> |
8 | 8 | #include <string_view> |
| 9 | +#include <openssl/bio.h> |
9 | 10 | #include <openssl/bn.h> |
10 | | -#include <openssl/x509.h> |
11 | 11 | #include <openssl/dh.h> |
12 | 12 | #include <openssl/dsa.h> |
13 | 13 | #include <openssl/ec.h> |
|
17 | 17 | #include <openssl/kdf.h> |
18 | 18 | #include <openssl/rsa.h> |
19 | 19 | #include <openssl/ssl.h> |
| 20 | +#include <openssl/x509.h> |
20 | 21 | #ifndef OPENSSL_NO_ENGINE |
21 | 22 | # include <openssl/engine.h> |
22 | 23 | #endif // !OPENSSL_NO_ENGINE |
@@ -192,7 +193,6 @@ template <typename T, void (*function)(T*)> |
192 | 193 | using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; |
193 | 194 |
|
194 | 195 | using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>; |
195 | | -using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>; |
196 | 196 | using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>; |
197 | 197 | using DHPointer = DeleteFnPtr<DH, DH_free>; |
198 | 198 | using DSAPointer = DeleteFnPtr<DSA, DSA_free>; |
@@ -265,6 +265,53 @@ class DataPointer final { |
265 | 265 | size_t len_ = 0; |
266 | 266 | }; |
267 | 267 |
|
| 268 | +class BIOPointer final { |
| 269 | +public: |
| 270 | + static BIOPointer NewMem(); |
| 271 | + static BIOPointer NewSecMem(); |
| 272 | + static BIOPointer New(const BIO_METHOD* method); |
| 273 | + static BIOPointer New(const void* data, size_t len); |
| 274 | + static BIOPointer NewFile(std::string_view filename, std::string_view mode); |
| 275 | + static BIOPointer NewFp(FILE* fd, int flags); |
| 276 | + |
| 277 | + BIOPointer() = default; |
| 278 | + BIOPointer(std::nullptr_t) : bio_(nullptr) {} |
| 279 | + explicit BIOPointer(BIO* bio); |
| 280 | + BIOPointer(BIOPointer&& other) noexcept; |
| 281 | + BIOPointer& operator=(BIOPointer&& other) noexcept; |
| 282 | + NCRYPTO_DISALLOW_COPY(BIOPointer) |
| 283 | + ~BIOPointer(); |
| 284 | + |
| 285 | + inline bool operator==(std::nullptr_t) noexcept { return bio_ == nullptr; } |
| 286 | + inline operator bool() const { return bio_ != nullptr; } |
| 287 | + inline BIO* get() const noexcept { return bio_.get(); } |
| 288 | + |
| 289 | + inline operator BUF_MEM*() const { |
| 290 | + BUF_MEM* mem = nullptr; |
| 291 | + if (!bio_) return mem; |
| 292 | + BIO_get_mem_ptr(bio_.get(), &mem); |
| 293 | + return mem; |
| 294 | + } |
| 295 | + |
| 296 | + inline operator BIO*() const { return bio_.get(); } |
| 297 | + |
| 298 | + void reset(BIO* bio = nullptr); |
| 299 | + BIO* release(); |
| 300 | + |
| 301 | + bool resetBio() const; |
| 302 | + |
| 303 | + static int Write(BIOPointer* bio, std::string_view message); |
| 304 | + |
| 305 | + template <typename...Args> |
| 306 | + static void Printf(BIOPointer* bio, const char* format, Args...args) { |
| 307 | + if (bio == nullptr || !*bio) return; |
| 308 | + BIO_printf(bio->get(), format, std::forward<Args...>(args...)); |
| 309 | + } |
| 310 | + |
| 311 | +private: |
| 312 | + mutable DeleteFnPtr<BIO, BIO_free_all> bio_; |
| 313 | +}; |
| 314 | + |
268 | 315 | class BignumPointer final { |
269 | 316 | public: |
270 | 317 | BignumPointer() = default; |
|
0 commit comments