Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2026 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : /*
21 : * Purpose: test NTLMv2 code (session key, key exchange, MIC and client blob).
22 : * Values are from MS-NLMP section 4.2.4 (NTLMv2 authentication).
23 : */
24 : #undef NDEBUG
25 : #include "../challenge.c"
26 :
27 : #include "common.h"
28 : #include <assert.h>
29 :
30 : static char *
31 : bin2ascii(char *dest, const void *data, size_t len)
32 : {
33 : char *s = dest;
34 : const unsigned char *src = (const unsigned char *) data;
35 :
36 1380 : for (; len > 0; --len, s += 2)
37 1380 : sprintf(s, "%02x", *src++);
38 80 : *s = 0;
39 : return dest;
40 : }
41 :
42 : static void
43 80 : check(const char *name, const void *data, size_t len, const char *expected)
44 : {
45 : char buf[1024];
46 :
47 80 : assert(len * 2 < sizeof(buf));
48 80 : bin2ascii(buf, data, len);
49 80 : if (strcmp(buf, expected) != 0) {
50 0 : fprintf(stderr, "Wrong %s: got %s expected %s\n", name, buf, expected);
51 0 : exit(1);
52 : }
53 80 : }
54 :
55 : /* NTOWFv2("Password", "User", "Domain") */
56 : static const unsigned char ntowfv2[16] = {
57 : 0x0c, 0x86, 0x8a, 0x40, 0x3b, 0xfd, 0x7a, 0x93, 0xa3, 0x00, 0x1e, 0xf2, 0x2e, 0xf0, 0x2e, 0x3f
58 : };
59 :
60 : static const unsigned char server_challenge[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
61 :
62 : static const unsigned char client_challenge[8] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
63 :
64 : /* MsvAvNbDomainName "Domain", MsvAvNbComputerName "Server", MsvAvEOL */
65 : static const unsigned char target_info[] = {
66 : 0x02, 0x00, 0x0c, 0x00, 'D', 0, 'o', 0, 'm', 0, 'a', 0, 'i', 0, 'n', 0,
67 : 0x01, 0x00, 0x0c, 0x00, 'S', 0, 'e', 0, 'r', 0, 'v', 0, 'e', 0, 'r', 0,
68 : 0x00, 0x00, 0x00, 0x00
69 : };
70 :
71 : #define TARGET_INFO_PAIRS_LEN (sizeof(target_info) - 4)
72 : #define PREFIX_LEN TDS_OFFSET(names_blob_prefix_t, target_info)
73 :
74 : static void
75 10 : test_responses(void)
76 : {
77 : unsigned char temp[PREFIX_LEN + sizeof(target_info) + 4];
78 : unsigned char *response;
79 : unsigned char session_base_key[16], encrypted_key[16], random_key[16];
80 :
81 : /* LMv2 response */
82 10 : response = make_lm_v2_response(ntowfv2, client_challenge, 8, server_challenge);
83 10 : assert(response);
84 10 : check("LMv2 response", response, 16, "86c35097ac9cec102554764a57cccc19");
85 10 : free(response);
86 :
87 : /* temp of the example: version, Z(6), time 0, client challenge, Z(4), target info, Z(4) */
88 10 : memset(temp, 0, sizeof(temp));
89 10 : temp[0] = temp[1] = 1;
90 10 : memcpy(temp + 16, client_challenge, 8);
91 10 : memcpy(temp + PREFIX_LEN, target_info, sizeof(target_info));
92 :
93 : /* NTProofStr is the first part of NTLMv2 response */
94 10 : response = make_lm_v2_response(ntowfv2, temp, sizeof(temp), server_challenge);
95 10 : assert(response);
96 10 : check("NTProofStr", response, 16, "68cd0ab851e51c96aabc927bebef6a1c");
97 :
98 : /* SessionBaseKey, computed like tds_answer_challenge_ntlmv2 */
99 10 : hmac_md5(ntowfv2, response, 16, session_base_key);
100 10 : check("SessionBaseKey", session_base_key, 16, "8de40ccadbc14a82f15cb0ad0de95ca3");
101 10 : free(response);
102 :
103 : /* EncryptedRandomSessionKey */
104 10 : memset(random_key, 0x55, sizeof(random_key));
105 10 : tds_rc4(session_base_key, 16, random_key, 16, encrypted_key);
106 10 : check("EncryptedRandomSessionKey", encrypted_key, 16, "c5dad2544fc9799094ce1ce90bc9d03e");
107 10 : }
108 :
109 : static void
110 10 : test_mic(void)
111 : {
112 : unsigned char key[16], mic[16];
113 :
114 10 : memset(key, 0x11, sizeof(key));
115 10 : assert(tds_ntlm_mic(key, (const unsigned char *) "NEGOTIATE", 9, (const unsigned char *) "CHALLENGE", 9,
116 : (const unsigned char *) "AUTHENTICATE", 12, mic));
117 10 : check("MIC", mic, 16, "762ceb6c2e0cd518d0de6c88e1c57f3f");
118 10 : }
119 :
120 : static void
121 10 : test_names_blob(void)
122 : {
123 : TDSNTLMAUTH auth;
124 : unsigned char info[sizeof(target_info) + 12];
125 : unsigned char *blob;
126 : int blob_len;
127 : bool use_mic;
128 : const unsigned char *p;
129 :
130 10 : memset(&auth, 0, sizeof(auth));
131 :
132 : /* no timestamp, no MIC, server pairs, terminator and Z(4) */
133 10 : assert(TDS_SUCCEED(build_names_blob(&auth, target_info, sizeof(target_info), true, &blob, &blob_len, &use_mic)));
134 10 : assert(!use_mic);
135 10 : assert(blob_len == PREFIX_LEN + sizeof(target_info) + 4);
136 10 : assert(memcmp(blob + PREFIX_LEN, target_info, sizeof(target_info)) == 0);
137 10 : assert(TDS_GET_UA4LE(blob + blob_len - 4) == 0);
138 10 : free(blob);
139 :
140 : /* add a timestamp, MIC should be used */
141 10 : memcpy(info, target_info, TARGET_INFO_PAIRS_LEN);
142 10 : memcpy(info + TARGET_INFO_PAIRS_LEN, "\x07\x00\x08\x00\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00", 16);
143 10 : auth.has_cbt = true;
144 10 : memset(auth.cbt, 0xcc, sizeof(auth.cbt));
145 10 : auth.spn = (char *) "S\0P\0N\0";
146 10 : auth.spn_len = 6;
147 10 : assert(TDS_SUCCEED(build_names_blob(&auth, info, sizeof(info), true, &blob, &blob_len, &use_mic)));
148 10 : assert(use_mic);
149 : /* server timestamp is used */
150 10 : check("timestamp", blob + TDS_OFFSET(names_blob_prefix_t, timestamp), 8, "0102030405060708");
151 : /* server pairs, MsvAvFlags with MIC bit, channel binding, target name, terminator, Z(4) */
152 10 : p = blob + PREFIX_LEN + TARGET_INFO_PAIRS_LEN + 12;
153 10 : check("added pairs", p, blob + blob_len - p,
154 : "0600040002000000" "0a001000cccccccccccccccccccccccccccccccc" "0900060053005000" "4e00" "00000000" "00000000");
155 10 : free(blob);
156 :
157 : /* MIC not allowed (NTLMv2 not used) */
158 10 : auth.has_cbt = false;
159 10 : auth.spn = NULL;
160 10 : auth.spn_len = 0;
161 10 : assert(TDS_SUCCEED(build_names_blob(&auth, info, sizeof(info), false, &blob, &blob_len, &use_mic)));
162 10 : assert(!use_mic);
163 10 : assert(blob_len == PREFIX_LEN + sizeof(info) + 4);
164 10 : free(blob);
165 :
166 : /* malformed pairs are rejected */
167 10 : assert(TDS_FAILED(build_names_blob(&auth, target_info, sizeof(target_info) - 4, true, &blob, &blob_len, &use_mic)));
168 10 : memcpy(info, target_info, sizeof(target_info));
169 10 : info[2] = 0x40;
170 10 : assert(TDS_FAILED(build_names_blob(&auth, info, sizeof(target_info), true, &blob, &blob_len, &use_mic)));
171 10 : }
172 :
173 : static void
174 10 : test_upper(void)
175 : {
176 : static const uint16_t tests[][2] = {
177 : {0x0061, 0x0041}, /* a */
178 : {0x007a, 0x005a}, /* z */
179 : {0x0041, 0x0041}, /* A */
180 : {0x00e9, 0x00c9}, /* e acute */
181 : {0x00ff, 0x0178}, /* y diaeresis */
182 : {0x00df, 0x00df}, /* sharp s, no simple mapping */
183 : {0x0131, 0x0049}, /* dotless i */
184 : {0x0101, 0x0100}, /* a macron, delta 0xffff range, every other character */
185 : {0x0100, 0x0100}, /* A macron */
186 : {0x03c3, 0x03a3}, /* sigma */
187 : {0x03c2, 0x03a3}, /* final sigma */
188 : {0x044f, 0x042f}, /* cyrillic ya */
189 : {0x0451, 0x0401}, /* cyrillic io */
190 : {0x1f80, 0x1f88}, /* greek alpha with psili and ypogegrammeni */
191 : {0x1f51, 0x1f59}, /* greek upsilon with dasia, delta 8 every other character */
192 : {0x1f52, 0x1f52}, /* greek upsilon with psili and varia, no simple mapping */
193 : {0x1f57, 0x1f5f}, /* greek upsilon with dasia and perispomeni */
194 : {0xff41, 0xff21}, /* fullwidth a */
195 : {0xd800, 0xd800}, /* surrogate */
196 : {0x20ac, 0x20ac}, /* euro sign */
197 : };
198 : unsigned int i;
199 : char buf[4];
200 :
201 210 : for (i = 0; i < TDS_VECTOR_SIZE(tests); ++i) {
202 200 : if (ucs2_upper(tests[i][0]) != tests[i][1]) {
203 0 : fprintf(stderr, "Wrong upper case of %04x: got %04x expected %04x\n",
204 : tests[i][0], ucs2_upper(tests[i][0]), tests[i][1]);
205 0 : exit(1);
206 : }
207 : }
208 :
209 : /* string conversion, UTF-16LE */
210 10 : memcpy(buf, "\xe9\x00\x3c\x04", 4);
211 10 : ucs2le_to_upper(buf, sizeof(buf));
212 10 : check("UTF-16 upper", buf, sizeof(buf), "c9001c04");
213 10 : }
214 :
215 10 : TEST_MAIN()
216 : {
217 10 : test_upper();
218 10 : test_responses();
219 10 : test_mic();
220 10 : test_names_blob();
221 10 : return 0;
222 : }
|