FreeRDP
Loading...
Searching...
No Matches
TestKerberosWrap.c
1
17
18#include <stdio.h>
19#include <string.h>
20
21#include <winpr/crt.h>
22#include <winpr/sspi.h>
23
24/* Include the source file directly because the SSPI entry points are static.
25 * Rename the exported tables so they do not collide with the copies in libwinpr. */
26#define KERBEROS_SecPkgInfoA TestKerberosWrap_SecPkgInfoA
27#define KERBEROS_SecPkgInfoW TestKerberosWrap_SecPkgInfoW
28#define KERBEROS_SecurityFunctionTableA TestKerberosWrap_SecurityFunctionTableA
29#define KERBEROS_SecurityFunctionTableW TestKerberosWrap_SecurityFunctionTableW
30// NOLINTNEXTLINE(bugprone-suspicious-include)
31#include "../Kerberos/kerberos.c"
32
33/*
34 * Wrap tokens are built with synthetic keys, so no KDC is required.
35 * RFC 4121 4.2.5 allows any RRC; MIT gss_wrap() emits RRC=0 while Windows
36 * and WinPR emit RRC=16+trailer. CredSSP splits the blob at cbSecurityTrailer,
37 * which does not have to match a Kerberos boundary.
38 */
39
40#define TEST_PLAINTEXT_LENGTH 32
41#define TEST_SEQUENCE 0x1540bb1eull
42
43static size_t checks = 0;
44static size_t failures = 0;
45
46static void check(BOOL condition, const char* name)
47{
48 checks++;
49 if (!condition)
50 {
51 (void)fprintf(stderr, "FAIL: %s\n", name);
52 failures++;
53 }
54}
55
56static BOOL make_token(krb5_context ctx, const krb5_keyblock* key, uint16_t ec, uint16_t rrc,
57 BOOL bad_inner_header, BYTE** wire, size_t* wire_length)
58{
59 const size_t clear_length = TEST_PLAINTEXT_LENGTH + ec + 16ull;
60 size_t encrypted_length = 0;
61 if (krb5_c_encrypt_length(ctx, key->enctype, clear_length, &encrypted_length) != 0)
62 return FALSE;
63
64 BYTE* clear = calloc(1, clear_length);
65 BYTE* encrypted = calloc(1, encrypted_length);
66 *wire = calloc(1, 16 + encrypted_length);
67 if (!clear || !encrypted || !*wire)
68 {
69 free(clear);
70 free(encrypted);
71 free(*wire);
72 *wire = nullptr;
73 return FALSE;
74 }
75
76 BYTE* header = *wire;
77 winpr_Data_Write_UINT16_BE(header, TOK_ID_WRAP);
78 header[2] = FLAG_WRAP_CONFIDENTIAL | FLAG_ACCEPTOR_SUBKEY;
79 header[3] = 0xFF;
80 winpr_Data_Write_UINT16_BE(&header[4], ec);
81 winpr_Data_Write_UINT64_BE(&header[8], TEST_SEQUENCE);
82
83 for (size_t x = 0; x < TEST_PLAINTEXT_LENGTH; x++)
84 clear[x] = (BYTE)(x ^ 0xA5);
85 memset(&clear[TEST_PLAINTEXT_LENGTH], 0xEE, ec);
86 /* The encrypted copy of the header carries RRC=0 (RFC 4121 4.2.4) */
87 memcpy(&clear[TEST_PLAINTEXT_LENGTH + ec], header, 16);
88 if (bad_inner_header)
89 clear[clear_length - 1] ^= 1;
90
91 krb5_data input = WINPR_C_ARRAY_INIT;
92 input.data = (char*)clear;
93 input.length = (unsigned int)clear_length;
94 krb5_enc_data output = WINPR_C_ARRAY_INIT;
95 output.ciphertext.data = (char*)encrypted;
96 output.ciphertext.length = (unsigned int)encrypted_length;
97 const krb5_error_code rv =
98 krb5_c_encrypt(ctx, key, KG_USAGE_INITIATOR_SEAL, nullptr, &input, &output);
99 if (rv == 0)
100 {
101 encrypted_length = output.ciphertext.length;
102 /* Rotate right by RRC octets, modulo the body length */
103 for (size_t x = 0; x < encrypted_length; x++)
104 header[16 + ((x + rrc) % encrypted_length)] = encrypted[x];
105 winpr_Data_Write_UINT16_BE(&header[6], rrc);
106 *wire_length = 16 + encrypted_length;
107 }
108
109 winpr_znfree(clear, clear_length);
110 free(encrypted);
111 if (rv != 0)
112 {
113 free(*wire);
114 *wire = nullptr;
115 }
116 return rv == 0;
117}
118
119static SECURITY_STATUS decrypt(KRB_CONTEXT* context, const BYTE* wire, size_t length, size_t split,
120 BOOL expect_success, const char* name)
121{
122 const size_t data_size = length - split;
123 BYTE* token = calloc((split > 0) ? split : 1, sizeof(BYTE));
124 BYTE* data = calloc((data_size > 0) ? data_size : 1, sizeof(BYTE));
125 if (!token || !data)
126 {
127 check(FALSE, "allocation");
128 free(token);
129 free(data);
130 return SEC_E_INSUFFICIENT_MEMORY;
131 }
132 memcpy(token, wire, split);
133 memcpy(data, &wire[split], data_size);
134
135 SecBuffer buffers[2] = {
136 { (ULONG)split, SECBUFFER_TOKEN, token },
137 { (ULONG)data_size, SECBUFFER_DATA, data },
138 };
139 SecBufferDesc desc = { SECBUFFER_VERSION, 2, buffers };
140 CtxtHandle handle = WINPR_C_ARRAY_INIT;
141 sspi_SecureHandleSetLowerPointer(&handle, context);
142 sspi_SecureHandleSetUpperPointer(&handle, (void*)KERBEROS_SSP_NAME);
143
144 ULONG qop = 123;
145 const SECURITY_STATUS status = kerberos_DecryptMessage(&handle, &desc, 0, &qop);
146 if (expect_success)
147 {
148 check(status == SEC_E_OK, name);
149 if (status == SEC_E_OK)
150 {
151 check(buffers[1].cbBuffer == TEST_PLAINTEXT_LENGTH, "exact plaintext size");
152 check(qop == 0, "confidential QOP");
153 /* Only read what was actually allocated */
154 BOOL equal = (buffers[1].cbBuffer == TEST_PLAINTEXT_LENGTH) &&
155 (data_size >= TEST_PLAINTEXT_LENGTH);
156 for (size_t x = 0; equal && (x < TEST_PLAINTEXT_LENGTH); x++)
157 equal = (data[x] == (BYTE)(x ^ 0xA5));
158 check(equal, "exact plaintext bytes");
159 }
160 }
161 else
162 {
163 check(status != SEC_E_OK, name);
164 check((qop == 123) && (buffers[1].cbBuffer == data_size) &&
165 (memcmp(data, &wire[split], data_size) == 0),
166 "rejection leaves the output untouched");
167 }
168
169 free(token);
170 free(data);
171 return status;
172}
173
174/* Tokens produced by kerberos_EncryptMessage (Windows layout) must keep working */
175static void check_winpr_sender(KRB_CONTEXT* receiver, size_t trailer_length)
176{
177 BYTE wire[256] = WINPR_C_ARRAY_INIT;
178 SecBuffer buffers[2] = {
179 { (ULONG)trailer_length, SECBUFFER_TOKEN, wire },
180 { TEST_PLAINTEXT_LENGTH, SECBUFFER_DATA, &wire[trailer_length] },
181 };
182 for (size_t x = 0; x < TEST_PLAINTEXT_LENGTH; x++)
183 wire[trailer_length + x] = (BYTE)(x ^ 0xA5);
184
185 SecBufferDesc desc = { SECBUFFER_VERSION, 2, buffers };
186 KRB_CONTEXT sender = *receiver;
187 sender.acceptor = FALSE;
188 sender.local_seq = receiver->remote_seq;
189 CtxtHandle handle = WINPR_C_ARRAY_INIT;
190 sspi_SecureHandleSetLowerPointer(&handle, &sender);
191 sspi_SecureHandleSetUpperPointer(&handle, (void*)KERBEROS_SSP_NAME);
192
193 const SECURITY_STATUS status = kerberos_EncryptMessage(&handle, 0, &desc, 0);
194 check(status == SEC_E_OK, "WinPR sender encrypt");
195 if (status == SEC_E_OK)
196 decrypt(receiver, wire, trailer_length + TEST_PLAINTEXT_LENGTH, trailer_length, TRUE,
197 "WinPR sender (RRC=16+trailer)");
198}
199
200static void check_malformed(KRB_CONTEXT* context, krb5_context ctx, const krb5_keyblock* key)
201{
202 BYTE* wire = nullptr;
203 size_t length = 0;
204 if (!make_token(ctx, key, 0, 0, FALSE, &wire, &length))
205 {
206 check(FALSE, "build token");
207 return;
208 }
209 const size_t split = length - TEST_PLAINTEXT_LENGTH;
210
211 check_winpr_sender(context, split);
212
213 SecBuffer oversized[2] = {
214 { (ULONG)split, SECBUFFER_TOKEN, wire },
215 { UINT32_MAX, SECBUFFER_DATA, wire },
216 };
217 SecBufferDesc oversized_desc = { SECBUFFER_VERSION, 2, oversized };
218 CtxtHandle handle = WINPR_C_ARRAY_INIT;
219 sspi_SecureHandleSetLowerPointer(&handle, context);
220 sspi_SecureHandleSetUpperPointer(&handle, (void*)KERBEROS_SSP_NAME);
221 ULONG qop = 123;
222 check(kerberos_DecryptMessage(&handle, &oversized_desc, 0, &qop) == SEC_E_INVALID_TOKEN,
223 "reject a length beyond krb5_data before reading data");
224
225 decrypt(context, wire, 16, 16, FALSE, "empty body");
226 decrypt(context, wire, length, 15, FALSE, "short token header");
227 decrypt(context, wire, length - 1, split, FALSE, "truncated token");
228
229 winpr_Data_Write_UINT16_BE(&wire[4], UINT16_MAX);
230 decrypt(context, wire, length, split, FALSE, "EC exceeds the plaintext");
231 winpr_Data_Write_UINT16_BE(&wire[4], 1);
232 check(decrypt(context, wire, length, split, FALSE, "EC differs from the encrypted header") ==
233 SEC_E_MESSAGE_ALTERED,
234 "EC is authenticated");
235 winpr_Data_Write_UINT16_BE(&wire[4], 0);
236
237 wire[2] |= FLAG_SENDER_IS_ACCEPTOR;
238 decrypt(context, wire, length, split, FALSE, "wrong direction");
239 wire[2] &= (BYTE)~FLAG_SENDER_IS_ACCEPTOR;
240
241 wire[2] &= (BYTE)~FLAG_WRAP_CONFIDENTIAL;
242 decrypt(context, wire, length, split, FALSE, "not sealed");
243 wire[2] |= FLAG_WRAP_CONFIDENTIAL;
244
245 wire[15] ^= 1;
246 check(decrypt(context, wire, length, split, FALSE, "wrong sequence") == SEC_E_OUT_OF_SEQUENCE,
247 "sequence number is checked");
248 free(wire);
249
250 if (!make_token(ctx, key, 0, 0, TRUE, &wire, &length))
251 {
252 check(FALSE, "build token");
253 return;
254 }
255 check(decrypt(context, wire, length, length - TEST_PLAINTEXT_LENGTH, FALSE,
256 "bad encrypted header") == SEC_E_MESSAGE_ALTERED,
257 "encrypted header is checked");
258 free(wire);
259}
260
261static BOOL check_enctype(krb5_context ctx, krb5_enctype enctype)
262{
263 const uint16_t rotations[] = { 0, 1, 15, 28, 40, UINT16_MAX };
264 const uint16_t fillers[] = { 0, 1, 16, 255, UINT16_MAX };
265 krb5_keyblock key = WINPR_C_ARRAY_INIT;
266 krb5_key opaque = nullptr;
267
268 if ((krb5_c_make_random_key(ctx, enctype, &key) != 0) ||
269 (krb5_k_create_key(ctx, &key, &opaque) != 0))
270 {
271 (void)fprintf(stderr, "cannot create a key for enctype %d\n", enctype);
272 krb5_free_keyblock_contents(ctx, &key);
273 return FALSE;
274 }
275
276 KRB_CREDENTIALS creds = WINPR_C_ARRAY_INIT;
277 creds.ctx = ctx;
278 KRB_CONTEXT context = WINPR_C_ARRAY_INIT;
279 context.credentials = &creds;
280 context.acceptor = TRUE;
281 context.flags = SSPI_GSS_C_CONF_FLAG | ISC_REQ_SEQUENCE_DETECT;
282 context.remote_seq = TEST_SEQUENCE;
283 context.keyset.acceptor_key = opaque;
284
285 for (size_t f = 0; f < ARRAYSIZE(fillers); f++)
286 {
287 for (size_t r = 0; r < ARRAYSIZE(rotations); r++)
288 {
289 BYTE* wire = nullptr;
290 size_t length = 0;
291 char name[128] = WINPR_C_ARRAY_INIT;
292
293 if (!make_token(ctx, &key, fillers[f], rotations[r], FALSE, &wire, &length))
294 {
295 check(FALSE, "build token");
296 continue;
297 }
298 const size_t split = length - TEST_PLAINTEXT_LENGTH - fillers[f];
299 (void)_snprintf(name, sizeof(name), "enctype=%d EC=%" PRIu16 " RRC=%" PRIu16, enctype,
300 fillers[f], rotations[r]);
301 decrypt(&context, wire, length, split, TRUE, name);
302 if (fillers[f] == 0)
303 {
304 decrypt(&context, wire, length, split - 1, TRUE, "split inside the trailer");
305 decrypt(&context, wire, length, split + 1, FALSE, "output buffer too small");
306 }
307 wire[length - 1] ^= 1;
308 decrypt(&context, wire, length, split, FALSE, "tampered ciphertext");
309 free(wire);
310 }
311 }
312
313 check_malformed(&context, ctx, &key);
314
315 krb5_k_free_key(ctx, opaque);
316 krb5_free_keyblock_contents(ctx, &key);
317 return TRUE;
318}
319
320int TestKerberosWrap(int argc, char* argv[])
321{
322 const krb5_enctype enctypes[] = {
323 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
324 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
325 ENCTYPE_AES128_CTS_HMAC_SHA256_128,
326 ENCTYPE_AES256_CTS_HMAC_SHA384_192,
327 };
328 krb5_context ctx = nullptr;
329
330 WINPR_UNUSED(argc);
331 WINPR_UNUSED(argv);
332
333 if (krb5_init_context(&ctx) != 0)
334 return -1;
335
336 for (size_t e = 0; e < ARRAYSIZE(enctypes); e++)
337 {
338 if (!check_enctype(ctx, enctypes[e]))
339 failures++;
340 }
341 krb5_free_context(ctx);
342
343 printf("TestKerberosWrap: %" PRIuz " checks, %" PRIuz " failures\n", checks, failures);
344 return (failures > 0) ? -1 : 0;
345}