fuzz_pb_tnc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2018 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <library.h>
  16. #include <batch/pb_tnc_batch.h>
  17. #include <messages/ietf/pb_error_msg.h>
  18. #include <state_machine/pb_tnc_state_machine.h>
  19. #include <utils/debug.h>
  20. int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
  21. {
  22. pb_tnc_batch_t *batch;
  23. pb_tnc_state_machine_t *state;
  24. pb_tnc_msg_t *msg;
  25. pb_error_msg_t *error;
  26. enumerator_t *enumerator;
  27. bool from_server;
  28. chunk_t chunk;
  29. dbg_default_set_level(-1);
  30. library_init(NULL, "fuzz_pb_tnc");
  31. plugin_loader_add_plugindirs(PLUGINDIR, PLUGINS);
  32. if (!lib->plugins->load(lib->plugins, PLUGINS))
  33. {
  34. return 1;
  35. }
  36. chunk = chunk_create((u_char*)buf, len);
  37. INIT(state,
  38. .receive_batch = (void*)return_true,
  39. .set_empty_cdata = (void*)nop,
  40. );
  41. /* parse incoming PB-TNC batch */
  42. batch = pb_tnc_batch_create_from_data(chunk);
  43. if (batch->process_header(batch, TRUE, FALSE, &from_server) == SUCCESS ||
  44. batch->process_header(batch, TRUE, TRUE, &from_server) == SUCCESS)
  45. {
  46. batch->process(batch, state);
  47. }
  48. /* enumerate correctly decoded PB-TNC messages */
  49. enumerator = batch->create_msg_enumerator(batch);
  50. while (enumerator->enumerate(enumerator, &msg))
  51. {
  52. msg->get_type(msg);
  53. }
  54. enumerator->destroy(enumerator);
  55. /* enumerate errors detected while parsing PB-TNC batch and messages */
  56. enumerator = batch->create_error_enumerator(batch);
  57. while (enumerator->enumerate(enumerator, &msg))
  58. {
  59. error = (pb_error_msg_t*)msg;
  60. error->get_error_code(error);
  61. }
  62. enumerator->destroy(enumerator);
  63. batch->destroy(batch);
  64. free(state);
  65. lib->plugins->unload(lib->plugins);
  66. library_deinit();
  67. return 0;
  68. }