2
0

fatalerrtest.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/ssl.h>
  10. #include <openssl/err.h>
  11. #include "ssltestlib.h"
  12. #include "testutil.h"
  13. #include <string.h>
  14. static char *cert = NULL;
  15. static char *privkey = NULL;
  16. static int test_fatalerr(void)
  17. {
  18. SSL_CTX *sctx = NULL, *cctx = NULL;
  19. SSL *sssl = NULL, *cssl = NULL;
  20. const char *msg = "Dummy";
  21. BIO *wbio = NULL;
  22. int ret = 0, len;
  23. char buf[80];
  24. unsigned char dummyrec[] = {
  25. 0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
  26. };
  27. if (!TEST_true(create_ssl_ctx_pair(TLS_method(), TLS_method(),
  28. TLS1_VERSION, TLS_MAX_VERSION,
  29. &sctx, &cctx, cert, privkey)))
  30. goto err;
  31. /*
  32. * Deliberately set the cipher lists for client and server to be different
  33. * to force a handshake failure.
  34. */
  35. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))
  36. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))
  37. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  38. "TLS_AES_128_GCM_SHA256"))
  39. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  40. "TLS_AES_256_GCM_SHA384"))
  41. || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
  42. NULL)))
  43. goto err;
  44. wbio = SSL_get_wbio(cssl);
  45. if (!TEST_ptr(wbio)) {
  46. printf("Unexpected NULL bio received\n");
  47. goto err;
  48. }
  49. /* Connection should fail */
  50. if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
  51. goto err;
  52. ERR_clear_error();
  53. /* Inject a plaintext record from client to server */
  54. if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
  55. goto err;
  56. /* SSL_read()/SSL_write should fail because of a previous fatal error */
  57. if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
  58. buf[len] = '\0';
  59. TEST_error("Unexpected success reading data: %s\n", buf);
  60. goto err;
  61. }
  62. if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
  63. goto err;
  64. ret = 1;
  65. err:
  66. SSL_free(sssl);
  67. SSL_free(cssl);
  68. SSL_CTX_free(sctx);
  69. SSL_CTX_free(cctx);
  70. return ret;
  71. }
  72. int setup_tests(void)
  73. {
  74. if (!TEST_ptr(cert = test_get_argument(0))
  75. || !TEST_ptr(privkey = test_get_argument(1)))
  76. return 0;
  77. ADD_TEST(test_fatalerr);
  78. return 1;
  79. }