auth_mschap_v1.c 11.8 KB
Newer Older
K
Kozlov Dmitry 已提交
1 2 3 4 5 6 7
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
8
#include <byteswap.h>
K
Kozlov Dmitry 已提交
9 10
#include <arpa/inet.h>

11 12
#include <openssl/md4.h>
#include <openssl/des.h>
13
#include <openssl/sha.h>
K
Kozlov Dmitry 已提交
14 15 16

#include "log.h"
#include "ppp.h"
17
#include "events.h"
K
Kozlov Dmitry 已提交
18 19 20 21
#include "ppp_auth.h"
#include "ppp_lcp.h"
#include "pwdb.h"

D
Dmitry Kozlov 已提交
22 23
#include "memdebug.h"

24 25
#define MSCHAP_V1 0x80

K
Kozlov Dmitry 已提交
26 27 28 29 30
#define CHAP_CHALLENGE 1
#define CHAP_RESPONSE  2
#define CHAP_SUCCESS   3
#define CHAP_FAILURE   4

31 32
#define VALUE_SIZE 8
#define RESPONSE_VALUE_SIZE (24+24+1)
K
Kozlov Dmitry 已提交
33 34 35

#define HDR_LEN (sizeof(struct chap_hdr_t)-2)

K
Kozlov Dmitry 已提交
36
static int conf_timeout = 5;
D
Dmitry Kozlov 已提交
37
static int conf_interval = 0;
K
Kozlov Dmitry 已提交
38
static int conf_max_failure = 3;
39
static int conf_any_login = 0;
40 41 42
static char *conf_msg_failure = "E=691 R=0";
static char *conf_msg_success = "Authentication successed";
;
D
Dmitry Kozlov 已提交
43

K
Kozlov Dmitry 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static int urandom_fd;

struct chap_hdr_t
{
	uint16_t proto;
	uint8_t code;
	uint8_t id;
	uint16_t len;
} __attribute__((packed));

struct chap_challenge_t
{
	struct chap_hdr_t hdr;
	uint8_t val_size;
	uint8_t val[VALUE_SIZE];
	char name[0];
} __attribute__((packed));

62 63 64 65 66 67
struct chap_response_t
{
	struct chap_hdr_t hdr;
	uint8_t val_size;
	uint8_t lm_hash[24];
	uint8_t nt_hash[24];
68
	uint8_t flags;
69 70 71
	char name[0];
} __attribute__((packed));

K
Kozlov Dmitry 已提交
72 73 74 75 76 77 78
struct chap_auth_data_t
{
	struct auth_data_t auth;
	struct ppp_handler_t h;
	struct ppp_t *ppp;
	int id;
	uint8_t val[VALUE_SIZE];
D
Dmitry Kozlov 已提交
79 80 81 82
	struct triton_timer_t timeout;
	struct triton_timer_t interval;
	int failure;
	int started:1;
K
Kozlov Dmitry 已提交
83 84 85 86
};

static void chap_send_challenge(struct chap_auth_data_t *ad);
static void chap_recv(struct ppp_handler_t *h);
87
static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *res, const char *name);
88 89
static void chap_timeout_timer(struct triton_timer_t *t);
static void chap_restart_timer(struct triton_timer_t *t);
90
static void set_mppe_keys(struct chap_auth_data_t *ad, uint8_t *z_hash);
K
Kozlov Dmitry 已提交
91 92 93 94

static void print_buf(const uint8_t *buf,int size)
{
	int i;
K
Kozlov Dmitry 已提交
95
	for (i = 0; i < size; i++)
96
		log_ppp_info2("%x", buf[i]);
K
Kozlov Dmitry 已提交
97
}
K
Kozlov Dmitry 已提交
98
static void print_str(const char *buf, int size)
K
Kozlov Dmitry 已提交
99 100
{
	int i;
K
Kozlov Dmitry 已提交
101
	for(i = 0; i < size; i++)
102
		log_ppp_info2("%c", buf[i]);
K
Kozlov Dmitry 已提交
103 104 105 106
}

