22#include <winpr/sspi.h>
26#define KERBEROS_SecPkgInfoA TestKerberosWrap_SecPkgInfoA
27#define KERBEROS_SecPkgInfoW TestKerberosWrap_SecPkgInfoW
28#define KERBEROS_SecurityFunctionTableA TestKerberosWrap_SecurityFunctionTableA
29#define KERBEROS_SecurityFunctionTableW TestKerberosWrap_SecurityFunctionTableW
31#include "../Kerberos/kerberos.c"
40#define TEST_PLAINTEXT_LENGTH 32
41#define TEST_SEQUENCE 0x1540bb1eull
43static size_t checks = 0;
44static size_t failures = 0;
46static void check(BOOL condition,
const char* name)
51 (void)fprintf(stderr,
"FAIL: %s\n", name);
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)
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)
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)
77 winpr_Data_Write_UINT16_BE(header, TOK_ID_WRAP);
78 header[2] = FLAG_WRAP_CONFIDENTIAL | FLAG_ACCEPTOR_SUBKEY;
80 winpr_Data_Write_UINT16_BE(&header[4], ec);
81 winpr_Data_Write_UINT64_BE(&header[8], TEST_SEQUENCE);
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);
87 memcpy(&clear[TEST_PLAINTEXT_LENGTH + ec], header, 16);
89 clear[clear_length - 1] ^= 1;
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);
101 encrypted_length = output.ciphertext.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;
109 winpr_znfree(clear, clear_length);
119static SECURITY_STATUS decrypt(KRB_CONTEXT* context,
const BYTE* wire,
size_t length,
size_t split,
120 BOOL expect_success,
const char* name)
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));
127 check(FALSE,
"allocation");
130 return SEC_E_INSUFFICIENT_MEMORY;
132 memcpy(token, wire, split);
133 memcpy(data, &wire[split], data_size);
136 { (ULONG)split, SECBUFFER_TOKEN, token },
137 { (ULONG)data_size, SECBUFFER_DATA, data },
140 CtxtHandle handle = WINPR_C_ARRAY_INIT;
141 sspi_SecureHandleSetLowerPointer(&handle, context);
142 sspi_SecureHandleSetUpperPointer(&handle, (
void*)KERBEROS_SSP_NAME);
145 const SECURITY_STATUS status = kerberos_DecryptMessage(&handle, &desc, 0, &qop);
148 check(status == SEC_E_OK, name);
149 if (status == SEC_E_OK)
151 check(buffers[1].cbBuffer == TEST_PLAINTEXT_LENGTH,
"exact plaintext size");
152 check(qop == 0,
"confidential QOP");
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");
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");
175static void check_winpr_sender(KRB_CONTEXT* receiver,
size_t trailer_length)
177 BYTE wire[256] = WINPR_C_ARRAY_INIT;
179 { (ULONG)trailer_length, SECBUFFER_TOKEN, wire },
180 { TEST_PLAINTEXT_LENGTH, SECBUFFER_DATA, &wire[trailer_length] },
182 for (
size_t x = 0; x < TEST_PLAINTEXT_LENGTH; x++)
183 wire[trailer_length + x] = (BYTE)(x ^ 0xA5);
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);
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)");
200static void check_malformed(KRB_CONTEXT* context, krb5_context ctx,
const krb5_keyblock* key)
202 BYTE* wire =
nullptr;
204 if (!make_token(ctx, key, 0, 0, FALSE, &wire, &length))
206 check(FALSE,
"build token");
209 const size_t split = length - TEST_PLAINTEXT_LENGTH;
211 check_winpr_sender(context, split);
214 { (ULONG)split, SECBUFFER_TOKEN, wire },
215 { UINT32_MAX, SECBUFFER_DATA, wire },
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);
222 check(kerberos_DecryptMessage(&handle, &oversized_desc, 0, &qop) == SEC_E_INVALID_TOKEN,
223 "reject a length beyond krb5_data before reading data");
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");
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);
237 wire[2] |= FLAG_SENDER_IS_ACCEPTOR;
238 decrypt(context, wire, length, split, FALSE,
"wrong direction");
239 wire[2] &= (BYTE)~FLAG_SENDER_IS_ACCEPTOR;
241 wire[2] &= (BYTE)~FLAG_WRAP_CONFIDENTIAL;
242 decrypt(context, wire, length, split, FALSE,
"not sealed");
243 wire[2] |= FLAG_WRAP_CONFIDENTIAL;
246 check(decrypt(context, wire, length, split, FALSE,
"wrong sequence") == SEC_E_OUT_OF_SEQUENCE,
247 "sequence number is checked");
250 if (!make_token(ctx, key, 0, 0, TRUE, &wire, &length))
252 check(FALSE,
"build token");
255 check(decrypt(context, wire, length, length - TEST_PLAINTEXT_LENGTH, FALSE,
256 "bad encrypted header") == SEC_E_MESSAGE_ALTERED,
257 "encrypted header is checked");
261static BOOL check_enctype(krb5_context ctx, krb5_enctype enctype)
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;
268 if ((krb5_c_make_random_key(ctx, enctype, &key) != 0) ||
269 (krb5_k_create_key(ctx, &key, &opaque) != 0))
271 (void)fprintf(stderr,
"cannot create a key for enctype %d\n", enctype);
272 krb5_free_keyblock_contents(ctx, &key);
276 KRB_CREDENTIALS creds = WINPR_C_ARRAY_INIT;
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;
285 for (
size_t f = 0; f < ARRAYSIZE(fillers); f++)
287 for (
size_t r = 0; r < ARRAYSIZE(rotations); r++)
289 BYTE* wire =
nullptr;
291 char name[128] = WINPR_C_ARRAY_INIT;
293 if (!make_token(ctx, &key, fillers[f], rotations[r], FALSE, &wire, &length))
295 check(FALSE,
"build token");
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);
304 decrypt(&context, wire, length, split - 1, TRUE,
"split inside the trailer");
305 decrypt(&context, wire, length, split + 1, FALSE,
"output buffer too small");
307 wire[length - 1] ^= 1;
308 decrypt(&context, wire, length, split, FALSE,
"tampered ciphertext");
313 check_malformed(&context, ctx, &key);
315 krb5_k_free_key(ctx, opaque);
316 krb5_free_keyblock_contents(ctx, &key);
320int TestKerberosWrap(
int argc,
char* argv[])
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,
328 krb5_context ctx =
nullptr;
333 if (krb5_init_context(&ctx) != 0)
336 for (
size_t e = 0; e < ARRAYSIZE(enctypes); e++)
338 if (!check_enctype(ctx, enctypes[e]))
341 krb5_free_context(ctx);
343 printf(
"TestKerberosWrap: %" PRIuz
" checks, %" PRIuz
" failures\n", checks, failures);
344 return (failures > 0) ? -1 : 0;