Repository navigation
Expand file tree
/
Copy pathprotocol_tests.cpp
More file actions
190 lines (186 loc) · 8.03 KB
/
Copy pathprotocol_tests.cpp
File metadata and controls
190 lines (186 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "Common/Socket.h"
#include "Common/NetUtils.h"
#include "Common/CryptoConfig.h"
#include "Common/DiscoveryCommon.h"
#include "EasyConnect Client/ClientTCP.h"
#include "EasyConnect Client/ServerDiscovery.h"
#include "EasyConnect Server/ServerTCP.h"
#include "EasyConnect Server/ClientDiscovery.h"
#include <bcrypt.h>
#include <array>
#include <future>
#include <iostream>
#include <memory>
#include <stdexcept>
void require(bool ok, const char *message)
{
if (!ok)
throw std::runtime_error(message);
}
template <class F> void rejects(F action)
{
bool rejected = false;
try
{
action();
}
catch (const std::exception &)
{
rejected = true;
}
require(rejected, "Expected rejection.");
}
std::vector<unsigned char> referenceCbc(std::vector<unsigned char> input, unsigned keyBytes,
const std::array<unsigned char, 16> &iv)
{
BCRYPT_ALG_HANDLE algorithm = nullptr;
BCRYPT_KEY_HANDLE key = nullptr;
auto check = [](NTSTATUS result) { require(result >= 0, "Windows CNG reference failed"); };
try
{
check(BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_AES_ALGORITHM, nullptr, 0));
check(BCryptSetProperty(algorithm, BCRYPT_CHAINING_MODE,
reinterpret_cast<PUCHAR>(const_cast<wchar_t *>(BCRYPT_CHAIN_MODE_CBC)),
sizeof(BCRYPT_CHAIN_MODE_CBC), 0));
check(BCryptGenerateSymmetricKey(algorithm, &key, nullptr, 0, const_cast<PUCHAR>(AES_KEY_256),
keyBytes, 0));
auto mutableIv = iv;
ULONG size = 0;
unsigned char emptyInput = 0;
auto *inputData = input.empty() ? &emptyInput : input.data();
check(BCryptEncrypt(key, inputData, static_cast<ULONG>(input.size()), nullptr, mutableIv.data(), 16,
nullptr, 0, &size, BCRYPT_BLOCK_PADDING));
std::vector<unsigned char> result(size);
mutableIv = iv;
check(BCryptEncrypt(key, inputData, static_cast<ULONG>(input.size()), nullptr, mutableIv.data(), 16,
result.data(), size, &size, BCRYPT_BLOCK_PADDING));
result.resize(size);
BCryptDestroyKey(key);
BCryptCloseAlgorithmProvider(algorithm, 0);
return result;
}
catch (...)
{
if (key)
BCryptDestroyKey(key);
if (algorithm)
BCryptCloseAlgorithmProvider(algorithm, 0);
throw;
}
}
struct Pair
{
Winsock winsock;
Socket listener, client, server;
Pair()
{
listener.reset(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
socketCheck(listener.get() != INVALID_SOCKET, "Fixture listener");
auto endpoint = ipv4("127.0.0.1", 0);
socketCheck(bind(listener.get(), reinterpret_cast<sockaddr *>(&endpoint), sizeof(endpoint)) == 0,
"Bind");
socketCheck(listen(listener.get(), 1) == 0, "Listen");
int size = sizeof(endpoint);
socketCheck(getsockname(listener.get(), reinterpret_cast<sockaddr *>(&endpoint), &size) == 0, "Port");
client.reset(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
socketCheck(connect(client.get(), reinterpret_cast<sockaddr *>(&endpoint), sizeof(endpoint)) == 0,
"Connect");
server.reset(accept(listener.get(), nullptr, nullptr));
socketCheck(server.get() != INVALID_SOCKET, "Accept");
setTimeouts(client.get(), 2000);
setTimeouts(server.get(), 2000);
}
};
int main()
{
try
{
const std::array<unsigned char, 16> iv{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
for (const int bits : {128, 192, 256})
{
AES aes(bits);
for (const unsigned length : {0u, 1u, 15u, 16u, 17u, 31u, 32u, 255u})
{
std::vector<unsigned char> data(length);
for (unsigned i = 0; i < length; ++i)
data[i] = static_cast<unsigned char>(i * 17);
unsigned cipherLength = 0, plainLength = 0;
const std::unique_ptr<unsigned char[]> cipher(
aes.EncryptCBC_PKCS7(data.data(), length, AES_KEY_256, iv.data(), cipherLength));
const auto reference = referenceCbc(data, static_cast<unsigned>(bits / 8), iv);
require(reference == std::vector<unsigned char>(cipher.get(), cipher.get() + cipherLength),
"Cipher differs from Windows CNG");
const std::unique_ptr<unsigned char[]> plain(
aes.DecryptCBC_PKCS7(cipher.get(), cipherLength, AES_KEY_256, iv.data(), plainLength));
require(data == std::vector<unsigned char>(plain.get(), plain.get() + plainLength),
"AES roundtrip");
}
}
AES aes(256);
rejects([&] {
unsigned length;
aes.DecryptCBC_PKCS7(iv.data(), 15, AES_KEY_256, iv.data(), length);
});
unsigned length = 0;
const std::unique_ptr<unsigned char[]> padded(
aes.EncryptCBC_PKCS7(nullptr, 0, AES_KEY_256, iv.data(), length));
auto badIv = iv;
badIv[14] ^= 1; // Last padding byte remains 16; another padding byte is invalid.
rejects([&] {
unsigned output;
std::unique_ptr<unsigned char[]> ignored(
aes.DecryptCBC_PKCS7(padded.get(), length, AES_KEY_256, badIv.data(), output));
});
for (const std::uint32_t invalid : {0u, 16u, 17u, 31u, 33u, 0xffffffffu})
{
Pair pair;
const auto header = htonl(invalid);
require(sendAll(pair.client.get(), reinterpret_cast<const char *>(&header), 4), "Header send");
rejects([&] { receivePacket(pair.server.get(), aes, AES_KEY_256); });
}
{
Pair pair;
for (const std::vector<unsigned char> data :
{std::vector<unsigned char>{}, std::vector<unsigned char>{0, 1, 0, 255}})
{
require(sendPacket(pair.client.get(), data.data(), data.size(), aes, AES_KEY_256),
"Packet send");
const auto received = receivePacket(pair.server.get(), aes, AES_KEY_256);
require(received && *received == data, "Empty/binary message framing");
}
rejects([&] { sendPacket(pair.client.get(), iv.data(), MAX_PLAINTEXT + 1, aes, AES_KEY_256); });
shutdown(pair.client.get(), SD_SEND);
require(!receivePacket(pair.server.get(), aes, AES_KEY_256), "EOF distinct from empty message");
}
auto packet = discoveryPacket(0xfedcba9876543210ULL);
require(discoveryToken(packet.data(), 16) == 0xfedcba9876543210ULL, "Discovery token");
for (std::size_t size = 0; size < 16; ++size)
require(!discoveryToken(packet.data(), size), "Short discovery");
packet[0] ^= 1;
require(!discoveryToken(packet.data(), 16), "Wrong discovery magic");
ServerDiscovery finder("127.0.0.1", 0);
auto discovery = std::async(std::launch::async, [&] { return finder.waitForServer(2000); });
ClientDiscovery broadcaster("127.0.0.1", finder.port());
require(broadcaster.broadcastAndAwaitResponse(), "Loopback discovery");
require(discovery.get() == "127.0.0.1", "Discovered endpoint");
ServerTCP server("127.0.0.1", 0);
auto echo = std::async(std::launch::async, [&] {
require(server.acceptClient(2000), "Accept deadline");
server.runEchoLoop();
});
{
ClientTCP client("127.0.0.1", server.port());
const std::vector<unsigned char> binary{0, 1, 2, 0, 255, 42};
require(client.exchange(binary) == binary, "Paired echo");
require(client.exchange({}).empty(), "Paired empty echo");
}
echo.get();
std::cout << "CNG cross-checks, packet validation, discovery and TCP echo passed\n";
return 0;
}
catch (const std::exception &error)
{
std::cerr << error.what() << '\n';
return 1;
}
}