FreeRDP
Loading...
Searching...
No Matches
TestRdpearMessages.c
1
19
20#include <stdio.h>
21
22#include <winpr/print.h>
23#include <winpr/ndr.h>
24#include <winpr/nt.h>
25#include <winpr/string.h>
26
27#include <freerdp/channels/log.h>
28
29#include <rdpear-common/rdpear_common.h>
30
31/* The calls are object data of KerbCredIsoRemoteInput and NtlmCredIsoRemoteInput (MS-RDPEAR
32 * 2.2.1.2.9 and 2.2.1.3.7), they are wrapped in the type serialization and object headers by
33 * wrap_call(). The replies are complete type serialization buffers.
34 */
35
36/* ========================== calls ========================== */
37
38static const BYTE kerb_negotiate_version_call[] = {
39 0x00, 0x00, 0x02, 0x00, /* referent id */
40 0x00, 0x00, 0x00, 0x00, /* align 8 */
41 0x00, 0x01, 0x00, 0x01, /* CallId, discriminant */
42 0x00, 0x00, 0x00, 0x00, /* MaxSupportedVersion */
43};
44
45/* a captured CreateApReqAuthenticator call */
46static const BYTE kerb_create_ap_req_authenticator_call[] = {
47 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x03, 0x01, 0x04, 0x00, 0x02, 0x00,
48 0x38, 0x9e, 0xef, 0x6b, 0x0c, 0x00, 0x02, 0x00, 0x18, 0x00, 0x02, 0x00, 0x20, 0x00, 0x02, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x02, 0x00, 0x2c, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00,
50 0x13, 0x8a, 0xa5, 0xa8, 0x12, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
51 0x20, 0x00, 0x00, 0x00, 0xc9, 0x03, 0x42, 0xa8, 0x17, 0x8f, 0xd9, 0xc4, 0x9b, 0xd2, 0xc4, 0x6e,
52 0x73, 0x64, 0x98, 0x7b, 0x90, 0xf5, 0x9a, 0x28, 0x77, 0x8e, 0xca, 0xde, 0x29, 0x2e, 0xa3, 0x8d,
53 0x8a, 0x56, 0x36, 0xd5, 0x01, 0x00, 0x01, 0x00, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
54 0x1c, 0x00, 0x1e, 0x00, 0x14, 0x00, 0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x0e, 0x00, 0x00, 0x00, 0x41, 0x00, 0x64, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x69, 0x00,
56 0x73, 0x00, 0x74, 0x00, 0x72, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00, 0x75, 0x00, 0x72, 0x00,
57 0x1c, 0x00, 0x1e, 0x00, 0x1c, 0x00, 0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x0e, 0x00, 0x00, 0x00, 0x48, 0x00, 0x41, 0x00, 0x52, 0x00, 0x44, 0x00, 0x45, 0x00, 0x4e, 0x00,
59 0x49, 0x00, 0x4e, 0x00, 0x47, 0x00, 0x33, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4d, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
61 0x28, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
62 0x1b, 0x00, 0x00, 0x00, 0x30, 0x00, 0x02, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x30, 0x19, 0xa0, 0x03,
63 0x02, 0x01, 0x07, 0xa1, 0x12, 0x04, 0x10, 0xe4, 0xaa, 0xff, 0x2b, 0x93, 0x97, 0x4c, 0xf2, 0x5c,
64 0x0b, 0x49, 0x85, 0x72, 0x92, 0x94, 0x54, 0x00,
65};
66
67/* the captured call with a client name of an odd length: "Administrateu", the ClientRealm that
68 * follows is aligned on 4 */
69static const BYTE kerb_create_ap_req_authenticator_odd_call[] = {
70 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x03, 0x01, 0x04, 0x00, 0x02, 0x00,
71 0x38, 0x9e, 0xef, 0x6b, 0x0c, 0x00, 0x02, 0x00, 0x18, 0x00, 0x02, 0x00, 0x20, 0x00, 0x02, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x02, 0x00, 0x2c, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00,
73 0x13, 0x8a, 0xa5, 0xa8, 0x12, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00,
74 0x20, 0x00, 0x00, 0x00, 0xc9, 0x03, 0x42, 0xa8, 0x17, 0x8f, 0xd9, 0xc4, 0x9b, 0xd2, 0xc4, 0x6e,
75 0x73, 0x64, 0x98, 0x7b, 0x90, 0xf5, 0x9a, 0x28, 0x77, 0x8e, 0xca, 0xde, 0x29, 0x2e, 0xa3, 0x8d,
76 0x8a, 0x56, 0x36, 0xd5, 0x01, 0x00, 0x01, 0x00, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
77 0x1a, 0x00, 0x1c, 0x00, 0x14, 0x00, 0x02, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x0d, 0x00, 0x00, 0x00, 0x41, 0x00, 0x64, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x69, 0x00,
79 0x73, 0x00, 0x74, 0x00, 0x72, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00, 0x75, 0x00, 0x00, 0x00,
80 0x1c, 0x00, 0x1e, 0x00, 0x1c, 0x00, 0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x0e, 0x00, 0x00, 0x00, 0x48, 0x00, 0x41, 0x00, 0x52, 0x00, 0x44, 0x00, 0x45, 0x00, 0x4e, 0x00,
82 0x49, 0x00, 0x4e, 0x00, 0x47, 0x00, 0x33, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4d, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
84 0x28, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
85 0x1b, 0x00, 0x00, 0x00, 0x30, 0x00, 0x02, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x30, 0x19, 0xa0, 0x03,
86 0x02, 0x01, 0x07, 0xa1, 0x12, 0x04, 0x10, 0xe4, 0xaa, 0xff, 0x2b, 0x93, 0x97, 0x4c, 0xf2, 0x5c,
87 0x0b, 0x49, 0x85, 0x72, 0x92, 0x94, 0x54, 0x00,
88};
89
90static const BYTE kerb_decrypt_ap_reply_call[] = {
91 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
92 0x04, 0x01, 0x04, 0x01, /* CallId, discriminant */
93 0x04, 0x00, 0x02, 0x00, /* EncryptedReply */
94 0x08, 0x00, 0x02, 0x00, /* Key */
95 0x31, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, /* EncryptedReply: Pdu, Asn1BufferSize */
96 0x0c, 0x00, 0x02, 0x00, /* EncryptedReply: Asn1Buffer */
97 0x05, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x00, 0x00, 0x00, /* Asn1Buffer */
98 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, /* Key: reserved1, reserved2 */
99 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00, /* Key: reserved3 */
100 0x04, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, /* reserved3.value */
101};
102
103static const BYTE kerb_unpack_kdc_reply_body_call[] = {
104 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
105 0x05, 0x01, 0x05, 0x01, /* CallId, discriminant */
106 0x04, 0x00, 0x02, 0x00, /* EncryptedData */
107 0x08, 0x00, 0x02, 0x00, /* Key */
108 0x00, 0x00, 0x00, 0x00, /* StrengthenKey */
109 0x1a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, /* Pdu, KeyUsage */
110 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, /* EncryptedData: Pdu, Asn1BufferSize */
111 0x0c, 0x00, 0x02, 0x00, /* EncryptedData: Asn1Buffer */
112 0x03, 0x00, 0x00, 0x00, 0xf1, 0xf2, 0xf3, 0x00, /* Asn1Buffer */
113 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, /* Key: reserved1, reserved2 */
114 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00, /* Key: reserved3 */
115 0x02, 0x00, 0x00, 0x00, 0x99, 0x98, /* reserved3.value */
116};
117
118static const BYTE kerb_compute_tgs_checksum_call[] = {
119 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
120 0x06, 0x01, 0x06, 0x01, /* CallId, discriminant */
121 0x04, 0x00, 0x02, 0x00, /* requestBody */
122 0x08, 0x00, 0x02, 0x00, /* Key */
123 0x10, 0x00, 0x00, 0x00, /* ChecksumType */
124 0x0b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, /* requestBody: Pdu, Asn1BufferSize */
125 0x0c, 0x00, 0x02, 0x00, /* requestBody: Asn1Buffer */
126 0x03, 0x00, 0x00, 0x00, 0xb1, 0xb2, 0xb3, 0x00, /* Asn1Buffer */
127 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, /* Key: reserved1, reserved2 */
128 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00, /* Key: reserved3 */
129 0x02, 0x00, 0x00, 0x00, 0xc1, 0xc2, /* reserved3.value */
130};
131
132static const BYTE kerb_build_encrypted_auth_data_call[] = {
133 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
134 0x07, 0x01, 0x07, 0x01, /* CallId, discriminant */
135 0x05, 0x00, 0x00, 0x00, /* KeyUsage */
136 0x04, 0x00, 0x02, 0x00, /* Key */
137 0x08, 0x00, 0x02, 0x00, /* PlainAuthData */
138 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, /* Key: reserved1, reserved2 */
139 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00, /* Key: reserved3 */
140 0x03, 0x00, 0x00, 0x00, 0xd1, 0xd2, 0xd3, 0x00, /* reserved3.value */
141 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, /* PlainAuthData: Pdu, Asn1BufferSize */
142 0x10, 0x00, 0x02, 0x00, /* PlainAuthData: Asn1Buffer */
143 0x02, 0x00, 0x00, 0x00, 0xe1, 0xe2, /* Asn1Buffer */
144};
145
146static const BYTE kerb_pack_ap_reply_call[] = {
147 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
148 0x08, 0x01, 0x08, 0x01, /* CallId, discriminant */
149 0x04, 0x00, 0x02, 0x00, /* Reply */
150 0x08, 0x00, 0x02, 0x00, /* ReplyBody */
151 0x0c, 0x00, 0x02, 0x00, /* SessionKey */
152 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, /* Reply: Pdu, Asn1BufferSize */
153 0x10, 0x00, 0x02, 0x00, /* Reply: Asn1Buffer */
154 0x01, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, /* Asn1Buffer */
155 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, /* ReplyBody: Pdu, Asn1BufferSize */
156 0x14, 0x00, 0x02, 0x00, /* ReplyBody: Asn1Buffer */
157 0x02, 0x00, 0x00, 0x00, 0x66, 0x65, 0x00, 0x00, /* Asn1Buffer */
158 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, /* SessionKey: reserved1, reserved2 */
159 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x02, 0x00, /* SessionKey: reserved3 */
160 0x01, 0x00, 0x00, 0x00, 0x55, /* reserved3.value */
161};
162
163static const BYTE ntlm_negotiate_version_call[] = {
164 0x00, 0x00, 0x02, 0x00, /* referent id */
165 0x00, 0x02, 0x00, 0x02, /* CallId, discriminant */
166 0x00, 0x00, 0x00, 0x00, /* MaxSupportedVersion */
167};
168
169static const BYTE ntlm_lm20_get_ntlm3_challenge_response_call[] = {
170 0x00, 0x00, 0x02, 0x00, /* referent id */
171 0x01, 0x02, 0x01, 0x02, /* CallId, discriminant */
172 0x04, 0x00, 0x02, 0x00, /* Credential */
173 0x08, 0x00, 0x02, 0x00, /* UserName */
174 0x0c, 0x00, 0x02, 0x00, /* LogonDomainName */
175 0x00, 0x00, 0x00, 0x00, /* ServerName */
176 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* ChallengeToClient */
177 0x00, 0x00, 0x00, 0x00, /* Credential: reserved1, reserved2, reserved3, padding */
178 0x02, 0x00, /* Credential: reserved4 */
179 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b,
180 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, /* Credential: reserved5 */
181 0x00, 0x00, /* padding */
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Credential: reservedSize, reserved6 */
183 0x06, 0x00, 0x08, 0x00, 0x10, 0x00, 0x02, 0x00, /* UserName: Length, MaximumLength, Buffer */
184 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, /* Buffer */
185 'u', 0x00, 's', 0x00, 'r', 0x00, 0x00, 0x00, /* Buffer, padding */
186 0x04, 0x00, 0x04, 0x00, 0x14, 0x00, 0x02, 0x00, /* LogonDomainName */
187 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, /* Buffer */
188 'D', 0x00, 'M', 0x00,
189};
190
191static const BYTE ntlm_calculate_nt_response_call[] = {
192 0x00, 0x00, 0x02, 0x00, /* referent id */
193 0x02, 0x02, 0x02, 0x02, /* CallId, discriminant */
194 0x04, 0x00, 0x02, 0x00, /* NtChallenge */
195 0x08, 0x00, 0x02, 0x00, /* Credential */
196 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, /* NtChallenge */
197 0x00, 0x00, 0x00, 0x00, /* Credential: reserved1, reserved2, reserved3, padding */
198 0x02, 0x00, /* Credential: reserved4 */
199 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
200 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, /* Credential: reserved5 */
201 0x00, 0x00, /* padding */
202 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00, /* Credential: reservedSize, reserved6 */
203 0x01, 0x00, 0x00, 0x00, 0xee, /* reserved6 */
204};
205
206static const BYTE ntlm_compare_credentials_call[] = {
207 0x00, 0x00, 0x02, 0x00, /* referent id */
208 0x04, 0x02, 0x04, 0x02, /* CallId, discriminant */
209 0x04, 0x00, 0x02, 0x00, /* LhsCredential */
210 0x08, 0x00, 0x02, 0x00, /* RhsCredential */
211 0x01, 0x00, 0x01, 0x00, /* Lhs: reserved1, reserved2, reserved3, padding */
212 0x02, 0x00, /* Lhs: reserved4 */
213 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
214 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, /* Lhs: reserved5 */
215 0x00, 0x00, /* padding */
216 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00, /* Lhs: reservedSize, reserved6 */
217 0x03, 0x00, 0x00, 0x00, 0xa0, 0xa1, 0xa2, 0x00, /* reserved6, padding */
218 0x00, 0x01, 0x00, 0x00, /* Rhs: reserved1, reserved2, reserved3 */
219 0x02, 0x00, /* Rhs: reserved4 */
220 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
221 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, /* Rhs: reserved5 */
222 0x00, 0x00, /* padding */
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Rhs: reservedSize, reserved6 */
224};
225
226/* a Kerberos call that is not described: decoded without its arm */
227static const BYTE kerb_verify_checksum_call[] = {
228 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, /* referent id, align 8 */
229 0x0b, 0x01, 0x0b, 0x01, /* CallId, discriminant */
230 0x11, 0x22, 0x33, 0x44, /* not decoded */
231};
232
233/* ========================== replies ========================== */
234
235/* The Kerberos replies and ntlm_negotiate_version_reply are the bytes written by the hand coded
236 * encoder of rdpear_prepare_response(), before the move to the union descriptors.
237 */
238
239static const BYTE kerb_negotiate_version_reply[] = {
240 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243};
244static const BYTE kerb_create_ap_req_authenticator_reply[] = {
245 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04,
248 0x03, 0x02, 0x01, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00,
249 0x44, 0x33, 0x22, 0x11, 0x03, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0x00,
250};
251static const BYTE kerb_asn1_data_reply[] = {
252 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00,
254 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x04, 0x00,
255 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
256};
257static const BYTE kerb_unpack_kdc_reply_body_reply[] = {
258 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260 0x05, 0x01, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55, 0x1a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
261 0x04, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x51, 0x52, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00,
262};
263static const BYTE kerb_pack_ap_reply_reply[] = {
264 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00,
266 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00,
267 0x02, 0x00, 0x05, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x00, 0x00, 0x00,
268};
269static const BYTE kerb_unhandled_reply[] = {
270 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
271 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273};
274static const BYTE ntlm_negotiate_version_reply[] = {
275 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
276 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278};
279
280/* the NTLM replies are derived from the IDL */
281static const BYTE ntlm_lm20_not_supported_reply[] = {
282 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, /* type serialization header */
283 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* object header */
284 0x00, 0x00, 0x02, 0x00, /* referent id */
285 0x01, 0x02, 0x00, 0x00, /* CallId, padding */
286 0xbb, 0x00, 0x00, 0xc0, /* Status: STATUS_NOT_SUPPORTED */
287 0x01, 0x02, 0x00, 0x00, /* discriminant, padding */
288 0x00, 0x00, 0x00, 0x00, /* Ntlm3ResponseLength, padding */
289 0x00, 0x00, 0x00, 0x00, /* Ntlm3Response */
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Lm3Response */
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, /* UserSessionKey */
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* LmSessionKey */
295};
296
297static const BYTE ntlm_lm20_reply[] = {
298 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, /* type serialization header */
299 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* object header */
300 0x00, 0x00, 0x02, 0x00, /* referent id */
301 0x01, 0x02, 0x00, 0x00, /* CallId, padding */
302 0x00, 0x00, 0x00, 0x00, /* Status */
303 0x01, 0x02, 0x00, 0x00, /* discriminant, padding */
304 0x03, 0x00, 0x00, 0x00, /* Ntlm3ResponseLength, padding */
305 0x04, 0x00, 0x02, 0x00, /* Ntlm3Response */
306 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
307 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* Lm3Response */
308 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
309 0x3c, 0x3d, 0x3e, 0x3f, /* UserSessionKey */
310 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* LmSessionKey */
311 0x03, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0xa3, /* Ntlm3Response */
312 0x00, /* object padding */
313};
314
315static const BYTE ntlm_calculate_nt_response_reply[] = {
316 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, /* type serialization header */
317 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* object header */
318 0x00, 0x00, 0x02, 0x00, /* referent id */
319 0x02, 0x02, 0x00, 0x00, /* CallId, padding */
320 0x00, 0x00, 0x00, 0x00, /* Status */
321 0x02, 0x02, /* discriminant */
322 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
323 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* NtResponse */
324 0x00, 0x00, /* object padding */
325};
326
327static const BYTE ntlm_compare_credentials_reply[] = {
328 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, /* type serialization header */
329 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* object header */
330 0x00, 0x00, 0x02, 0x00, /* referent id */
331 0x04, 0x02, 0x00, 0x00, /* CallId, padding */
332 0x00, 0x00, 0x00, 0x00, /* Status */
333 0x04, 0x02, 0x00, 0x00, /* discriminant, padding */
334 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* AreNtOwfsEqual, AreLmOwfsEqual */
335 0x01, 0x00, 0x00, 0x00, /* AreShaOwfsEqual */
336 0x00, 0x00, 0x00, 0x00, /* object padding */
337};
338
339/* ========================== helpers ========================== */
340
342static wStream* wrap_call(const BYTE* data, size_t len)
343{
344 const size_t padding = (8 - (len % 8)) % 8;
345 wStream* s = Stream_New(nullptr, 16 + len + padding);
346 if (!s)
347 return nullptr;
348
349 const BYTE header[] = { 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc };
350 Stream_Write(s, header, sizeof(header));
351 Stream_Write_UINT32(s, (UINT32)(len + padding));
352 Stream_Write_UINT32(s, 0);
353 Stream_Write(s, data, len);
354 Stream_Zero(s, padding);
355 Stream_SealLength(s);
356 Stream_SetPosition(s, 0);
357 return s;
358}
359
360static BOOL check_bytes(const char* what, const BYTE* got, size_t gotLen, const BYTE* expected,
361 size_t expectedLen)
362{
363 if ((gotLen == expectedLen) && (memcmp(got, expected, gotLen) == 0))
364 return TRUE;
365
366 (void)fprintf(stderr, "%s: mismatch, got %" PRIuz " bytes:\n", what, gotLen);
367 for (size_t i = 0; i < gotLen; i++)
368 (void)fprintf(stderr, "0x%02" PRIx8 ",%s", got[i], ((i % 12) == 11) ? "\n" : " ");
369 (void)fprintf(stderr, "\n");
370 return FALSE;
371}
372
373static BOOL check_asn1(const KERB_ASN1_DATA* asn1, UINT32 pdu, const BYTE* data, UINT32 len)
374{
375 return asn1 && (asn1->Pdu == pdu) && (asn1->Asn1BufferHints.count == len) && asn1->Asn1Buffer &&
376 (memcmp(asn1->Asn1Buffer, data, len) == 0);
377}
378
379static BOOL check_key(const KERB_RPC_ENCRYPTION_KEY* key, UINT32 enctype, const BYTE* data,
380 UINT32 len)
381{
382 return key && (key->reserved2 == enctype) && (key->reserved3.length == len) &&
383 key->reserved3.value && (memcmp(key->reserved3.value, data, len) == 0);
384}
385
386static BOOL check_unicode_string(const RPC_UNICODE_STRING* str, const char* expected)
387{
388 if (!str)
389 return FALSE;
390
391 char* utf8 = ConvertWCharNToUtf8Alloc(str->Buffer, str->strLength, nullptr);
392 const BOOL rc = utf8 && (strcmp(utf8, expected) == 0);
393 free(utf8);
394 return rc;
395}
396
397/* ========================== call decoding ========================== */
398
399static BOOL decode_kerb(wStream* s, KerbCredIsoRemoteInput* input)
400{
401 WinPrNdrDecoder* decoder = winpr_ndr_decoder_new_fromStream(s);
402 const BOOL rc = decoder && rdpear_kerb_input_read(decoder, s, input);
404 return rc;
405}
406
407static BOOL decode_ntlm(wStream* s, NtlmCredIsoRemoteInput* input)
408{
409 WinPrNdrDecoder* decoder = winpr_ndr_decoder_new_fromStream(s);
410 const BOOL rc = decoder && rdpear_ntlm_input_read(decoder, s, input);
412 return rc;
413}
414
415typedef BOOL (*CallCheckFn)(wStream* s);
416
417static BOOL check_kerb_negotiate_version(wStream* s)
418{
419 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
420 const BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbNegotiateVersion) &&
421 (input.Input.NegotiateVersion == 0);
422 rdpear_kerb_input_free(&input);
423 return rc;
424}
425
426static BOOL check_kerb_create_ap_req_authenticator(wStream* s)
427{
428 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
430 const BYTE gssChecksum[] = { 0x30, 0x19, 0xa0, 0x03, 0x02, 0x01, 0x07, 0xa1, 0x12,
431 0x04, 0x10, 0xe4, 0xaa, 0xff, 0x2b, 0x93, 0x97, 0x4c,
432 0xf2, 0x5c, 0x0b, 0x49, 0x85, 0x72, 0x92, 0x94, 0x54 };
433 const BYTE authData[] = { 0x30, 0x00 };
434
435 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbCreateApReqAuthenticator);
436 rc = rc && (req->SequenceNumber == 0x6bef9e38) && (req->KeyUsage == 7) && !req->SubKey &&
437 req->EncryptionKey && (req->EncryptionKey->reserved2 == 0x12) &&
438 (req->EncryptionKey->reserved3.length == 0x20) && req->ClientName &&
439 (req->ClientName->NameType == 1) && (req->ClientName->nameHints.count == 1) &&
440 (req->ClientName->Names[0].strLength == 14) && req->ClientRealm &&
441 (req->ClientRealm->strLength == 14) && req->SkewTime && (req->SkewTime->QuadPart == 0) &&
442 check_asn1(req->AuthData, 0, authData, sizeof(authData)) &&
443 check_asn1(req->GssChecksum, 8, gssChecksum, sizeof(gssChecksum));
444
445 rdpear_kerb_input_free(&input);
446 return rc;
447}
448
449static BOOL check_kerb_create_ap_req_authenticator_odd(wStream* s)
450{
451 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
453
454 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbCreateApReqAuthenticator);
455 rc = rc && req->ClientName && (req->ClientName->nameHints.count == 1) &&
456 check_unicode_string(&req->ClientName->Names[0], "Administrateu") &&
457 check_unicode_string(req->ClientRealm, "HARDENING3.COM") && req->GssChecksum &&
458 (req->GssChecksum->Asn1BufferHints.count == 27);
459
460 rdpear_kerb_input_free(&input);
461 return rc;
462}
463
464static BOOL check_kerb_decrypt_ap_reply(wStream* s)
465{
466 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
467 const DecryptApReplyReq* req = &input.Input.DecryptApReply;
468 const BYTE reply[] = { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 };
469 const BYTE key[] = { 0x01, 0x02, 0x03, 0x04 };
470
471 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbDecryptApReply);
472 rc = rc && check_asn1(req->EncryptedReply, 0x31, reply, sizeof(reply)) &&
473 check_key(req->Key, 0x12, key, sizeof(key));
474
475 rdpear_kerb_input_free(&input);
476 return rc;
477}
478
479static BOOL check_kerb_unpack_kdc_reply_body(wStream* s)
480{
481 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
482 const UnpackKdcReplyBodyReq* req = &input.Input.UnpackKdcReplyBody;
483 const BYTE data[] = { 0xf1, 0xf2, 0xf3 };
484 const BYTE key[] = { 0x99, 0x98 };
485
486 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbUnpackKdcReplyBody);
487 rc = rc && (req->Pdu == 0x1a) && (req->KeyUsage == 3) && !req->StrengthenKey &&
488 check_asn1(req->EncryptedData, 4, data, sizeof(data)) &&
489 check_key(req->Key, 0x12, key, sizeof(key));
490
491 rdpear_kerb_input_free(&input);
492 return rc;
493}
494
495static BOOL check_kerb_compute_tgs_checksum(wStream* s)
496{
497 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
498 const ComputeTgsChecksumReq* req = &input.Input.ComputeTgsChecksum;
499 const BYTE body[] = { 0xb1, 0xb2, 0xb3 };
500 const BYTE key[] = { 0xc1, 0xc2 };
501
502 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbComputeTgsChecksum);
503 rc = rc && (req->ChecksumType == 0x10) &&
504 check_asn1(req->requestBody, 0x0b, body, sizeof(body)) &&
505 check_key(req->Key, 0x11, key, sizeof(key));
506
507 rdpear_kerb_input_free(&input);
508 return rc;
509}
510
511static BOOL check_kerb_build_encrypted_auth_data(wStream* s)
512{
513 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
514 const BuildEncryptedAuthDataReq* req = &input.Input.BuildEncryptedAuthData;
515 const BYTE data[] = { 0xe1, 0xe2 };
516 const BYTE key[] = { 0xd1, 0xd2, 0xd3 };
517
518 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbBuildEncryptedAuthData);
519 rc = rc && (req->KeyUsage == 5) && check_asn1(req->PlainAuthData, 1, data, sizeof(data)) &&
520 check_key(req->Key, 0x12, key, sizeof(key));
521
522 rdpear_kerb_input_free(&input);
523 return rc;
524}
525
526static BOOL check_kerb_pack_ap_reply(wStream* s)
527{
528 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
529 const PackApReplyReq* req = &input.Input.PackApReply;
530 const BYTE reply[] = { 0x77 };
531 const BYTE body[] = { 0x66, 0x65 };
532 const BYTE key[] = { 0x55 };
533
534 BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbPackApReply);
535 rc = rc && check_asn1(req->Reply, 2, reply, sizeof(reply)) &&
536 check_asn1(req->ReplyBody, 3, body, sizeof(body)) &&
537 check_key(req->SessionKey, 0x12, key, sizeof(key));
538
539 rdpear_kerb_input_free(&input);
540 return rc;
541}
542
543static BOOL check_kerb_verify_checksum(wStream* s)
544{
545 KerbCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
546 const BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbVerifyChecksum);
547 rdpear_kerb_input_free(&input);
548 return rc;
549}
550
551static BOOL check_ntlm_negotiate_version(wStream* s)
552{
553 NtlmCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
554 const BOOL rc = decode_ntlm(s, &input) && (input.CallId == RemoteCallNtlmNegotiateVersion) &&
555 (input.Input.NegotiateVersion == 0);
556 rdpear_ntlm_input_free(&input);
557 return rc;
558}
559
560static BOOL check_credential(const MSV1_0_REMOTE_ENCRYPTED_SECRETS* cred, BYTE reserved2,
561 BYTE firstKeyByte, const BYTE* secret, UINT32 secretLen)
562{
563 if (!cred || (cred->reserved1 != 0) || (cred->reserved2 != reserved2) ||
564 (cred->reserved3 != 0) || (cred->reserved4 != 2) || (cred->reservedSize.count != secretLen))
565 return FALSE;
566
567 for (size_t i = 0; i < sizeof(cred->reserved5); i++)
568 {
569 if (cred->reserved5[i] != (BYTE)(firstKeyByte + i))
570 return FALSE;
571 }
572
573 if (!secretLen)
574 return !cred->reserved6;
575 return cred->reserved6 && (memcmp(cred->reserved6, secret, secretLen) == 0);
576}
577
578static BOOL check_ntlm_lm20_get_ntlm3_challenge_response(wStream* s)
579{
580 NtlmCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
582 const BYTE challenge[] = { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
583
584 BOOL rc =
585 decode_ntlm(s, &input) && (input.CallId == RemoteCallNtlmLm20GetNtlm3ChallengeResponse);
586 rc = rc && check_credential(req->Credential, 0, 0x70, nullptr, 0) &&
587 check_unicode_string(req->UserName, "usr") &&
588 check_unicode_string(req->LogonDomainName, "DM") && !req->ServerName &&
589 (memcmp(req->ChallengeToClient, challenge, sizeof(challenge)) == 0);
590
591 rdpear_ntlm_input_free(&input);
592 return rc;
593}
594
595static BOOL check_ntlm_calculate_nt_response(wStream* s)
596{
597 NtlmCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
598 const CalculateNtResponseReq* req = &input.Input.CalculateNtResponse;
599 const BYTE challenge[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
600 const BYTE secret[] = { 0xee };
601
602 BOOL rc = decode_ntlm(s, &input) && (input.CallId == RemoteCallNtlmCalculateNtResponse);
603 rc = rc && req->NtChallenge &&
604 (memcmp(req->NtChallenge->Data, challenge, sizeof(challenge)) == 0) &&
605 check_credential(req->Credential, 0, 0x50, secret, sizeof(secret));
606
607 rdpear_ntlm_input_free(&input);
608 return rc;
609}
610
611static BOOL check_ntlm_compare_credentials(wStream* s)
612{
613 NtlmCredIsoRemoteInput input = WINPR_C_ARRAY_INIT;
614 const CompareCredentialsReq* req = &input.Input.CompareCredentials;
615 const BYTE secret[] = { 0xa0, 0xa1, 0xa2 };
616
617 BOOL rc = decode_ntlm(s, &input) && (input.CallId == RemoteCallNtlmCompareCredentials);
618 rc = rc && (req->LhsCredential->reserved1 == 1) && (req->LhsCredential->reserved3 == 1) &&
619 req->RhsCredential && (req->RhsCredential->reserved2 == 1) &&
620 (req->LhsCredential->reservedSize.count == sizeof(secret)) &&
621 (memcmp(req->LhsCredential->reserved6, secret, sizeof(secret)) == 0) &&
622 (req->LhsCredential->reserved5[0] == 0x10) &&
623 check_credential(req->RhsCredential, 1, 0x30, nullptr, 0);
624
625 rdpear_ntlm_input_free(&input);
626 return rc;
627}
628
629typedef struct
630{
631 const char* name;
632 const BYTE* data;
633 size_t len;
634 CallCheckFn check;
635} CallVector;
636
637#define CALL_VECTOR(NAME) { #NAME, NAME##_call, sizeof(NAME##_call), check_##NAME }
638
639static const CallVector callVectors[] = {
640 CALL_VECTOR(kerb_negotiate_version),
641 CALL_VECTOR(kerb_create_ap_req_authenticator),
642 CALL_VECTOR(kerb_create_ap_req_authenticator_odd),
643 CALL_VECTOR(kerb_decrypt_ap_reply),
644 CALL_VECTOR(kerb_unpack_kdc_reply_body),
645 CALL_VECTOR(kerb_compute_tgs_checksum),
646 CALL_VECTOR(kerb_build_encrypted_auth_data),
647 CALL_VECTOR(kerb_pack_ap_reply),
648 CALL_VECTOR(kerb_verify_checksum),
649 CALL_VECTOR(ntlm_negotiate_version),
650 CALL_VECTOR(ntlm_lm20_get_ntlm3_challenge_response),
651 CALL_VECTOR(ntlm_calculate_nt_response),
652 CALL_VECTOR(ntlm_compare_credentials),
653};
654
655static BOOL test_calls(void)
656{
657 for (size_t i = 0; i < ARRAYSIZE(callVectors); i++)
658 {
659 const CallVector* v = &callVectors[i];
660 wStream* s = wrap_call(v->data, v->len);
661 const BOOL ok = s && v->check(s);
662 Stream_Free(s, TRUE);
663 if (!ok)
664 {
665 (void)fprintf(stderr, "%s: decoding failed\n", v->name);
666 return FALSE;
667 }
668 }
669 return TRUE;
670}
671
673static BOOL test_truncated_calls(void)
674{
675 for (size_t i = 0; i < ARRAYSIZE(callVectors); i++)
676 {
677 const CallVector* v = &callVectors[i];
678
679 /* the arm of a call that is not described is not decoded */
680 if (v->check == check_kerb_verify_checksum)
681 continue;
682
683 for (size_t len = 0; len < v->len; len++)
684 {
685 wStream* s = wrap_call(v->data, len);
686 if (!s)
687 return FALSE;
688
689 /* the object length is padded, the truncated part must be really missing */
690 Stream_SetLength(s, 16 + len);
691 Stream_SetPosition(s, 8);
692 Stream_Write_UINT32(s, (UINT32)len);
693 Stream_SetPosition(s, 0);
694
695 const BOOL ok = v->check(s);
696 Stream_Free(s, TRUE);
697 if (ok && (len + 4 <= v->len))
698 {
699 (void)fprintf(stderr, "%s: truncated call of %" PRIuz " bytes accepted\n", v->name,
700 len);
701 return FALSE;
702 }
703 }
704 }
705 return TRUE;
706}
707
708static BOOL test_bad_discriminant(void)
709{
710 BYTE data[sizeof(kerb_decrypt_ap_reply_call)] = WINPR_C_ARRAY_INIT;
711 memcpy(data, kerb_decrypt_ap_reply_call, sizeof(data));
712 data[10] = 0x05; /* discriminant: RemoteCallKerbUnpackKdcReplyBody */
713
714 wStream* s = wrap_call(data, sizeof(data));
715 const BOOL ok = s && !check_kerb_decrypt_ap_reply(s);
716 Stream_Free(s, TRUE);
717 return ok;
718}
719
720/* ========================== reply encoding ========================== */
721
722static BOOL check_kerb_reply(const char* what, const KerbCredIsoRemoteOutput* output,
723 const BYTE* expected, size_t expectedLen)
724{
725 WinPrNdrEncoder* encoder = winpr_ndr_encoder_new(FALSE, 1);
726 wStream* s = Stream_New(nullptr, 64);
727 const BOOL rc =
728 encoder && s && rdpear_kerb_output_write(encoder, s, output) &&
729 check_bytes(what, Stream_Buffer(s), Stream_GetPosition(s), expected, expectedLen);
730 Stream_Free(s, TRUE);
732 return rc;
733}
734
735static BOOL check_ntlm_reply(const char* what, const NtlmCredIsoRemoteOutput* output,
736 const BYTE* expected, size_t expectedLen)
737{
738 WinPrNdrEncoder* encoder = winpr_ndr_encoder_new(FALSE, 1);
739 wStream* s = Stream_New(nullptr, 64);
740 const BOOL rc =
741 encoder && s && rdpear_ntlm_output_write(encoder, s, output) &&
742 check_bytes(what, Stream_Buffer(s), Stream_GetPosition(s), expected, expectedLen);
743 Stream_Free(s, TRUE);
745 return rc;
746}
747
748static void fill_bytes(BYTE* data, size_t len, BYTE first)
749{
750 for (size_t i = 0; i < len; i++)
751 data[i] = (BYTE)(first + i);
752}
753
754static BOOL test_kerb_replies(void)
755{
756 BOOL rc = TRUE;
757
758 const KerbCredIsoRemoteOutput version = { .CallId = RemoteCallKerbNegotiateVersion };
759 rc &= check_kerb_reply("kerb_negotiate_version", &version, kerb_negotiate_version_reply,
760 sizeof(kerb_negotiate_version_reply));
761
762 BYTE authenticator[] = { 0xaa, 0xbb, 0xcc };
764 createApReq = { .CallId = RemoteCallKerbCreateApReqAuthenticator,
765 .Output = {
766 .CreateApReqAuthenticator = {
767 .AuthenticatorTime = { .QuadPart = 0x0102030405060708 },
768 .Authenticator = { 7, { sizeof(authenticator) }, authenticator },
769 .KerbProtocolError = 0x11223344 } } };
770 rc &= check_kerb_reply("kerb_create_ap_req_authenticator", &createApReq,
771 kerb_create_ap_req_authenticator_reply,
772 sizeof(kerb_create_ap_req_authenticator_reply));
773
774 BYTE asn1Buffer[] = { 0x01, 0x02, 0x03, 0x04 };
775 const KerbCredIsoRemoteOutput asn1 = {
776 .CallId = RemoteCallKerbDecryptApReply,
777 .Output = { .DecryptApReply = { 0x31, { sizeof(asn1Buffer) }, asn1Buffer } }
778 };
779 rc &= check_kerb_reply("kerb_asn1_data", &asn1, kerb_asn1_data_reply,
780 sizeof(kerb_asn1_data_reply));
781
782 BYTE replyBody[] = { 0x51, 0x52, 0x53 };
783 const KerbCredIsoRemoteOutput unpack = {
784 .CallId = RemoteCallKerbUnpackKdcReplyBody,
785 .Output = { .UnpackKdcReplyBody = { 0x55667788,
786 { 0x1a, { sizeof(replyBody) }, replyBody } } }
787 };
788 rc &= check_kerb_reply("kerb_unpack_kdc_reply_body", &unpack, kerb_unpack_kdc_reply_body_reply,
789 sizeof(kerb_unpack_kdc_reply_body_reply));
790
791 BYTE packed[] = { 0x61, 0x62, 0x63, 0x64, 0x65 };
792 const KerbCredIsoRemoteOutput pack = { .CallId = RemoteCallKerbPackApReply,
793 .Output = {
794 .PackApReply = { { sizeof(packed) }, packed } } };
795 rc &= check_kerb_reply("kerb_pack_ap_reply", &pack, kerb_pack_ap_reply_reply,
796 sizeof(kerb_pack_ap_reply_reply));
797
798 const KerbCredIsoRemoteOutput unhandled = { .CallId = RemoteCallKerbVerifyChecksum };
799 rc &= check_kerb_reply("kerb_unhandled", &unhandled, kerb_unhandled_reply,
800 sizeof(kerb_unhandled_reply));
801 return rc;
802}
803
804static BOOL test_ntlm_replies(void)
805{
806 BOOL rc = TRUE;
807
808 const NtlmCredIsoRemoteOutput version = { .CallId = RemoteCallNtlmNegotiateVersion };
809 rc &= check_ntlm_reply("ntlm_negotiate_version", &version, ntlm_negotiate_version_reply,
810 sizeof(ntlm_negotiate_version_reply));
811
812 const NtlmCredIsoRemoteOutput notSupported = { .CallId =
813 RemoteCallNtlmLm20GetNtlm3ChallengeResponse,
814 .Status = (UINT32)STATUS_NOT_SUPPORTED };
815 rc &= check_ntlm_reply("ntlm_lm20_not_supported", &notSupported, ntlm_lm20_not_supported_reply,
816 sizeof(ntlm_lm20_not_supported_reply));
817
818 BYTE ntlm3Response[] = { 0xa1, 0xa2, 0xa3 };
819 NtlmCredIsoRemoteOutput lm20 = { .CallId = RemoteCallNtlmLm20GetNtlm3ChallengeResponse };
821 resp->Ntlm3ResponseLength.count = sizeof(ntlm3Response);
822 resp->Ntlm3Response = ntlm3Response;
823 fill_bytes(resp->Lm3Response.Response, sizeof(resp->Lm3Response.Response), 0x10);
824 fill_bytes(resp->Lm3Response.ChallengeFromClient, sizeof(resp->Lm3Response.ChallengeFromClient),
825 0x20);
826 fill_bytes(resp->UserSessionKey.Data, sizeof(resp->UserSessionKey.Data), 0x30);
827 fill_bytes(resp->LmSessionKey.Data, sizeof(resp->LmSessionKey.Data), 0x40);
828 rc &= check_ntlm_reply("ntlm_lm20", &lm20, ntlm_lm20_reply, sizeof(ntlm_lm20_reply));
829
830 NtlmCredIsoRemoteOutput ntResponse = { .CallId = RemoteCallNtlmCalculateNtResponse };
831 fill_bytes(ntResponse.Output.CalculateNtResponse.Data,
832 sizeof(ntResponse.Output.CalculateNtResponse.Data), 0x60);
833 rc &= check_ntlm_reply("ntlm_calculate_nt_response", &ntResponse,
834 ntlm_calculate_nt_response_reply,
835 sizeof(ntlm_calculate_nt_response_reply));
836
837 const NtlmCredIsoRemoteOutput compare = { .CallId = RemoteCallNtlmCompareCredentials,
838 .Output = {
839 .CompareCredentials = { TRUE, FALSE, TRUE } } };
840 rc &= check_ntlm_reply("ntlm_compare_credentials", &compare, ntlm_compare_credentials_reply,
841 sizeof(ntlm_compare_credentials_reply));
842 return rc;
843}
844
845int TestRdpearMessages(int argc, char* argv[])
846{
847 WINPR_UNUSED(argc);
848 WINPR_UNUSED(argv);
849
850 (void)WLog_SetLogLevel(WLog_Get("com.winpr.ndr"), WLOG_OFF);
851 (void)WLog_SetLogLevel(WLog_Get(CHANNELS_TAG("rdpear")), WLOG_OFF);
852
853 if (!test_calls())
854 return -1;
855 if (!test_truncated_calls())
856 return -2;
857 if (!test_bad_discriminant())
858 return -3;
859 if (!test_kerb_replies())
860 return -4;
861 if (!test_ntlm_replies())
862 return -5;
863 return 0;
864}
NDR encoder and decoder ([C706] chapter 14, [MS-RPCE] 2.2.5 and 2.2.6).
static INLINE void winpr_ndr_decoder_destroy(WinPrNdrDecoder **pcontext)
frees a decoder and resets the pointer
Definition ndr.h:657
WINPR_API WinPrNdrEncoder * winpr_ndr_encoder_new(BOOL bigEndianDrep, BYTE version)
creates an encoder
Definition ndr.c:194
static INLINE void winpr_ndr_encoder_destroy(WinPrNdrEncoder **pcontext)
frees an encoder and resets the pointer
Definition ndr.h:405
WINPR_API WinPrNdrDecoder * winpr_ndr_decoder_new_fromStream(wStream *s)
reads the common type header ([MS-RPCE] 2.2.6.1) and creates a decoder for its version and endianness
Definition ndr.c:1124
UINT32 count
Definition ndr.h:554
2.2.2.1.8 BuildEncryptedAuthData
2.2.2.2.3 CalculateNtResponse
2.2.2.2.5 CompareCredentials
2.2.2.1.7 ComputeTgsChecksum
2.2.2.1.4 CreateApReqAuthenticator
2.2.1.2.1 KERB_ASN1_DATA
2.2.1.2.8 KERB_RPC_ENCRYPTION_KEY
KerbCredIsoRemoteInputUnion Input
2.2.2.2.2 Lm20GetNtlm3ChallengeResponse
2.2.1.3.6 MSV1_0_REMOTE_ENCRYPTED_SECRETS, opaque for the CredSSP server
BYTE reserved5[RDPEAR_MSV1_0_CREDENTIAL_KEY_LENGTH]
NtlmCredIsoRemoteInputUnion Input
NtlmCredIsoRemoteOutputUnion Output
2.3.10 RPC_UNICODE_STRING (MS-DTYP)
2.2.2.1.6 UnpackKdcReplyBody
CreateApReqAuthenticatorReq CreateApReqAuthenticator
Lm20GetNtlm3ChallengeResponseReq Lm20GetNtlm3ChallengeResponse
Lm20GetNtlm3ChallengeResponseResp Lm20GetNtlm3ChallengeResponse