|
10 | 10 | #include <stdio.h> |
11 | 11 | #include <limits.h> |
12 | 12 | #include <errno.h> |
| 13 | +#include <assert.h> |
13 | 14 | #include "../ssl_locl.h" |
14 | 15 | #include <openssl/evp.h> |
15 | 16 | #include <openssl/buffer.h> |
@@ -347,6 +348,22 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, size_t len, |
347 | 348 | int i; |
348 | 349 | size_t tmpwrit; |
349 | 350 |
|
| 351 | + if (s->mode & SSL_MODE_QUIC_HACK) { |
| 352 | + /* If we have an alert to send, lets send it */ |
| 353 | + if (s->s3->alert_dispatch) { |
| 354 | + i = s->method->ssl_dispatch_alert(s); |
| 355 | + if (i <= 0) { |
| 356 | + /* SSLfatal() already called if appropriate */ |
| 357 | + return i; |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + s->rwstate = SSL_WRITING; |
| 362 | + *written = len; |
| 363 | + |
| 364 | + return 1; |
| 365 | + } |
| 366 | + |
350 | 367 | s->rwstate = SSL_NOTHING; |
351 | 368 | tot = s->rlayer.wnum; |
352 | 369 | /* |
@@ -659,6 +676,10 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf, |
659 | 676 | size_t totlen = 0, len, wpinited = 0; |
660 | 677 | size_t j; |
661 | 678 |
|
| 679 | + if (s->mode & SSL_MODE_QUIC_HACK) { |
| 680 | + assert(0); |
| 681 | + } |
| 682 | + |
662 | 683 | for (j = 0; j < numpipes; j++) |
663 | 684 | totlen += pipelens[j]; |
664 | 685 | /* |
@@ -1123,6 +1144,10 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len, |
1123 | 1144 | size_t currbuf = 0; |
1124 | 1145 | size_t tmpwrit = 0; |
1125 | 1146 |
|
| 1147 | + if (s->mode & SSL_MODE_QUIC_HACK) { |
| 1148 | + assert(0); |
| 1149 | + } |
| 1150 | + |
1126 | 1151 | if ((s->rlayer.wpend_tot > len) |
1127 | 1152 | || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) |
1128 | 1153 | && (s->rlayer.wpend_buf != buf)) |
@@ -1226,6 +1251,117 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf, |
1226 | 1251 | } |
1227 | 1252 | } |
1228 | 1253 |
|
| 1254 | + if (s->mode & SSL_MODE_QUIC_HACK) { |
| 1255 | + /* In QUIC, we only expect handshake protocol. Alerts are |
| 1256 | + notified by decicated API function. */ |
| 1257 | + if (!ossl_statem_get_in_handshake(s)) { |
| 1258 | + /* We found handshake data, so we're going back into init */ |
| 1259 | + ossl_statem_set_in_init(s, 1); |
| 1260 | + |
| 1261 | + i = s->handshake_func(s); |
| 1262 | + /* SSLfatal() already called if appropriate */ |
| 1263 | + if (i < 0) |
| 1264 | + return i; |
| 1265 | + if (i == 0) { |
| 1266 | + return -1; |
| 1267 | + } |
| 1268 | + *readbytes = 0; |
| 1269 | + return 1; |
| 1270 | + } |
| 1271 | + |
| 1272 | + if (s->rlayer.packet_length == 0) { |
| 1273 | + if (rbuf->left < 4) { |
| 1274 | + if (rbuf->len - rbuf->offset < 4 - rbuf->left) { |
| 1275 | + memmove(rbuf->buf, rbuf->buf + rbuf->offset - rbuf->left, |
| 1276 | + rbuf->left); |
| 1277 | + rbuf->offset = rbuf->left; |
| 1278 | + } |
| 1279 | + s->rwstate = SSL_READING; |
| 1280 | + /* TODO(size_t): Convert this function */ |
| 1281 | + ret = BIO_read(s->rbio, rbuf->buf + rbuf->offset, |
| 1282 | + rbuf->len - rbuf->offset); |
| 1283 | + if (ret < 0) { |
| 1284 | + return -1; |
| 1285 | + } |
| 1286 | + /* TODO Check this is really ok */ |
| 1287 | + if (ret == 0) { |
| 1288 | + *readbytes = 0; |
| 1289 | + return 1; |
| 1290 | + } |
| 1291 | + |
| 1292 | + rbuf->left += ret; |
| 1293 | + rbuf->offset += ret; |
| 1294 | + |
| 1295 | + if (rbuf->left < 4) { |
| 1296 | + *readbytes = 0; |
| 1297 | + return 1; |
| 1298 | + } |
| 1299 | + rbuf->offset -= rbuf->left; |
| 1300 | + } |
| 1301 | + |
| 1302 | + switch (rbuf->buf[rbuf->offset]) { |
| 1303 | + case SSL3_MT_CLIENT_HELLO: |
| 1304 | + case SSL3_MT_SERVER_HELLO: |
| 1305 | + case SSL3_MT_NEWSESSION_TICKET: |
| 1306 | + case SSL3_MT_END_OF_EARLY_DATA: |
| 1307 | + case SSL3_MT_ENCRYPTED_EXTENSIONS: |
| 1308 | + case SSL3_MT_CERTIFICATE: |
| 1309 | + case SSL3_MT_CERTIFICATE_REQUEST: |
| 1310 | + case SSL3_MT_CERTIFICATE_VERIFY: |
| 1311 | + case SSL3_MT_FINISHED: |
| 1312 | + case SSL3_MT_KEY_UPDATE: |
| 1313 | + case SSL3_MT_MESSAGE_HASH: |
| 1314 | + break; |
| 1315 | + default: |
| 1316 | + SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES, |
| 1317 | + ERR_R_INTERNAL_ERROR); |
| 1318 | + return -1; |
| 1319 | + } |
| 1320 | + |
| 1321 | + s->rlayer.packet_length = (rbuf->buf[rbuf->offset + 1] << 16) |
| 1322 | + + (rbuf->buf[rbuf->offset + 2] << 8) |
| 1323 | + + rbuf->buf[rbuf->offset + 3] + 4; |
| 1324 | + } |
| 1325 | + |
| 1326 | + if (s->rlayer.packet_length) { |
| 1327 | + size_t n; |
| 1328 | + |
| 1329 | + n = len < s->rlayer.packet_length ? len : s->rlayer.packet_length; |
| 1330 | + if (rbuf->left == 0) { |
| 1331 | + s->rwstate = SSL_READING; |
| 1332 | + ret = BIO_read(s->rbio, buf, n); |
| 1333 | + if (ret >= 0) { |
| 1334 | + s->rlayer.packet_length -= ret; |
| 1335 | + *readbytes = ret; |
| 1336 | + if (recvd_type) { |
| 1337 | + *recvd_type = SSL3_RT_HANDSHAKE; |
| 1338 | + } |
| 1339 | + return 1; |
| 1340 | + } |
| 1341 | + return -1; |
| 1342 | + } |
| 1343 | + |
| 1344 | + n = n < rbuf->left ? n : rbuf->left; |
| 1345 | + |
| 1346 | + memcpy(buf, rbuf->buf + rbuf->offset, n); |
| 1347 | + rbuf->offset += n; |
| 1348 | + rbuf->left -= n; |
| 1349 | + s->rlayer.packet_length -= n; |
| 1350 | + if (rbuf->left == 0) { |
| 1351 | + rbuf->offset = 0; |
| 1352 | + } |
| 1353 | + *readbytes = n; |
| 1354 | + if (recvd_type) { |
| 1355 | + *recvd_type = SSL3_RT_HANDSHAKE; |
| 1356 | + } |
| 1357 | + return 1; |
| 1358 | + } |
| 1359 | + |
| 1360 | + SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES, |
| 1361 | + ERR_R_INTERNAL_ERROR); |
| 1362 | + return -1; |
| 1363 | + } |
| 1364 | + |
1229 | 1365 | if ((type && (type != SSL3_RT_APPLICATION_DATA) |
1230 | 1366 | && (type != SSL3_RT_HANDSHAKE)) || (peek |
1231 | 1367 | && (type != |
|
0 commit comments