GeezerVison is showing github.com — src: use smart pointers for NodeBIO · nodejs/node@d85b0a3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -483,7 +483,7 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) { | 483 | 483 |
| 484 | 484 | // Takes a string or buffer and loads it into a BIO. | 485 | 485 | // Caller responsible for BIO_free_all-ing the returned object. | 486 | | -static BIO* LoadBIO(Environment* env, Local<Value> v) { | | 486 | +static BIOPointer LoadBIO(Environment* env, Local<Value> v) { | 487 | 487 | HandleScope scope(env->isolate()); | 488 | 488 |
| 489 | 489 | if (v->IsString()) { | @@ -738,9 +738,12 @@ static X509_STORE* NewRootCertStore() { | 738 | 738 |
| 739 | 739 | if (root_certs_vector.empty()) { | 740 | 740 | for (size_t i = 0; i < arraysize(root_certs); i++) { | 741 | | - BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i])); | 742 | | - X509* x509 = PEM_read_bio_X509(bp, nullptr, NoPasswordCallback, nullptr); | 743 | | - BIO_free(bp); | | 741 | + X509* x509 = | | 742 | + PEM_read_bio_X509(NodeBIO::NewFixed(root_certs[i], | | 743 | + strlen(root_certs[i])).get(), | | 744 | + nullptr, // no re-use of X509 structure | | 745 | + NoPasswordCallback, | | 746 | + nullptr); // no callback data | 744 | 747 |
| 745 | 748 | // Parse errors from the built-in roots are fatal. | 746 | 749 | CHECK_NOT_NULL(x509); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto_bio.cc +11-15Lines changed: 11 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,36 +38,32 @@ namespace crypto { | 38 | 38 | #endif | 39 | 39 |
| 40 | 40 |
| 41 | | -BIO* NodeBIO::New() { | | 41 | +BIOPointer NodeBIO::New(Environment* env) { | 42 | 42 | // The const_cast doesn't violate const correctness. OpenSSL's usage of | 43 | 43 | // BIO_METHOD is effectively const but BIO_new() takes a non-const argument. | 44 | | - return BIO_new(const_cast<BIO_METHOD*>(GetMethod())); | | 44 | + BIOPointer bio(BIO_new(const_cast<BIO_METHOD*>(GetMethod()))); | | 45 | + if (bio && env != nullptr) | | 46 | + NodeBIO::FromBIO(bio.get())->env_ = env; | | 47 | + return bio; | 45 | 48 | } | 46 | 49 |
| 47 | 50 |
| 48 | | -BIO* NodeBIO::NewFixed(const char* data, size_t len) { | 49 | | - BIO* bio = New(); | | 51 | +BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) { | | 52 | + BIOPointer bio = New(env); | 50 | 53 |
| 51 | | - if (bio == nullptr || | | 54 | + if (!bio || | 52 | 55 | len > INT_MAX || | 53 | | - BIO_write(bio, data, len) != static_cast<int>(len) || | 54 | | - BIO_set_mem_eof_return(bio, 0) != 1) { | 55 | | - BIO_free(bio); | 56 | | - return nullptr; | | 56 | + BIO_write(bio.get(), data, len) != static_cast<int>(len) || | | 57 | + BIO_set_mem_eof_return(bio.get(), 0) != 1) { | | 58 | + return BIOPointer(); | 57 | 59 | } | 58 | 60 |
| 59 | 61 | return bio; | 60 | 62 | } | 61 | 63 |
| 62 | 64 |
| 63 | | -void NodeBIO::AssignEnvironment(Environment* env) { | 64 | | - env_ = env; | 65 | | -} | 66 | | - | 67 | | - | 68 | 65 | int NodeBIO::New(BIO* bio) { | 69 | 66 | BIO_set_data(bio, new NodeBIO()); | 70 | | - | 71 | 67 | BIO_set_init(bio, 1); | 72 | 68 |
| 73 | 69 | return 1; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto_bio.h +15-18Lines changed: 15 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 |
| 25 | 25 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | 26 | 26 |
| | 27 | +#include "node_crypto.h" | 27 | 28 | #include "openssl/bio.h" | 28 | 29 | #include "env-inl.h" | 29 | 30 | #include "util-inl.h" | | 32 | 33 | namespace node { | 33 | 34 | namespace crypto { | 34 | 35 |
| | 36 | +// This class represents buffers for OpenSSL I/O, implemented as a singly-linked | | 37 | +// list of chunks. It can be used both for writing data from Node to OpenSSL | | 38 | +// and back, but only one direction per instance. | | 39 | +// The structure is only accessed, and owned by, the OpenSSL BIOPointer | | 40 | +// (a.k.a. std::unique_ptr<BIO>). | 35 | 41 | class NodeBIO : public MemoryRetainer { | 36 | 42 | public: | 37 | | - NodeBIO() : env_(nullptr), | 38 | | - initial_(kInitialBufferLength), | 39 | | - length_(0), | 40 | | - eof_return_(-1), | 41 | | - read_head_(nullptr), | 42 | | - write_head_(nullptr) { | 43 | | - } | 44 | | - | 45 | 43 | ~NodeBIO(); | 46 | 44 |
| 47 | | - static BIO* New(); | | 45 | + static BIOPointer New(Environment* env = nullptr); | 48 | 46 |
| 49 | 47 | // NewFixed takes a copy of `len` bytes from `data` and returns a BIO that, | 50 | 48 | // when read from, returns those bytes followed by EOF. | 51 | | - static BIO* NewFixed(const char* data, size_t len); | 52 | | - | 53 | | - void AssignEnvironment(Environment* env); | | 49 | + static BIOPointer NewFixed(const char* data, size_t len, | | 50 | + Environment* env = nullptr); | 54 | 51 |
| 55 | 52 | // Move read head to next buffer if needed | 56 | 53 | void TryMoveReadHead(); | @@ -161,12 +158,12 @@ class NodeBIO : public MemoryRetainer { | 161 | 158 | char* data_; | 162 | 159 | }; | 163 | 160 |
| 164 | | - Environment* env_; | 165 | | - size_t initial_; | 166 | | - size_t length_; | 167 | | - int eof_return_; | 168 | | - Buffer* read_head_; | 169 | | - Buffer* write_head_; | | 161 | + Environment* env_ = nullptr; | | 162 | + size_t initial_ = kInitialBufferLength; | | 163 | + size_t length_ = 0; | | 164 | + int eof_return_ = -1; | | 165 | + Buffer* read_head_ = nullptr; | | 166 | + Buffer* write_head_ = nullptr; | 170 | 167 | }; | 171 | 168 |
| 172 | 169 | } // namespace crypto | |
Collapse file Copy file name to clipboardExpand all lines: src/tls_wrap.cc +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -112,11 +112,9 @@ void TLSWrap::NewSessionDoneCb() { | 112 | 112 |
| 113 | 113 |
| 114 | 114 | void TLSWrap::InitSSL() { | 115 | | - // Initialize SSL | 116 | | - enc_in_ = crypto::NodeBIO::New(); | 117 | | - enc_out_ = crypto::NodeBIO::New(); | 118 | | - crypto::NodeBIO::FromBIO(enc_in_)->AssignEnvironment(env()); | 119 | | - crypto::NodeBIO::FromBIO(enc_out_)->AssignEnvironment(env()); | | 115 | + // Initialize SSL – OpenSSL takes ownership of these. | | 116 | + enc_in_ = crypto::NodeBIO::New(env()).release(); | | 117 | + enc_out_ = crypto::NodeBIO::New(env()).release(); | 120 | 118 |
| 121 | 119 | SSL_set_bio(ssl_.get(), enc_in_, enc_out_); | 122 | 120 |
| |
|