28
28
namespace node {
29
29
namespace crypto {
30
30
31
- const BIO_METHOD NodeBIO::method = {
32
- BIO_TYPE_MEM,
33
- " node.js SSL buffer" ,
34
- NodeBIO::Write,
35
- NodeBIO::Read,
36
- NodeBIO::Puts,
37
- NodeBIO::Gets,
38
- NodeBIO::Ctrl,
39
- NodeBIO::New,
40
- NodeBIO::Free,
41
- nullptr
42
- };
31
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
32
+ #define BIO_set_data (bio, data ) bio->ptr = data
33
+ #define BIO_get_data (bio ) bio->ptr
34
+ #define BIO_set_shutdown (bio, shutdown_ ) bio->shutdown = shutdown_
35
+ #define BIO_get_shutdown (bio ) bio->shutdown
36
+ #define BIO_set_init (bio, init_ ) bio->init = init_
37
+ #define BIO_get_init (bio ) bio->init
38
+ #endif
43
39
44
40
45
41
BIO* NodeBIO::New () {
46
42
// The const_cast doesn't violate const correctness. OpenSSL's usage of
47
43
// BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
48
- return BIO_new (const_cast <BIO_METHOD*>(&method ));
44
+ return BIO_new (const_cast <BIO_METHOD*>(GetMethod () ));
49
45
}
50
46
51
47
@@ -70,12 +66,11 @@ void NodeBIO::AssignEnvironment(Environment* env) {
70
66
71
67
72
68
int NodeBIO::New (BIO* bio) {
73
- bio-> ptr = new NodeBIO ();
69
+ BIO_set_data ( bio, new NodeBIO () );
74
70
75
71
// XXX Why am I doing it?!
76
- bio->shutdown = 1 ;
77
- bio->init = 1 ;
78
- bio->num = -1 ;
72
+ BIO_set_shutdown (bio, 1 );
73
+ BIO_set_init (bio, 1 );
79
74
80
75
return 1 ;
81
76
}
@@ -85,10 +80,10 @@ int NodeBIO::Free(BIO* bio) {
85
80
if (bio == nullptr )
86
81
return 0 ;
87
82
88
- if (bio-> shutdown ) {
89
- if (bio-> init && bio-> ptr != nullptr ) {
83
+ if (BIO_get_shutdown ( bio) ) {
84
+ if (BIO_get_init ( bio) && BIO_get_data ( bio) != nullptr ) {
90
85
delete FromBIO (bio);
91
- bio-> ptr = nullptr ;
86
+ BIO_set_data ( bio, nullptr ) ;
92
87
}
93
88
}
94
89
@@ -97,13 +92,13 @@ int NodeBIO::Free(BIO* bio) {
97
92
98
93
99
94
int NodeBIO::Read (BIO* bio, char * out, int len) {
100
- int bytes;
101
95
BIO_clear_retry_flags (bio);
102
96
103
- bytes = FromBIO (bio)->Read (out, len);
97
+ NodeBIO* nbio = FromBIO (bio);
98
+ int bytes = nbio->Read (out, len);
104
99
105
100
if (bytes == 0 ) {
106
- bytes = bio-> num ;
101
+ bytes = nbio-> eof_return () ;
107
102
if (bytes != 0 ) {
108
103
BIO_set_retry_read (bio);
109
104
}
@@ -161,7 +156,7 @@ int NodeBIO::Puts(BIO* bio, const char* str) {
161
156
162
157
163
158
int NodeBIO::Gets (BIO* bio, char * out, int size) {
164
- NodeBIO* nbio = FromBIO (bio);
159
+ NodeBIO* nbio = FromBIO (bio);
165
160
166
161
if (nbio->Length () == 0 )
167
162
return 0 ;
@@ -201,7 +196,7 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
201
196
ret = nbio->Length () == 0 ;
202
197
break ;
203
198
case BIO_C_SET_BUF_MEM_EOF_RETURN:
204
- bio-> num = num ;
199
+ nbio-> set_eof_return ( num) ;
205
200
break ;
206
201
case BIO_CTRL_INFO:
207
202
ret = nbio->Length ();
@@ -216,10 +211,10 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
216
211
ret = 0 ;
217
212
break ;
218
213
case BIO_CTRL_GET_CLOSE:
219
- ret = bio-> shutdown ;
214
+ ret = BIO_get_shutdown ( bio) ;
220
215
break ;
221
216
case BIO_CTRL_SET_CLOSE:
222
- bio-> shutdown = num;
217
+ BIO_set_shutdown ( bio, num) ;
223
218
break ;
224
219
case BIO_CTRL_WPENDING:
225
220
ret = 0 ;
@@ -241,6 +236,41 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
241
236
}
242
237
243
238
239
+ const BIO_METHOD* NodeBIO::GetMethod () {
240
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
241
+ static const BIO_METHOD method = {
242
+ BIO_TYPE_MEM,
243
+ " node.js SSL buffer" ,
244
+ Write,
245
+ Read,
246
+ Puts,
247
+ Gets,
248
+ Ctrl,
249
+ New,
250
+ Free,
251
+ nullptr
252
+ };
253
+
254
+ return &method;
255
+ #else
256
+ static BIO_METHOD* method = nullptr ;
257
+
258
+ if (method == nullptr ) {
259
+ method = BIO_meth_new (BIO_TYPE_MEM, " node.js SSL buffer" );
260
+ BIO_meth_set_write (method, Write);
261
+ BIO_meth_set_read (method, Read);
262
+ BIO_meth_set_puts (method, Puts);
263
+ BIO_meth_set_gets (method, Gets);
264
+ BIO_meth_set_ctrl (method, Ctrl);
265
+ BIO_meth_set_create (method, New);
266
+ BIO_meth_set_destroy (method, Free);
267
+ }
268
+
269
+ return method;
270
+ #endif
271
+ }
272
+
273
+
244
274
void NodeBIO::TryMoveReadHead () {
245
275
// `read_pos_` and `write_pos_` means the position of the reader and writer
246
276
// inside the buffer, respectively. When they're equal - its safe to reset
@@ -488,5 +518,12 @@ NodeBIO::~NodeBIO() {
488
518
write_head_ = nullptr ;
489
519
}
490
520
521
+
522
+ NodeBIO* NodeBIO::FromBIO (BIO* bio) {
523
+ CHECK_NE (BIO_get_data (bio), nullptr );
524
+ return static_cast <NodeBIO*>(BIO_get_data (bio));
525
+ }
526
+
527
+
491
528
} // namespace crypto
492
529
} // namespace node
0 commit comments