Coverage Report

Created: 2026-06-08 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/fuzz/gnutls_server_rawpk_fuzzer.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2017 Nikos Mavrogiannopoulos
3
 * Copyright (C) 2019 Tom Vrancken (dev@tomvrancken.nl)
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/***
26
 * This fuzzer tests the behavior of the GnuTLS library in server mode,
27
 * specifically dealing with raw public keys during the handshake.
28
 * 
29
 * The fuzzer corpus generated as first input was generated with the
30
 * following parameters for gnutls-serv and gnutls-cli:
31
 * 
32
 * gnutls-serv --priority NORMAL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK
33
 * gnutls-cli localhost:5556 --priority NORMAL:-CTYPE-ALL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK --no-ca-verification
34
 * 
35
 * The above yields a handshake where both the client and server present
36
 * a raw public-key to each other.
37
 */
38
39
#include <assert.h>
40
#include <stdint.h>
41
#include <unistd.h>
42
#include <string.h>
43
#include <stdlib.h>
44
45
#include <gnutls/gnutls.h>
46
47
#include "certs.h"
48
#include "mem.h"
49
#include "fuzzer.h"
50
51
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
52
8.53k
{
53
8.53k
  IGNORE_CERTS;
54
8.53k
  int res;
55
8.53k
  gnutls_session_t session;
56
8.53k
  gnutls_certificate_credentials_t rawpk_cred;
57
8.53k
  struct mem_st memdata;
58
59
8.53k
  res = gnutls_init(&session, GNUTLS_SERVER | GNUTLS_ENABLE_RAWPK);
60
8.53k
  assert(res >= 0);
61
62
8.53k
  res = gnutls_certificate_allocate_credentials(&rawpk_cred);
63
8.53k
  assert(res >= 0);
64
65
8.53k
  res = gnutls_certificate_set_rawpk_key_mem(
66
8.53k
    rawpk_cred, &rawpk_public_key1, &rawpk_private_key1,
67
8.53k
    GNUTLS_X509_FMT_PEM, NULL, 0, NULL, 0, 0);
68
8.53k
  assert(res >= 0);
69
70
8.53k
  gnutls_certificate_set_known_dh_params(rawpk_cred,
71
8.53k
                 GNUTLS_SEC_PARAM_MEDIUM);
72
73
8.53k
  res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
74
8.53k
             rawpk_cred);
75
8.53k
  assert(res >= 0);
76
77
8.53k
  res = gnutls_priority_set_direct(
78
8.53k
    session,
79
8.53k
    "NORMAL:" VERS_STR
80
8.53k
    ":-CTYPE-ALL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK",
81
8.53k
    NULL);
82
8.53k
  assert(res >= 0);
83
84
8.53k
  memdata.data = data;
85
8.53k
  memdata.size = size;
86
87
8.53k
  gnutls_transport_set_push_function(session, mem_push);
88
8.53k
  gnutls_transport_set_pull_function(session, mem_pull);
89
8.53k
  gnutls_transport_set_pull_timeout_function(session, mem_pull_timeout);
90
8.53k
  gnutls_transport_set_ptr(session, &memdata);
91
92
536k
  do {
93
536k
    res = gnutls_handshake(session);
94
536k
  } while (res < 0 && gnutls_error_is_fatal(res) == 0);
95
8.53k
  if (res >= 0) {
96
1
    for (;;) {
97
1
      char buf[16384];
98
1
      res = gnutls_record_recv(session, buf, sizeof(buf));
99
1
      if (res <= 0) {
100
1
        break;
101
1
      }
102
1
    }
103
1
  }
104
105
8.53k
  gnutls_deinit(session);
106
8.53k
  gnutls_certificate_free_credentials(rawpk_cred);
107
8.53k
  return 0;
108
8.53k
}