No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

bssl_shim.cc 22 KiB

Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
hace 10 años
hace 10 años
hace 10 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
hace 10 años
hace 10 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
hace 10 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
hace 10 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
hace 10 años
Add DTLS timeout and retransmit tests. This extends the packet adaptor protocol to send three commands: type command = | Packet of []byte | Timeout of time.Duration | TimeoutAck When the shim processes a Timeout in BIO_read, it sends TimeoutAck, fails the BIO_read, returns out of the SSL stack, advances the clock, calls DTLSv1_handle_timeout, and continues. If the Go side sends Timeout right between sending handshake flight N and reading flight N+1, the shim won't read the Timeout until it has sent flight N+1 (it only processes packet commands in BIO_read), so the TimeoutAck comes after N+1. Go then drops all packets before the TimeoutAck, thus dropping one transmit of flight N+1 without having to actually process the packets to determine the end of the flight. The shim then sees the updated clock, calls DTLSv1_handle_timeout, and re-sends flight N+1 for Go to process for real. When dropping packets, Go checks the epoch and increments sequence numbers so that we can continue to be strict here. This requires tracking the initial sequence number of the next epoch. The final Finished message takes an additional special-case to test. DTLS triggers retransmits on either a timeout or seeing a stale flight. OpenSSL only implements the former which should be sufficient (and is necessary) EXCEPT for the final Finished message. If the peer's final Finished message is lost, it won't be waiting for a message from us, so it won't time out anything. That retransmit must be triggered on stale message, so we retransmit the Finished message in Go. Change-Id: I3ffbdb1de525beb2ee831b304670a3387877634c Reviewed-on: https://boringssl-review.googlesource.com/3212 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #if !defined(OPENSSL_WINDOWS)
  16. #include <arpa/inet.h>
  17. #include <netinet/in.h>
  18. #include <signal.h>
  19. #include <sys/socket.h>
  20. #include <unistd.h>
  21. #endif
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <openssl/bio.h>
  25. #include <openssl/buf.h>
  26. #include <openssl/bytestring.h>
  27. #include <openssl/ssl.h>
  28. #include "async_bio.h"
  29. #include "packeted_bio.h"
  30. #include "test_config.h"
  31. static int usage(const char *program) {
  32. fprintf(stderr, "Usage: %s [flags...]\n",
  33. program);
  34. return 1;
  35. }
  36. static int g_ex_data_index = 0;
  37. static int g_ex_data_clock_index = 0;
  38. static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
  39. return SSL_set_ex_data(ssl, g_ex_data_index, (void *)config) == 1;
  40. }
  41. static const TestConfig *GetConfigPtr(SSL *ssl) {
  42. return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
  43. }
  44. static bool SetClockPtr(SSL *ssl, OPENSSL_timeval *clock) {
  45. return SSL_set_ex_data(ssl, g_ex_data_clock_index, (void *)clock) == 1;
  46. }
  47. static OPENSSL_timeval *GetClockPtr(SSL *ssl) {
  48. return (OPENSSL_timeval *)SSL_get_ex_data(ssl, g_ex_data_clock_index);
  49. }
  50. static EVP_PKEY *LoadPrivateKey(const std::string &file) {
  51. BIO *bio = BIO_new(BIO_s_file());
  52. if (bio == NULL) {
  53. return NULL;
  54. }
  55. if (!BIO_read_filename(bio, file.c_str())) {
  56. BIO_free(bio);
  57. return NULL;
  58. }
  59. EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
  60. BIO_free(bio);
  61. return pkey;
  62. }
  63. static int early_callback_called = 0;
  64. static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
  65. early_callback_called = 1;
  66. const TestConfig *config = GetConfigPtr(ctx->ssl);
  67. if (config->expected_server_name.empty()) {
  68. return 1;
  69. }
  70. const uint8_t *extension_data;
  71. size_t extension_len;
  72. CBS extension, server_name_list, host_name;
  73. uint8_t name_type;
  74. if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
  75. &extension_data,
  76. &extension_len)) {
  77. fprintf(stderr, "Could not find server_name extension.\n");
  78. return -1;
  79. }
  80. CBS_init(&extension, extension_data, extension_len);
  81. if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
  82. CBS_len(&extension) != 0 ||
  83. !CBS_get_u8(&server_name_list, &name_type) ||
  84. name_type != TLSEXT_NAMETYPE_host_name ||
  85. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  86. CBS_len(&server_name_list) != 0) {
  87. fprintf(stderr, "Could not decode server_name extension.\n");
  88. return -1;
  89. }
  90. if (!CBS_mem_equal(&host_name,
  91. (const uint8_t*)config->expected_server_name.data(),
  92. config->expected_server_name.size())) {
  93. fprintf(stderr, "Server name mismatch.\n");
  94. }
  95. return 1;
  96. }
  97. static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
  98. return 1;
  99. }
  100. static int next_protos_advertised_callback(SSL *ssl,
  101. const uint8_t **out,
  102. unsigned int *out_len,
  103. void *arg) {
  104. const TestConfig *config = GetConfigPtr(ssl);
  105. if (config->advertise_npn.empty())
  106. return SSL_TLSEXT_ERR_NOACK;
  107. *out = (const uint8_t*)config->advertise_npn.data();
  108. *out_len = config->advertise_npn.size();
  109. return SSL_TLSEXT_ERR_OK;
  110. }
  111. static int next_proto_select_callback(SSL* ssl,
  112. uint8_t** out,
  113. uint8_t* outlen,
  114. const uint8_t* in,
  115. unsigned inlen,
  116. void* arg) {
  117. const TestConfig *config = GetConfigPtr(ssl);
  118. if (config->select_next_proto.empty())
  119. return SSL_TLSEXT_ERR_NOACK;
  120. *out = (uint8_t*)config->select_next_proto.data();
  121. *outlen = config->select_next_proto.size();
  122. return SSL_TLSEXT_ERR_OK;
  123. }
  124. static int alpn_select_callback(SSL* ssl,
  125. const uint8_t** out,
  126. uint8_t* outlen,
  127. const uint8_t* in,
  128. unsigned inlen,
  129. void* arg) {
  130. const TestConfig *config = GetConfigPtr(ssl);
  131. if (config->select_alpn.empty())
  132. return SSL_TLSEXT_ERR_NOACK;
  133. if (!config->expected_advertised_alpn.empty() &&
  134. (config->expected_advertised_alpn.size() != inlen ||
  135. memcmp(config->expected_advertised_alpn.data(),
  136. in, inlen) != 0)) {
  137. fprintf(stderr, "bad ALPN select callback inputs\n");
  138. exit(1);
  139. }
  140. *out = (const uint8_t*)config->select_alpn.data();
  141. *outlen = config->select_alpn.size();
  142. return SSL_TLSEXT_ERR_OK;
  143. }
  144. static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) {
  145. if (*cookie_len < 32) {
  146. fprintf(stderr, "Insufficient space for cookie\n");
  147. return 0;
  148. }
  149. *cookie_len = 32;
  150. memset(cookie, 42, *cookie_len);
  151. return 1;
  152. }
  153. static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
  154. if (cookie_len != 32) {
  155. fprintf(stderr, "Cookie length mismatch.\n");
  156. return 0;
  157. }
  158. for (size_t i = 0; i < cookie_len; i++) {
  159. if (cookie[i] != 42) {
  160. fprintf(stderr, "Cookie mismatch.\n");
  161. return 0;
  162. }
  163. }
  164. return 1;
  165. }
  166. static unsigned psk_client_callback(SSL *ssl, const char *hint,
  167. char *out_identity,
  168. unsigned max_identity_len,
  169. uint8_t *out_psk, unsigned max_psk_len) {
  170. const TestConfig *config = GetConfigPtr(ssl);
  171. if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
  172. fprintf(stderr, "Server PSK hint did not match.\n");
  173. return 0;
  174. }
  175. // Account for the trailing '\0' for the identity.
  176. if (config->psk_identity.size() >= max_identity_len ||
  177. config->psk.size() > max_psk_len) {
  178. fprintf(stderr, "PSK buffers too small\n");
  179. return 0;
  180. }
  181. BUF_strlcpy(out_identity, config->psk_identity.c_str(),
  182. max_identity_len);
  183. memcpy(out_psk, config->psk.data(), config->psk.size());
  184. return config->psk.size();
  185. }
  186. static unsigned psk_server_callback(SSL *ssl, const char *identity,
  187. uint8_t *out_psk, unsigned max_psk_len) {
  188. const TestConfig *config = GetConfigPtr(ssl);
  189. if (strcmp(identity, config->psk_identity.c_str()) != 0) {
  190. fprintf(stderr, "Client PSK identity did not match.\n");
  191. return 0;
  192. }
  193. if (config->psk.size() > max_psk_len) {
  194. fprintf(stderr, "PSK buffers too small\n");
  195. return 0;
  196. }
  197. memcpy(out_psk, config->psk.data(), config->psk.size());
  198. return config->psk.size();
  199. }
  200. static void current_time_cb(SSL *ssl, OPENSSL_timeval *out_clock) {
  201. *out_clock = *GetClockPtr(ssl);
  202. }
  203. static SSL_CTX *setup_ctx(const TestConfig *config) {
  204. SSL_CTX *ssl_ctx = NULL;
  205. DH *dh = NULL;
  206. ssl_ctx = SSL_CTX_new(config->is_dtls ? DTLS_method() : TLS_method());
  207. if (ssl_ctx == NULL) {
  208. goto err;
  209. }
  210. if (config->is_dtls) {
  211. // DTLS needs read-ahead to function on a datagram BIO.
  212. //
  213. // TODO(davidben): this should not be necessary. DTLS code should only
  214. // expect a datagram BIO.
  215. SSL_CTX_set_read_ahead(ssl_ctx, 1);
  216. }
  217. if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
  218. goto err;
  219. }
  220. if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
  221. goto err;
  222. }
  223. dh = DH_get_2048_256(NULL);
  224. if (dh == NULL ||
  225. !SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
  226. goto err;
  227. }
  228. SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
  229. ssl_ctx->select_certificate_cb = select_certificate_callback;
  230. SSL_CTX_set_next_protos_advertised_cb(
  231. ssl_ctx, next_protos_advertised_callback, NULL);
  232. if (!config->select_next_proto.empty()) {
  233. SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
  234. }
  235. if (!config->select_alpn.empty()) {
  236. SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
  237. }
  238. SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
  239. SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
  240. ssl_ctx->tlsext_channel_id_enabled_new = 1;
  241. ssl_ctx->current_time_cb = current_time_cb;
  242. DH_free(dh);
  243. return ssl_ctx;
  244. err:
  245. if (dh != NULL) {
  246. DH_free(dh);
  247. }
  248. if (ssl_ctx != NULL) {
  249. SSL_CTX_free(ssl_ctx);
  250. }
  251. return NULL;
  252. }
  253. static int retry_async(SSL *ssl, int ret, BIO *bio,
  254. OPENSSL_timeval *clock_delta) {
  255. // No error; don't retry.
  256. if (ret >= 0) {
  257. return 0;
  258. }
  259. if (clock_delta->tv_usec != 0 || clock_delta->tv_sec != 0) {
  260. // Process the timeout and retry.
  261. OPENSSL_timeval *clock = GetClockPtr(ssl);
  262. clock->tv_usec += clock_delta->tv_usec;
  263. clock->tv_sec += clock->tv_usec / 1000000;
  264. clock->tv_usec %= 1000000;
  265. clock->tv_sec += clock_delta->tv_sec;
  266. memset(clock_delta, 0, sizeof(*clock_delta));
  267. if (DTLSv1_handle_timeout(ssl) < 0) {
  268. printf("Error retransmitting.\n");
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. // See if we needed to read or write more. If so, allow one byte through on
  274. // the appropriate end to maximally stress the state machine.
  275. int err = SSL_get_error(ssl, ret);
  276. if (err == SSL_ERROR_WANT_READ) {
  277. async_bio_allow_read(bio, 1);
  278. return 1;
  279. } else if (err == SSL_ERROR_WANT_WRITE) {
  280. async_bio_allow_write(bio, 1);
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. static int do_exchange(SSL_SESSION **out_session,
  286. SSL_CTX *ssl_ctx,
  287. const TestConfig *config,
  288. bool is_resume,
  289. int fd,
  290. SSL_SESSION *session) {
  291. early_callback_called = 0;
  292. OPENSSL_timeval clock = {0}, clock_delta = {0};
  293. SSL *ssl = SSL_new(ssl_ctx);
  294. if (ssl == NULL) {
  295. BIO_print_errors_fp(stdout);
  296. return 1;
  297. }
  298. if (!SetConfigPtr(ssl, config) ||
  299. !SetClockPtr(ssl, &clock)) {
  300. BIO_print_errors_fp(stdout);
  301. return 1;
  302. }
  303. if (config->fallback_scsv) {
  304. if (!SSL_enable_fallback_scsv(ssl)) {
  305. BIO_print_errors_fp(stdout);
  306. return 1;
  307. }
  308. }
  309. if (!config->key_file.empty()) {
  310. if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
  311. SSL_FILETYPE_PEM)) {
  312. BIO_print_errors_fp(stdout);
  313. return 1;
  314. }
  315. }
  316. if (!config->cert_file.empty()) {
  317. if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
  318. SSL_FILETYPE_PEM)) {
  319. BIO_print_errors_fp(stdout);
  320. return 1;
  321. }
  322. }
  323. if (config->require_any_client_certificate) {
  324. SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  325. skip_verify);
  326. }
  327. if (config->false_start) {
  328. SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
  329. }
  330. if (config->cbc_record_splitting) {
  331. SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
  332. }
  333. if (config->partial_write) {
  334. SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
  335. }
  336. if (config->no_tls12) {
  337. SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
  338. }
  339. if (config->no_tls11) {
  340. SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
  341. }
  342. if (config->no_tls1) {
  343. SSL_set_options(ssl, SSL_OP_NO_TLSv1);
  344. }
  345. if (config->no_ssl3) {
  346. SSL_set_options(ssl, SSL_OP_NO_SSLv3);
  347. }
  348. if (config->cookie_exchange) {
  349. SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
  350. }
  351. if (config->tls_d5_bug) {
  352. SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
  353. }
  354. if (config->allow_unsafe_legacy_renegotiation) {
  355. SSL_set_options(ssl, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  356. }
  357. if (!config->expected_channel_id.empty()) {
  358. SSL_enable_tls_channel_id(ssl);
  359. }
  360. if (!config->send_channel_id.empty()) {
  361. EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
  362. if (pkey == NULL) {
  363. BIO_print_errors_fp(stdout);
  364. return 1;
  365. }
  366. SSL_enable_tls_channel_id(ssl);
  367. if (!SSL_set1_tls_channel_id(ssl, pkey)) {
  368. EVP_PKEY_free(pkey);
  369. BIO_print_errors_fp(stdout);
  370. return 1;
  371. }
  372. EVP_PKEY_free(pkey);
  373. }
  374. if (!config->host_name.empty()) {
  375. SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
  376. }
  377. if (!config->advertise_alpn.empty()) {
  378. SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
  379. config->advertise_alpn.size());
  380. }
  381. if (!config->psk.empty()) {
  382. SSL_set_psk_client_callback(ssl, psk_client_callback);
  383. SSL_set_psk_server_callback(ssl, psk_server_callback);
  384. }
  385. if (!config->psk_identity.empty() &&
  386. !SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
  387. BIO_print_errors_fp(stdout);
  388. return 1;
  389. }
  390. if (!config->srtp_profiles.empty() &&
  391. !SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
  392. BIO_print_errors_fp(stdout);
  393. return 1;
  394. }
  395. if (config->enable_ocsp_stapling &&
  396. !SSL_enable_ocsp_stapling(ssl)) {
  397. BIO_print_errors_fp(stdout);
  398. return 1;
  399. }
  400. if (config->enable_signed_cert_timestamps &&
  401. !SSL_enable_signed_cert_timestamps(ssl)) {
  402. BIO_print_errors_fp(stdout);
  403. return 1;
  404. }
  405. SSL_enable_fastradio_padding(ssl, config->fastradio_padding);
  406. if (config->min_version != 0) {
  407. SSL_set_min_version(ssl, (uint16_t)config->min_version);
  408. }
  409. if (config->max_version != 0) {
  410. SSL_set_max_version(ssl, (uint16_t)config->max_version);
  411. }
  412. if (config->mtu != 0) {
  413. SSL_set_options(ssl, SSL_OP_NO_QUERY_MTU);
  414. SSL_set_mtu(ssl, config->mtu);
  415. }
  416. BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
  417. if (bio == NULL) {
  418. BIO_print_errors_fp(stdout);
  419. return 1;
  420. }
  421. if (config->is_dtls) {
  422. BIO *packeted = packeted_bio_create(&clock_delta);
  423. BIO_push(packeted, bio);
  424. bio = packeted;
  425. }
  426. if (config->async) {
  427. BIO *async =
  428. config->is_dtls ? async_bio_create_datagram() : async_bio_create();
  429. BIO_push(async, bio);
  430. bio = async;
  431. }
  432. SSL_set_bio(ssl, bio, bio);
  433. if (session != NULL) {
  434. if (SSL_set_session(ssl, session) != 1) {
  435. fprintf(stderr, "failed to set session\n");
  436. return 2;
  437. }
  438. }
  439. int ret;
  440. do {
  441. if (config->is_server) {
  442. ret = SSL_accept(ssl);
  443. } else {
  444. ret = SSL_connect(ssl);
  445. }
  446. } while (config->async && retry_async(ssl, ret, bio, &clock_delta));
  447. if (ret != 1) {
  448. SSL_free(ssl);
  449. BIO_print_errors_fp(stdout);
  450. return 2;
  451. }
  452. if (is_resume && (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
  453. fprintf(stderr, "session was%s reused\n",
  454. SSL_session_reused(ssl) ? "" : " not");
  455. return 2;
  456. }
  457. if (!config->expected_server_name.empty()) {
  458. const char *server_name =
  459. SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  460. if (server_name != config->expected_server_name) {
  461. fprintf(stderr, "servername mismatch (got %s; want %s)\n",
  462. server_name, config->expected_server_name.c_str());
  463. return 2;
  464. }
  465. if (!early_callback_called) {
  466. fprintf(stderr, "early callback not called\n");
  467. return 2;
  468. }
  469. }
  470. if (!config->expected_certificate_types.empty()) {
  471. uint8_t *certificate_types;
  472. int num_certificate_types =
  473. SSL_get0_certificate_types(ssl, &certificate_types);
  474. if (num_certificate_types !=
  475. (int)config->expected_certificate_types.size() ||
  476. memcmp(certificate_types,
  477. config->expected_certificate_types.data(),
  478. num_certificate_types) != 0) {
  479. fprintf(stderr, "certificate types mismatch\n");
  480. return 2;
  481. }
  482. }
  483. if (!config->expected_next_proto.empty()) {
  484. const uint8_t *next_proto;
  485. unsigned next_proto_len;
  486. SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
  487. if (next_proto_len != config->expected_next_proto.size() ||
  488. memcmp(next_proto, config->expected_next_proto.data(),
  489. next_proto_len) != 0) {
  490. fprintf(stderr, "negotiated next proto mismatch\n");
  491. return 2;
  492. }
  493. }
  494. if (!config->expected_alpn.empty()) {
  495. const uint8_t *alpn_proto;
  496. unsigned alpn_proto_len;
  497. SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
  498. if (alpn_proto_len != config->expected_alpn.size() ||
  499. memcmp(alpn_proto, config->expected_alpn.data(),
  500. alpn_proto_len) != 0) {
  501. fprintf(stderr, "negotiated alpn proto mismatch\n");
  502. return 2;
  503. }
  504. }
  505. if (!config->expected_channel_id.empty()) {
  506. uint8_t channel_id[64];
  507. if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
  508. fprintf(stderr, "no channel id negotiated\n");
  509. return 2;
  510. }
  511. if (config->expected_channel_id.size() != 64 ||
  512. memcmp(config->expected_channel_id.data(),
  513. channel_id, 64) != 0) {
  514. fprintf(stderr, "channel id mismatch\n");
  515. return 2;
  516. }
  517. }
  518. if (config->expect_extended_master_secret) {
  519. if (!ssl->session->extended_master_secret) {
  520. fprintf(stderr, "No EMS for session when expected");
  521. return 2;
  522. }
  523. }
  524. if (!config->expected_ocsp_response.empty()) {
  525. const uint8_t *data;
  526. size_t len;
  527. SSL_get0_ocsp_response(ssl, &data, &len);
  528. if (config->expected_ocsp_response.size() != len ||
  529. memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
  530. fprintf(stderr, "OCSP response mismatch\n");
  531. return 2;
  532. }
  533. }
  534. if (!config->expected_signed_cert_timestamps.empty()) {
  535. const uint8_t *data;
  536. size_t len;
  537. SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
  538. if (config->expected_signed_cert_timestamps.size() != len ||
  539. memcmp(config->expected_signed_cert_timestamps.data(),
  540. data, len) != 0) {
  541. fprintf(stderr, "SCT list mismatch\n");
  542. return 2;
  543. }
  544. }
  545. if (config->renegotiate) {
  546. if (config->async) {
  547. fprintf(stderr, "--renegotiate is not supported with --async.\n");
  548. return 2;
  549. }
  550. SSL_renegotiate(ssl);
  551. ret = SSL_do_handshake(ssl);
  552. if (ret != 1) {
  553. SSL_free(ssl);
  554. BIO_print_errors_fp(stdout);
  555. return 2;
  556. }
  557. SSL_set_state(ssl, SSL_ST_ACCEPT);
  558. ret = SSL_do_handshake(ssl);
  559. if (ret != 1) {
  560. SSL_free(ssl);
  561. BIO_print_errors_fp(stdout);
  562. return 2;
  563. }
  564. }
  565. if (config->write_different_record_sizes) {
  566. if (config->is_dtls) {
  567. fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
  568. return 6;
  569. }
  570. // This mode writes a number of different record sizes in an attempt to
  571. // trip up the CBC record splitting code.
  572. uint8_t buf[32769];
  573. memset(buf, 0x42, sizeof(buf));
  574. static const size_t kRecordSizes[] = {
  575. 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
  576. for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
  577. i++) {
  578. int w;
  579. const size_t len = kRecordSizes[i];
  580. size_t off = 0;
  581. if (len > sizeof(buf)) {
  582. fprintf(stderr, "Bad kRecordSizes value.\n");
  583. return 5;
  584. }
  585. do {
  586. w = SSL_write(ssl, buf + off, len - off);
  587. if (w > 0) {
  588. off += (size_t) w;
  589. }
  590. } while ((config->async && retry_async(ssl, w, bio, &clock_delta)) ||
  591. (w > 0 && off < len));
  592. if (w < 0 || off != len) {
  593. SSL_free(ssl);
  594. BIO_print_errors_fp(stdout);
  595. return 4;
  596. }
  597. }
  598. } else {
  599. if (config->shim_writes_first) {
  600. int w;
  601. do {
  602. w = SSL_write(ssl, "hello", 5);
  603. } while (config->async && retry_async(ssl, w, bio, &clock_delta));
  604. }
  605. for (;;) {
  606. uint8_t buf[512];
  607. int n;
  608. do {
  609. n = SSL_read(ssl, buf, sizeof(buf));
  610. } while (config->async && retry_async(ssl, n, bio, &clock_delta));
  611. int err = SSL_get_error(ssl, n);
  612. if (err == SSL_ERROR_ZERO_RETURN ||
  613. (n == 0 && err == SSL_ERROR_SYSCALL)) {
  614. if (n != 0) {
  615. fprintf(stderr, "Invalid SSL_get_error output\n");
  616. return 3;
  617. }
  618. /* Accept shutdowns with or without close_notify.
  619. * TODO(davidben): Write tests which distinguish these two cases. */
  620. break;
  621. } else if (err != SSL_ERROR_NONE) {
  622. if (n > 0) {
  623. fprintf(stderr, "Invalid SSL_get_error output\n");
  624. return 3;
  625. }
  626. SSL_free(ssl);
  627. BIO_print_errors_fp(stdout);
  628. return 3;
  629. }
  630. /* Successfully read data. */
  631. if (n <= 0) {
  632. fprintf(stderr, "Invalid SSL_get_error output\n");
  633. return 3;
  634. }
  635. for (int i = 0; i < n; i++) {
  636. buf[i] ^= 0xff;
  637. }
  638. int w;
  639. do {
  640. w = SSL_write(ssl, buf, n);
  641. } while (config->async && retry_async(ssl, w, bio, &clock_delta));
  642. if (w != n) {
  643. SSL_free(ssl);
  644. BIO_print_errors_fp(stdout);
  645. return 4;
  646. }
  647. }
  648. }
  649. if (out_session) {
  650. *out_session = SSL_get1_session(ssl);
  651. }
  652. SSL_shutdown(ssl);
  653. SSL_free(ssl);
  654. return 0;
  655. }
  656. int main(int argc, char **argv) {
  657. #if !defined(OPENSSL_WINDOWS)
  658. signal(SIGPIPE, SIG_IGN);
  659. #endif
  660. if (!SSL_library_init()) {
  661. return 1;
  662. }
  663. g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  664. g_ex_data_clock_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  665. if (g_ex_data_index < 0 || g_ex_data_clock_index < 0) {
  666. return 1;
  667. }
  668. TestConfig config;
  669. if (!ParseConfig(argc - 1, argv + 1, &config)) {
  670. return usage(argv[0]);
  671. }
  672. SSL_CTX *ssl_ctx = setup_ctx(&config);
  673. if (ssl_ctx == NULL) {
  674. BIO_print_errors_fp(stdout);
  675. return 1;
  676. }
  677. SSL_SESSION *session = NULL;
  678. int ret = do_exchange(&session,
  679. ssl_ctx, &config,
  680. false /* is_resume */,
  681. 3 /* fd */, NULL /* session */);
  682. if (ret != 0) {
  683. goto out;
  684. }
  685. if (config.resume) {
  686. ret = do_exchange(NULL,
  687. ssl_ctx, &config,
  688. true /* is_resume */,
  689. 4 /* fd */,
  690. config.is_server ? NULL : session);
  691. if (ret != 0) {
  692. goto out;
  693. }
  694. }
  695. ret = 0;
  696. out:
  697. SSL_SESSION_free(session);
  698. SSL_CTX_free(ssl_ctx);
  699. return ret;
  700. }