summaryrefslogtreecommitdiffstats
path: root/src/crypto/ntlm.c
blob: 870af2132f58c777d8039c97ec0d5887d943abf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * NT LAN Manager (NTLM) authentication
 *
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/md4.h>
#include <ipxe/md5.h>
#include <ipxe/hmac.h>
#include <ipxe/ntlm.h>

/** Negotiate message
 *
 * This message content is fixed since there is no need to specify the
 * calling workstation name or domain name, and the set of flags is
 * mandated by the MS-NLMP specification.
 */
const struct ntlm_negotiate ntlm_negotiate = {
	.header = {
		.magic = NTLM_MAGIC,
		.type = cpu_to_le32 ( NTLM_NEGOTIATE ),
	},
	.flags = cpu_to_le32 ( NTLM_NEGOTIATE_EXTENDED_SESSIONSECURITY |
			       NTLM_NEGOTIATE_ALWAYS_SIGN |
			       NTLM_NEGOTIATE_NTLM |
			       NTLM_REQUEST_TARGET |
			       NTLM_NEGOTIATE_UNICODE ),
};

/**
 * Parse NTLM Challenge
 *
 * @v challenge		Challenge message
 * @v len		Length of Challenge message
 * @v info		Challenge information to fill in
 * @ret rc		Return status code
 */
int ntlm_challenge ( struct ntlm_challenge *challenge, size_t len,
		     struct ntlm_challenge_info *info ) {
	size_t offset;

	DBGC ( challenge, "NTLM challenge message:\n" );
	DBGC_HDA ( challenge, 0, challenge, len );

	/* Sanity checks */
	if ( len < sizeof ( *challenge ) ) {
		DBGC ( challenge, "NTLM underlength challenge (%zd bytes)\n",
		       len );
		return -EINVAL;
	}

	/* Extract nonce */
	info->nonce = &challenge->nonce;
	DBGC ( challenge, "NTLM challenge nonce:\n" );
	DBGC_HDA ( challenge, 0, info->nonce, sizeof ( *info->nonce ) );

	/* Extract target information */
	info->len = le16_to_cpu ( challenge->info.len );
	offset = le32_to_cpu ( challenge->info.offset );
	if ( ( offset > len ) ||
	     ( info->len > ( len - offset ) ) ) {
		DBGC ( challenge, "NTLM target information outside "
		       "challenge\n" );
		DBGC_HDA ( challenge, 0, challenge, len );
		return -EINVAL;
	}
	info->target = ( ( ( void * ) challenge ) + offset );
	DBGC ( challenge, "NTLM challenge target information:\n" );
	DBGC_HDA ( challenge, 0, info->target, info->len );

	return 0;
}

/**
 * Calculate NTLM verification key
 *
 * @v domain		Domain name (or NULL)
 * @v username		User name (or NULL)
 * @v password		Password (or NULL)
 * @v key		Key to fill in
 *
 * This is the NTOWFv2() function as defined in MS-NLMP.
 */
void ntlm_key ( const char *domain, const char *username,
		const char *password, struct ntlm_key *key ) {
	struct digest_algorithm *md4 = &md4_algorithm;
	struct digest_algorithm *md5 = &md5_algorithm;
	union {
		uint8_t md4[MD4_CTX_SIZE];
		uint8_t md5[MD5_CTX_SIZE];
	} ctx;
	uint8_t digest[MD4_DIGEST_SIZE];
	size_t digest_len;
	uint8_t c;
	uint16_t wc;

	/* Use empty usernames/passwords if not specified */
	if ( ! domain )
		domain = "";
	if ( ! username )
		username = "";
	if ( ! password )
		password = "";

	/* Construct MD4 digest of (Unicode) password */
	digest_init ( md4, ctx.md4 );
	while ( ( c = *(password++) ) ) {
		wc = cpu_to_le16 ( c );
		digest_update ( md4, ctx.md4, &wc, sizeof ( wc ) );
	}
	digest_final ( md4, ctx.md4, digest );

	/* Construct HMAC-MD5 of (Unicode) upper-case username */
	digest_len = sizeof ( digest );
	hmac_init ( md5, ctx.md5, digest, &digest_len );
	while ( ( c = *(username++) ) ) {
		wc = cpu_to_le16 ( toupper ( c ) );
		hmac_update ( md5, ctx.md5, &wc, sizeof ( wc ) );
	}
	while ( ( c = *(domain++) ) ) {
		wc = cpu_to_le16 ( c );
		hmac_update ( md5, ctx.md5, &wc, sizeof ( wc ) );
	}
	hmac_final ( md5, ctx.md5, digest, &digest_len, key->raw );
	DBGC ( key, "NTLM key:\n" );
	DBGC_HDA ( key, 0, key, sizeof ( *key ) );
}

