22#include <winpr/print.h>
25#include <winpr/string.h>
27#include <freerdp/channels/log.h>
29#include <rdpear-common/rdpear_common.h>
38static const BYTE kerb_negotiate_version_call[] = {
39 0x00, 0x00, 0x02, 0x00,
40 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x01, 0x00, 0x01,
42 0x00, 0x00, 0x00, 0x00,
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,
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,
90static const BYTE kerb_decrypt_ap_reply_call[] = {
91 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x04, 0x01, 0x04, 0x01,
93 0x04, 0x00, 0x02, 0x00,
94 0x08, 0x00, 0x02, 0x00,
95 0x31, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
96 0x0c, 0x00, 0x02, 0x00,
97 0x05, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
99 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
100 0x04, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
103static const BYTE kerb_unpack_kdc_reply_body_call[] = {
104 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x05, 0x01, 0x05, 0x01,
106 0x04, 0x00, 0x02, 0x00,
107 0x08, 0x00, 0x02, 0x00,
108 0x00, 0x00, 0x00, 0x00,
109 0x1a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
110 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
111 0x0c, 0x00, 0x02, 0x00,
112 0x03, 0x00, 0x00, 0x00, 0xf1, 0xf2, 0xf3, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
114 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
115 0x02, 0x00, 0x00, 0x00, 0x99, 0x98,
118static const BYTE kerb_compute_tgs_checksum_call[] = {
119 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x06, 0x01, 0x06, 0x01,
121 0x04, 0x00, 0x02, 0x00,
122 0x08, 0x00, 0x02, 0x00,
123 0x10, 0x00, 0x00, 0x00,
124 0x0b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
125 0x0c, 0x00, 0x02, 0x00,
126 0x03, 0x00, 0x00, 0x00, 0xb1, 0xb2, 0xb3, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
128 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
129 0x02, 0x00, 0x00, 0x00, 0xc1, 0xc2,
132static const BYTE kerb_build_encrypted_auth_data_call[] = {
133 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x07, 0x01, 0x07, 0x01,
135 0x05, 0x00, 0x00, 0x00,
136 0x04, 0x00, 0x02, 0x00,
137 0x08, 0x00, 0x02, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
139 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00,
140 0x03, 0x00, 0x00, 0x00, 0xd1, 0xd2, 0xd3, 0x00,
141 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
142 0x10, 0x00, 0x02, 0x00,
143 0x02, 0x00, 0x00, 0x00, 0xe1, 0xe2,
146static const BYTE kerb_pack_ap_reply_call[] = {
147 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
148 0x08, 0x01, 0x08, 0x01,
149 0x04, 0x00, 0x02, 0x00,
150 0x08, 0x00, 0x02, 0x00,
151 0x0c, 0x00, 0x02, 0x00,
152 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
153 0x10, 0x00, 0x02, 0x00,
154 0x01, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00,
155 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
156 0x14, 0x00, 0x02, 0x00,
157 0x02, 0x00, 0x00, 0x00, 0x66, 0x65, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
159 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x02, 0x00,
160 0x01, 0x00, 0x00, 0x00, 0x55,
163static const BYTE ntlm_negotiate_version_call[] = {
164 0x00, 0x00, 0x02, 0x00,
165 0x00, 0x02, 0x00, 0x02,
166 0x00, 0x00, 0x00, 0x00,
169static const BYTE ntlm_lm20_get_ntlm3_challenge_response_call[] = {
170 0x00, 0x00, 0x02, 0x00,
171 0x01, 0x02, 0x01, 0x02,
172 0x04, 0x00, 0x02, 0x00,
173 0x08, 0x00, 0x02, 0x00,
174 0x0c, 0x00, 0x02, 0x00,
175 0x00, 0x00, 0x00, 0x00,
176 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
177 0x00, 0x00, 0x00, 0x00,
179 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b,
180 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x06, 0x00, 0x08, 0x00, 0x10, 0x00, 0x02, 0x00,
184 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
185 'u', 0x00,
's', 0x00,
'r', 0x00, 0x00, 0x00,
186 0x04, 0x00, 0x04, 0x00, 0x14, 0x00, 0x02, 0x00,
187 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
188 'D', 0x00,
'M', 0x00,
191static const BYTE ntlm_calculate_nt_response_call[] = {
192 0x00, 0x00, 0x02, 0x00,
193 0x02, 0x02, 0x02, 0x02,
194 0x04, 0x00, 0x02, 0x00,
195 0x08, 0x00, 0x02, 0x00,
196 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
197 0x00, 0x00, 0x00, 0x00,
199 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
200 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
202 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00,
203 0x01, 0x00, 0x00, 0x00, 0xee,
206static const BYTE ntlm_compare_credentials_call[] = {
207 0x00, 0x00, 0x02, 0x00,
208 0x04, 0x02, 0x04, 0x02,
209 0x04, 0x00, 0x02, 0x00,
210 0x08, 0x00, 0x02, 0x00,
211 0x01, 0x00, 0x01, 0x00,
213 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
214 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
216 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x00,
217 0x03, 0x00, 0x00, 0x00, 0xa0, 0xa1, 0xa2, 0x00,
218 0x00, 0x01, 0x00, 0x00,
220 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
221 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43,
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227static const BYTE kerb_verify_checksum_call[] = {
228 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x0b, 0x01, 0x0b, 0x01,
230 0x11, 0x22, 0x33, 0x44,
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,
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,
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,
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,
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,
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,
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,
281static const BYTE ntlm_lm20_not_supported_reply[] = {
282 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc,
283 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 0x00, 0x00, 0x02, 0x00,
285 0x01, 0x02, 0x00, 0x00,
286 0xbb, 0x00, 0x00, 0xc0,
287 0x01, 0x02, 0x00, 0x00,
288 0x00, 0x00, 0x00, 0x00,
289 0x00, 0x00, 0x00, 0x00,
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,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297static const BYTE ntlm_lm20_reply[] = {
298 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc,
299 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x00, 0x00, 0x02, 0x00,
301 0x01, 0x02, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00,
303 0x01, 0x02, 0x00, 0x00,
304 0x03, 0x00, 0x00, 0x00,
305 0x04, 0x00, 0x02, 0x00,
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,
308 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
309 0x3c, 0x3d, 0x3e, 0x3f,
310 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
311 0x03, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0xa3,
315static const BYTE ntlm_calculate_nt_response_reply[] = {
316 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc,
317 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x02, 0x00,
319 0x02, 0x02, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00,
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,
327static const BYTE ntlm_compare_credentials_reply[] = {
328 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc,
329 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330 0x00, 0x00, 0x02, 0x00,
331 0x04, 0x02, 0x00, 0x00,
332 0x00, 0x00, 0x00, 0x00,
333 0x04, 0x02, 0x00, 0x00,
334 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335 0x01, 0x00, 0x00, 0x00,
336 0x00, 0x00, 0x00, 0x00,
342static wStream* wrap_call(
const BYTE* data,
size_t len)
344 const size_t padding = (8 - (len % 8)) % 8;
345 wStream* s = Stream_New(
nullptr, 16 + len + padding);
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);
360static BOOL check_bytes(
const char* what,
const BYTE* got,
size_t gotLen,
const BYTE* expected,
363 if ((gotLen == expectedLen) && (memcmp(got, expected, gotLen) == 0))
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");
373static BOOL check_asn1(
const KERB_ASN1_DATA* asn1, UINT32 pdu,
const BYTE* data, UINT32 len)
375 return asn1 && (asn1->Pdu == pdu) && (asn1->Asn1BufferHints.
count == len) && asn1->Asn1Buffer &&
376 (memcmp(asn1->Asn1Buffer, data, len) == 0);
382 return key && (key->reserved2 == enctype) && (key->reserved3.length == len) &&
383 key->reserved3.value && (memcmp(key->reserved3.value, data, len) == 0);
391 char* utf8 = ConvertWCharNToUtf8Alloc(str->Buffer, str->strLength,
nullptr);
392 const BOOL rc = utf8 && (strcmp(utf8, expected) == 0);
402 const BOOL rc = decoder && rdpear_kerb_input_read(decoder, s, input);
410 const BOOL rc = decoder && rdpear_ntlm_input_read(decoder, s, input);
415typedef BOOL (*CallCheckFn)(
wStream* s);
417static BOOL check_kerb_negotiate_version(
wStream* s)
420 const BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbNegotiateVersion) &&
421 (input.
Input.NegotiateVersion == 0);
422 rdpear_kerb_input_free(&input);
426static BOOL check_kerb_create_ap_req_authenticator(
wStream* s)
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 };
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));
445 rdpear_kerb_input_free(&input);
449static BOOL check_kerb_create_ap_req_authenticator_odd(
wStream* s)
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);
460 rdpear_kerb_input_free(&input);
464static BOOL check_kerb_decrypt_ap_reply(
wStream* s)
468 const BYTE reply[] = { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 };
469 const BYTE key[] = { 0x01, 0x02, 0x03, 0x04 };
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));
475 rdpear_kerb_input_free(&input);
479static BOOL check_kerb_unpack_kdc_reply_body(
wStream* s)
483 const BYTE data[] = { 0xf1, 0xf2, 0xf3 };
484 const BYTE key[] = { 0x99, 0x98 };
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));
491 rdpear_kerb_input_free(&input);
495static BOOL check_kerb_compute_tgs_checksum(
wStream* s)
499 const BYTE body[] = { 0xb1, 0xb2, 0xb3 };
500 const BYTE key[] = { 0xc1, 0xc2 };
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));
507 rdpear_kerb_input_free(&input);
511static BOOL check_kerb_build_encrypted_auth_data(
wStream* s)
515 const BYTE data[] = { 0xe1, 0xe2 };
516 const BYTE key[] = { 0xd1, 0xd2, 0xd3 };
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));
522 rdpear_kerb_input_free(&input);
526static BOOL check_kerb_pack_ap_reply(
wStream* s)
530 const BYTE reply[] = { 0x77 };
531 const BYTE body[] = { 0x66, 0x65 };
532 const BYTE key[] = { 0x55 };
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));
539 rdpear_kerb_input_free(&input);
543static BOOL check_kerb_verify_checksum(
wStream* s)
546 const BOOL rc = decode_kerb(s, &input) && (input.CallId == RemoteCallKerbVerifyChecksum);
547 rdpear_kerb_input_free(&input);
551static BOOL check_ntlm_negotiate_version(
wStream* s)
554 const BOOL rc = decode_ntlm(s, &input) && (input.CallId == RemoteCallNtlmNegotiateVersion) &&
555 (input.
Input.NegotiateVersion == 0);
556 rdpear_ntlm_input_free(&input);
561 BYTE firstKeyByte,
const BYTE* secret, UINT32 secretLen)
563 if (!cred || (cred->reserved1 != 0) || (cred->reserved2 != reserved2) ||
564 (cred->reserved3 != 0) || (cred->reserved4 != 2) || (cred->reservedSize.
count != secretLen))
567 for (
size_t i = 0; i <
sizeof(cred->
reserved5); i++)
569 if (cred->
reserved5[i] != (BYTE)(firstKeyByte + i))
574 return !cred->reserved6;
575 return cred->reserved6 && (memcmp(cred->reserved6, secret, secretLen) == 0);
578static BOOL check_ntlm_lm20_get_ntlm3_challenge_response(
wStream* s)
582 const BYTE challenge[] = { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
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);
591 rdpear_ntlm_input_free(&input);
595static BOOL check_ntlm_calculate_nt_response(
wStream* s)
599 const BYTE challenge[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
600 const BYTE secret[] = { 0xee };
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));
607 rdpear_ntlm_input_free(&input);
611static BOOL check_ntlm_compare_credentials(
wStream* s)
615 const BYTE secret[] = { 0xa0, 0xa1, 0xa2 };
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);
625 rdpear_ntlm_input_free(&input);
637#define CALL_VECTOR(NAME) { #NAME, NAME##_call, sizeof(NAME##_call), check_##NAME }
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),
655static BOOL test_calls(
void)
657 for (
size_t i = 0; i < ARRAYSIZE(callVectors); i++)
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);
665 (void)fprintf(stderr,
"%s: decoding failed\n", v->name);
673static BOOL test_truncated_calls(
void)
675 for (
size_t i = 0; i < ARRAYSIZE(callVectors); i++)
677 const CallVector* v = &callVectors[i];
680 if (v->check == check_kerb_verify_checksum)
683 for (
size_t len = 0; len < v->len; len++)
685 wStream* s = wrap_call(v->data, len);
690 Stream_SetLength(s, 16 + len);
691 Stream_SetPosition(s, 8);
692 Stream_Write_UINT32(s, (UINT32)len);
693 Stream_SetPosition(s, 0);
695 const BOOL ok = v->check(s);
696 Stream_Free(s, TRUE);
697 if (ok && (len + 4 <= v->len))
699 (void)fprintf(stderr,
"%s: truncated call of %" PRIuz
" bytes accepted\n", v->name,
708static BOOL test_bad_discriminant(
void)
710 BYTE data[
sizeof(kerb_decrypt_ap_reply_call)] = WINPR_C_ARRAY_INIT;
711 memcpy(data, kerb_decrypt_ap_reply_call,
sizeof(data));
714 wStream* s = wrap_call(data,
sizeof(data));
715 const BOOL ok = s && !check_kerb_decrypt_ap_reply(s);
716 Stream_Free(s, TRUE);
723 const BYTE* expected,
size_t expectedLen)
726 wStream* s = Stream_New(
nullptr, 64);
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);
736 const BYTE* expected,
size_t expectedLen)
739 wStream* s = Stream_New(
nullptr, 64);
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);
748static void fill_bytes(BYTE* data,
size_t len, BYTE first)
750 for (
size_t i = 0; i < len; i++)
751 data[i] = (BYTE)(first + i);
754static BOOL test_kerb_replies(
void)
759 rc &= check_kerb_reply(
"kerb_negotiate_version", &version, kerb_negotiate_version_reply,
760 sizeof(kerb_negotiate_version_reply));
762 BYTE authenticator[] = { 0xaa, 0xbb, 0xcc };
764 createApReq = { .CallId = RemoteCallKerbCreateApReqAuthenticator,
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));
774 BYTE asn1Buffer[] = { 0x01, 0x02, 0x03, 0x04 };
776 .CallId = RemoteCallKerbDecryptApReply,
777 .Output = { .DecryptApReply = { 0x31, {
sizeof(asn1Buffer) }, asn1Buffer } }
779 rc &= check_kerb_reply(
"kerb_asn1_data", &asn1, kerb_asn1_data_reply,
780 sizeof(kerb_asn1_data_reply));
782 BYTE replyBody[] = { 0x51, 0x52, 0x53 };
784 .CallId = RemoteCallKerbUnpackKdcReplyBody,
785 .Output = { .UnpackKdcReplyBody = { 0x55667788,
786 { 0x1a, {
sizeof(replyBody) }, replyBody } } }
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));
791 BYTE packed[] = { 0x61, 0x62, 0x63, 0x64, 0x65 };
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));
799 rc &= check_kerb_reply(
"kerb_unhandled", &unhandled, kerb_unhandled_reply,
800 sizeof(kerb_unhandled_reply));
804static BOOL test_ntlm_replies(
void)
809 rc &= check_ntlm_reply(
"ntlm_negotiate_version", &version, ntlm_negotiate_version_reply,
810 sizeof(ntlm_negotiate_version_reply));
813 RemoteCallNtlmLm20GetNtlm3ChallengeResponse,
814 .Status = (UINT32)STATUS_NOT_SUPPORTED };
815 rc &= check_ntlm_reply(
"ntlm_lm20_not_supported", ¬Supported, ntlm_lm20_not_supported_reply,
816 sizeof(ntlm_lm20_not_supported_reply));
818 BYTE ntlm3Response[] = { 0xa1, 0xa2, 0xa3 };
821 resp->Ntlm3ResponseLength.
count =
sizeof(ntlm3Response);
823 fill_bytes(resp->Lm3Response.Response,
sizeof(resp->Lm3Response.Response), 0x10);
824 fill_bytes(resp->Lm3Response.ChallengeFromClient,
sizeof(resp->Lm3Response.ChallengeFromClient),
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));
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));
839 .CompareCredentials = { TRUE, FALSE, TRUE } } };
840 rc &= check_ntlm_reply(
"ntlm_compare_credentials", &compare, ntlm_compare_credentials_reply,
841 sizeof(ntlm_compare_credentials_reply));
845int TestRdpearMessages(
int argc,
char* argv[])
850 (void)WLog_SetLogLevel(WLog_Get(
"com.winpr.ndr"), WLOG_OFF);
851 (void)WLog_SetLogLevel(WLog_Get(CHANNELS_TAG(
"rdpear")), WLOG_OFF);
855 if (!test_truncated_calls())
857 if (!test_bad_discriminant())
859 if (!test_kerb_replies())
861 if (!test_ntlm_replies())
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
WINPR_API WinPrNdrEncoder * winpr_ndr_encoder_new(BOOL bigEndianDrep, BYTE version)
creates an encoder
static INLINE void winpr_ndr_encoder_destroy(WinPrNdrEncoder **pcontext)
frees an encoder and resets the pointer
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
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.8 KERB_RPC_ENCRYPTION_KEY
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]
NtlmCredIsoRemoteOutputUnion Output
2.3.10 RPC_UNICODE_STRING (MS-DTYP)
2.2.2.1.6 UnpackKdcReplyBody
Lm20GetNtlm3ChallengeResponseResp Lm20GetNtlm3ChallengeResponse