fatalerrtest.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2017 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. int main(int argc, char *argv[])
  13. {
  14. SSL_CTX *sctx, *cctx;
  15. SSL *sssl, *cssl;
  16. const char *msg = "Dummy";
  17. BIO *err = NULL, *wbio = NULL;
  18. int ret = 1, len;
  19. char buf[80];
  20. unsigned char dummyrec[] = {
  21. 0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
  22. };
  23. if (argc != 3) {
  24. printf("Incorrect number of parameters\n");
  25. return 1;
  26. }
  27. SSL_library_init();
  28. SSL_load_error_strings();
  29. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  30. CRYPTO_malloc_debug_init();
  31. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  32. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  33. if (!create_ssl_ctx_pair(SSLv23_method(), SSLv23_method(), &sctx, &cctx,
  34. argv[1], argv[2])) {
  35. printf("Failed to create SSL_CTX pair\n");
  36. goto err;
  37. }
  38. /*
  39. * Deliberately set the cipher lists for client and server to be different
  40. * to force a handshake failure.
  41. */
  42. if (!SSL_CTX_set_cipher_list(sctx, "AES128-SHA")
  43. || !SSL_CTX_set_cipher_list(cctx, "AES256-SHA")) {
  44. printf("Failed to set cipher lists\n");
  45. goto err;
  46. }
  47. if (!create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) {
  48. printf("Failed to create SSL objectx\n");
  49. goto err;
  50. }
  51. wbio = SSL_get_wbio(cssl);
  52. if (wbio == NULL) {
  53. printf("Unexpected NULL bio received\n");
  54. goto err;
  55. }
  56. if (create_ssl_connection(sssl, cssl)) {
  57. printf("Unexpected success creating a connection\n");
  58. goto err;
  59. }
  60. ERR_clear_error();
  61. /* Inject a plaintext record from client to server */
  62. if (BIO_write(wbio, dummyrec, sizeof(dummyrec)) <= 0) {
  63. printf("Unexpected failure injecting dummy record\n");
  64. goto err;
  65. }
  66. /* SSL_read()/SSL_write should fail because of a previous fatal error */
  67. if ((len = SSL_read(sssl, buf, sizeof(buf - 1))) > 0) {
  68. buf[len] = '\0';
  69. printf("Unexpected success reading data: %s\n", buf);
  70. goto err;
  71. }
  72. if (SSL_write(sssl, msg, strlen(msg)) > 0) {
  73. printf("Unexpected success writing data\n");
  74. goto err;
  75. }
  76. ret = 0;
  77. err:
  78. SSL_free(sssl);
  79. SSL_free(cssl);
  80. SSL_CTX_free(sctx);
  81. SSL_CTX_free(cctx);
  82. ERR_print_errors_fp(stderr);
  83. if (ret) {
  84. printf("Fatal err test: FAILED\n");
  85. }
  86. ERR_free_strings();
  87. ERR_remove_thread_state(NULL);
  88. EVP_cleanup();
  89. CRYPTO_cleanup_all_ex_data();
  90. CRYPTO_mem_leaks(err);
  91. BIO_free(err);
  92. return ret;
  93. }