1 /*- 2 * Copyright (c) 2000 Semen Ustimenko <semenu@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/usr.sbin/ppp/mppe.c,v 1.4.2.6 2002/09/01 02:12:29 brian Exp $ 27 */ 28 29 #include <sys/param.h> 30 31 #include <sys/socket.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/in.h> 34 #include <netinet/ip.h> 35 #include <sys/un.h> 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <termios.h> 41 #include <openssl/rc4.h> 42 43 #include "defs.h" 44 #include "mbuf.h" 45 #include "log.h" 46 #include "timer.h" 47 #include "fsm.h" 48 #include "lqr.h" 49 #include "hdlc.h" 50 #include "lcp.h" 51 #include "ccp.h" 52 #include "throughput.h" 53 #include "layer.h" 54 #include "link.h" 55 #include "chap_ms.h" 56 #include "proto.h" 57 #include "mppe.h" 58 #include "ua.h" 59 #include "descriptor.h" 60 #ifndef NORADIUS 61 #include "radius.h" 62 #endif 63 #include "ncpaddr.h" 64 #include "iplist.h" 65 #include "slcompress.h" 66 #include "ipcp.h" 67 #include "ipv6cp.h" 68 #include "filter.h" 69 #include "mp.h" 70 #include "ncp.h" 71 #include "bundle.h" 72 73 /* 74 * Documentation: 75 * 76 * draft-ietf-pppext-mppe-04.txt 77 * draft-ietf-pppext-mppe-keys-02.txt 78 */ 79 80 #define MPPE_OPT_STATELESS 0x1000000 81 #define MPPE_OPT_COMPRESSED 0x01 82 #define MPPE_OPT_40BIT 0x20 83 #define MPPE_OPT_56BIT 0x80 84 #define MPPE_OPT_128BIT 0x40 85 #define MPPE_OPT_BITMASK 0xe0 86 #define MPPE_OPT_MASK (MPPE_OPT_STATELESS | MPPE_OPT_BITMASK) 87 88 #define MPPE_FLUSHED 0x8000 89 #define MPPE_ENCRYPTED 0x1000 90 #define MPPE_HEADER_BITMASK 0xf000 91 #define MPPE_HEADER_FLAG 0x00ff 92 #define MPPE_HEADER_FLAGMASK 0x00ff 93 #define MPPE_HEADER_FLAGSHIFT 8 94 #define MPPE_HEADER_STATEFUL_KEYCHANGES 16 95 96 struct mppe_state { 97 unsigned stateless : 1; 98 unsigned flushnext : 1; 99 unsigned flushrequired : 1; 100 int cohnum; 101 int keylen; /* 8 or 16 bytes */ 102 int keybits; /* 40, 56 or 128 bits */ 103 char sesskey[MPPE_KEY_LEN]; 104 char mastkey[MPPE_KEY_LEN]; 105 RC4_KEY rc4key; 106 }; 107 108 int MPPE_MasterKeyValid = 0; 109 int MPPE_IsServer = 0; 110 char MPPE_MasterKey[MPPE_KEY_LEN]; 111 112 /* 113 * The peer has missed a packet. Mark the next output frame to be FLUSHED 114 */ 115 static int 116 MPPEResetOutput(void *v) 117 { 118 struct mppe_state *mop = (struct mppe_state *)v; 119 120 if (mop->stateless) 121 log_Printf(LogCCP, "MPPE: Unexpected output channel reset\n"); 122 else { 123 log_Printf(LogCCP, "MPPE: Output channel reset\n"); 124 mop->flushnext = 1; 125 } 126 127 return 0; /* Ask FSM not to ACK */ 128 } 129 130 static void 131 MPPEReduceSessionKey(struct mppe_state *mp) 132 { 133 switch(mp->keybits) { 134 case 40: 135 mp->sesskey[2] = 0x9e; 136 mp->sesskey[1] = 0x26; 137 case 56: 138 mp->sesskey[0] = 0xd1; 139 case 128: 140 break; 141 } 142 } 143 144 static void 145 MPPEKeyChange(struct mppe_state *mp) 146 { 147 char InterimKey[MPPE_KEY_LEN]; 148 RC4_KEY RC4Key; 149 150 GetNewKeyFromSHA(mp->mastkey, mp->sesskey, mp->keylen, InterimKey); 151 RC4_set_key(&RC4Key, mp->keylen, InterimKey); 152 RC4(&RC4Key, mp->keylen, InterimKey, mp->sesskey); 153 154 MPPEReduceSessionKey(mp); 155 } 156 157 static struct mbuf * 158 MPPEOutput(void *v, struct ccp *ccp, struct link *l, int pri, u_short *proto, 159 struct mbuf *mp) 160 { 161 struct mppe_state *mop = (struct mppe_state *)v; 162 struct mbuf *mo; 163 u_short nproto, prefix; 164 int dictinit, ilen, len; 165 char *rp; 166 167 ilen = m_length(mp); 168 dictinit = 0; 169 170 log_Printf(LogDEBUG, "MPPE: Output: Proto %02x (%d bytes)\n", *proto, ilen); 171 if (*proto < 0x21 && *proto > 0xFA) { 172 log_Printf(LogDEBUG, "MPPE: Output: Not encrypting\n"); 173 ccp->compout += ilen; 174 ccp->uncompout += ilen; 175 return mp; 176 } 177 178 log_DumpBp(LogDEBUG, "MPPE: Output: Encrypt packet:", mp); 179 180 /* Get mbuf for prefixes */ 181 mo = m_get(4, MB_CCPOUT); 182 mo->m_next = mp; 183 184 rp = MBUF_CTOP(mo); 185 prefix = MPPE_ENCRYPTED | mop->cohnum; 186 187 if (mop->stateless || 188 (mop->cohnum & MPPE_HEADER_FLAGMASK) == MPPE_HEADER_FLAG) { 189 /* Change our key */ 190 log_Printf(LogDEBUG, "MPPEOutput: Key changed [%d]\n", mop->cohnum); 191 MPPEKeyChange(mop); 192 dictinit = 1; 193 } 194 195 if (mop->stateless || mop->flushnext) { 196 prefix |= MPPE_FLUSHED; 197 dictinit = 1; 198 mop->flushnext = 0; 199 } 200 201 if (dictinit) { 202 /* Initialise our dictionary */ 203 log_Printf(LogDEBUG, "MPPEOutput: Dictionary initialised [%d]\n", 204 mop->cohnum); 205 RC4_set_key(&mop->rc4key, mop->keylen, mop->sesskey); 206 } 207 208 /* Set MPPE packet prefix */ 209 ua_htons(&prefix, rp); 210 211 /* Save encrypted protocol number */ 212 nproto = htons(*proto); 213 RC4(&mop->rc4key, 2, (char *)&nproto, rp + 2); 214 215 /* Encrypt main packet */ 216 rp = MBUF_CTOP(mp); 217 RC4(&mop->rc4key, ilen, rp, rp); 218 219 mop->cohnum++; 220 mop->cohnum &= ~MPPE_HEADER_BITMASK; 221 222 /* Set the protocol number */ 223 *proto = ccp_Proto(ccp); 224 len = m_length(mo); 225 ccp->uncompout += ilen; 226 ccp->compout += len; 227 228 log_Printf(LogDEBUG, "MPPE: Output: Encrypted: Proto %02x (%d bytes)\n", 229 *proto, len); 230 231 return mo; 232 } 233 234 static void 235 MPPEResetInput(void *v) 236 { 237 log_Printf(LogCCP, "MPPE: Unexpected input channel ack\n"); 238 } 239 240 static struct mbuf * 241 MPPEInput(void *v, struct ccp *ccp, u_short *proto, struct mbuf *mp) 242 { 243 struct mppe_state *mip = (struct mppe_state *)v; 244 u_short prefix; 245 char *rp; 246 int dictinit, flushed, ilen, len, n; 247 248 ilen = m_length(mp); 249 dictinit = 0; 250 ccp->compin += ilen; 251 252 log_Printf(LogDEBUG, "MPPE: Input: Proto %02x (%d bytes)\n", *proto, ilen); 253 log_DumpBp(LogDEBUG, "MPPE: Input: Packet:", mp); 254 255 mp = mbuf_Read(mp, &prefix, 2); 256 prefix = ntohs(prefix); 257 flushed = prefix & MPPE_FLUSHED; 258 prefix &= ~flushed; 259 if ((prefix & MPPE_HEADER_BITMASK) != MPPE_ENCRYPTED) { 260 log_Printf(LogERROR, "MPPE: Input: Invalid packet (flags = 0x%x)\n", 261 (prefix & MPPE_HEADER_BITMASK) | flushed); 262 m_freem(mp); 263 return NULL; 264 } 265 266 prefix &= ~MPPE_HEADER_BITMASK; 267 268 if (!flushed && mip->stateless) { 269 log_Printf(LogCCP, "MPPEInput: Packet without MPPE_FLUSHED set" 270 " in stateless mode\n"); 271 flushed = MPPE_FLUSHED; 272 /* Should we really continue ? */ 273 } 274 275 if (mip->stateless) { 276 /* Change our key for each missed packet in stateless mode */ 277 while (prefix != mip->cohnum) { 278 log_Printf(LogDEBUG, "MPPEInput: Key changed [%u]\n", prefix); 279 MPPEKeyChange(mip); 280 /* 281 * mip->cohnum contains what we received last time in stateless 282 * mode. 283 */ 284 mip->cohnum++; 285 mip->cohnum &= ~MPPE_HEADER_BITMASK; 286 } 287 dictinit = 1; 288 } else { 289 if (flushed) { 290 /* 291 * We can always process a flushed packet. 292 * Catch up on any outstanding key changes. 293 */ 294 n = (prefix >> MPPE_HEADER_FLAGSHIFT) - 295 (mip->cohnum >> MPPE_HEADER_FLAGSHIFT); 296 if (n < 0) 297 n += MPPE_HEADER_STATEFUL_KEYCHANGES; 298 while (n--) { 299 log_Printf(LogDEBUG, "MPPEInput: Key changed during catchup [%u]\n", 300 prefix); 301 MPPEKeyChange(mip); 302 } 303 mip->flushrequired = 0; 304 mip->cohnum = prefix; 305 dictinit = 1; 306 } 307 308 if (mip->flushrequired) { 309 /* 310 * Perhaps we should be lenient if 311 * (prefix & MPPE_HEADER_FLAGMASK) == MPPE_HEADER_FLAG 312 * The spec says that we shouldn't be though.... 313 */ 314 log_Printf(LogDEBUG, "MPPE: Not flushed - discarded\n"); 315 fsm_Output(&ccp->fsm, CODE_RESETREQ, ccp->fsm.reqid++, NULL, 0, 316 MB_CCPOUT); 317 m_freem(mp); 318 return NULL; 319 } 320 321 if (prefix != mip->cohnum) { 322 /* 323 * We're in stateful mode and didn't receive the expected 324 * packet. Send a reset request, but don't tell the CCP layer 325 * about it as we don't expect to receive a Reset ACK ! 326 * Guess what... M$ invented this ! 327 */ 328 log_Printf(LogCCP, "MPPE: Input: Got seq %u, not %u\n", 329 prefix, mip->cohnum); 330 fsm_Output(&ccp->fsm, CODE_RESETREQ, ccp->fsm.reqid++, NULL, 0, 331 MB_CCPOUT); 332 mip->flushrequired = 1; 333 m_freem(mp); 334 return NULL; 335 } 336 337 if ((prefix & MPPE_HEADER_FLAGMASK) == MPPE_HEADER_FLAG) { 338 log_Printf(LogDEBUG, "MPPEInput: Key changed [%u]\n", prefix); 339 MPPEKeyChange(mip); 340 dictinit = 1; 341 } else if (flushed) 342 dictinit = 1; 343 344 /* 345 * mip->cohnum contains what we expect to receive next time in stateful 346 * mode. 347 */ 348 mip->cohnum++; 349 mip->cohnum &= ~MPPE_HEADER_BITMASK; 350 } 351 352 if (dictinit) { 353 log_Printf(LogDEBUG, "MPPEInput: Dictionary initialised [%u]\n", prefix); 354 RC4_set_key(&mip->rc4key, mip->keylen, mip->sesskey); 355 } 356 357 mp = mbuf_Read(mp, proto, 2); 358 RC4(&mip->rc4key, 2, (char *)proto, (char *)proto); 359 *proto = ntohs(*proto); 360 361 rp = MBUF_CTOP(mp); 362 len = m_length(mp); 363 RC4(&mip->rc4key, len, rp, rp); 364 365 log_Printf(LogDEBUG, "MPPEInput: Decrypted: Proto %02x (%d bytes)\n", 366 *proto, len); 367 log_DumpBp(LogDEBUG, "MPPEInput: Decrypted: Packet:", mp); 368 369 ccp->uncompin += len; 370 371 return mp; 372 } 373 374 static void 375 MPPEDictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf *mi) 376 { 377 } 378 379 static const char * 380 MPPEDispOpts(struct fsm_opt *o) 381 { 382 static char buf[70]; 383 u_int32_t val; 384 char ch; 385 int len, n; 386 387 ua_ntohl(o->data, &val); 388 len = 0; 389 if ((n = snprintf(buf, sizeof buf, "value 0x%08x ", (unsigned)val)) > 0) 390 len += n; 391 if (!(val & MPPE_OPT_BITMASK)) { 392 if ((n = snprintf(buf + len, sizeof buf - len, "(0")) > 0) 393 len += n; 394 } else { 395 ch = '('; 396 if (val & MPPE_OPT_128BIT) { 397 if ((n = snprintf(buf + len, sizeof buf - len, "%c128", ch)) > 0) 398 len += n; 399 ch = '/'; 400 } 401 if (val & MPPE_OPT_56BIT) { 402 if ((n = snprintf(buf + len, sizeof buf - len, "%c56", ch)) > 0) 403 len += n; 404 ch = '/'; 405 } 406 if (val & MPPE_OPT_40BIT) { 407 if ((n = snprintf(buf + len, sizeof buf - len, "%c40", ch)) > 0) 408 len += n; 409 ch = '/'; 410 } 411 } 412 413 if ((n = snprintf(buf + len, sizeof buf - len, " bits, state%s", 414 (val & MPPE_OPT_STATELESS) ? "less" : "ful")) > 0) 415 len += n; 416 417 if (val & MPPE_OPT_COMPRESSED) { 418 if ((n = snprintf(buf + len, sizeof buf - len, ", compressed")) > 0) 419 len += n; 420 } 421 422 snprintf(buf + len, sizeof buf - len, ")"); 423 424 return buf; 425 } 426 427 static int 428 MPPEUsable(struct fsm *fp) 429 { 430 int ok; 431 #ifndef NORADIUS 432 struct radius *r = &fp->bundle->radius; 433 434 /* 435 * If the radius server gave us RAD_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES, 436 * use that instead of our configuration value. 437 */ 438 if (*r->cfg.file) { 439 ok = r->mppe.sendkeylen && r->mppe.recvkeylen; 440 if (!ok) 441 log_Printf(LogCCP, "MPPE: Not permitted by RADIUS server\n"); 442 } else 443 #endif 444 { 445 struct lcp *lcp = &fp->link->lcp; 446 ok = (lcp->want_auth == PROTO_CHAP && lcp->want_authtype == 0x81) || 447 (lcp->his_auth == PROTO_CHAP && lcp->his_authtype == 0x81); 448 if (!ok) 449 log_Printf(LogCCP, "MPPE: Not usable without CHAP81\n"); 450 } 451 452 return ok; 453 } 454 455 static int 456 MPPERequired(struct fsm *fp) 457 { 458 #ifndef NORADIUS 459 /* 460 * If the radius server gave us RAD_MICROSOFT_MS_MPPE_ENCRYPTION_POLICY, 461 * use that instead of our configuration value. 462 */ 463 if (*fp->bundle->radius.cfg.file && fp->bundle->radius.mppe.policy) 464 return fp->bundle->radius.mppe.policy == MPPE_POLICY_REQUIRED ? 1 : 0; 465 #endif 466 467 return fp->link->ccp.cfg.mppe.required; 468 } 469 470 static u_int32_t 471 MPPE_ConfigVal(struct bundle *bundle, const struct ccp_config *cfg) 472 { 473 u_int32_t val; 474 475 val = cfg->mppe.state == MPPE_STATELESS ? MPPE_OPT_STATELESS : 0; 476 #ifndef NORADIUS 477 /* 478 * If the radius server gave us RAD_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES, 479 * use that instead of our configuration value. 480 */ 481 if (*bundle->radius.cfg.file && bundle->radius.mppe.types) { 482 if (bundle->radius.mppe.types & MPPE_TYPE_40BIT) 483 val |= MPPE_OPT_40BIT; 484 if (bundle->radius.mppe.types & MPPE_TYPE_128BIT) 485 val |= MPPE_OPT_128BIT; 486 } else 487 #endif 488 switch(cfg->mppe.keybits) { 489 case 128: 490 val |= MPPE_OPT_128BIT; 491 break; 492 case 56: 493 val |= MPPE_OPT_56BIT; 494 break; 495 case 40: 496 val |= MPPE_OPT_40BIT; 497 break; 498 case 0: 499 val |= MPPE_OPT_128BIT | MPPE_OPT_56BIT | MPPE_OPT_40BIT; 500 break; 501 } 502 503 return val; 504 } 505 506 /* 507 * What options should we use for our first configure request 508 */ 509 static void 510 MPPEInitOptsOutput(struct bundle *bundle, struct fsm_opt *o, 511 const struct ccp_config *cfg) 512 { 513 u_int32_t mval; 514 515 o->hdr.len = 6; 516 517 if (!MPPE_MasterKeyValid) { 518 log_Printf(LogCCP, "MPPE: MasterKey is invalid," 519 " MPPE is available only with CHAP81 authentication\n"); 520 mval = 0; 521 ua_htonl(&mval, o->data); 522 return; 523 } 524 525 526 mval = MPPE_ConfigVal(bundle, cfg); 527 ua_htonl(&mval, o->data); 528 } 529 530 /* 531 * Our CCP request was NAK'd with the given options 532 */ 533 static int 534 MPPESetOptsOutput(struct bundle *bundle, struct fsm_opt *o, 535 const struct ccp_config *cfg) 536 { 537 u_int32_t mval, peer; 538 539 ua_ntohl(o->data, &peer); 540 541 if (!MPPE_MasterKeyValid) 542 /* Treat their NAK as a REJ */ 543 return MODE_NAK; 544 545 mval = MPPE_ConfigVal(bundle, cfg); 546 547 /* 548 * If we haven't been configured with a specific number of keybits, allow 549 * whatever the peer asks for. 550 */ 551 if (!cfg->mppe.keybits) { 552 mval &= ~MPPE_OPT_BITMASK; 553 mval |= (peer & MPPE_OPT_BITMASK); 554 if (!(mval & MPPE_OPT_BITMASK)) 555 mval |= MPPE_OPT_128BIT; 556 } 557 558 /* Adjust our statelessness */ 559 if (cfg->mppe.state == MPPE_ANYSTATE) { 560 mval &= ~MPPE_OPT_STATELESS; 561 mval |= (peer & MPPE_OPT_STATELESS); 562 } 563 564 ua_htonl(&mval, o->data); 565 566 return MODE_ACK; 567 } 568 569 /* 570 * The peer has requested the given options 571 */ 572 static int 573 MPPESetOptsInput(struct bundle *bundle, struct fsm_opt *o, 574 const struct ccp_config *cfg) 575 { 576 u_int32_t mval, peer; 577 int res = MODE_ACK; 578 579 ua_ntohl(o->data, &peer); 580 if (!MPPE_MasterKeyValid) { 581 if (peer != 0) { 582 peer = 0; 583 ua_htonl(&peer, o->data); 584 return MODE_NAK; 585 } else 586 return MODE_ACK; 587 } 588 589 mval = MPPE_ConfigVal(bundle, cfg); 590 591 if (peer & ~MPPE_OPT_MASK) 592 /* He's asking for bits we don't know about */ 593 res = MODE_NAK; 594 595 if (peer & MPPE_OPT_STATELESS) { 596 if (cfg->mppe.state == MPPE_STATEFUL) 597 /* Peer can't have stateless */ 598 res = MODE_NAK; 599 else 600 /* Peer wants stateless, that's ok */ 601 mval |= MPPE_OPT_STATELESS; 602 } else { 603 if (cfg->mppe.state == MPPE_STATELESS) 604 /* Peer must have stateless */ 605 res = MODE_NAK; 606 else 607 /* Peer doesn't want stateless, that's ok */ 608 mval &= ~MPPE_OPT_STATELESS; 609 } 610 611 /* If we've got a configured number of keybits - the peer must use that */ 612 if (cfg->mppe.keybits) { 613 ua_htonl(&mval, o->data); 614 return peer == mval ? res : MODE_NAK; 615 } 616 617 /* If a specific number of bits hasn't been requested, we'll need to NAK */ 618 switch (peer & MPPE_OPT_BITMASK) { 619 case MPPE_OPT_128BIT: 620 case MPPE_OPT_56BIT: 621 case MPPE_OPT_40BIT: 622 break; 623 default: 624 res = MODE_NAK; 625 } 626 627 /* Suggest the best number of bits */ 628 mval &= ~MPPE_OPT_BITMASK; 629 if (peer & MPPE_OPT_128BIT) 630 mval |= MPPE_OPT_128BIT; 631 else if (peer & MPPE_OPT_56BIT) 632 mval |= MPPE_OPT_56BIT; 633 else if (peer & MPPE_OPT_40BIT) 634 mval |= MPPE_OPT_40BIT; 635 else 636 mval |= MPPE_OPT_128BIT; 637 ua_htonl(&mval, o->data); 638 639 return res; 640 } 641 642 static struct mppe_state * 643 MPPE_InitState(struct fsm_opt *o) 644 { 645 struct mppe_state *mp; 646 u_int32_t val; 647 648 if ((mp = calloc(1, sizeof *mp)) != NULL) { 649 ua_ntohl(o->data, &val); 650 651 switch (val & MPPE_OPT_BITMASK) { 652 case MPPE_OPT_128BIT: 653 mp->keylen = 16; 654 mp->keybits = 128; 655 break; 656 case MPPE_OPT_56BIT: 657 mp->keylen = 8; 658 mp->keybits = 56; 659 break; 660 case MPPE_OPT_40BIT: 661 mp->keylen = 8; 662 mp->keybits = 40; 663 break; 664 default: 665 log_Printf(LogWARN, "Unexpected MPPE options 0x%08x\n", val); 666 free(mp); 667 return NULL; 668 } 669 670 mp->stateless = !!(val & MPPE_OPT_STATELESS); 671 } 672 673 return mp; 674 } 675 676 static void * 677 MPPEInitInput(struct bundle *bundle, struct fsm_opt *o) 678 { 679 struct mppe_state *mip; 680 681 if (!MPPE_MasterKeyValid) { 682 log_Printf(LogWARN, "MPPE: Cannot initialise without CHAP81\n"); 683 return NULL; 684 } 685 686 if ((mip = MPPE_InitState(o)) == NULL) { 687 log_Printf(LogWARN, "MPPEInput: Cannot initialise - unexpected options\n"); 688 return NULL; 689 } 690 691 log_Printf(LogDEBUG, "MPPE: InitInput: %d-bits\n", mip->keybits); 692 693 #ifndef NORADIUS 694 if (*bundle->radius.cfg.file && bundle->radius.mppe.recvkey) { 695 if (mip->keylen > bundle->radius.mppe.recvkeylen) 696 mip->keylen = bundle->radius.mppe.recvkeylen; 697 if (mip->keylen > sizeof mip->mastkey) 698 mip->keylen = sizeof mip->mastkey; 699 memcpy(mip->mastkey, bundle->radius.mppe.recvkey, mip->keylen); 700 } else 701 #endif 702 GetAsymetricStartKey(MPPE_MasterKey, mip->mastkey, mip->keylen, 0, 703 MPPE_IsServer); 704 705 GetNewKeyFromSHA(mip->mastkey, mip->mastkey, mip->keylen, mip->sesskey); 706 707 MPPEReduceSessionKey(mip); 708 709 log_Printf(LogCCP, "MPPE: Input channel initiated\n"); 710 711 if (!mip->stateless) { 712 /* 713 * We need to initialise our dictionary here as the first packet we 714 * receive is unlikely to have the FLUSHED bit set. 715 */ 716 log_Printf(LogDEBUG, "MPPEInitInput: Dictionary initialised [%d]\n", 717 mip->cohnum); 718 RC4_set_key(&mip->rc4key, mip->keylen, mip->sesskey); 719 } else { 720 /* 721 * We do the first key change here as the first packet is expected 722 * to have a sequence number of 0 and we'll therefore not expect 723 * to have to change the key at that point. 724 */ 725 log_Printf(LogDEBUG, "MPPEInitInput: Key changed [%d]\n", mip->cohnum); 726 MPPEKeyChange(mip); 727 } 728 729 return mip; 730 } 731 732 static void * 733 MPPEInitOutput(struct bundle *bundle, struct fsm_opt *o) 734 { 735 struct mppe_state *mop; 736 737 if (!MPPE_MasterKeyValid) { 738 log_Printf(LogWARN, "MPPE: Cannot initialise without CHAP81\n"); 739 return NULL; 740 } 741 742 if ((mop = MPPE_InitState(o)) == NULL) { 743 log_Printf(LogWARN, "MPPEOutput: Cannot initialise - unexpected options\n"); 744 return NULL; 745 } 746 747 log_Printf(LogDEBUG, "MPPE: InitOutput: %d-bits\n", mop->keybits); 748 749 #ifndef NORADIUS 750 if (*bundle->radius.cfg.file && bundle->radius.mppe.sendkey) { 751 if (mop->keylen > bundle->radius.mppe.sendkeylen) 752 mop->keylen = bundle->radius.mppe.sendkeylen; 753 if (mop->keylen > sizeof mop->mastkey) 754 mop->keylen = sizeof mop->mastkey; 755 memcpy(mop->mastkey, bundle->radius.mppe.sendkey, mop->keylen); 756 } else 757 #endif 758 GetAsymetricStartKey(MPPE_MasterKey, mop->mastkey, mop->keylen, 1, 759 MPPE_IsServer); 760 761 GetNewKeyFromSHA(mop->mastkey, mop->mastkey, mop->keylen, mop->sesskey); 762 763 MPPEReduceSessionKey(mop); 764 765 log_Printf(LogCCP, "MPPE: Output channel initiated\n"); 766 767 if (!mop->stateless) { 768 /* 769 * We need to initialise our dictionary now as the first packet we 770 * send won't have the FLUSHED bit set. 771 */ 772 log_Printf(LogDEBUG, "MPPEInitOutput: Dictionary initialised [%d]\n", 773 mop->cohnum); 774 RC4_set_key(&mop->rc4key, mop->keylen, mop->sesskey); 775 } 776 777 return mop; 778 } 779 780 static void 781 MPPETermInput(void *v) 782 { 783 free(v); 784 } 785 786 static void 787 MPPETermOutput(void *v) 788 { 789 free(v); 790 } 791 792 const struct ccp_algorithm MPPEAlgorithm = { 793 TY_MPPE, 794 CCP_NEG_MPPE, 795 MPPEDispOpts, 796 MPPEUsable, 797 MPPERequired, 798 { 799 MPPESetOptsInput, 800 MPPEInitInput, 801 MPPETermInput, 802 MPPEResetInput, 803 MPPEInput, 804 MPPEDictSetup 805 }, 806 { 807 2, 808 MPPEInitOptsOutput, 809 MPPESetOptsOutput, 810 MPPEInitOutput, 811 MPPETermOutput, 812 MPPEResetOutput, 813 MPPEOutput 814 }, 815 }; 816