static struct auth_data_t* auth_data_init(struct ppp_t *ppp)
{
K
Kozlov Dmitry 已提交
107
	struct chap_auth_data_t *d = _malloc(sizeof(*d));
K
Kozlov Dmitry 已提交
108

K
Kozlov Dmitry 已提交
109 110 111
	memset(d, 0, sizeof(*d));
	d->auth.proto = PPP_CHAP;
	d->ppp = ppp;
K
Kozlov Dmitry 已提交
112 113 114 115

	return &d->auth;
}

K
Kozlov Dmitry 已提交
116
static void auth_data_free(struct ppp_t *ppp, struct auth_data_t *auth)
K
Kozlov Dmitry 已提交
117
{
K
Kozlov Dmitry 已提交
118
	struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
K
Kozlov Dmitry 已提交
119

120 121 122 123 124 125
	if (d->timeout.tpd)
		triton_timer_del(&d->timeout);

	if (d->interval.tpd)
		triton_timer_del(&d->interval);

D
Dmitry Kozlov 已提交
126
	_free(d);
K
Kozlov Dmitry 已提交
127 128 129 130
}

static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth)
{
K
Kozlov Dmitry 已提交
131
	struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
K
Kozlov Dmitry 已提交
132

K
Kozlov Dmitry 已提交
133 134
	d->h.proto = PPP_CHAP;
	d->h.recv = chap_recv;
135
	d->timeout.expire = chap_timeout_timer;
K
Kozlov Dmitry 已提交
136
	d->timeout.period = conf_timeout * 1000;
137
	d->interval.expire = chap_restart_timer;
K
Kozlov Dmitry 已提交
138
	d->interval.period = conf_interval * 1000;
K
Kozlov Dmitry 已提交
139

K
Kozlov Dmitry 已提交
140
	ppp_register_chan_handler(ppp, &d->h);
K
Kozlov Dmitry 已提交
141 142 143 144 145 146 147 148

	chap_send_challenge(d);

	return 0;
}

static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth)
{
K
Kozlov Dmitry 已提交
149
	struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
K
Kozlov Dmitry 已提交
150

D
Dmitry Kozlov 已提交
151 152 153 154 155 156
	if (d->timeout.tpd)
		triton_timer_del(&d->timeout);

	if (d->interval.tpd)
		triton_timer_del(&d->interval);

K
Kozlov Dmitry 已提交
157
	ppp_unregister_handler(ppp, &d->h);
K
Kozlov Dmitry 已提交
158 159 160 161

	return 0;
}

162
static void chap_timeout_timer(struct triton_timer_t *t)
D
Dmitry Kozlov 已提交
163 164 165
{
	struct chap_auth_data_t *d = container_of(t, typeof(*d), timeout);

K
Kozlov Dmitry 已提交
166 167
	if (conf_ppp_verbose)
		log_ppp_warn("mschap-v1: timeout\n");
D
Dmitry Kozlov 已提交
168 169 170

	if (++d->failure == conf_max_failure) {
		if (d->started)
171
			ppp_terminate(d->ppp, TERM_USER_ERROR, 0);
D
Dmitry Kozlov 已提交
172
		else
173
			ppp_auth_failed(d->ppp, NULL);
D
Dmitry Kozlov 已提交
174 175 176 177 178 179
	} else {
		--d->id;
		chap_send_challenge(d);
	}
}

180
static void chap_restart_timer(struct triton_timer_t *t)
D
Dmitry Kozlov 已提交
181 182 183 184 185 186
{
	struct chap_auth_data_t *d = container_of(t, typeof(*d), interval);
	
	chap_send_challenge(d);
}

K
Kozlov Dmitry 已提交
187 188
static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr)
{
K
Kozlov Dmitry 已提交
189
	*ptr = MSCHAP_V1;
K
Kozlov Dmitry 已提交
190 191 192 193 194
	return 1;
}

