Skip to content

Commit 9bc7530

Browse files
jasnellnpaun
authored andcommitted
src: move more crypto code to ncrypto
PR-URL: nodejs/node#54320 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 7bcce39 commit 9bc7530

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

ncrypto.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,4 +963,57 @@ X509Pointer X509Pointer::IssuerFrom(const SSL_CTX* ctx, const X509View& cert) {
963963
X509Pointer X509Pointer::PeerFrom(const SSLPointer& ssl) {
964964
return X509Pointer(SSL_get_peer_certificate(ssl.get()));
965965
}
966+
// ============================================================================
967+
// BIOPointer
968+
969+
BIOPointer::BIOPointer(BIO* bio) : bio_(bio) {}
970+
971+
BIOPointer::BIOPointer(BIOPointer&& other) noexcept : bio_(other.release()) {}
972+
973+
BIOPointer& BIOPointer::operator=(BIOPointer&& other) noexcept {
974+
if (this == &other) return *this;
975+
this->~BIOPointer();
976+
return *new (this) BIOPointer(std::move(other));
977+
}
978+
979+
BIOPointer::~BIOPointer() { reset(); }
980+
981+
void BIOPointer::reset(BIO* bio) { bio_.reset(bio); }
982+
983+
BIO* BIOPointer::release() { return bio_.release(); }
984+
985+
bool BIOPointer::resetBio() const {
986+
if (!bio_) return 0;
987+
return BIO_reset(bio_.get()) == 1;
988+
}
989+
990+
BIOPointer BIOPointer::NewMem() {
991+
return BIOPointer(BIO_new(BIO_s_mem()));
992+
}
993+
994+
BIOPointer BIOPointer::NewSecMem() {
995+
return BIOPointer(BIO_new(BIO_s_secmem()));
996+
}
997+
998+
BIOPointer BIOPointer::New(const BIO_METHOD* method) {
999+
return BIOPointer(BIO_new(method));
1000+
}
1001+
1002+
BIOPointer BIOPointer::New(const void* data, size_t len) {
1003+
return BIOPointer(BIO_new_mem_buf(data, len));
1004+
}
1005+
1006+
BIOPointer BIOPointer::NewFile(std::string_view filename, std::string_view mode) {
1007+
return BIOPointer(BIO_new_file(filename.data(), mode.data()));
1008+
}
1009+
1010+
BIOPointer BIOPointer::NewFp(FILE* fd, int close_flag) {
1011+
return BIOPointer(BIO_new_fp(fd, close_flag));
1012+
}
1013+
1014+
int BIOPointer::Write(BIOPointer* bio, std::string_view message) {
1015+
if (bio == nullptr || !*bio) return 0;
1016+
return BIO_write(bio->get(), message.data(), message.size());
1017+
}
1018+
9661019
} // namespace ncrypto

ncrypto.h

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <optional>
77
#include <string>
88
#include <string_view>
9+
#include <openssl/bio.h>
910
#include <openssl/bn.h>
10-
#include <openssl/x509.h>
1111
#include <openssl/dh.h>
1212
#include <openssl/dsa.h>
1313
#include <openssl/ec.h>
@@ -17,6 +17,7 @@
1717
#include <openssl/kdf.h>
1818
#include <openssl/rsa.h>
1919
#include <openssl/ssl.h>
20+
#include <openssl/x509.h>
2021
#ifndef OPENSSL_NO_ENGINE
2122
# include <openssl/engine.h>
2223
#endif // !OPENSSL_NO_ENGINE
@@ -192,7 +193,6 @@ template <typename T, void (*function)(T*)>
192193
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
193194

194195
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
195-
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
196196
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
197197
using DHPointer = DeleteFnPtr<DH, DH_free>;
198198
using DSAPointer = DeleteFnPtr<DSA, DSA_free>;
@@ -265,6 +265,53 @@ class DataPointer final {
265265
size_t len_ = 0;
266266
};
267267

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+
268315
class BignumPointer final {
269316
public:
270317
BignumPointer() = default;

0 commit comments

Comments
 (0)