Skip to content

Commit 03e008d

Browse files
committed
tls_wrap: embed TLS encryption into streamwrap
1 parent 4c48a39 commit 03e008d

File tree

6 files changed

+1375
-22
lines changed

6 files changed

+1375
-22
lines changed

node.gyp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@
159159
'conditions': [
160160
[ 'node_use_openssl=="true"', {
161161
'defines': [ 'HAVE_OPENSSL=1' ],
162-
'sources': [ 'src/node_crypto.cc', 'src/node_crypto_bio.cc' ],
162+
'sources': [
163+
'src/node_crypto.cc',
164+
'src/node_crypto_bio.cc',
165+
'src/tls_wrap.cc',
166+
'src/tls_wrap.h'
167+
],
163168
'conditions': [
164169
[ 'node_shared_openssl=="false"', {
165170
'dependencies': [ './deps/openssl/openssl.gyp:openssl' ],

src/node_crypto_bio.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ int NodeBIO::Read(BIO* bio, char* out, int len) {
8282
}
8383

8484

85+
char* NodeBIO::Peek(size_t* size) {
86+
*size = read_head_->write_pos_ - read_head_->read_pos_;
87+
return read_head_->data_ + read_head_->read_pos_;
88+
}
89+
90+
8591
int NodeBIO::Write(BIO* bio, const char* data, int len) {
8692
BIO_clear_retry_flags(bio);
8793

@@ -318,6 +324,29 @@ void NodeBIO::Write(const char* data, size_t size) {
318324
}
319325

320326

327+
char* NodeBIO::PeekWritable(size_t* size) {
328+
size_t available = kBufferLength - write_head_->write_pos_;
329+
if (*size != 0 && available > *size)
330+
available = *size;
331+
else
332+
*size = available;
333+
334+
return write_head_->data_ + write_head_->write_pos_;
335+
}
336+
337+
338+
void NodeBIO::Commit(size_t size) {
339+
write_head_->write_pos_ += size;
340+
length_ += size;
341+
assert(write_head_->write_pos_ <= kBufferLength);
342+
343+
// Allocate new buffer if write head is full,
344+
// and there're no other place to go
345+
TryAllocateForWrite();
346+
write_head_ = write_head_->next_;
347+
}
348+
349+
321350
void NodeBIO::TryAllocateForWrite() {
322351
// If write head is full, next buffer is either read head or not empty.
323352
if (write_head_->write_pos_ == kBufferLength &&

src/node_crypto_bio.h

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class NodeBIO {
3030
return &method_;
3131
}
3232

33+
NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
34+
// Loop head
35+
head_.next_ = &head_;
36+
}
37+
38+
~NodeBIO();
39+
3340
static int New(BIO* bio);
3441
static int Free(BIO* bio);
3542
static int Read(BIO* bio, char* out, int len);
@@ -38,27 +45,6 @@ class NodeBIO {
3845
static int Gets(BIO* bio, char* out, int size);
3946
static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
4047

41-
protected:
42-
static const size_t kBufferLength = 16 * 1024;
43-
44-
class Buffer {
45-
public:
46-
Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
47-
}
48-
49-
size_t read_pos_;
50-
size_t write_pos_;
51-
Buffer* next_;
52-
char data_[kBufferLength];
53-
};
54-
55-
NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
56-
// Loop head
57-
head_.next_ = &head_;
58-
}
59-
60-
~NodeBIO();
61-
6248
// Allocate new buffer for write if needed
6349
void TryAllocateForWrite();
6450

@@ -69,6 +55,10 @@ class NodeBIO {
6955
// Deallocate children of write head's child if they're empty
7056
void FreeEmpty();
7157

58+
// Return pointer to internal data and amount of
59+
// contiguous data available to read
60+
char* Peek(size_t* size);
61+
7262
// Find first appearance of `delim` in buffer or `limit` if `delim`
7363
// wasn't found.
7464
size_t IndexOf(char delim, size_t limit);
@@ -79,6 +69,13 @@ class NodeBIO {
7969
// Put `len` bytes from `data` into buffer
8070
void Write(const char* data, size_t size);
8171

72+
// Return pointer to internal data and amount of
73+
// contiguous data available for future writes
74+
char* PeekWritable(size_t* size);
75+
76+
// Commit reserved data
77+
void Commit(size_t size);
78+
8279
// Return size of buffer in bytes
8380
size_t inline Length() {
8481
return length_;
@@ -89,6 +86,20 @@ class NodeBIO {
8986
return static_cast<NodeBIO*>(bio->ptr);
9087
}
9188

89+
protected:
90+
static const size_t kBufferLength = 16 * 1024;
91+
92+
class Buffer {
93+
public:
94+
Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
95+
}
96+
97+
size_t read_pos_;
98+
size_t write_pos_;
99+
Buffer* next_;
100+
char data_[kBufferLength];
101+
};
102+
92103
size_t length_;
93104
Buffer head_;
94105
Buffer* read_head_;

src/node_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ NODE_EXT_LIST_ITEM(node_zlib)
3434
// libuv rewrite
3535
NODE_EXT_LIST_ITEM(node_timer_wrap)
3636
NODE_EXT_LIST_ITEM(node_tcp_wrap)
37+
NODE_EXT_LIST_ITEM(node_tls_wrap)
3738
NODE_EXT_LIST_ITEM(node_udp_wrap)
3839
NODE_EXT_LIST_ITEM(node_pipe_wrap)
3940
NODE_EXT_LIST_ITEM(node_cares_wrap)

0 commit comments

Comments
 (0)