static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr)
{
K
Kozlov Dmitry 已提交
195
	if (*ptr == MSCHAP_V1)
K
Kozlov Dmitry 已提交
196
		return LCP_OPT_ACK;
197
	return LCP_OPT_NAK;
K
Kozlov Dmitry 已提交
198 199
}

200
static void chap_send_failure(struct chap_auth_data_t *ad, char *mschap_error)
K
Kozlov Dmitry 已提交
201
{
202 203 204 205 206 207 208
	struct chap_hdr_t *hdr = _malloc(sizeof(*hdr) + strlen(mschap_error) + 1);
	hdr->proto = htons(PPP_CHAP);
	hdr->code = CHAP_FAILURE;
	hdr->id = ad->id;
	hdr->len = htons(HDR_LEN + strlen(mschap_error));
	strcpy((char *)(hdr + 1), mschap_error);

K
Kozlov Dmitry 已提交
209
	if (conf_ppp_verbose)
210 211 212
		log_ppp_info2("send [MSCHAP-v1 Failure id=%x \"%s\"]\n", hdr->id, mschap_error);

	ppp_chan_send(ad->ppp, hdr, ntohs(hdr->len) + 2);
K
Kozlov Dmitry 已提交
213

214
	_free(hdr);
K
Kozlov Dmitry 已提交
215 216 217 218
}

static void chap_send_success(struct chap_auth_data_t *ad)
{
219 220 221 222 223 224
	struct chap_hdr_t *hdr = _malloc(sizeof(*hdr) + strlen(conf_msg_success) + 1);
	hdr->proto = htons(PPP_CHAP);
	hdr->code = CHAP_SUCCESS;
	hdr->id = ad->id;
	hdr->len = htons(HDR_LEN + strlen(conf_msg_success));
	strcpy((char *)(hdr + 1), conf_msg_success);
K
Kozlov Dmitry 已提交
225
	
K
Kozlov Dmitry 已提交
226
	if (conf_ppp_verbose)
227
		log_ppp_info2("send [MSCHAP-v1 Success id=%x \"%s\"]\n", hdr->id, conf_msg_success);
K
Kozlov Dmitry 已提交
228

229 230 231
	ppp_chan_send(ad->ppp, hdr, ntohs(hdr->len) + 2);

	_free(hdr);
K
Kozlov Dmitry 已提交
232 233 234 235
}

static void chap_send_challenge(struct chap_auth_data_t *ad)
{
K
Kozlov Dmitry 已提交
236 237 238 239 240 241
	struct chap_challenge_t msg = {
		.hdr.proto = htons(PPP_CHAP),
		.hdr.code = CHAP_CHALLENGE,
		.hdr.id = ++ad->id,
		.hdr.len = htons(sizeof(msg) - 2),
		.val_size = VALUE_SIZE,
K
Kozlov Dmitry 已提交
242 243
	};

K
Kozlov Dmitry 已提交
244 245
	read(urandom_fd, ad->val, VALUE_SIZE);
	memcpy(msg.val, ad->val, VALUE_SIZE);
K
Kozlov Dmitry 已提交
246

K
Kozlov Dmitry 已提交
247
	if (conf_ppp_verbose) {
248
		log_ppp_info2("send [MSCHAP-v1 Challenge id=%x <", msg.hdr.id);
K
Kozlov Dmitry 已提交
249
		print_buf(msg.val, VALUE_SIZE);
250
		log_ppp_info2(">]\n");
K
Kozlov Dmitry 已提交
251
	}
K
Kozlov Dmitry 已提交
252

K
Kozlov Dmitry 已提交
253
	ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2);
D
Dmitry Kozlov 已提交
254 255 256

	if (conf_timeout && !ad->timeout.tpd)
		triton_timer_add(ad->ppp->ctrl->ctx, &ad->timeout, 0);
K
Kozlov Dmitry 已提交
257 258 259 260
}