/**
 * Construct NTLM responses
 *
 * @v info		Challenge information
 * @v key		Verification key
 * @v nonce		Nonce, or NULL to use a random nonce
 * @v lm		LAN Manager response to fill in
 * @v nt		NT response to fill in
 */
void ntlm_response ( struct ntlm_challenge_info *info, struct ntlm_key *key,
		     struct ntlm_nonce *nonce, struct ntlm_lm_response *lm,
		     struct ntlm_nt_response *nt ) {
	struct digest_algorithm *md5 = &md5_algorithm;
	struct ntlm_nonce tmp_nonce;
	uint8_t ctx[MD5_CTX_SIZE];
	size_t key_len = sizeof ( *key );
	unsigned int i;

	/* Generate random nonce, if needed */
	if ( ! nonce ) {
		for ( i = 0 ; i < sizeof ( tmp_nonce ) ; i++ )
			tmp_nonce.raw[i] = random();
		nonce = &tmp_nonce;
	}

	/* Construct LAN Manager response */
	memcpy ( &lm->nonce, nonce, sizeof ( lm->nonce ) );
	hmac_init ( md5, ctx, key->raw, &key_len );
	hmac_update ( md5, ctx, info->nonce, sizeof ( *info->nonce ) );
	hmac_update ( md5, ctx, &lm->nonce, sizeof ( lm->nonce ) );
	hmac_final ( md5, ctx, key->raw, &key_len, lm->digest );
	DBGC ( key, "NTLM LAN Manager response:\n" );
	DBGC_HDA ( key, 0, lm, sizeof ( *lm ) );

	/* Construct NT response */
	memset ( nt, 0, sizeof ( *nt ) );
	nt->version = NTLM_VERSION_NTLMV2;
	nt->high = NTLM_VERSION_NTLMV2;
	memcpy ( &nt->nonce, nonce, sizeof ( nt->nonce ) );
	hmac_init ( md5, ctx, key->raw, &key_len );
	hmac_update ( md5, ctx, info->nonce, sizeof ( *info->nonce ) );
	hmac_update ( md5, ctx, &nt->version,
		      ( sizeof ( *nt ) -
			offsetof ( typeof ( *nt ), version ) ) );
	hmac_update ( md5, ctx, info->target, info->len );
	hmac_update ( md5, ctx, &nt->zero, sizeof ( nt->zero ) );
	hmac_final ( md5, ctx, key->raw, &key_len, nt->digest );
	DBGC ( key, "NTLM NT response prefix:\n" );
	DBGC_HDA ( key, 0, nt, sizeof ( *nt ) );
}

/**
 * Append data to NTLM message
 *
 * @v header		Message header, or NULL to only calculate next payload
 * @v data		Data descriptor
 * @v payload		Data payload
 * @v len		Length of data
 * @ret payload		Next data payload
 */
static void * ntlm_append ( struct ntlm_header *header, struct ntlm_data *data,
			    void *payload, size_t len ) {

	/* Populate data descriptor */
	if ( header ) {
		data->offset = cpu_to_le32 ( payload - ( ( void * ) header ) );
		data->len = data->max_len = cpu_to_le16 ( len );
	}

	return ( payload + len );
}

