1 |
|
|
// Copyright Joyent, Inc. and other Node contributors. |
2 |
|
|
// |
3 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a |
4 |
|
|
// copy of this software and associated documentation files (the |
5 |
|
|
// "Software"), to deal in the Software without restriction, including |
6 |
|
|
// without limitation the rights to use, copy, modify, merge, publish, |
7 |
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit |
8 |
|
|
// persons to whom the Software is furnished to do so, subject to the |
9 |
|
|
// following conditions: |
10 |
|
|
// |
11 |
|
|
// The above copyright notice and this permission notice shall be included |
12 |
|
|
// in all copies or substantial portions of the Software. |
13 |
|
|
// |
14 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 |
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
16 |
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
17 |
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
18 |
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
19 |
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 |
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 |
|
|
|
22 |
|
|
#ifndef SRC_CRYPTO_CRYPTO_BIO_H_ |
23 |
|
|
#define SRC_CRYPTO_CRYPTO_BIO_H_ |
24 |
|
|
|
25 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
26 |
|
|
|
27 |
|
|
#include "node_crypto.h" |
28 |
|
|
#include "openssl/bio.h" |
29 |
|
|
#include "util.h" |
30 |
|
|
#include "v8.h" |
31 |
|
|
|
32 |
|
|
namespace node { |
33 |
|
|
|
34 |
|
|
class Environment; |
35 |
|
|
|
36 |
|
|
namespace crypto { |
37 |
|
|
// This class represents buffers for OpenSSL I/O, implemented as a singly-linked |
38 |
|
|
// list of chunks. It can be used either for writing data from Node to OpenSSL, |
39 |
|
|
// or for reading data back, but not both. |
40 |
|
|
// The structure is only accessed, and owned by, the OpenSSL BIOPointer |
41 |
|
|
// (a.k.a. std::unique_ptr<BIO>). |
42 |
|
|
class NodeBIO : public MemoryRetainer { |
43 |
|
|
public: |
44 |
|
|
~NodeBIO() override; |
45 |
|
|
|
46 |
|
|
static BIOPointer New(Environment* env = nullptr); |
47 |
|
|
|
48 |
|
|
// NewFixed takes a copy of `len` bytes from `data` and returns a BIO that, |
49 |
|
|
// when read from, returns those bytes followed by EOF. |
50 |
|
|
static BIOPointer NewFixed(const char* data, size_t len, |
51 |
|
|
Environment* env = nullptr); |
52 |
|
|
|
53 |
|
|
// Move read head to next buffer if needed |
54 |
|
|
void TryMoveReadHead(); |
55 |
|
|
|
56 |
|
|
// Allocate new buffer for write if needed |
57 |
|
|
void TryAllocateForWrite(size_t hint); |
58 |
|
|
|
59 |
|
|
// Read `len` bytes maximum into `out`, return actual number of read bytes |
60 |
|
|
size_t Read(char* out, size_t size); |
61 |
|
|
|
62 |
|
|
// Memory optimization: |
63 |
|
|
// Deallocate children of write head's child if they're empty |
64 |
|
|
void FreeEmpty(); |
65 |
|
|
|
66 |
|
|
// Return pointer to internal data and amount of |
67 |
|
|
// contiguous data available to read |
68 |
|
|
char* Peek(size_t* size); |
69 |
|
|
|
70 |
|
|
// Return pointers and sizes of multiple internal data chunks available for |
71 |
|
|
// reading |
72 |
|
|
size_t PeekMultiple(char** out, size_t* size, size_t* count); |
73 |
|
|
|
74 |
|
|
// Find first appearance of `delim` in buffer or `limit` if `delim` |
75 |
|
|
// wasn't found. |
76 |
|
|
size_t IndexOf(char delim, size_t limit); |
77 |
|
|
|
78 |
|
|
// Discard all available data |
79 |
|
|
void Reset(); |
80 |
|
|
|
81 |
|
|
// Put `len` bytes from `data` into buffer |
82 |
|
|
void Write(const char* data, size_t size); |
83 |
|
|
|
84 |
|
|
// Return pointer to contiguous block of reserved data and the size available |
85 |
|
|
// for future writes. Call Commit() once the write is complete. |
86 |
|
|
char* PeekWritable(size_t* size); |
87 |
|
|
|
88 |
|
|
// Specify how much data was written into the block returned by |
89 |
|
|
// PeekWritable(). |
90 |
|
|
void Commit(size_t size); |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
// Return size of buffer in bytes |
94 |
|
3757036 |
inline size_t Length() const { |
95 |
|
3757036 |
return length_; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
// Provide a hint about the size of the next pending set of writes. TLS |
99 |
|
|
// writes records of a maximum length of 16k of data plus a 5-byte header, |
100 |
|
|
// a MAC (up to 20 bytes for SSLv3, TLS 1.0, TLS 1.1, and up to 32 bytes |
101 |
|
|
// for TLS 1.2), and padding if a block cipher is used. If there is a |
102 |
|
|
// large write this will result in potentially many buffers being |
103 |
|
|
// allocated and gc'ed which can cause long pauses. By providing a |
104 |
|
|
// guess about the amount of buffer space that will be needed in the |
105 |
|
|
// next allocation this overhead is removed. |
106 |
|
3022 |
inline void set_allocate_tls_hint(size_t size) { |
107 |
|
3022 |
constexpr size_t kThreshold = 16 * 1024; |
108 |
✓✓ |
3022 |
if (size >= kThreshold) { |
109 |
|
885 |
allocate_hint_ = (size / kThreshold + 1) * (kThreshold + 5 + 32); |
110 |
|
|
} |
111 |
|
3022 |
} |
112 |
|
|
|
113 |
|
38197 |
inline void set_eof_return(int num) { |
114 |
|
38197 |
eof_return_ = num; |
115 |
|
38197 |
} |
116 |
|
|
|
117 |
|
7779 |
inline int eof_return() { |
118 |
|
7779 |
return eof_return_; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
11468 |
inline void set_initial(size_t initial) { |
122 |
|
11468 |
initial_ = initial; |
123 |
|
11468 |
} |
124 |
|
|
|
125 |
|
|
static NodeBIO* FromBIO(BIO* bio); |
126 |
|
|
|
127 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override { |
128 |
|
|
tracker->TrackFieldWithSize("buffer", length_, "NodeBIO::Buffer"); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
SET_MEMORY_INFO_NAME(NodeBIO) |
132 |
|
|
SET_SELF_SIZE(NodeBIO) |
133 |
|
|
|
134 |
|
|
private: |
135 |
|
|
static int New(BIO* bio); |
136 |
|
|
static int Free(BIO* bio); |
137 |
|
|
static int Read(BIO* bio, char* out, int len); |
138 |
|
|
static int Write(BIO* bio, const char* data, int len); |
139 |
|
|
static int Puts(BIO* bio, const char* str); |
140 |
|
|
static int Gets(BIO* bio, char* out, int size); |
141 |
|
|
static long Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) |
142 |
|
|
void* ptr); |
143 |
|
|
|
144 |
|
|
static const BIO_METHOD* GetMethod(); |
145 |
|
|
|
146 |
|
|
// Enough to handle the most of the client hellos |
147 |
|
|
static const size_t kInitialBufferLength = 1024; |
148 |
|
|
static const size_t kThroughputBufferLength = 16384; |
149 |
|
|
|
150 |
|
|
class Buffer { |
151 |
|
|
public: |
152 |
|
43541 |
Buffer(Environment* env, size_t len) : env_(env), |
153 |
|
|
read_pos_(0), |
154 |
|
|
write_pos_(0), |
155 |
|
|
len_(len), |
156 |
|
43541 |
next_(nullptr) { |
157 |
|
43541 |
data_ = new char[len]; |
158 |
✓✓ |
43541 |
if (env_ != nullptr) |
159 |
|
5344 |
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len); |
160 |
|
43541 |
} |
161 |
|
|
|
162 |
|
87056 |
~Buffer() { |
163 |
✓✗ |
43528 |
delete[] data_; |
164 |
✓✓ |
43528 |
if (env_ != nullptr) { |
165 |
|
5331 |
const int64_t len = static_cast<int64_t>(len_); |
166 |
|
5331 |
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len); |
167 |
|
|
} |
168 |
|
43528 |
} |
169 |
|
|
|
170 |
|
|
Environment* env_; |
171 |
|
|
size_t read_pos_; |
172 |
|
|
size_t write_pos_; |
173 |
|
|
size_t len_; |
174 |
|
|
Buffer* next_; |
175 |
|
|
char* data_; |
176 |
|
|
}; |
177 |
|
|
|
178 |
|
|
Environment* env_ = nullptr; |
179 |
|
|
size_t initial_ = kInitialBufferLength; |
180 |
|
|
size_t length_ = 0; |
181 |
|
|
size_t allocate_hint_ = 0; |
182 |
|
|
int eof_return_ = -1; |
183 |
|
|
Buffer* read_head_ = nullptr; |
184 |
|
|
Buffer* write_head_ = nullptr; |
185 |
|
|
|
186 |
|
|
friend void node::crypto::InitCryptoOnce(); |
187 |
|
|
}; |
188 |
|
|
|
189 |
|
|
} // namespace crypto |
190 |
|
|
} // namespace node |
191 |
|
|
|
192 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
193 |
|
|
|
194 |
|
|
#endif // SRC_CRYPTO_CRYPTO_BIO_H_ |