2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 /* see bearssl_ssl.h */
29 br_sslio_init(br_sslio_context
*ctx
,
30 br_ssl_engine_context
*engine
,
31 int (*low_read
)(void *read_context
,
32 unsigned char *data
, size_t len
),
34 int (*low_write
)(void *write_context
,
35 const unsigned char *data
, size_t len
),
39 ctx
->low_read
= low_read
;
40 ctx
->read_context
= read_context
;
41 ctx
->low_write
= low_write
;
42 ctx
->write_context
= write_context
;
46 * Run the engine, until the specified target state is achieved, or
47 * an error occurs. The target state is SENDAPP, RECVAPP, or the
48 * combination of both (the combination matches either). When a match is
49 * achieved, this function returns 0. On error, it returns -1.
52 run_until(br_sslio_context
*ctx
, unsigned target
)
57 state
= br_ssl_engine_current_state(ctx
->engine
);
58 if (state
& BR_SSL_CLOSED
) {
63 * If there is some record data to send, do it. This takes
64 * precedence over everything else.
66 if (state
& BR_SSL_SENDREC
) {
71 buf
= br_ssl_engine_sendrec_buf(ctx
->engine
, &len
);
72 wlen
= ctx
->low_write(ctx
->write_context
, buf
, len
);
75 * If we received a close_notify and we
76 * still send something, then we have our
77 * own response close_notify to send, and
78 * the peer is allowed by RFC 5246 not to
81 if (!ctx
->engine
->shutdown_recv
) {
83 ctx
->engine
, BR_ERR_IO
);
88 br_ssl_engine_sendrec_ack(ctx
->engine
, wlen
);
94 * If we reached our target, then we are finished.
101 * If some application data must be read, and we did not
102 * exit, then this means that we are trying to write data,
103 * and that's not possible until the application data is
104 * read. This may happen if using a shared in/out buffer,
105 * and the underlying protocol is not strictly half-duplex.
106 * This is unrecoverable here, so we report an error.
108 if (state
& BR_SSL_RECVAPP
) {
113 * If we reached that point, then either we are trying
114 * to read data and there is some, or the engine is stuck
115 * until a new record is obtained.
117 if (state
& BR_SSL_RECVREC
) {
122 buf
= br_ssl_engine_recvrec_buf(ctx
->engine
, &len
);
123 rlen
= ctx
->low_read(ctx
->read_context
, buf
, len
);
125 br_ssl_engine_fail(ctx
->engine
, BR_ERR_IO
);
129 br_ssl_engine_recvrec_ack(ctx
->engine
, rlen
);
135 * We can reach that point if the target RECVAPP, and
136 * the state contains SENDAPP only. This may happen with
137 * a shared in/out buffer. In that case, we must flush
138 * the buffered data to "make room" for a new incoming
141 br_ssl_engine_flush(ctx
->engine
, 0);
145 /* see bearssl_ssl.h */
147 br_sslio_read(br_sslio_context
*ctx
, void *dst
, size_t len
)
155 if (run_until(ctx
, BR_SSL_RECVAPP
) < 0) {
158 buf
= br_ssl_engine_recvapp_buf(ctx
->engine
, &alen
);
162 memcpy(dst
, buf
, alen
);
163 br_ssl_engine_recvapp_ack(ctx
->engine
, alen
);
167 /* see bearssl_ssl.h */
169 br_sslio_read_all(br_sslio_context
*ctx
, void *dst
, size_t len
)
177 rlen
= br_sslio_read(ctx
, buf
, len
);
187 /* see bearssl_ssl.h */
189 br_sslio_write(br_sslio_context
*ctx
, const void *src
, size_t len
)
197 if (run_until(ctx
, BR_SSL_SENDAPP
) < 0) {
200 buf
= br_ssl_engine_sendapp_buf(ctx
->engine
, &alen
);
204 memcpy(buf
, src
, alen
);
205 br_ssl_engine_sendapp_ack(ctx
->engine
, alen
);
209 /* see bearssl_ssl.h */
211 br_sslio_write_all(br_sslio_context
*ctx
, const void *src
, size_t len
)
213 const unsigned char *buf
;
219 wlen
= br_sslio_write(ctx
, buf
, len
);
229 /* see bearssl_ssl.h */
231 br_sslio_flush(br_sslio_context
*ctx
)
234 * We trigger a flush. We know the data is gone when there is
235 * no longer any record data to send, and we can either read
236 * or write application data. The call to run_until() does the
237 * job because it ensures that any assembled record data is
238 * first sent down the wire before considering anything else.
240 br_ssl_engine_flush(ctx
->engine
, 0);
241 return run_until(ctx
, BR_SSL_SENDAPP
| BR_SSL_RECVAPP
);
244 /* see bearssl_ssl.h */
246 br_sslio_close(br_sslio_context
*ctx
)
248 br_ssl_engine_close(ctx
->engine
);
249 while (br_ssl_engine_current_state(ctx
->engine
) != BR_SSL_CLOSED
) {
251 * Discard any incoming application data.
255 run_until(ctx
, BR_SSL_RECVAPP
);
256 if (br_ssl_engine_recvapp_buf(ctx
->engine
, &len
) != NULL
) {
257 br_ssl_engine_recvapp_ack(ctx
->engine
, len
);
260 return br_ssl_engine_last_error(ctx
->engine
) == BR_ERR_OK
;