fuzz-stun.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <pjlib.h>
  22. #include <pjlib-util.h>
  23. #include <pjnath.h>
  24. #define kMinInputLength 10
  25. #define kMaxInputLength 5120
  26. pj_pool_factory *mem;
  27. int stun_parse(uint8_t *data,size_t Size) {
  28. pj_status_t status;
  29. pj_pool_t *pool;
  30. pj_stun_msg *msg;
  31. pj_stun_auth_cred cred;
  32. const pj_str_t USERNAME = {"A", 1};
  33. const pj_str_t PASSWORD = {"A", 1};
  34. pool = pj_pool_create(mem, "decode_test", 1024, 1024, NULL);
  35. status = pj_stun_msg_decode(pool, data, Size, PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, &msg, NULL, NULL);
  36. if(status != PJ_SUCCESS){
  37. goto ret_error;
  38. }
  39. pj_bzero(&cred, sizeof(cred));
  40. cred.type = PJ_STUN_AUTH_CRED_STATIC;
  41. cred.data.static_cred.username = USERNAME;
  42. cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
  43. cred.data.static_cred.data = PASSWORD;
  44. pj_stun_authenticate_request(data, (unsigned)Size, msg, &cred, pool, NULL, NULL);
  45. pj_pool_release(pool);
  46. return status;
  47. ret_error:
  48. pj_pool_release(pool);
  49. return status;
  50. }
  51. extern int
  52. LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  53. {
  54. if (Size < kMinInputLength || Size > kMaxInputLength) {
  55. return 1;
  56. }
  57. int ret = 0;
  58. uint8_t *data;
  59. pj_caching_pool caching_pool;
  60. /* Add NULL byte */
  61. data = (uint8_t *)calloc((Size+1), sizeof(uint8_t));
  62. memcpy((void *)data, (void *)Data, Size);
  63. /* init Calls */
  64. pj_init();
  65. pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0);
  66. pj_log_set_level(0);
  67. mem = &caching_pool.factory;
  68. /* Call fuzzer */
  69. ret = stun_parse(data, Size);
  70. free(data);
  71. pj_caching_pool_destroy(&caching_pool);
  72. return ret;
  73. }