Bug 1885489 - Part 9: Add SnapshotIterator::readObject(). r=iain
[gecko.git] / security / nss / fuzz / tls_common.cc
blobb00ab26bf63d9c48a7ed69f6f2deec515cad63b5
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/. */
5 #include <assert.h>
7 #include "ssl.h"
8 #include "sslexp.h"
10 #include "tls_common.h"
12 static PRTime FixedTime(void*) { return 1234; }
14 // Fix the time input, to avoid any time-based variation.
15 void FixTime(PRFileDesc* fd) {
16 SECStatus rv = SSL_SetTimeFunc(fd, FixedTime, nullptr);
17 assert(rv == SECSuccess);
20 PRStatus EnableAllProtocolVersions() {
21 SSLVersionRange supported;
23 SECStatus rv = SSL_VersionRangeGetSupported(ssl_variant_stream, &supported);
24 assert(rv == SECSuccess);
26 rv = SSL_VersionRangeSetDefault(ssl_variant_stream, &supported);
27 assert(rv == SECSuccess);
29 return PR_SUCCESS;
32 void EnableAllCipherSuites(PRFileDesc* fd) {
33 for (uint16_t i = 0; i < SSL_NumImplementedCiphers; ++i) {
34 SECStatus rv = SSL_CipherPrefSet(fd, SSL_ImplementedCiphers[i], true);
35 assert(rv == SECSuccess);
39 void DoHandshake(PRFileDesc* fd, bool isServer) {
40 SECStatus rv = SSL_ResetHandshake(fd, isServer);
41 assert(rv == SECSuccess);
43 do {
44 rv = SSL_ForceHandshake(fd);
45 } while (rv != SECSuccess && PR_GetError() == PR_WOULD_BLOCK_ERROR);
47 // If the handshake succeeds, let's read some data from the server, if any.
48 if (rv == SECSuccess) {
49 uint8_t block[1024];
50 int32_t nb;
52 // Read application data and echo it back.
53 while ((nb = PR_Read(fd, block, sizeof(block))) > 0) {
54 PR_Write(fd, block, nb);