static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr)
{
K
Kozlov Dmitry 已提交
261
	struct chap_response_t *msg = (struct chap_response_t*)hdr;
262
	char *name;
263
	char *mschap_error = conf_msg_failure;
264
	int r;
K
Kozlov Dmitry 已提交
265

D
Dmitry Kozlov 已提交
266 267 268
	if (ad->timeout.tpd)
		triton_timer_del(&ad->timeout);

K
Kozlov Dmitry 已提交
269
	if (conf_ppp_verbose) {
270
		log_ppp_info2("recv [MSCHAP-v1 Response id=%x <", msg->hdr.id);
K
Kozlov Dmitry 已提交
271
		print_buf(msg->lm_hash, 24);
272
		log_ppp_info2(">, <");
K
Kozlov Dmitry 已提交
273
		print_buf(msg->nt_hash, 24);
274
		log_ppp_info2(">, F=%i, name=\"", msg->flags);
K
Kozlov Dmitry 已提交
275
		print_str(msg->name, ntohs(msg->hdr.len) - sizeof(*msg) + 2);
276
		log_ppp_info2("\"]\n");
K
Kozlov Dmitry 已提交
277
	}
K
Kozlov Dmitry 已提交
278

K
Kozlov Dmitry 已提交
279 280
	if (msg->hdr.id != ad->id) {
		if (conf_ppp_verbose)
D
Dmitry Kozlov 已提交
281 282
			log_ppp_warn("mschap-v1: id mismatch\n");
		return;
K
Kozlov Dmitry 已提交
283 284
	}

K
Kozlov Dmitry 已提交
285 286
	if (msg->val_size != RESPONSE_VALUE_SIZE)	{
		log_ppp_error("mschap-v1: incorrect value-size (%i)\n", msg->val_size);
D
Dmitry Kozlov 已提交
287
		if (ad->started)
288
			ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0);
D
Dmitry Kozlov 已提交
289
		else
290
			ppp_auth_failed(ad->ppp, NULL);
D
Dmitry Kozlov 已提交
291
		return;
K
Kozlov Dmitry 已提交
292 293
	}

K
Kozlov Dmitry 已提交
294
	name = _strndup(msg->name, ntohs(msg->hdr.len) - sizeof(*msg) + 2);
295
	if (!name) {
K
Kozlov Dmitry 已提交
296
		log_emerg("mschap-v1: out of memory\n");
D
Dmitry Kozlov 已提交
297
		if (ad->started)
298
			ppp_terminate(ad->ppp, TERM_NAS_ERROR, 0);
D
Dmitry Kozlov 已提交
299
		else
300
			ppp_auth_failed(ad->ppp, NULL);
301 302
		return;
	}
303 304

	if (conf_any_login) {
305
		if (ppp_auth_successed(ad->ppp, name)) {
306
			chap_send_failure(ad, mschap_error);
307 308 309 310
			ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0);
			_free(name);
			return;
		}
311 312 313 314 315
		chap_send_success(ad);
		ad->started = 1;
		return;
	}

316
	r = pwdb_check(ad->ppp, name, PPP_CHAP, MSCHAP_V1, ad->id, ad->val, VALUE_SIZE, msg->lm_hash, msg->nt_hash, msg->flags, &mschap_error);
317
	if (r == PWDB_NO_IMPL)
318 319
		if (chap_check_response(ad, msg, name))
			r = PWDB_DENIED;
320 321
	
	if (r == PWDB_DENIED) {
322
		chap_send_failure(ad, mschap_error);
D
Dmitry Kozlov 已提交
323
		if (ad->started)
324
			ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0);
D
Dmitry Kozlov 已提交
325
		else
326
			ppp_auth_failed(ad->ppp, name);
D
Dmitry Kozlov 已提交
327
		_free(name);
328
	} else {
D
Dmitry Kozlov 已提交
329
		if (!ad->started) {
330
			if (ppp_auth_successed(ad->ppp, name)) {
331
				chap_send_failure(ad, mschap_error);
332 333 334 335 336 337 338 339 340 341
				ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0);
				_free(name);
			} else {
				chap_send_success(ad);
				ad->started = 1;
				if (conf_interval)
					triton_timer_add(ad->ppp->ctrl->ctx, &ad->interval, 0);
			}
		} else {
			chap_send_success(ad);
342
			_free(name);
343
		}
