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.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * NT LAN Manager (NTLM) authentication
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <byteswap.h>
  34. #include <ipxe/md4.h>
  35. #include <ipxe/md5.h>
  36. #include <ipxe/hmac.h>
  37. #include <ipxe/ntlm.h>
  38. /** Negotiate message
  39. *
  40. * This message content is fixed since there is no need to specify the
  41. * calling workstation name or domain name, and the set of flags is
  42. * mandated by the MS-NLMP specification.
  43. */
  44. const struct ntlm_negotiate ntlm_negotiate = {
  45. .header = {
  46. .magic = NTLM_MAGIC,
  47. .type = cpu_to_le32 ( NTLM_NEGOTIATE ),
  48. },
  49. .flags = cpu_to_le32 ( NTLM_NEGOTIATE_EXTENDED_SESSIONSECURITY |
  50. NTLM_NEGOTIATE_ALWAYS_SIGN |
  51. NTLM_NEGOTIATE_NTLM |
  52. NTLM_REQUEST_TARGET |
  53. NTLM_NEGOTIATE_UNICODE ),
  54. };
  55. /**
  56. * Parse NTLM Challenge
  57. *
  58. * @v challenge Challenge message
  59. * @v len Length of Challenge message
  60. * @v info Challenge information to fill in
  61. * @ret rc Return status code
  62. */
  63. int ntlm_challenge ( struct ntlm_challenge *challenge, size_t len,
  64. struct ntlm_challenge_info *info ) {
  65. size_t offset;
  66. DBGC ( challenge, "NTLM challenge message:\n" );
  67. DBGC_HDA ( challenge, 0, challenge, len );
  68. /* Sanity checks */
  69. if ( len < sizeof ( *challenge ) ) {
  70. DBGC ( challenge, "NTLM underlength challenge (%zd bytes)\n",
  71. len );
  72. return -EINVAL;
  73. }
  74. /* Extract nonce */
  75. info->nonce = &challenge->nonce;
  76. DBGC ( challenge, "NTLM challenge nonce:\n" );
  77. DBGC_HDA ( challenge, 0, info->nonce, sizeof ( *info->nonce ) );
  78. /* Extract target information */
  79. info->len = le16_to_cpu ( challenge->info.len );
  80. offset = le32_to_cpu ( challenge->info.offset );
  81. if ( ( offset > len ) ||
  82. ( info->len > ( len - offset ) ) ) {
  83. DBGC ( challenge, "NTLM target information outside "
  84. "challenge\n" );
  85. DBGC_HDA ( challenge, 0, challenge, len );
  86. return -EINVAL;
  87. }
  88. info->target = ( ( ( void * ) challenge ) + offset );
  89. DBGC ( challenge, "NTLM challenge target information:\n" );
  90. DBGC_HDA ( challenge, 0, info->target, info->len );
  91. return 0;
  92. }
  93. /**
  94. * Calculate NTLM verification key
  95. *
  96. * @v domain Domain name (or NULL)
  97. * @v username User name (or NULL)
  98. * @v password Password (or NULL)
  99. * @v key Key to fill in
  100. *
  101. * This is the NTOWFv2() function as defined in MS-NLMP.
  102. */
  103. void ntlm_key ( const char *domain, const char *username,
  104. const char *password, struct ntlm_key *key ) {
  105. struct digest_algorithm *md4 = &md4_algorithm;
  106. struct digest_algorithm *md5 = &md5_algorithm;
  107. union {
  108. uint8_t md4[MD4_CTX_SIZE];
  109. uint8_t md5[MD5_CTX_SIZE];
  110. } ctx;
  111. uint8_t digest[MD4_DIGEST_SIZE];
  112. size_t digest_len;
  113. uint8_t c;
  114. uint16_t wc;
  115. /* Use empty usernames/passwords if not specified */
  116. if ( ! domain )
  117. domain = "";
  118. if ( ! username )
  119. username = "";
  120. if ( ! password )
  121. password = "";
  122. /* Construct MD4 digest of (Unicode) password */
  123. digest_init ( md4, ctx.md4 );
  124. while ( ( c = *(password++) ) ) {
  125. wc = cpu_to_le16 ( c );
  126. digest_update ( md4, ctx.md4, &wc, sizeof ( wc ) );
  127. }
  128. digest_final ( md4, ctx.md4, digest );
  129. /* Construct HMAC-MD5 of (Unicode) upper-case username */
  130. digest_len = sizeof ( digest );
  131. hmac_init ( md5, ctx.md5, digest, &digest_len );
  132. while ( ( c = *(username++) ) ) {
  133. wc = cpu_to_le16 ( toupper ( c ) );
  134. hmac_update ( md5, ctx.md5, &wc, sizeof ( wc ) );
  135. }
  136. while ( ( c = *(domain++) ) ) {
  137. wc = cpu_to_le16 ( c );
  138. hmac_update ( md5, ctx.md5, &wc, sizeof ( wc ) );
  139. }
  140. hmac_final ( md5, ctx.md5, digest, &digest_len, key->raw );
  141. DBGC ( key, "NTLM key:\n" );
  142. DBGC_HDA ( key, 0, key, sizeof ( *key ) );
  143. }
  144. /**
  145. * Construct NTLM responses
  146. *
  147. * @v info Challenge information
  148. * @v key Verification key
  149. * @v nonce Nonce, or NULL to use a random nonce
  150. * @v lm LAN Manager response to fill in
  151. * @v nt NT response to fill in
  152. */
  153. void ntlm_response ( struct ntlm_challenge_info *info, struct ntlm_key *key,
  154. struct ntlm_nonce *nonce, struct ntlm_lm_response *lm,
  155. struct ntlm_nt_response *nt ) {
  156. struct digest_algorithm *md5 = &md5_algorithm;
  157. struct ntlm_nonce tmp_nonce;
  158. uint8_t ctx[MD5_CTX_SIZE];
  159. size_t key_len = sizeof ( *key );
  160. unsigned int i;
  161. /* Generate random nonce, if needed */
  162. if ( ! nonce ) {
  163. for ( i = 0 ; i < sizeof ( tmp_nonce ) ; i++ )
  164. tmp_nonce.raw[i] = random();
  165. nonce = &tmp_nonce;
  166. }
  167. /* Construct LAN Manager response */
  168. memcpy ( &lm->nonce, nonce, sizeof ( lm->nonce ) );
  169. hmac_init ( md5, ctx, key->raw, &key_len );
  170. hmac_update ( md5, ctx, info->nonce, sizeof ( *info->nonce ) );
  171. hmac_update ( md5, ctx, &lm->nonce, sizeof ( lm->nonce ) );
  172. hmac_final ( md5, ctx, key->raw, &key_len, lm->digest );
  173. DBGC ( key, "NTLM LAN Manager response:\n" );
  174. DBGC_HDA ( key, 0, lm, sizeof ( *lm ) );
  175. /* Construct NT response */
  176. memset ( nt, 0, sizeof ( *nt ) );
  177. nt->version = NTLM_VERSION_NTLMV2;
  178. nt->high = NTLM_VERSION_NTLMV2;
  179. memcpy ( &nt->nonce, nonce, sizeof ( nt->nonce ) );
  180. hmac_init ( md5, ctx, key->raw, &key_len );
  181. hmac_update ( md5, ctx, info->nonce, sizeof ( *info->nonce ) );
  182. hmac_update ( md5, ctx, &nt->version,
  183. ( sizeof ( *nt ) -
  184. offsetof ( typeof ( *nt ), version ) ) );
  185. hmac_update ( md5, ctx, info->target, info->len );
  186. hmac_update ( md5, ctx, &nt->zero, sizeof ( nt->zero ) );
  187. hmac_final ( md5, ctx, key->raw, &key_len, nt->digest );
  188. DBGC ( key, "NTLM NT response prefix:\n" );
  189. DBGC_HDA ( key, 0, nt, sizeof ( *nt ) );
  190. }
  191. /**
  192. * Append data to NTLM message
  193. *
  194. * @v header Message header, or NULL to only calculate next payload
  195. * @v data Data descriptor
  196. * @v payload Data payload
  197. * @v len Length of data
  198. * @ret payload Next data payload
  199. */
  200. static void * ntlm_append ( struct ntlm_header *header, struct ntlm_data *data,
  201. void *payload, size_t len ) {
  202. /* Populate data descriptor */
  203. if ( header ) {
  204. data->offset = cpu_to_le32 ( payload - ( ( void * ) header ) );
  205. data->len = data->max_len = cpu_to_le16 ( len );
  206. }
  207. return ( payload + len );
  208. }
  209. /**
  210. * Append Unicode string data to NTLM message
  211. *
  212. * @v header Message header, or NULL to only calculate next payload
  213. * @v data Data descriptor
  214. * @v payload Data payload
  215. * @v string String to append, or NULL
  216. * @ret payload Next data payload
  217. */
  218. static void * ntlm_append_string ( struct ntlm_header *header,
  219. struct ntlm_data *data, void *payload,
  220. const char *string ) {
  221. uint16_t *tmp = payload;
  222. uint8_t c;
  223. /* Convert string to Unicode */
  224. for ( tmp = payload ; ( string && ( c = *(string++) ) ) ; tmp++ ) {
  225. if ( header )
  226. *tmp = cpu_to_le16 ( c );
  227. }
  228. /* Append string data */
  229. return ntlm_append ( header, data, payload,
  230. ( ( ( void * ) tmp ) - payload ) );
  231. }
  232. /**
  233. * Construct NTLM Authenticate message
  234. *
  235. * @v info Challenge information
  236. * @v domain Domain name, or NULL
  237. * @v username User name, or NULL
  238. * @v workstation Workstation name, or NULL
  239. * @v lm LAN Manager response
  240. * @v nt NT response
  241. * @v auth Message to fill in, or NULL to only calculate length
  242. * @ret len Length of message
  243. */
  244. size_t ntlm_authenticate ( struct ntlm_challenge_info *info, const char *domain,
  245. const char *username, const char *workstation,
  246. struct ntlm_lm_response *lm,
  247. struct ntlm_nt_response *nt,
  248. struct ntlm_authenticate *auth ) {
  249. void *tmp;
  250. size_t nt_len;
  251. size_t len;
  252. /* Construct response header */
  253. if ( auth ) {
  254. memset ( auth, 0, sizeof ( *auth ) );
  255. memcpy ( auth->header.magic, ntlm_negotiate.header.magic,
  256. sizeof ( auth->header.magic ) );
  257. auth->header.type = cpu_to_le32 ( NTLM_AUTHENTICATE );
  258. auth->flags = ntlm_negotiate.flags;
  259. }
  260. tmp = ( ( ( void * ) auth ) + sizeof ( *auth ) );
  261. /* Construct LAN Manager response */
  262. if ( auth )
  263. memcpy ( tmp, lm, sizeof ( *lm ) );
  264. tmp = ntlm_append ( &auth->header, &auth->lm, tmp, sizeof ( *lm ) );
  265. /* Construct NT response */
  266. nt_len = ( sizeof ( *nt ) + info->len + sizeof ( nt->zero ) );
  267. if ( auth ) {
  268. memcpy ( tmp, nt, sizeof ( *nt ) );
  269. memcpy ( ( tmp + sizeof ( *nt ) ), info->target, info->len );
  270. memset ( ( tmp + sizeof ( *nt ) + info->len ), 0,
  271. sizeof ( nt->zero ) );
  272. }
  273. tmp = ntlm_append ( &auth->header, &auth->nt, tmp, nt_len );
  274. /* Populate domain, user, and workstation names */
  275. tmp = ntlm_append_string ( &auth->header, &auth->domain, tmp, domain );
  276. tmp = ntlm_append_string ( &auth->header, &auth->user, tmp, username );
  277. tmp = ntlm_append_string ( &auth->header, &auth->workstation, tmp,
  278. workstation );
  279. /* Calculate length */
  280. len = ( tmp - ( ( void * ) auth ) );
  281. if ( auth ) {
  282. DBGC ( auth, "NTLM authenticate message:\n" );
  283. DBGC_HDA ( auth, 0, auth, len );
  284. }
  285. return len;
  286. }
  287. /**
  288. * Calculate NTLM Authenticate message length
  289. *
  290. * @v info Challenge information
  291. * @v domain Domain name, or NULL
  292. * @v username User name, or NULL
  293. * @v workstation Workstation name, or NULL
  294. * @ret len Length of Authenticate message
  295. */
  296. size_t ntlm_authenticate_len ( struct ntlm_challenge_info *info,
  297. const char *domain, const char *username,
  298. const char *workstation ) {
  299. return ntlm_authenticate ( info, domain, username, workstation,
  300. NULL, NULL, NULL );
  301. }