fatalerrtest.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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(NULL, TLS_method(), TLS_method(),
  28. TLS1_VERSION, 0,
  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. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  73. int setup_tests(void)
  74. {
  75. if (!test_skip_common_options()) {
  76. TEST_error("Error parsing test options\n");
  77. return 0;
  78. }
  79. if (!TEST_ptr(cert = test_get_argument(0))
  80. || !TEST_ptr(privkey = test_get_argument(1)))
  81. return 0;
  82. ADD_TEST(test_fatalerr);
  83. return 1;
  84. }