344 345 346 347 348
	}
}

static void des_encrypt(const uint8_t *input, const uint8_t *key, uint8_t *output)
{
K
Kozlov Dmitry 已提交
349
	int i, j, parity;
350 351 352 353 354 355 356 357 358
	union
	{
		uint64_t u64;
		uint8_t buf[8];
	} p_key;
	DES_cblock cb;
	DES_cblock res;
	DES_key_schedule ks;

K
Kozlov Dmitry 已提交
359 360
	memcpy(p_key.buf, key, 7);
	p_key.u64 = bswap_64(p_key.u64);
361

K
Kozlov Dmitry 已提交
362 363 364 365 366 367
	for (i = 0; i < 8; i++) {
		cb[i] = (((p_key.u64 << (7 * i)) >> 56) & 0xfe);
		for( j = 0, parity = 0; j < 7; j++)
			if ((cb[i] >> (j + 1)) & 1)
				parity++;
		cb[i] |= (~parity) & 1;
368 369 370
	}

	DES_set_key_checked(&cb, &ks);
K
Kozlov Dmitry 已提交
371 372 373
	memcpy(cb, input, 8);
	DES_ecb_encrypt(&cb, &res, &ks, DES_ENCRYPT);
	memcpy(output, res, 8);	
374 375
}

376
static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *msg, const char *name)
377 378 379 380 381 382 383 384
{
	MD4_CTX md4_ctx;
	uint8_t z_hash[21];
	uint8_t nt_hash[24];
	char *passwd;
	char *u_passwd;
	int i;
	
385
	passwd = pwdb_get_passwd(ad->ppp,name);
K
Kozlov Dmitry 已提交
386 387 388
	if (!passwd) {
		if (conf_ppp_verbose)
			log_ppp_warn("mschap-v1: user not found\n");
389
		chap_send_failure(ad, conf_msg_failure);
390
		return PWDB_DENIED;
K
Kozlov Dmitry 已提交
391 392
	}

K
Kozlov Dmitry 已提交
393 394 395 396
	u_passwd = _malloc(strlen(passwd) * 2);
	for (i = 0; i< strlen(passwd); i++) {
		u_passwd[i * 2] = passwd[i];
		u_passwd[i * 2 + 1] = 0;
K
Kozlov Dmitry 已提交
397 398
	}

K
Kozlov Dmitry 已提交
399
	memset(z_hash, 0, sizeof(z_hash));
400
	MD4_Init(&md4_ctx);
K
Kozlov Dmitry 已提交
401 402
	MD4_Update(&md4_ctx, u_passwd, strlen(passwd) * 2);
	MD4_Final(z_hash, &md4_ctx);
403

K
Kozlov Dmitry 已提交
404 405 406
	des_encrypt(ad->val, z_hash, nt_hash);
	des_encrypt(ad->val, z_hash + 7, nt_hash + 8);
	des_encrypt(ad->val, z_hash + 14, nt_hash + 16);
407

408 409
	set_mppe_keys(ad, z_hash);

D
Dmitry Kozlov 已提交
410 411
	_free(passwd);
	_free(u_passwd);
412

K
Kozlov Dmitry 已提交
413
	return memcmp(nt_hash, msg->nt_hash, 24) ? PWDB_DENIED : PWDB_SUCCESS;
K
Kozlov Dmitry 已提交
414 415
}

K
Kozlov Dmitry 已提交
416
static int chap_check(uint8_t *ptr)
K
Kozlov Dmitry 已提交
417
{
K
Kozlov Dmitry 已提交
418 419 420
	return *ptr == MSCHAP_V1;
}

