Skip to content
This repository was archived by the owner on Mar 9, 2019. It is now read-only.

Commit 480a86b

Browse files
committed
port to openssl-1.1.0
Summary: Port to the new opaque types in openssl-1.1.0 This was build against both openssl-1.1.0 and openssl-1.0.2
1 parent b93da64 commit 480a86b

File tree

3 files changed

+118
-56
lines changed

3 files changed

+118
-56
lines changed

corelib/net/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void sock_dtor(ph_job_t *job)
117117
sock->sslwbuf = NULL;
118118
}
119119
if (sock->ssl) {
120-
SSL_CTX *ctx = sock->ssl->ctx;
120+
SSL_CTX *ctx = SSL_get_SSL_CTX(sock->ssl);
121121

122122
if (sock->ssl_stream) {
123123
ph_stm_close(sock->ssl_stream);

corelib/openssl/bio_bufq.c

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
#include "phenom/openssl.h"
1919
#include <openssl/bio.h>
2020

21+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
22+
#define BIO_set_init(b, val) (b)->init = (val)
23+
#define BIO_set_data(b, val) (b)->ptr = (val)
24+
#define BIO_get_data(b) (b)->ptr
25+
#endif
26+
2127
/* Implements an OpenSSL BIO that writes to a phenom bufq */
2228

2329
static int bio_bufq_write(BIO *h, const char *buf, int size)
2430
{
2531
uint64_t n;
26-
ph_bufq_t *q = h->ptr;
32+
ph_bufq_t *q = BIO_get_data(h);
2733

2834
BIO_clear_retry_flags(h);
2935
if (ph_bufq_append(q, buf, size, &n) != PH_OK) {
@@ -59,12 +65,16 @@ static long bio_bufq_ctrl(BIO *h, int cmd, // NOLINT(runtime/int)
5965
return 1;
6066
}
6167

68+
static void bio_bufq_clear(BIO *h)
69+
{
70+
BIO_set_init(h, 0);
71+
BIO_set_data(h, NULL);
72+
BIO_clear_flags(h, ~0);
73+
}
74+
6275
static int bio_bufq_new(BIO *h)
6376
{
64-
h->init = 0;
65-
h->num = 0;
66-
h->ptr = NULL;
67-
h->flags = 0;
77+
bio_bufq_clear(h);
6878
return 1;
6979
}
7080

@@ -74,38 +84,60 @@ static int bio_bufq_free(BIO *h)
7484
return 0;
7585
}
7686

77-
h->ptr = NULL;
78-
h->init = 0;
79-
h->flags = 0;
80-
87+
bio_bufq_clear(h);
8188
return 1;
8289
}
8390

84-
static BIO_METHOD method_bufq = {
85-
// See bio_stream.c
86-
81 | BIO_TYPE_SOURCE_SINK,
87-
"phenom-bufq",
88-
bio_bufq_write,
89-
bio_bufq_read,
90-
bio_bufq_puts,
91-
NULL, /* no gets */
92-
bio_bufq_ctrl,
93-
bio_bufq_new,
94-
bio_bufq_free,
95-
NULL, /* no callback ctrl */
96-
};
97-
9891
BIO *ph_openssl_bio_wrap_bufq(ph_bufq_t *bufq)
9992
{
100-
BIO *h;
93+
static BIO_METHOD *bm;
94+
95+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
96+
static BIO_METHOD old_meth = {
97+
// See bio_stream.c
98+
81 | BIO_TYPE_SOURCE_SINK,
99+
"phenom-bufq",
100+
bio_bufq_write,
101+
bio_bufq_read,
102+
bio_bufq_puts,
103+
NULL, /* no gets */
104+
bio_bufq_ctrl,
105+
bio_bufq_new,
106+
bio_bufq_free,
107+
NULL, /* no callback ctrl */
108+
};
109+
110+
bm = &old_meth;
111+
#else
112+
if (!bm) {
113+
BIO_METHOD *bm = BIO_meth_new(81/*see bio_stream.c*/ | BIO_TYPE_SOURCE_SINK,
114+
"phenom-bufq");
115+
if (!bm) {
116+
return NULL;
117+
}
118+
119+
BIO_meth_set_write(bm, bio_bufq_write);
120+
BIO_meth_set_read(bm, bio_bufq_read);
121+
BIO_meth_set_puts(bm, bio_bufq_puts);
122+
BIO_meth_set_ctrl(bm, bio_bufq_ctrl);
123+
BIO_meth_set_create(bm, bio_bufq_new);
124+
BIO_meth_set_destroy(bm, bio_bufq_free);
125+
}
126+
#endif
101127

102-
h = BIO_new(&method_bufq);
128+
BIO *h = BIO_new(bm);
103129
if (!h) {
104130
return NULL;
105131
}
106132

133+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
107134
h->ptr = bufq;
108135
h->init = 1;
136+
#else
137+
BIO_set_data(h, bufq);
138+
BIO_set_init(h, 1);
139+
#endif
140+
109141
return h;
110142
}
111143

corelib/openssl/bio_stream.c

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
#include "phenom/openssl.h"
2020
#include <openssl/bio.h>
2121

22+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
23+
#define BIO_set_init(b, val) (b)->init = (val)
24+
#define BIO_set_data(b, val) (b)->ptr = (val)
25+
#define BIO_get_data(b) (b)->ptr
26+
#endif
27+
2228
/* Implements an OpenSSL BIO that invokes phenom streams */
2329

2430
static bool should_retry(ph_stream_t *stm)
@@ -33,10 +39,17 @@ static bool should_retry(ph_stream_t *stm)
3339
}
3440
}
3541

