Coverage Report

Created: 2024-05-20 06:23

/src/nss/fuzz/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 <assert.h>
6
7
#include "ssl.h"
8
#include "sslexp.h"
9
10
#include "tls_common.h"
11
12
47.2k
static PRTime FixedTime(void*) { return 1234; }
13
14
// Fix the time input, to avoid any time-based variation.
15
69.0k
void FixTime(PRFileDesc* fd) {
16
69.0k
  SECStatus rv = SSL_SetTimeFunc(fd, FixedTime, nullptr);
17
69.0k
  assert(rv == SECSuccess);
18
69.0k
}
19
20
36.4k
PRStatus EnableAllProtocolVersions() {
21
36.4k
  SSLVersionRange supported;
22
23
36.4k
  SECStatus rv = SSL_VersionRangeGetSupported(ssl_variant_stream, &supported);
24
36.4k
  assert(rv == SECSuccess);
25
26
36.4k
  rv = SSL_VersionRangeSetDefault(ssl_variant_stream, &supported);
27
36.4k
  assert(rv == SECSuccess);
28
29
36.4k
  return PR_SUCCESS;
30
36.4k
}
31
32
36.4k
void EnableAllCipherSuites(PRFileDesc* fd) {
33
2.62M
  for (uint16_t i = 0; i < SSL_NumImplementedCiphers; ++i) {
34
2.58M
    SECStatus rv = SSL_CipherPrefSet(fd, SSL_ImplementedCiphers[i], true);
35
2.58M
    assert(rv == SECSuccess);
36
2.58M
  }
37
36.4k
}
38
39
69.0k
void DoHandshake(PRFileDesc* fd, bool isServer) {
40
69.0k
  SECStatus rv = SSL_ResetHandshake(fd, isServer);
41
69.0k
  assert(rv == SECSuccess);
42
43
69.6k
  do {
44
69.6k
    rv = SSL_ForceHandshake(fd);
45
69.6k
  } while (rv != SECSuccess && PR_GetError() == PR_WOULD_BLOCK_ERROR);
46
47
  // If the handshake succeeds, let's read some data from the server, if any.
48
69.0k
  if (rv == SECSuccess) {
49
1.21k
    uint8_t block[1024];
50
1.21k
    int32_t nb;
51
52
    // Read application data and echo it back.
53
4.14k
    while ((nb = PR_Read(fd, block, sizeof(block))) > 0) {
54
2.92k
      PR_Write(fd, block, nb);
55
2.92k
    }
56
1.21k
  }
57
69.0k
}