Coverage Report

Created: 2025-12-10 07:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/node/src/crypto/crypto_bio.h
Line
Count
Source
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 ncrypto::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 ncrypto::BIOPointer NewFixed(const char* data,
51
                                      size_t len,
52
                                      Environment* env = nullptr);
53
54
  // Move read head to next buffer if needed
55
  void TryMoveReadHead();
56
57
  // Allocate new buffer for write if needed
58
  void TryAllocateForWrite(size_t hint);
59
60
  // Read `len` bytes maximum into `out`, return actual number of read bytes
61
  size_t Read(char* out, size_t size);
62
63
  // Memory optimization:
64
  // Deallocate children of write head's child if they're empty
65
  void FreeEmpty();
66
67
  // Return pointer to internal data and amount of
68
  // contiguous data available to read
69
  char* Peek(size_t* size);
70
71
  // Return pointers and sizes of multiple internal data chunks available for
72
  // reading
73
  size_t PeekMultiple(char** out, size_t* size, size_t* count);
74
75
  // Find first appearance of `delim` in buffer or `limit` if `delim`
76
  // wasn't found.
77
  size_t IndexOf(char delim, size_t limit);
78
79
  // Discard all available data
80
  void Reset();
81
82
  // Put `len` bytes from `data` into buffer
83
  void Write(const char* data, size_t size);
84
85
  // Return pointer to contiguous block of reserved data and the size available
86
  // for future writes. Call Commit() once the write is complete.
87
  char* PeekWritable(size_t* size);
88
89
  // Specify how much data was written into the block returned by
90
  // PeekWritable().
91
  void Commit(size_t size);
92
93
94
  // Return size of buffer in bytes
95
0
  inline size_t Length() const {
96
0
    return length_;
97
0
  }
98
99
  // Provide a hint about the size of the next pending set of writes. TLS
100
  // writes records of a maximum length of 16k of data plus a 5-byte header,
101
  // a MAC (up to 20 bytes for SSLv3, TLS 1.0, TLS 1.1, and up to 32 bytes
102
  // for TLS 1.2), and padding if a block cipher is used.  If there is a
103
  // large write this will result in potentially many buffers being
104
  // allocated and gc'ed which can cause long pauses. By providing a
105
  // guess about the amount of buffer space that will be needed in the
106
  // next allocation this overhead is removed.
107
0
  inline void set_allocate_tls_hint(size_t size) {
108
0
    constexpr size_t kThreshold = 16 * 1024;
109
0
    if (size >= kThreshold) {
110
0
      allocate_hint_ = (size / kThreshold + 1) * (kThreshold + 5 + 32);
111
0
    }
112
0
  }
113
114
0
  inline void set_eof_return(int num) {
115
0
    eof_return_ = num;
116
0
  }
117
118
0
  inline int eof_return() {
119
0
    return eof_return_;
120
0
  }
121
122
0
  inline void set_initial(size_t initial) {
123
0
    initial_ = initial;
124
0
  }
125
126
  static NodeBIO* FromBIO(BIO* bio);
127
128
0
  void MemoryInfo(MemoryTracker* tracker) const override {
129
0
    tracker->TrackFieldWithSize("buffer", length_, "NodeBIO::Buffer");
130
0
  }
131
132
  SET_MEMORY_INFO_NAME(NodeBIO)
133
  SET_SELF_SIZE(NodeBIO)
134
135
 private:
136
  static int New(BIO* bio);
137
  static int Free(BIO* bio);
138
  static int Read(BIO* bio, char* out, int len);
139
  static int Write(BIO* bio, const char* data, int len);
140
  static int Puts(BIO* bio, const char* str);
141
  static int Gets(BIO* bio, char* out, int size);
142
  static long Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
143
                   void* ptr);
144
145
  static const BIO_METHOD* GetMethod();
146
147
  // Enough to handle the most of the client hellos
148
  static const size_t kInitialBufferLength = 1024;
149
  static const size_t kThroughputBufferLength = 16384;
150
151
  class Buffer {
152
   public:
153
0
    Buffer(Environment* env, size_t len) : env_(env),
154
0
                                           read_pos_(0),
155
0
                                           write_pos_(0),
156
0
                                           len_(len),
157
0
                                           next_(nullptr) {
158
0
      data_ = new char[len];
159
0
      if (env_ != nullptr) {
160
0
        env_->external_memory_accounter()->Increase(env_->isolate(), len);
161
0
      }
162
0
    }
163
164
0
    ~Buffer() {
165
0
      delete[] data_;
166
0
      if (env_ != nullptr) {
167
0
        env_->external_memory_accounter()->Decrease(env_->isolate(), len_);
168
0
      }
169
0
    }
170
171
    Environment* env_;
172
    size_t read_pos_;
173
    size_t write_pos_;
174
    size_t len_;
175
    Buffer* next_;
176
    char* data_;
177
  };
178
179
  Environment* env_ = nullptr;
180
  size_t initial_ = kInitialBufferLength;
181
  size_t length_ = 0;
182
  size_t allocate_hint_ = 0;
183
  int eof_return_ = -1;
184
  Buffer* read_head_ = nullptr;
185
  Buffer* write_head_ = nullptr;
186
};
187
188
}  // namespace crypto
189
}  // namespace node
190
191
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
192
193
#endif  // SRC_CRYPTO_CRYPTO_BIO_H_