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