Coverage Report

Created: 2026-08-18 06:34

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
173k
static PRTime FixedTime(void*) { return 1234; }
17
18
namespace TlsCommon {
19
20
// Fix the time input, to avoid any time-based variation.
21
62.8k
void FixTime(PRFileDesc* fd) {
22
62.8k
  SECStatus rv = SSL_SetTimeFunc(fd, FixedTime, nullptr);
23
62.8k
  assert(rv == SECSuccess);
24
62.8k
}
25
26
62.8k
void EnableAllProtocolVersions() {
27
62.8k
  SSLVersionRange supported;
28
62.8k
  SECStatus rv;
29
30
  // Enable all supported versions for TCP.
31
62.8k
  rv = SSL_VersionRangeGetSupported(ssl_variant_stream, &supported);
32
62.8k
  assert(rv == SECSuccess);
33
34
62.8k
  rv = SSL_VersionRangeSetDefault(ssl_variant_stream, &supported);
35
62.8k
  assert(rv == SECSuccess);
36
37
  // Enable all supported versions for UDP.
38
62.8k
  rv = SSL_VersionRangeGetSupported(ssl_variant_datagram, &supported);
39
62.8k
  assert(rv == SECSuccess);
40
41
62.8k
  rv = SSL_VersionRangeSetDefault(ssl_variant_datagram, &supported);
42
62.8k
  assert(rv == SECSuccess);
43
62.8k
}
44
45
62.8k
void EnableAllCipherSuites(PRFileDesc* fd) {
46
4.52M
  for (uint16_t i = 0; i < SSL_NumImplementedCiphers; ++i) {
47
4.46M
    SECStatus rv = SSL_CipherPrefSet(fd, SSL_ImplementedCiphers[i], true);
48
4.46M
    assert(rv == SECSuccess);
49
4.46M
  }
50
62.8k
}
51
52
62.8k
void DoHandshake(PRFileDesc* fd, bool isServer) {
53
62.8k
  SECStatus rv = SSL_ResetHandshake(fd, isServer);
54
62.8k
  assert(rv == SECSuccess);
55
56
81.0k
  do {
57
81.0k
    rv = SSL_ForceHandshake(fd);
58
81.0k
  } while (rv != SECSuccess && PR_GetError() == PR_WOULD_BLOCK_ERROR);
59
60
  // If the handshake succeeds, read application data and echo it back.
61
62.8k
  if (rv == SECSuccess) {
62
4.45k
    uint8_t block[1024];
63
4.45k
    int32_t nb;
64
65
525k
    while ((nb = PR_Read(fd, block, sizeof(block))) > 0) {
66
521k
      PR_Write(fd, block, nb);
67
521k
    }
68
4.45k
  }
69
62.8k
}
70
71
6
SECStatus DummyCompressionEncode(const SECItem* input, SECItem* output) {
72
6
  if (!input || !input->data || input->len == 0 || !output) {
73
0
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
74
0
    return SECFailure;
75
0
  }
76
77
6
  SECITEM_CopyItem(nullptr, output, input);
78
79
6
  return SECSuccess;
80
6
}
81
82
SECStatus DummyCompressionDecode(const SECItem* input, unsigned char* output,
83
0
                                 size_t outputLen, size_t* usedLen) {
84
0
  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
0
  if (input->len > outputLen) {
90
0
    PORT_SetError(SEC_ERROR_BAD_DATA);
91
0
    return SECFailure;
92
0
  }
93
94
0
  PORT_Memcpy(output, input->data, input->len);
95
0
  *usedLen = input->len;
96
97
0
  return SECSuccess;
98
0
}
99
100
}  // namespace TlsCommon