diff options
author | tb <> | 2022-01-10 23:39:48 +0000 |
---|---|---|
committer | tb <> | 2022-01-10 23:39:48 +0000 |
commit | 8e0707c19b96680125b964593e3d106e43ecb729 (patch) | |
tree | ea21d81ecc93f6b17b02ee80ddb1abdab81d21df /src/lib/libtls | |
parent | 92a0a272516cf93a0613290a82dc2128d6a125cb (diff) | |
download | openbsd-8e0707c19b96680125b964593e3d106e43ecb729.tar.gz openbsd-8e0707c19b96680125b964593e3d106e43ecb729.tar.bz2 openbsd-8e0707c19b96680125b964593e3d106e43ecb729.zip |
Convert tls_bio_cb for opaque BIO
joint with jsing
Diffstat (limited to 'src/lib/libtls')
-rw-r--r-- | src/lib/libtls/tls_bio_cb.c | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/src/lib/libtls/tls_bio_cb.c b/src/lib/libtls/tls_bio_cb.c index 0091808fc2..dad9d23efb 100644 --- a/src/lib/libtls/tls_bio_cb.c +++ b/src/lib/libtls/tls_bio_cb.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls_bio_cb.c,v 1.19 2017/01/12 16:18:39 jsing Exp $ */ | 1 | /* $OpenBSD: tls_bio_cb.c,v 1.20 2022/01/10 23:39:48 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2016 Tobias Pape <tobias@netshed.de> | 3 | * Copyright (c) 2016 Tobias Pape <tobias@netshed.de> |
4 | * | 4 | * |
@@ -29,19 +29,41 @@ static int bio_cb_read(BIO *bio, char *buf, int size); | |||
29 | static int bio_cb_puts(BIO *bio, const char *str); | 29 | static int bio_cb_puts(BIO *bio, const char *str); |
30 | static long bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr); | 30 | static long bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr); |
31 | 31 | ||
32 | static BIO_METHOD bio_cb_method = { | 32 | static BIO_METHOD *bio_cb_method; |
33 | .type = BIO_TYPE_MEM, | 33 | |
34 | .name = "libtls_callbacks", | 34 | static pthread_mutex_t bio_cb_method_lock = PTHREAD_MUTEX_INITIALIZER; |
35 | .bwrite = bio_cb_write, | 35 | |
36 | .bread = bio_cb_read, | 36 | static void |
37 | .bputs = bio_cb_puts, | 37 | bio_cb_method_init(void) |
38 | .ctrl = bio_cb_ctrl, | 38 | { |
39 | }; | 39 | BIO_METHOD *bio_method; |
40 | |||
41 | if (bio_cb_method != NULL) | ||
42 | return; | ||
43 | |||
44 | bio_method = BIO_meth_new(BIO_TYPE_MEM, "libtls_callbacks"); | ||
45 | if (bio_method == NULL) | ||
46 | return; | ||
47 | |||
48 | BIO_meth_set_write(bio_method, bio_cb_write); | ||
49 | BIO_meth_set_read(bio_method, bio_cb_read); | ||
50 | BIO_meth_set_puts(bio_method, bio_cb_puts); | ||
51 | BIO_meth_set_ctrl(bio_method, bio_cb_ctrl); | ||
52 | |||
53 | bio_cb_method = bio_method; | ||
54 | } | ||
40 | 55 | ||
41 | static BIO_METHOD * | 56 | static BIO_METHOD * |
42 | bio_s_cb(void) | 57 | bio_s_cb(void) |
43 | { | 58 | { |
44 | return (&bio_cb_method); | 59 | if (bio_cb_method != NULL) |
60 | return (bio_cb_method); | ||
61 | |||
62 | pthread_mutex_lock(&bio_cb_method_lock); | ||
63 | bio_cb_method_init(); | ||
64 | pthread_mutex_unlock(&bio_cb_method_lock); | ||
65 | |||
66 | return (bio_cb_method); | ||
45 | } | 67 | } |
46 | 68 | ||
47 | static int | 69 | static int |
@@ -57,10 +79,10 @@ bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr) | |||
57 | 79 | ||
58 | switch (cmd) { | 80 | switch (cmd) { |
59 | case BIO_CTRL_GET_CLOSE: | 81 | case BIO_CTRL_GET_CLOSE: |
60 | ret = (long)bio->shutdown; | 82 | ret = (long)BIO_get_shutdown(bio); |
61 | break; | 83 | break; |
62 | case BIO_CTRL_SET_CLOSE: | 84 | case BIO_CTRL_SET_CLOSE: |
63 | bio->shutdown = (int)num; | 85 | BIO_set_shutdown(bio, (int)num); |
64 | break; | 86 | break; |
65 | case BIO_CTRL_DUP: | 87 | case BIO_CTRL_DUP: |
66 | case BIO_CTRL_FLUSH: | 88 | case BIO_CTRL_FLUSH: |
@@ -69,7 +91,7 @@ bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr) | |||
69 | case BIO_CTRL_GET: | 91 | case BIO_CTRL_GET: |
70 | case BIO_CTRL_SET: | 92 | case BIO_CTRL_SET: |
71 | default: | 93 | default: |
72 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); | 94 | ret = BIO_ctrl(BIO_next(bio), cmd, num, ptr); |
73 | } | 95 | } |
74 | 96 | ||
75 | return (ret); | 97 | return (ret); |
@@ -78,7 +100,7 @@ bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr) | |||
78 | static int | 100 | static int |
79 | bio_cb_write(BIO *bio, const char *buf, int num) | 101 | bio_cb_write(BIO *bio, const char *buf, int num) |
80 | { | 102 | { |
81 | struct tls *ctx = bio->ptr; | 103 | struct tls *ctx = BIO_get_data(bio); |
82 | int rv; | 104 | int rv; |
83 | 105 | ||
84 | BIO_clear_retry_flags(bio); | 106 | BIO_clear_retry_flags(bio); |
@@ -96,7 +118,7 @@ bio_cb_write(BIO *bio, const char *buf, int num) | |||
96 | static int | 118 | static int |
97 | bio_cb_read(BIO *bio, char *buf, int size) | 119 | bio_cb_read(BIO *bio, char *buf, int size) |
98 | { | 120 | { |
99 | struct tls *ctx = bio->ptr; | 121 | struct tls *ctx = BIO_get_data(bio); |
100 | int rv; | 122 | int rv; |
101 | 123 | ||
102 | BIO_clear_retry_flags(bio); | 124 | BIO_clear_retry_flags(bio); |
@@ -115,8 +137,9 @@ int | |||
115 | tls_set_cbs(struct tls *ctx, tls_read_cb read_cb, tls_write_cb write_cb, | 137 | tls_set_cbs(struct tls *ctx, tls_read_cb read_cb, tls_write_cb write_cb, |
116 | void *cb_arg) | 138 | void *cb_arg) |
117 | { | 139 | { |
118 | int rv = -1; | 140 | const BIO_METHOD *bio_cb; |
119 | BIO *bio; | 141 | BIO *bio; |
142 | int rv = -1; | ||
120 | 143 | ||
121 | if (read_cb == NULL || write_cb == NULL) { | 144 | if (read_cb == NULL || write_cb == NULL) { |
122 | tls_set_errorx(ctx, "no callbacks provided"); | 145 | tls_set_errorx(ctx, "no callbacks provided"); |
@@ -127,12 +150,16 @@ tls_set_cbs(struct tls *ctx, tls_read_cb read_cb, tls_write_cb write_cb, | |||
127 | ctx->write_cb = write_cb; | 150 | ctx->write_cb = write_cb; |
128 | ctx->cb_arg = cb_arg; | 151 | ctx->cb_arg = cb_arg; |
129 | 152 | ||
130 | if ((bio = BIO_new(bio_s_cb())) == NULL) { | 153 | if ((bio_cb = bio_s_cb()) == NULL) { |
154 | tls_set_errorx(ctx, "failed to create callback method"); | ||
155 | goto err; | ||
156 | } | ||
157 | if ((bio = BIO_new(bio_cb)) == NULL) { | ||
131 | tls_set_errorx(ctx, "failed to create callback i/o"); | 158 | tls_set_errorx(ctx, "failed to create callback i/o"); |
132 | goto err; | 159 | goto err; |
133 | } | 160 | } |
134 | bio->ptr = ctx; | 161 | BIO_set_data(bio, ctx); |
135 | bio->init = 1; | 162 | BIO_set_init(bio, 1); |
136 | 163 | ||
137 | SSL_set_bio(ctx->ssl_conn, bio, bio); | 164 | SSL_set_bio(ctx->ssl_conn, bio, bio); |
138 | 165 | ||