You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ntlm_test.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * NTLM authentication self-tests
  27. *
  28. * The test vectors are taken from the MS-NLMP specification document.
  29. *
  30. */
  31. /* Forcibly enable assertions */
  32. #undef NDEBUG
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <byteswap.h>
  36. #include <ipxe/ntlm.h>
  37. #include <ipxe/test.h>
  38. /** A key generation test */
  39. struct ntlm_key_test {
  40. /** Domain name (or NULL) */
  41. const char *domain;
  42. /** User name (or NULL) */
  43. const char *username;
  44. /** Password (or NULL) */
  45. const char *password;
  46. /** Expected key */
  47. struct ntlm_key expected;
  48. };
  49. /** An authentication test */
  50. struct ntlm_authenticate_test {
  51. /** Domain name (or NULL) */
  52. const char *domain;
  53. /** User name (or NULL) */
  54. const char *username;
  55. /** Password (or NULL) */
  56. const char *password;
  57. /** Workstation (or NULL) */
  58. const char *workstation;
  59. /** Nonce */
  60. struct ntlm_nonce nonce;
  61. /** Challenge message */
  62. struct ntlm_challenge *challenge;
  63. /** Length of Challenge message */
  64. size_t challenge_len;
  65. /** Expected Authenticate message */
  66. struct ntlm_authenticate *expected;
  67. /** Expected length of Authenticate message */
  68. size_t expected_len;
  69. };
  70. /** Define inline message data */
  71. #define DATA(...) { __VA_ARGS__ }
  72. /** Define a key generation digest test */
  73. #define KEY_TEST( name, DOMAIN, USERNAME, PASSWORD, EXPECTED ) \
  74. static struct ntlm_key_test name = { \
  75. .domain = DOMAIN, \
  76. .username = USERNAME, \
  77. .password = PASSWORD, \
  78. .expected = { \
  79. .raw = EXPECTED, \
  80. }, \
  81. };
  82. /** Define an authentication test */
  83. #define AUTHENTICATE_TEST( name, DOMAIN, USERNAME, PASSWORD, \
  84. WORKSTATION, NONCE, CHALLENGE, EXPECTED ) \
  85. static const uint8_t name ## _challenge[] = CHALLENGE; \
  86. static const uint8_t name ## _expected[] = EXPECTED; \
  87. static struct ntlm_authenticate_test name = { \
  88. .domain = DOMAIN, \
  89. .username = USERNAME, \
  90. .password = PASSWORD, \
  91. .workstation = WORKSTATION, \
  92. .nonce = { \
  93. .raw = NONCE, \
  94. }, \
  95. .challenge = ( ( void * ) name ## _challenge ), \
  96. .challenge_len = sizeof ( name ## _challenge ), \
  97. .expected = ( ( void * ) name ## _expected ), \
  98. .expected_len = sizeof ( name ## _expected ), \
  99. };
  100. /** NTOWFv2() test from MS-NLMP specification */
  101. KEY_TEST ( msnlmp_ntowfv2, "Domain", "User", "Password",
  102. DATA ( 0x0c, 0x86, 0x8a, 0x40, 0x3b, 0xfd, 0x7a, 0x93, 0xa3, 0x00,
  103. 0x1e, 0xf2, 0x2e, 0xf0, 0x2e, 0x3f ) );
  104. /** Authentication test from MS-NLMP specification */
  105. AUTHENTICATE_TEST ( msnlmp_authenticate,
  106. "Domain", "User", "Password", "COMPUTER",
  107. DATA ( 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa ),
  108. DATA ( 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x02, 0x00,
  109. 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x38, 0x00, 0x00, 0x00,
  110. 0x33, 0x82, 0x8a, 0xe2, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  111. 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x24, 0x00, 0x24, 0x00, 0x44, 0x00, 0x00, 0x00, 0x06, 0x00,
  113. 0x70, 0x17, 0x00, 0x00, 0x00, 0x0f, 0x53, 0x00, 0x65, 0x00,
  114. 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x02, 0x00,
  115. 0x0c, 0x00, 0x44, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x61, 0x00,
  116. 0x69, 0x00, 0x6e, 0x00, 0x01, 0x00, 0x0c, 0x00, 0x53, 0x00,
  117. 0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00,
  118. 0x00, 0x00, 0x00, 0x00 ),
  119. DATA ( 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x03, 0x00,
  120. 0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x6c, 0x00, 0x00, 0x00,
  121. 0x54, 0x00, 0x54, 0x00, 0x84, 0x00, 0x00, 0x00, 0x0c, 0x00,
  122. 0x0c, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00,
  123. 0x54, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x5c, 0x00,
  124. 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0xd8, 0x00, 0x00, 0x00,
  125. 0x35, 0x82, 0x88, 0xe2, 0x05, 0x01, 0x28, 0x0a, 0x00, 0x00,
  126. 0x00, 0x0f, 0x44, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x61, 0x00,
  127. 0x69, 0x00, 0x6e, 0x00, 0x55, 0x00, 0x73, 0x00, 0x65, 0x00,
  128. 0x72, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4d, 0x00, 0x50, 0x00,
  129. 0x55, 0x00, 0x54, 0x00, 0x45, 0x00, 0x52, 0x00, 0x86, 0xc3,
  130. 0x50, 0x97, 0xac, 0x9c, 0xec, 0x10, 0x25, 0x54, 0x76, 0x4a,
  131. 0x57, 0xcc, 0xcc, 0x19, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  132. 0xaa, 0xaa, 0x68, 0xcd, 0x0a, 0xb8, 0x51, 0xe5, 0x1c, 0x96,
  133. 0xaa, 0xbc, 0x92, 0x7b, 0xeb, 0xef, 0x6a, 0x1c, 0x01, 0x01,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  136. 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x0c, 0x00,
  137. 0x44, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x69, 0x00,
  138. 0x6e, 0x00, 0x01, 0x00, 0x0c, 0x00, 0x53, 0x00, 0x65, 0x00,
  139. 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc5, 0xda, 0xd2, 0x54,
  141. 0x4f, 0xc9, 0x79, 0x90, 0x94, 0xce, 0x1c, 0xe9, 0x0b, 0xc9,
  142. 0xd0, 0x3e ) );
  143. /**
  144. * Report key generation test result
  145. *
  146. * @v test Key generation test
  147. * @v file Test code file
  148. * @v line Test code line
  149. */
  150. static void ntlm_key_okx ( struct ntlm_key_test *test,
  151. const char *file, unsigned int line ) {
  152. struct ntlm_key key;
  153. ntlm_key ( test->domain, test->username, test->password, &key );
  154. okx ( memcmp ( &key, &test->expected, sizeof ( key ) ) == 0,
  155. file, line );
  156. }
  157. #define ntlm_key_ok( test ) \
  158. ntlm_key_okx ( test, __FILE__, __LINE__ )
  159. /**
  160. * Report NTLM variable-length data test result
  161. *
  162. * @v msg Message header
  163. * @v msg_len Length of message
  164. * @v data Variable-length data descriptor
  165. * @v expected Expected message header
  166. * @v expected_data Expected variable-length data descriptor
  167. * @v field Field name
  168. * @v file Test code file
  169. * @v line Test code line
  170. */
  171. static void ntlm_data_okx ( struct ntlm_header *msg, size_t msg_len,
  172. struct ntlm_data *data,
  173. struct ntlm_header *expected,
  174. struct ntlm_data *expected_data,
  175. const char *field, const char *file,
  176. unsigned int line ) {
  177. size_t offset;
  178. size_t len;
  179. void *raw;
  180. void *expected_raw;
  181. /* Verify data lies within message */
  182. okx ( data->len == data->max_len, file, line );
  183. offset = le32_to_cpu ( data->offset );
  184. len = le16_to_cpu ( data->len );
  185. okx ( offset <= msg_len, file, line );
  186. okx ( len <= ( msg_len - offset ), file, line );
  187. /* Verify content matches expected content */
  188. raw = ( ( ( void * ) msg ) + offset );
  189. expected_raw = ( ( ( void * ) expected ) +
  190. le32_to_cpu ( expected_data->offset ) );
  191. DBGC ( msg, "NTLM %s expected:\n", field );
  192. DBGC_HDA ( msg, 0, expected_raw, le16_to_cpu ( expected_data->len ) );
  193. DBGC ( msg, "NTLM %s actual:\n", field );
  194. DBGC_HDA ( msg, 0, raw, len );
  195. okx ( data->len == expected_data->len, file, line );
  196. okx ( memcmp ( raw, expected_raw, len ) == 0, file, line );
  197. }
  198. #define ntlm_data_ok( msg, msg_len, data, expected, expected_data ) \
  199. ntlm_data_okx ( msg, msg_len, data, expected, expected_data, \
  200. __FILE__, __LINE__ )
  201. /**
  202. * Report NTLM authentication test result
  203. *
  204. * @v test Authentication test
  205. * @v file Test code file
  206. * @v line Test code line
  207. */
  208. static void ntlm_authenticate_okx ( struct ntlm_authenticate_test *test,
  209. const char *file, unsigned int line ) {
  210. struct ntlm_authenticate *expected = test->expected;
  211. struct ntlm_challenge_info info;
  212. struct ntlm_authenticate *auth;
  213. struct ntlm_key key;
  214. struct ntlm_lm_response lm;
  215. struct ntlm_nt_response nt;
  216. size_t len;
  217. /* Parse Challenge message */
  218. okx ( ntlm_challenge ( test->challenge, test->challenge_len,
  219. &info ) == 0, file, line );
  220. /* Generate key */
  221. ntlm_key ( test->domain, test->username, test->password, &key );
  222. /* Generate responses */
  223. ntlm_response ( &info, &key, &test->nonce, &lm, &nt );
  224. /* Allocate buffer for Authenticate message */
  225. len = ntlm_authenticate_len ( &info, test->domain, test->username,
  226. test->workstation );
  227. okx ( len >= sizeof ( *auth ), file, line );
  228. auth = malloc ( len );
  229. okx ( auth != NULL, file, line );
  230. /* Construct Authenticate message */
  231. okx ( ntlm_authenticate ( &info, test->domain, test->username,
  232. test->workstation, &lm, &nt, auth ) == len,
  233. file, line );
  234. /* Verify header */
  235. okx ( memcmp ( &auth->header, &expected->header,
  236. sizeof ( auth->header ) ) == 0, file, line );
  237. /* Verify LAN Manager response */
  238. ntlm_data_okx ( &auth->header, len, &auth->lm, &expected->header,
  239. &expected->lm, "LM", file, line );
  240. /* Verify NT response */
  241. ntlm_data_okx ( &auth->header, len, &auth->nt, &expected->header,
  242. &expected->nt, "NT", file, line );
  243. /* Verify domain name */
  244. ntlm_data_okx ( &auth->header, len, &auth->domain, &expected->header,
  245. &expected->domain, "domain", file, line );
  246. /* Verify user name */
  247. ntlm_data_okx ( &auth->header, len, &auth->user, &expected->header,
  248. &expected->user, "user", file, line );
  249. /* Verify workstation name */
  250. ntlm_data_okx ( &auth->header, len, &auth->workstation,
  251. &expected->header, &expected->workstation,
  252. "workstation",file, line );
  253. /* Verify session key */
  254. if ( auth->flags & NTLM_NEGOTIATE_KEY_EXCH ) {
  255. ntlm_data_okx ( &auth->header, len, &auth->session,
  256. &expected->header, &expected->session,
  257. "session", file, line );
  258. }
  259. /* Free Authenticate message */
  260. free ( auth );
  261. }
  262. #define ntlm_authenticate_ok( test ) \
  263. ntlm_authenticate_okx ( test, __FILE__, __LINE__ )
  264. /**
  265. * Perform NTLM self-test
  266. *
  267. */
  268. static void ntlm_test_exec ( void ) {
  269. /* Verify key generation */
  270. ntlm_key_ok ( &msnlmp_ntowfv2 );
  271. /* Verify authentication response */
  272. ntlm_authenticate_ok ( &msnlmp_authenticate );
  273. }
  274. /** NTLM self-test */
  275. struct self_test ntlm_test __self_test = {
  276. .name = "ntlm",
  277. .exec = ntlm_test_exec,
  278. };