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 
FixedTime(void *)12 static PRTime FixedTime(void*) { return 1234; }
13 
14 // Fix the time input, to avoid any time-based variation.
FixTime(PRFileDesc * fd)15 void FixTime(PRFileDesc* fd) {
16   SECStatus rv = SSL_SetTimeFunc(fd, FixedTime, nullptr);
17   assert(rv == SECSuccess);
18 }
19 
EnableAllProtocolVersions()20 PRStatus EnableAllProtocolVersions() {
21   SSLVersionRange supported;
22 
23   SECStatus rv = SSL_VersionRangeGetSupported(ssl_variant_stream, &supported);
24   assert(rv == SECSuccess);
25 
26   rv = SSL_VersionRangeSetDefault(ssl_variant_stream, &supported);
27   assert(rv == SECSuccess);
28 
29   return PR_SUCCESS;
30 }
31 
EnableAllCipherSuites(PRFileDesc * fd)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);
36   }
37 }
38 
DoHandshake(PRFileDesc * fd,bool isServer)39 void DoHandshake(PRFileDesc* fd, bool isServer) {
40   SECStatus rv = SSL_ResetHandshake(fd, isServer);
41   assert(rv == SECSuccess);
42 
43   do {
44     rv = SSL_ForceHandshake(fd);
45   } 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   if (rv == SECSuccess) {
49     uint8_t block[1024];
50     int32_t nb;
51 
52     // Read application data and echo it back.
53     while ((nb = PR_Read(fd, block, sizeof(block))) > 0) {
54       PR_Write(fd, block, nb);
55     }
56   }
57 }
58