Coverage Report

Created: 2026-08-14 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/fuzz/gnutls_psk_server_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 <stdint.h>
26
#include <unistd.h>
27
#include <string.h>
28
#include <stdlib.h>
29
30
#include <gnutls/gnutls.h>
31
32
#include "certs.h"
33
#include "psk.h"
34
#include "mem.h"
35
#include "fuzzer.h"
36
37
static int psk_cb(gnutls_session_t session, const char *username,
38
      gnutls_datum_t *key)
39
0
{
40
0
  key->data = (unsigned char *)gnutls_malloc(16);
41
0
  assert(key->data != NULL);
42
43
0
  memcpy(key->data, psk_key16, 16);
44
0
  key->size = 16;
45
46
0
  return 0;
47
0
}
48
49
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
50
{
51
  int res;
52
  gnutls_datum_t rsa_cert, rsa_key;
53
  gnutls_datum_t ecdsa_cert, ecdsa_key;
54
  gnutls_datum_t ed25519_cert, ed25519_key;
55
  gnutls_datum_t ed448_cert, ed448_key;
56
  gnutls_session_t session;
57
  gnutls_certificate_credentials_t xcred;
58
  gnutls_psk_server_credentials_t pcred;
59
  struct mem_st memdata;
60
61
  res = gnutls_init(&session, GNUTLS_SERVER);
62
  assert(res >= 0);
63
64
  res = gnutls_certificate_allocate_credentials(&xcred);
65
  assert(res >= 0);
66
67
  res = gnutls_psk_allocate_server_credentials(&pcred);
68
  assert(res >= 0);
69
70
  gnutls_psk_set_server_credentials_function(pcred, psk_cb);
71
  gnutls_psk_set_server_known_dh_params(pcred, GNUTLS_SEC_PARAM_MEDIUM);
72
73
  rsa_cert.data = (unsigned char *)kRSACertificateDER;
74
  rsa_cert.size = sizeof(kRSACertificateDER);
75
  rsa_key.data = (unsigned char *)kRSAPrivateKeyDER;
76
  rsa_key.size = sizeof(kRSAPrivateKeyDER);
77
78
  ecdsa_cert.data = (unsigned char *)kECDSACertificateDER;
79
  ecdsa_cert.size = sizeof(kECDSACertificateDER);
80
  ecdsa_key.data = (unsigned char *)kECDSAPrivateKeyDER;
81
  ecdsa_key.size = sizeof(kECDSAPrivateKeyDER);
82
83
  ed25519_cert.data = (unsigned char *)kEd25519CertificateDER;
84
  ed25519_cert.size = sizeof(kEd25519CertificateDER);
85
  ed25519_key.data = (unsigned char *)kEd25519PrivateKeyDER;
86
  ed25519_key.size = sizeof(kEd25519PrivateKeyDER);
87
88
  ed448_cert.data = (unsigned char *)kEd448CertificateDER;
89
  ed448_cert.size = sizeof(kEd448CertificateDER);
90
  ed448_key.data = (unsigned char *)kEd448PrivateKeyDER;
91
  ed448_key.size = sizeof(kEd448PrivateKeyDER);
92
93
  res = gnutls_certificate_set_x509_key_mem(xcred, &rsa_cert, &rsa_key,
94
              GNUTLS_X509_FMT_DER);
95
  assert(res >= 0);
96
97
  res = gnutls_certificate_set_x509_key_mem(
98
    xcred, &ecdsa_cert, &ecdsa_key, GNUTLS_X509_FMT_DER);
99
  assert(res >= 0);
100
101
  res = gnutls_certificate_set_x509_key_mem(
102
    xcred, &ed25519_cert, &ed25519_key, GNUTLS_X509_FMT_DER);
103
  assert(res >= 0);
104
105
  res = gnutls_certificate_set_x509_key_mem(
106
    xcred, &ed448_cert, &ed448_key, GNUTLS_X509_FMT_DER);
107
  assert(res >= 0);
108
109
  gnutls_certificate_set_known_dh_params(xcred, GNUTLS_SEC_PARAM_MEDIUM);
110
111
  res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
112
  assert(res >= 0);
113
114
  res = gnutls_credentials_set(session, GNUTLS_CRD_PSK, pcred);
115
  assert(res >= 0);
116
117
  res = gnutls_priority_set_direct(
118
    session,
119
    "NORMAL:-KX-ALL:+ECDHE-PSK:+DHE-PSK:+PSK:+RSA-PSK:" VERS_STR,
120
    NULL);
121
  assert(res >= 0);
122
123
  memdata.data = data;
124
  memdata.size = size;
125
126
  gnutls_transport_set_push_function(session, mem_push);
127
  gnutls_transport_set_pull_function(session, mem_pull);
128
  gnutls_transport_set_pull_timeout_function(session, mem_pull_timeout);
129
  gnutls_transport_set_ptr(session, &memdata);
130
131
  do {
132
    res = gnutls_handshake(session);
133
  } while (res < 0 && gnutls_error_is_fatal(res) == 0);
134
  if (res >= 0) {
135
    for (;;) {
136
      char buf[16384];
137
      res = gnutls_record_recv(session, buf, sizeof(buf));
138
      if (res <= 0) {
139
        break;
140
      }
141
    }
142
  }
143
144
  gnutls_deinit(session);
145
  gnutls_certificate_free_credentials(xcred);
146
  gnutls_psk_free_server_credentials(pcred);
147
  return 0;
148
}