Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/fuzz/targets/lib/tls/common.cc
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "common.h"
6
7
#include <cassert>
8
#include <cstddef>
9
#include <cstdint>
10
11
#include "prio.h"
12
#include "secport.h"
13
#include "ssl.h"
14
#include "sslexp.h"
15
16
157k
static PRTime FixedTime(void*) { return 1234; }
17
18
namespace TlsCommon {
19
20
// Fix the time input, to avoid any time-based variation.
21
61.2k
void FixTime(PRFileDesc* fd) {
22
61.2k
  SECStatus rv = SSL_SetTimeFunc(fd, FixedTime, nullptr);
23
61.2k
  assert(rv == SECSuccess);
24
61.2k
}
25
26
61.2k
void EnableAllProtocolVersions() {
27
61.2k
  SSLVersionRange supported;
28
61.2k
  SECStatus rv;
29
30
  // Enable all supported versions for TCP.
31
61.2k
  rv = SSL_VersionRangeGetSupported(ssl_variant_stream, &supported);
32
61.2k
  assert(rv == SECSuccess);
33
34
61.2k
  rv = SSL_VersionRangeSetDefault(ssl_variant_stream, &supported);
35
61.2k
  assert(rv == SECSuccess);
36
37
  // Enable all supported versions for UDP.
38
61.2k
  rv = SSL_VersionRangeGetSupported(ssl_variant_datagram, &supported);
39
61.2k
  assert(rv == SECSuccess);
40
41
61.2k
  rv = SSL_VersionRangeSetDefault(ssl_variant_datagram, &supported);
42
61.2k
  assert(rv == SECSuccess);
43
61.2k
}
44
45
61.2k
void EnableAllCipherSuites(PRFileDesc* fd) {
46
4.41M
  for (uint16_t i = 0; i < SSL_NumImplementedCiphers; ++i) {
47
4.35M
    SECStatus rv = SSL_CipherPrefSet(fd, SSL_ImplementedCiphers[i], true);
48
4.35M
    assert(rv == SECSuccess);
49
4.35M
  }
50
61.2k
}
51
52
61.2k
void DoHandshake(PRFileDesc* fd, bool isServer) {
53
61.2k
  SECStatus rv = SSL_ResetHandshake(fd, isServer);
54
61.2k
  assert(rv == SECSuccess);
55
56
79.2k
  do {
57
79.2k
    rv = SSL_ForceHandshake(fd);
58
79.2k
  } while (rv != SECSuccess && PR_GetError() == PR_WOULD_BLOCK_ERROR);
59
60
  // If the handshake succeeds, read application data and echo it back.
61
61.2k
  if (rv == SECSuccess) {
62
3.71k
    uint8_t block[1024];
63
3.71k
    int32_t nb;
64
65
734k
    while ((nb = PR_Read(fd, block, sizeof(block))) > 0) {
66
731k
      PR_Write(fd, block, nb);
67
731k
    }
68
3.71k
  }
69
61.2k
}
70
71
5
SECStatus DummyCompressionEncode(const SECItem* input, SECItem* output) {
72
5
  if (!input || !input->data || input->len == 0 || !output) {
73
0
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
74
0
    return SECFailure;
75
0
  }
76
77
5
  SECITEM_CopyItem(nullptr, output, input);
78
79
5
  return SECSuccess;
80
5
}
81
82
SECStatus DummyCompressionDecode(const SECItem* input, unsigned char* output,
83
16
                                 size_t outputLen, size_t* usedLen) {
84
16
  if (!input || !input->data || input->len == 0 || !output || outputLen == 0) {
85
0
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
86
0
    return SECFailure;
87
0
  }
88
89
16
  if (input->len > outputLen) {
90
1
    PORT_SetError(SEC_ERROR_BAD_DATA);
91
1
    return SECFailure;
92
1
  }
93
94
15
  PORT_Memcpy(output, input->data, input->len);
95
15
  *usedLen = input->len;
96
97
15
  return SECSuccess;
98
16
}
99
100
}  // namespace TlsCommon