Skip to content

Commit 653f62a

Browse files
committed
crypto: remove NodeBIO::GetMethod()
Remove NodeBIO::GetMethod() and replace calls to BIO_new() with calls to the new NodeBIO::New() function. This commit basically reshuffles some code in order to make it explicit that the NodeBIO BIO_METHOD is const.
1 parent 8e596c4 commit 653f62a

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

src/node_crypto.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
321321
// Takes a string or buffer and loads it into a BIO.
322322
// Caller responsible for BIO_free_all-ing the returned object.
323323
static BIO* LoadBIO(Handle<Value> v) {
324-
BIO *bio = BIO_new(NodeBIO::GetMethod());
324+
BIO* bio = NodeBIO::New();
325325
if (!bio) return NULL;
326326

327327
HandleScope scope(node_isolate);
@@ -564,7 +564,7 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
564564
root_cert_store = X509_STORE_new();
565565

566566
for (int i = 0; root_certs[i]; i++) {
567-
BIO *bp = BIO_new(NodeBIO::GetMethod());
567+
BIO* bp = NodeBIO::New();
568568

569569
if (!BIO_write(bp, root_certs[i], strlen(root_certs[i]))) {
570570
BIO_free_all(bp);
@@ -1660,8 +1660,8 @@ void Connection::New(const FunctionCallbackInfo<Value>& args) {
16601660
conn->Wrap(args.This());
16611661

16621662
conn->ssl_ = SSL_new(sc->ctx_);
1663-
conn->bio_read_ = BIO_new(NodeBIO::GetMethod());
1664-
conn->bio_write_ = BIO_new(NodeBIO::GetMethod());
1663+
conn->bio_read_ = NodeBIO::New();
1664+
conn->bio_write_ = NodeBIO::New();
16651665

16661666
SSL_set_app_data(conn->ssl_, conn);
16671667

src/node_crypto_bio.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace node {
2727

28-
BIO_METHOD NodeBIO::method_ = {
28+
const BIO_METHOD NodeBIO::method = {
2929
BIO_TYPE_MEM,
3030
"node.js SSL buffer",
3131
NodeBIO::Write,
@@ -39,6 +39,13 @@ BIO_METHOD NodeBIO::method_ = {
3939
};
4040

4141

42+
BIO* NodeBIO::New() {
43+
// The const_cast doesn't violate const correctness. OpenSSL's usage of
44+
// BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
45+
return BIO_new(const_cast<BIO_METHOD*>(&method));
46+
}
47+
48+
4249
int NodeBIO::New(BIO* bio) {
4350
bio->ptr = new NodeBIO();
4451

src/node_crypto_bio.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,14 @@ namespace node {
2929

3030
class NodeBIO {
3131
public:
32-
static inline BIO_METHOD* GetMethod() {
33-
return &method_;
34-
}
35-
3632
NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
3733
// Loop head
3834
head_.next_ = &head_;
3935
}
4036

4137
~NodeBIO();
4238

43-
static int New(BIO* bio);
44-
static int Free(BIO* bio);
45-
static int Read(BIO* bio, char* out, int len);
46-
static int Write(BIO* bio, const char* data, int len);
47-
static int Puts(BIO* bio, const char* str);
48-
static int Gets(BIO* bio, char* out, int size);
49-
static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
39+
static BIO* New();
5040

5141
// Allocate new buffer for write if needed
5242
void TryAllocateForWrite();
@@ -89,10 +79,19 @@ class NodeBIO {
8979
return static_cast<NodeBIO*>(bio->ptr);
9080
}
9181

92-
protected:
82+
private:
83+
static int New(BIO* bio);
84+
static int Free(BIO* bio);
85+
static int Read(BIO* bio, char* out, int len);
86+
static int Write(BIO* bio, const char* data, int len);
87+
static int Puts(BIO* bio, const char* str);
88+
static int Gets(BIO* bio, char* out, int size);
89+
static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
90+
9391
// NOTE: Size is maximum TLS frame length, this is required if we want
9492
// to fit whole ClientHello into one Buffer of NodeBIO.
9593
static const size_t kBufferLength = 16 * 1024 + 5;
94+
static const BIO_METHOD method;
9695

9796
class Buffer {
9897
public:
@@ -109,8 +108,6 @@ class NodeBIO {
109108
Buffer head_;
110109
Buffer* read_head_;
111110
Buffer* write_head_;
112-
113-
static BIO_METHOD method_;
114111
};
115112

116113
} // namespace node

src/tls_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ void TLSCallbacks::InvokeQueued(int status) {
144144

145145
void TLSCallbacks::InitSSL() {
146146
// Initialize SSL
147-
enc_in_ = BIO_new(NodeBIO::GetMethod());
148-
enc_out_ = BIO_new(NodeBIO::GetMethod());
147+
enc_in_ = NodeBIO::New();
148+
enc_out_ = NodeBIO::New();
149149

150150
SSL_set_bio(ssl_, enc_in_, enc_out_);
151151

0 commit comments

Comments
 (0)