Coverage Report

Created: 2026-03-31 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/fuzz/gnutls_psk_client_fuzzer.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2017 Nikos Mavrogiannopoulos
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
 * DEALINGS IN THE SOFTWARE.
21
 *
22
 */
23
24
#include <assert.h>
25
#include <fcntl.h>
26
#include <stdint.h>
27
#include <sys/types.h>
28
#include <unistd.h>
29
#include <string.h>
30
#include <stdlib.h>
31
32
#include <gnutls/gnutls.h>
33
34
#include "psk.h"
35
#include "mem.h"
36
#include "fuzzer.h"
37
38
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39
8.66k
{
40
8.66k
  int res;
41
8.66k
  gnutls_session_t session;
42
8.66k
  gnutls_psk_client_credentials_t pcred;
43
8.66k
  struct mem_st memdata;
44
8.66k
  gnutls_datum_t psk_key;
45
46
8.66k
  psk_key.data = (unsigned char *)psk_key16;
47
8.66k
  psk_key.size = 16;
48
49
8.66k
  res = gnutls_init(&session, GNUTLS_CLIENT);
50
8.66k
  assert(res >= 0);
51
52
8.66k
  res = gnutls_psk_allocate_client_credentials(&pcred);
53
8.66k
  assert(res >= 0);
54
55
8.66k
  res = gnutls_psk_set_client_credentials(pcred, "test", &psk_key,
56
8.66k
            GNUTLS_PSK_KEY_RAW);
57
8.66k
  assert(res >= 0);
58
59
8.66k
  res = gnutls_credentials_set(session, GNUTLS_CRD_PSK, pcred);
60
8.66k
  assert(res >= 0);
61
62
8.66k
  res = gnutls_priority_set_direct(
63
8.66k
    session, "NORMAL:-KX-ALL:+ECDHE-PSK:+DHE-PSK:+PSK:" VERS_STR,
64
8.66k
    NULL);
65
8.66k
  assert(res >= 0);
66
67
8.66k
  memdata.data = data;
68
8.66k
  memdata.size = size;
69
70
8.66k
  gnutls_transport_set_push_function(session, mem_push);
71
8.66k
  gnutls_transport_set_pull_function(session, mem_pull);
72
8.66k
  gnutls_transport_set_pull_timeout_function(session, mem_pull_timeout);
73
8.66k
  gnutls_transport_set_ptr(session, &memdata);
74
75
465k
  do {
76
465k
    res = gnutls_handshake(session);
77
465k
  } while (res < 0 && gnutls_error_is_fatal(res) == 0);
78
8.66k
  if (res >= 0) {
79
1
    for (;;) {
80
1
      char buf[16384];
81
1
      res = gnutls_record_recv(session, buf, sizeof(buf));
82
1
      if (res <= 0) {
83
1
        break;
84
1
      }
85
1
    }
86
1
  }
87
88
8.66k
  gnutls_deinit(session);
89
8.66k
  gnutls_psk_free_client_credentials(pcred);
90
8.66k
  return 0;
91
8.66k
}