Skip to content

Commit

Permalink
tls_wrap: embed TLS encryption into streamwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jun 16, 2013
1 parent 4c48a39 commit 03e008d
Show file tree
Hide file tree
Showing 6 changed files with 1,375 additions and 22 deletions.
7 changes: 6 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@
'conditions': [
[ 'node_use_openssl=="true"', {
'defines': [ 'HAVE_OPENSSL=1' ],
'sources': [ 'src/node_crypto.cc', 'src/node_crypto_bio.cc' ],
'sources': [
'src/node_crypto.cc',
'src/node_crypto_bio.cc',
'src/tls_wrap.cc',
'src/tls_wrap.h'
],
'conditions': [
[ 'node_shared_openssl=="false"', {
'dependencies': [ './deps/openssl/openssl.gyp:openssl' ],
Expand Down
29 changes: 29 additions & 0 deletions src/node_crypto_bio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ int NodeBIO::Read(BIO* bio, char* out, int len) {
}


char* NodeBIO::Peek(size_t* size) {
*size = read_head_->write_pos_ - read_head_->read_pos_;
return read_head_->data_ + read_head_->read_pos_;
}


int NodeBIO::Write(BIO* bio, const char* data, int len) {
BIO_clear_retry_flags(bio);

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


char* NodeBIO::PeekWritable(size_t* size) {
size_t available = kBufferLength - write_head_->write_pos_;
if (*size != 0 && available > *size)
available = *size;
else
*size = available;

return write_head_->data_ + write_head_->write_pos_;
}


void NodeBIO::Commit(size_t size) {
write_head_->write_pos_ += size;
length_ += size;
assert(write_head_->write_pos_ <= kBufferLength);

// Allocate new buffer if write head is full,
// and there're no other place to go
TryAllocateForWrite();
write_head_ = write_head_->next_;
}


void NodeBIO::TryAllocateForWrite() {
// If write head is full, next buffer is either read head or not empty.
if (write_head_->write_pos_ == kBufferLength &&
Expand Down
53 changes: 32 additions & 21 deletions src/node_crypto_bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class NodeBIO {
return &method_;
}

NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
// Loop head
head_.next_ = &head_;
}

~NodeBIO();

static int New(BIO* bio);
static int Free(BIO* bio);
static int Read(BIO* bio, char* out, int len);
Expand All @@ -38,27 +45,6 @@ class NodeBIO {
static int Gets(BIO* bio, char* out, int size);
static long Ctrl(BIO* bio, int cmd, long num, void* ptr);

protected:
static const size_t kBufferLength = 16 * 1024;

class Buffer {
public:
Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
}

size_t read_pos_;
size_t write_pos_;
Buffer* next_;
char data_[kBufferLength];
};

NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
// Loop head
head_.next_ = &head_;
}

~NodeBIO();

// Allocate new buffer for write if needed
void TryAllocateForWrite();

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

// Return pointer to internal data and amount of
// contiguous data available to read
char* Peek(size_t* size);

// Find first appearance of `delim` in buffer or `limit` if `delim`
// wasn't found.
size_t IndexOf(char delim, size_t limit);
Expand All @@ -79,6 +69,13 @@ class NodeBIO {
// Put `len` bytes from `data` into buffer
void Write(const char* data, size_t size);

// Return pointer to internal data and amount of
// contiguous data available for future writes
char* PeekWritable(size_t* size);

// Commit reserved data
void Commit(size_t size);

// Return size of buffer in bytes
size_t inline Length() {
return length_;
Expand All @@ -89,6 +86,20 @@ class NodeBIO {
return static_cast<NodeBIO*>(bio->ptr);
}

protected:
static const size_t kBufferLength = 16 * 1024;

class Buffer {
public:
Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
}

size_t read_pos_;
size_t write_pos_;
Buffer* next_;
char data_[kBufferLength];
};

size_t length_;
Buffer head_;
Buffer* read_head_;
Expand Down
1 change: 1 addition & 0 deletions src/node_extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NODE_EXT_LIST_ITEM(node_zlib)
// libuv rewrite
NODE_EXT_LIST_ITEM(node_timer_wrap)
NODE_EXT_LIST_ITEM(node_tcp_wrap)
NODE_EXT_LIST_ITEM(node_tls_wrap)
NODE_EXT_LIST_ITEM(node_udp_wrap)
NODE_EXT_LIST_ITEM(node_pipe_wrap)
NODE_EXT_LIST_ITEM(node_cares_wrap)
Expand Down
Loading

0 comments on commit 03e008d

Please sign in to comment.