/**
 * Append Unicode string data to NTLM message
 *
 * @v header		Message header, or NULL to only calculate next payload
 * @v data		Data descriptor
 * @v payload		Data payload
 * @v string		String to append, or NULL
 * @ret payload		Next data payload
 */
static void * ntlm_append_string ( struct ntlm_header *header,
				   struct ntlm_data *data, void *payload,
				   const char *string ) {
	uint16_t *tmp = payload;
	uint8_t c;

	/* Convert string to Unicode */
	for ( tmp = payload ; ( string && ( c = *(string++) ) ) ; tmp++ ) {
		if ( header )
			*tmp = cpu_to_le16 ( c );
	}

	/* Append string data */
	return ntlm_append ( header, data, payload,
			     ( ( ( void * ) tmp ) - payload ) );
}

/**
 * Construct NTLM Authenticate message
 *
 * @v info		Challenge information
 * @v domain		Domain name, or NULL
 * @v username		User name, or NULL
 * @v workstation	Workstation name, or NULL
 * @v lm		LAN Manager response
 * @v nt		NT response
 * @v auth		Message to fill in, or NULL to only calculate length
 * @ret len		Length of message
 */
size_t ntlm_authenticate ( struct ntlm_challenge_info *info, const char *domain,
			   const char *username, const char *workstation,
			   struct ntlm_lm_response *lm,
			   struct ntlm_nt_response *nt,
			   struct ntlm_authenticate *auth ) {
	void *tmp;
	size_t nt_len;
	size_t len;

	/* Construct response header */
	if ( auth ) {
		memset ( auth, 0, sizeof ( *auth ) );
		memcpy ( auth->header.magic, ntlm_negotiate.header.magic,
			 sizeof ( auth->header.magic ) );
		auth->header.type = cpu_to_le32 ( NTLM_AUTHENTICATE );
		auth->flags = ntlm_negotiate.flags;
	}
	tmp = ( ( ( void * ) auth ) + sizeof ( *auth ) );

	/* Construct LAN Manager response */
	if ( auth )
		memcpy ( tmp, lm, sizeof ( *lm ) );
	tmp = ntlm_append ( &auth->header, &auth->lm, tmp, sizeof ( *lm ) );

	/* Construct NT response */
	nt_len = ( sizeof ( *nt ) + info->len + sizeof ( nt->zero ) );
	if ( auth ) {
		memcpy ( tmp, nt, sizeof ( *nt ) );
		memcpy ( ( tmp + sizeof ( *nt ) ), info->target, info->len );
		memset ( ( tmp + sizeof ( *nt ) + info->len ), 0,
			 sizeof ( nt->zero ) );
	}
	tmp = ntlm_append ( &auth->header, &auth->nt, tmp, nt_len );

	/* Populate domain, user, and workstation names */
	tmp = ntlm_append_string ( &auth->header, &auth->domain, tmp, domain );
	tmp = ntlm_append_string ( &auth->header, &auth->user, tmp, username );
	tmp = ntlm_append_string ( &auth->header, &auth->workstation, tmp,
				   workstation );

	/* Calculate length */
	len = ( tmp - ( ( void * ) auth ) );
	if ( auth ) {
		DBGC ( auth, "NTLM authenticate message:\n" );
		DBGC_HDA ( auth, 0, auth, len );
	}

	return len;
}

/**
 * Calculate NTLM Authenticate message length
 *
 * @v info		Challenge information
 * @v domain		Domain name, or NULL
 * @v username		User name, or NULL
 * @v workstation	Workstation name, or NULL
 * @ret len		Length of Authenticate message
 */
size_t ntlm_authenticate_len ( struct ntlm_challenge_info *info,
			       const char *domain, const char *username,
			       const char *workstation ) {

	return ntlm_authenticate ( info, domain, username, workstation,
				   NULL, NULL, NULL );
}