421 422 423 424 425 426 427 428
static void set_mppe_keys(struct chap_auth_data_t *ad, uint8_t *z_hash)
{
	MD4_CTX md4_ctx;
	SHA_CTX sha_ctx;
	uint8_t digest[20];

	struct ev_mppe_keys_t ev_mppe = {
		.ppp = ad->ppp,
K
Kozlov Dmitry 已提交
429
		.policy = -1,
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
		.recv_key = digest,
		.send_key = digest,
	};

	//NtPasswordHashHash
	MD4_Init(&md4_ctx);
	MD4_Update(&md4_ctx, z_hash, 16);
	MD4_Final(digest, &md4_ctx);

	//Get_Start_Key
	SHA1_Init(&sha_ctx);
	SHA1_Update(&sha_ctx, digest, 16);
	SHA1_Update(&sha_ctx, digest, 16);
	SHA1_Update(&sha_ctx, ad->val, VALUE_SIZE);
	SHA1_Final(digest, &sha_ctx);	

	triton_event_fire(EV_MPPE_KEYS, &ev_mppe);
}

449 450 451 452 453 454 455 456 457
static int chap_restart(struct ppp_t *ppp, struct auth_data_t *auth)
{
	struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
	
	chap_send_challenge(d);

	return 0;
}

K
Kozlov Dmitry 已提交
458 459 460 461 462 463 464 465 466
static struct ppp_auth_handler_t chap = {
	.name          = "MSCHAP-v1",
	.init          = auth_data_init,
	.free          = auth_data_free,
	.send_conf_req = lcp_send_conf_req,
	.recv_conf_req = lcp_recv_conf_req,
	.start         = chap_start,
	.finish        = chap_finish,
	.check         = chap_check,
467
	.restart       = chap_restart,
K
Kozlov Dmitry 已提交
468 469 470 471
};

static void chap_recv(struct ppp_handler_t *h)
{
K
Kozlov Dmitry 已提交
472
	struct chap_auth_data_t *d = container_of(h, typeof(*d), h);
D
Dmitry Kozlov 已提交
473
	struct chap_hdr_t *hdr = (struct chap_hdr_t *)d->ppp->buf;
K
Kozlov Dmitry 已提交
474

D
Dmitry Kozlov 已提交
475
	if (d->ppp->buf_size < sizeof(*hdr) || ntohs(hdr->len) < HDR_LEN || ntohs(hdr->len) < d->ppp->buf_size - 2) {
476
		log_ppp_warn("mschap-v1: short packet received\n");
K
Kozlov Dmitry 已提交
477 478 479
		return;
	}

K
Kozlov Dmitry 已提交
480 481
	if (hdr->code == CHAP_RESPONSE)
		chap_recv_response(d, hdr);
K
Kozlov Dmitry 已提交
482
	else
K
Kozlov Dmitry 已提交
483
		log_ppp_warn("mschap-v1: unknown code received %x\n", hdr->code);
K
Kozlov Dmitry 已提交
484 485
}

486
static void auth_mschap_v1_init()
K
Kozlov Dmitry 已提交
487
{
D
Dmitry Kozlov 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501
	char *opt;

	opt = conf_get_opt("auth", "timeout");
	if (opt && atoi(opt) > 0)
		conf_timeout = atoi(opt);

	opt = conf_get_opt("auth", "interval");
	if (opt && atoi(opt) > 0)
		conf_interval = atoi(opt);

	opt = conf_get_opt("auth", "max-failure");
	if (opt && atoi(opt) > 0)
		conf_max_failure = atoi(opt);

502 503 504 505
	opt = conf_get_opt("auth", "any-login");
	if (opt && atoi(opt) > 0)
		conf_any_login = 1;

D
Dmitry Kozlov 已提交
506 507 508
	urandom_fd = open("/dev/urandom", O_RDONLY);
	if (urandom_fd < 0) {
		log_emerg("mschap-v1: failed to open /dev/urandom: %s\n", strerror(errno));
K
Kozlov Dmitry 已提交
509 510 511
		return;
	}
	if (ppp_auth_register_handler(&chap))
D
Dmitry Kozlov 已提交
512
		log_emerg("mschap-v1: failed to register handler\n");
K
Kozlov Dmitry 已提交
513 514
}

D
Dmitry Kozlov 已提交
515
DEFINE_INIT(5, auth_mschap_v1_init);