42+
static void bio_stm_clear(BIO *h)
43+
{
44+
BIO_set_init(h, 0);
45+
BIO_set_data(h, NULL);
46+
BIO_clear_flags(h, ~0);
47+
}
48+
3649
static int bio_stm_write(BIO *h, const char *buf, int size)
3750
{
3851
uint64_t nwrote;
39-
ph_stream_t *stm = h->ptr;
52+
ph_stream_t *stm = BIO_get_data(h);
4053

4154
if (buf == NULL || size == 0 || stm == NULL) {
4255
return 0;
@@ -62,7 +75,7 @@ static int bio_stm_puts(BIO *h, const char *str)
6275
static int bio_stm_read(BIO *h, char *buf, int size)
6376
{
6477
uint64_t nread;
65-
ph_stream_t *stm = h->ptr;
78+
ph_stream_t *stm = BIO_get_data(h);
6679

6780
if (buf == NULL || size == 0 || stm == NULL) {
6881
return 0;
@@ -83,7 +96,7 @@ static int bio_stm_read(BIO *h, char *buf, int size)
8396
static long bio_stm_ctrl(BIO *h, int cmd, // NOLINT(runtime/int)
8497
long arg1, void *arg2) // NOLINT(runtime/int)
8598
{
86-
ph_stream_t *stm = h->ptr;
99+
ph_stream_t *stm = BIO_get_data(h);
87100

88101
switch (cmd) {
89102
case BIO_CTRL_FLUSH:
@@ -98,11 +111,7 @@ static long bio_stm_ctrl(BIO *h, int cmd, // NOLINT(runtime/int)
98111

99112
static int bio_stm_new(BIO *h)
100113
{
101-
h->init = 0;
102-
h->num = 0;
103-
h->ptr = NULL;
104-
h->flags = 0;
105-
114+
bio_stm_clear(h);
106115
return 1;
107116
}
108117

@@ -112,40 +121,61 @@ static int bio_stm_free(BIO *h)
112121
return 0;
113122
}
114123

115-
h->ptr = NULL;
116-
h->init = 0;
117-
h->flags = 0;
118-
124+
bio_stm_clear(h);
119125
return 1;
120126
}
121127

122-
static BIO_METHOD method_stm = {
123-
// There are no clear rules on how the type numbers are assigned, so we'll
124-
// just pick 'P' as our type number and hope it doesn't collide any time
125-
// soon.
126-
80 /* 'P' */ | BIO_TYPE_SOURCE_SINK,
127-
"phenom-stream",
128-
bio_stm_write,
129-
bio_stm_read,
130-
bio_stm_puts,
131-
NULL, /* no gets */
132-
bio_stm_ctrl,
133-
bio_stm_new,
134-
bio_stm_free,
135-
NULL, /* no callback ctrl */
136-
};
137-
138128
BIO *ph_openssl_bio_wrap_stream(ph_stream_t *stm)
139129
{
140-
BIO *h;
130+
static BIO_METHOD *bm;
131+
132+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
133+
static BIO_METHOD old_meth = {
134+
// There are no clear rules on how the type numbers are assigned, so we'll
135+
// just pick 'P' as our type number and hope it doesn't collide any time
136+
// soon. TODO: consider using: BIO_TYPE_BIO | BIO_get_new_index ();
137+
80 | BIO_TYPE_SOURCE_SINK,
138+
"phenom-stream",
139+
bio_stm_write,
140+
bio_stm_read,
141+
bio_stm_puts,
142+
NULL, /* no gets */
143+
bio_stm_ctrl,
144+
bio_stm_new,
145+
bio_stm_free,
146+
NULL, /* no callback ctrl */
147+
};
148+
149+
bm = &old_meth;
150+
#else
151+
if (!bm) {
152+
bm = BIO_meth_new(80 /* 'P' */ | BIO_TYPE_SOURCE_SINK, "phenom-stream");
153+
if (!bm) {
154+
return NULL;
155+
}
156+
157+
BIO_meth_set_write(bm, bio_stm_write);
158+
BIO_meth_set_read(bm, bio_stm_read);
159+
BIO_meth_set_puts(bm, bio_stm_puts);
160+
BIO_meth_set_ctrl(bm, bio_stm_ctrl);
161+
BIO_meth_set_create(bm, bio_stm_new);
162+
BIO_meth_set_destroy(bm, bio_stm_free);
163+
}
164+
#endif
141165

142-
h = BIO_new(&method_stm);
166+
BIO *h = BIO_new(bm);
143167
if (!h) {
144168
return NULL;
145169
}
146170

171+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
147172
h->ptr = stm;
148173
h->init = 1;
174+
#else
175+
BIO_set_data(h, stm);
176+
BIO_set_init(h, 1);
177+
#endif
178+
149179
return h;
150180
}
151181

0 commit comments

Comments
 (0)