diff options
Diffstat (limited to 'src/lib/libtls/tls_bio_cb.c')
| -rw-r--r-- | src/lib/libtls/tls_bio_cb.c | 100 |
1 files changed, 51 insertions, 49 deletions
diff --git a/src/lib/libtls/tls_bio_cb.c b/src/lib/libtls/tls_bio_cb.c index a5ab206c62..3aca2d818c 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.9 2016/11/04 10:54:25 jsing Exp $ */ | 1 | /* $OpenBSD: tls_bio_cb.c,v 1.10 2016/11/04 15:37:49 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2016 Tobias Pape <tobias@netshed.de> | 3 | * Copyright (c) 2016 Tobias Pape <tobias@netshed.de> |
| 4 | * | 4 | * |
| @@ -24,16 +24,16 @@ | |||
| 24 | #include <tls.h> | 24 | #include <tls.h> |
| 25 | #include "tls_internal.h" | 25 | #include "tls_internal.h" |
| 26 | 26 | ||
| 27 | static int bio_cb_write(BIO *b, const char *buf, int num); | 27 | static int bio_cb_write(BIO *bio, const char *buf, int num); |
| 28 | static int bio_cb_read(BIO *b, char *buf, int size); | 28 | static int bio_cb_read(BIO *bio, char *buf, int size); |
| 29 | static int bio_cb_puts(BIO *b, const char *str); | 29 | static int bio_cb_puts(BIO *bio, const char *str); |
| 30 | static long bio_cb_ctrl(BIO *b, int cmd, long num, void *ptr); | 30 | static long bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr); |
| 31 | static int bio_cb_new(BIO *b); | 31 | static int bio_cb_new(BIO *bio); |
| 32 | static int bio_cb_free(BIO *data); | 32 | static int bio_cb_free(BIO *bio); |
| 33 | 33 | ||
| 34 | struct bio_cb_st { | 34 | struct bio_cb_st { |
| 35 | int (*write_cb)(BIO *h, const char *buf, int num, void *cb_arg); | 35 | int (*write_cb)(BIO *bio, const char *buf, int num, void *cb_arg); |
| 36 | int (*read_cb)(BIO *h, char *buf, int size, void *cb_arg); | 36 | int (*read_cb)(BIO *bio, char *buf, int size, void *cb_arg); |
| 37 | void *cb_arg; | 37 | void *cb_arg; |
| 38 | }; | 38 | }; |
| 39 | 39 | ||
| @@ -55,7 +55,7 @@ bio_s_cb(void) | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | static int | 57 | static int |
| 58 | bio_cb_new(BIO *bi) | 58 | bio_cb_new(BIO *bio) |
| 59 | { | 59 | { |
| 60 | struct bio_cb_st *bcb; | 60 | struct bio_cb_st *bcb; |
| 61 | 61 | ||
| @@ -63,24 +63,24 @@ bio_cb_new(BIO *bi) | |||
| 63 | if (bcb == NULL) | 63 | if (bcb == NULL) |
| 64 | return (0); | 64 | return (0); |
| 65 | 65 | ||
| 66 | bi->shutdown = 1; | 66 | bio->shutdown = 1; |
| 67 | bi->init = 1; | 67 | bio->init = 1; |
| 68 | bi->num = -1; | 68 | bio->num = -1; |
| 69 | bi->ptr = (char *)bcb; | 69 | bio->ptr = (char *)bcb; |
| 70 | 70 | ||
| 71 | return (1); | 71 | return (1); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | static int | 74 | static int |
| 75 | bio_cb_free(BIO *bi) | 75 | bio_cb_free(BIO *bio) |
| 76 | { | 76 | { |
| 77 | if (bi == NULL) | 77 | if (bio == NULL) |
| 78 | return (0); | 78 | return (0); |
| 79 | 79 | ||
| 80 | if (bi->shutdown) { | 80 | if (bio->shutdown) { |
| 81 | if ((bi->init) && (bi->ptr != NULL)) { | 81 | if ((bio->init) && (bio->ptr != NULL)) { |
| 82 | free(bi->ptr); | 82 | free(bio->ptr); |
| 83 | bi->ptr = NULL; | 83 | bio->ptr = NULL; |
| 84 | } | 84 | } |
| 85 | } | 85 | } |
| 86 | 86 | ||
| @@ -88,39 +88,39 @@ bio_cb_free(BIO *bi) | |||
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | static int | 90 | static int |
| 91 | bio_cb_read(BIO *b, char *buf, int size) | 91 | bio_cb_read(BIO *bio, char *buf, int size) |
| 92 | { | 92 | { |
| 93 | struct bio_cb_st *bcb = b->ptr; | 93 | struct bio_cb_st *bcb = bio->ptr; |
| 94 | return (bcb->read_cb(b, buf, size, bcb->cb_arg)); | 94 | return (bcb->read_cb(bio, buf, size, bcb->cb_arg)); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | static int | 97 | static int |
| 98 | bio_cb_write(BIO *b, const char *buf, int num) | 98 | bio_cb_write(BIO *bio, const char *buf, int num) |
| 99 | { | 99 | { |
| 100 | struct bio_cb_st *bcb = b->ptr; | 100 | struct bio_cb_st *bcb = bio->ptr; |
| 101 | return (bcb->write_cb(b, buf, num, bcb->cb_arg)); | 101 | return (bcb->write_cb(bio, buf, num, bcb->cb_arg)); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | static int | 104 | static int |
| 105 | bio_cb_puts(BIO *b, const char *str) | 105 | bio_cb_puts(BIO *bio, const char *str) |
| 106 | { | 106 | { |
| 107 | int n; | 107 | int n; |
| 108 | 108 | ||
| 109 | n = strlen(str); | 109 | n = strlen(str); |
| 110 | return (bio_cb_write(b, str, n)); | 110 | return (bio_cb_write(bio, str, n)); |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | static long | 113 | static long |
| 114 | bio_cb_ctrl(BIO *b, int cmd, long num, void *ptr) | 114 | bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr) |
| 115 | { | 115 | { |
| 116 | long ret = 1; | 116 | long ret = 1; |
| 117 | 117 | ||
| 118 | switch (cmd) { | 118 | switch (cmd) { |
| 119 | case BIO_CTRL_GET_CLOSE: | 119 | case BIO_CTRL_GET_CLOSE: |
| 120 | ret = (long)b->shutdown; | 120 | ret = (long)bio->shutdown; |
| 121 | break; | 121 | break; |
| 122 | case BIO_CTRL_SET_CLOSE: | 122 | case BIO_CTRL_SET_CLOSE: |
| 123 | b->shutdown = (int)num; | 123 | bio->shutdown = (int)num; |
| 124 | break; | 124 | break; |
| 125 | case BIO_CTRL_DUP: | 125 | case BIO_CTRL_DUP: |
| 126 | case BIO_CTRL_FLUSH: | 126 | case BIO_CTRL_FLUSH: |
| @@ -129,43 +129,43 @@ bio_cb_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 129 | case BIO_CTRL_GET: | 129 | case BIO_CTRL_GET: |
| 130 | case BIO_CTRL_SET: | 130 | case BIO_CTRL_SET: |
| 131 | default: | 131 | default: |
| 132 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 132 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | return (ret); | 135 | return (ret); |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | static int | 138 | static int |
| 139 | tls_bio_write_cb(BIO *h, const char *buf, int num, void *cb_arg) | 139 | tls_bio_write_cb(BIO *bio, const char *buf, int num, void *cb_arg) |
| 140 | { | 140 | { |
| 141 | struct tls *ctx = cb_arg; | 141 | struct tls *ctx = cb_arg; |
| 142 | int rv; | 142 | int rv; |
| 143 | 143 | ||
| 144 | BIO_clear_retry_flags(h); | 144 | BIO_clear_retry_flags(bio); |
| 145 | rv = (ctx->write_cb)(ctx, buf, num, ctx->cb_arg); | 145 | rv = (ctx->write_cb)(ctx, buf, num, ctx->cb_arg); |
| 146 | if (rv == TLS_WANT_POLLIN) { | 146 | if (rv == TLS_WANT_POLLIN) { |
| 147 | BIO_set_retry_read(h); | 147 | BIO_set_retry_read(bio); |
| 148 | rv = -1; | 148 | rv = -1; |
| 149 | } else if (rv == TLS_WANT_POLLOUT) { | 149 | } else if (rv == TLS_WANT_POLLOUT) { |
| 150 | BIO_set_retry_write(h); | 150 | BIO_set_retry_write(bio); |
| 151 | rv = -1; | 151 | rv = -1; |
| 152 | } | 152 | } |
| 153 | return (rv); | 153 | return (rv); |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | static int | 156 | static int |
| 157 | tls_bio_read_cb(BIO *h, char *buf, int size, void *cb_arg) | 157 | tls_bio_read_cb(BIO *bio, char *buf, int size, void *cb_arg) |
| 158 | { | 158 | { |
| 159 | struct tls *ctx = cb_arg; | 159 | struct tls *ctx = cb_arg; |
| 160 | int rv; | 160 | int rv; |
| 161 | 161 | ||
| 162 | BIO_clear_retry_flags(h); | 162 | BIO_clear_retry_flags(bio); |
| 163 | rv = (ctx->read_cb)(ctx, buf, size, ctx->cb_arg); | 163 | rv = (ctx->read_cb)(ctx, buf, size, ctx->cb_arg); |
| 164 | if (rv == TLS_WANT_POLLIN) { | 164 | if (rv == TLS_WANT_POLLIN) { |
| 165 | BIO_set_retry_read(h); | 165 | BIO_set_retry_read(bio); |
| 166 | rv = -1; | 166 | rv = -1; |
| 167 | } else if (rv == TLS_WANT_POLLOUT) { | 167 | } else if (rv == TLS_WANT_POLLOUT) { |
| 168 | BIO_set_retry_write(h); | 168 | BIO_set_retry_write(bio); |
| 169 | rv = -1; | 169 | rv = -1; |
| 170 | } | 170 | } |
| 171 | return (rv); | 171 | return (rv); |
| @@ -175,22 +175,23 @@ static BIO * | |||
| 175 | tls_get_new_cb_bio(struct tls *ctx) | 175 | tls_get_new_cb_bio(struct tls *ctx) |
| 176 | { | 176 | { |
| 177 | struct bio_cb_st *b; | 177 | struct bio_cb_st *b; |
| 178 | BIO *bcb; | 178 | BIO *bio; |
| 179 | |||
| 179 | if (ctx->read_cb == NULL || ctx->write_cb == NULL) | 180 | if (ctx->read_cb == NULL || ctx->write_cb == NULL) |
| 180 | tls_set_errorx(ctx, "no callbacks registered"); | 181 | tls_set_errorx(ctx, "no callbacks registered"); |
| 181 | 182 | ||
| 182 | bcb = BIO_new(bio_s_cb()); | 183 | bio = BIO_new(bio_s_cb()); |
| 183 | if (bcb == NULL) { | 184 | if (bio == NULL) { |
| 184 | tls_set_errorx(ctx, "failed to create callback i/o"); | 185 | tls_set_errorx(ctx, "failed to create callback i/o"); |
| 185 | return (NULL); | 186 | return (NULL); |
| 186 | } | 187 | } |
| 187 | 188 | ||
| 188 | b = (struct bio_cb_st *)bcb->ptr; | 189 | b = (struct bio_cb_st *)bio->ptr; |
| 189 | b->read_cb = tls_bio_read_cb; | 190 | b->read_cb = tls_bio_read_cb; |
| 190 | b->write_cb = tls_bio_write_cb; | 191 | b->write_cb = tls_bio_write_cb; |
| 191 | b->cb_arg = ctx; | 192 | b->cb_arg = ctx; |
| 192 | 193 | ||
| 193 | return (bcb); | 194 | return (bio); |
| 194 | } | 195 | } |
| 195 | 196 | ||
| 196 | int | 197 | int |
| @@ -198,18 +199,19 @@ tls_set_cbs(struct tls *ctx, tls_read_cb read_cb, tls_write_cb write_cb, | |||
| 198 | void *cb_arg) | 199 | void *cb_arg) |
| 199 | { | 200 | { |
| 200 | int rv = -1; | 201 | int rv = -1; |
| 201 | BIO *bcb; | 202 | BIO *bio; |
| 203 | |||
| 202 | ctx->read_cb = read_cb; | 204 | ctx->read_cb = read_cb; |
| 203 | ctx->write_cb = write_cb; | 205 | ctx->write_cb = write_cb; |
| 204 | ctx->cb_arg = cb_arg; | 206 | ctx->cb_arg = cb_arg; |
| 205 | 207 | ||
| 206 | bcb = tls_get_new_cb_bio(ctx); | 208 | bio = tls_get_new_cb_bio(ctx); |
| 207 | if (bcb == NULL) { | 209 | if (bio == NULL) { |
| 208 | tls_set_errorx(ctx, "failed to create callback i/o"); | 210 | tls_set_errorx(ctx, "failed to create callback i/o"); |
| 209 | goto err; | 211 | goto err; |
| 210 | } | 212 | } |
| 211 | 213 | ||
| 212 | SSL_set_bio(ctx->ssl_conn, bcb, bcb); | 214 | SSL_set_bio(ctx->ssl_conn, bio, bio); |
| 213 | 215 | ||
| 214 | rv = 0; | 216 | rv = 0; |
| 215 | 217 | ||
