client.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <time.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/dsa.h>
  15. #include <openssl/ec.h>
  16. #include <openssl/dh.h>
  17. #include <openssl/err.h>
  18. #include "fuzzer.h"
  19. /* unused, to avoid warning. */
  20. static int idx;
  21. #define FUZZTIME 1485898104
  22. #define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
  23. /*
  24. * This might not work in all cases (and definitely not on Windows
  25. * because of the way linkers are) and callees can still get the
  26. * current time instead of the fixed time. This will just result
  27. * in things not being fully reproducible and have a slightly
  28. * different coverage.
  29. */
  30. #if !defined(_WIN32)
  31. time_t time(time_t *t) TIME_IMPL(t)
  32. #endif
  33. int FuzzerInitialize(int *argc, char ***argv)
  34. {
  35. STACK_OF(SSL_COMP) *comp_methods;
  36. FuzzerSetRand();
  37. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
  38. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
  39. ERR_clear_error();
  40. CRYPTO_free_ex_index(0, -1);
  41. idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  42. comp_methods = SSL_COMP_get_compression_methods();
  43. if (comp_methods != NULL)
  44. sk_SSL_COMP_sort(comp_methods);
  45. return 1;
  46. }
  47. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  48. {
  49. SSL *client = NULL;
  50. BIO *in;
  51. BIO *out;
  52. SSL_CTX *ctx;
  53. if (len == 0)
  54. return 0;
  55. /* This only fuzzes the initial flow from the client so far. */
  56. ctx = SSL_CTX_new(SSLv23_method());
  57. if (ctx == NULL)
  58. goto end;
  59. client = SSL_new(ctx);
  60. if (client == NULL)
  61. goto end;
  62. OPENSSL_assert(SSL_set_min_proto_version(client, 0) == 1);
  63. OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
  64. SSL_set_tlsext_host_name(client, "localhost");
  65. in = BIO_new(BIO_s_mem());
  66. if (in == NULL)
  67. goto end;
  68. out = BIO_new(BIO_s_mem());
  69. if (out == NULL) {
  70. BIO_free(in);
  71. goto end;
  72. }
  73. SSL_set_bio(client, in, out);
  74. SSL_set_connect_state(client);
  75. OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
  76. if (SSL_do_handshake(client) == 1) {
  77. /* Keep reading application data until error or EOF. */
  78. uint8_t tmp[1024];
  79. for (;;) {
  80. if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
  81. break;
  82. }
  83. }
  84. }
  85. end:
  86. SSL_free(client);
  87. ERR_clear_error();
  88. SSL_CTX_free(ctx);
  89. return 0;
  90. }
  91. void FuzzerCleanup(void)
  92. {
  93. FuzzerClearRand();
  94. }