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