| 1 |
/* |
| 2 |
Copyright (C) 1993-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1991, 1993 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
/* |
| 49 |
* Copyright (C) 1990 by the Massachusetts Institute of Technology |
| 50 |
* |
| 51 |
* Export of this software from the United States of America is assumed |
| 52 |
* to require a specific license from the United States Government. |
| 53 |
* It is the responsibility of any person or organization contemplating |
| 54 |
* export to obtain such a license before exporting. |
| 55 |
* |
| 56 |
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and |
| 57 |
* distribute this software and its documentation for any purpose and |
| 58 |
* without fee is hereby granted, provided that the above copyright |
| 59 |
* notice appear in all copies and that both that copyright notice and |
| 60 |
* this permission notice appear in supporting documentation, and that |
| 61 |
* the name of M.I.T. not be used in advertising or publicity pertaining |
| 62 |
* to distribution of the software without specific, written prior |
| 63 |
* permission. M.I.T. makes no representations about the suitability of |
| 64 |
* this software for any purpose. It is provided "as is" without express |
| 65 |
* or implied warranty. |
| 66 |
*/ |
| 67 |
|
| 68 |
#include <config.h> |
| 69 |
|
| 70 |
#ifdef ENCRYPTION |
| 71 |
|
| 72 |
# undef ENCRYPT_NAMES /* Ignore enctype_names[]. */ |
| 73 |
# include <arpa/telnet.h> |
| 74 |
|
| 75 |
# include "encrypt.h" |
| 76 |
# include "misc.h" |
| 77 |
|
| 78 |
# include <stdlib.h> |
| 79 |
# include <string.h> |
| 80 |
|
| 81 |
# include <stdio.h> |
| 82 |
# include <attribute.h> |
| 83 |
# include "genget.h" |
| 84 |
|
| 85 |
/* String representation of our capabilities. |
| 86 |
* Solaris will gain DES_OFB64! |
| 87 |
*/ |
| 88 |
# ifdef HAVE_CONST_CHAR_ENCTYPE_NAMES |
| 89 |
const char *enctype_names[] = |
| 90 |
# else |
| 91 |
char *enctype_names[] = |
| 92 |
# endif |
| 93 |
{ |
| 94 |
"ANY", "DES_CFB64", "DES_OFB64", 0, |
| 95 |
}; |
| 96 |
|
| 97 |
/* |
| 98 |
* These functions pointers point to the current routines |
| 99 |
* for encrypting and decrypting data. |
| 100 |
*/ |
| 101 |
void (*encrypt_output) (unsigned char *, int); |
| 102 |
int (*decrypt_input) (int); |
| 103 |
|
| 104 |
int encrypt_debug_mode = 0; |
| 105 |
static int decrypt_mode = 0; |
| 106 |
static int encrypt_mode = 0; |
| 107 |
static int encrypt_verbose = 0; |
| 108 |
static int autoencrypt = 0; |
| 109 |
static int autodecrypt = 0; |
| 110 |
static int havesessionkey = 0; |
| 111 |
static int Server = 0; |
| 112 |
static char *Name = "Noname"; |
| 113 |
|
| 114 |
# define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0) |
| 115 |
|
| 116 |
/* Only the type ENCTYPE_DES_CFB64 seems universal. */ |
| 117 |
# ifdef ENCTYPE_DES_OFB64 |
| 118 |
static long i_support_encrypt = |
| 119 |
typemask (ENCTYPE_DES_CFB64) | typemask (ENCTYPE_DES_OFB64); |
| 120 |
static long |
| 121 |
i_support_decrypt = |
| 122 |
typemask (ENCTYPE_DES_CFB64) | typemask (ENCTYPE_DES_OFB64); |
| 123 |
# else/* !ENCTYPE_DES_OFB64 */ |
| 124 |
static long i_support_encrypt = typemask (ENCTYPE_DES_CFB64); |
| 125 |
static long i_support_decrypt = typemask (ENCTYPE_DES_CFB64); |
| 126 |
# endif/* !ENCTYPE_DES_OFB64 */ |
| 127 |
|
| 128 |
static long i_wont_support_encrypt = 0; |
| 129 |
static long i_wont_support_decrypt = 0; |
| 130 |
# define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt) |
| 131 |
# define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt) |
| 132 |
|
| 133 |
static long remote_supports_encrypt = 0; |
| 134 |
static long remote_supports_decrypt = 0; |
| 135 |
|
| 136 |
static Encryptions encryptions[] = { |
| 137 |
# if defined DES_ENCRYPTION || defined SHISHI |
| 138 |
{"DES_CFB64", ENCTYPE_DES_CFB64, |
| 139 |
cfb64_encrypt, |
| 140 |
cfb64_decrypt, |
| 141 |
cfb64_init, |
| 142 |
cfb64_start, |
| 143 |
cfb64_is, |
| 144 |
cfb64_reply, |
| 145 |
cfb64_session, |
| 146 |
cfb64_keyid, |
| 147 |
cfb64_printsub}, |
| 148 |
# ifdef ENCTYPE_DES_OFB64 |
| 149 |
{"DES_OFB64", ENCTYPE_DES_OFB64, |
| 150 |
ofb64_encrypt, |
| 151 |
ofb64_decrypt, |
| 152 |
ofb64_init, |
| 153 |
ofb64_start, |
| 154 |
ofb64_is, |
| 155 |
ofb64_reply, |
| 156 |
ofb64_session, |
| 157 |
ofb64_keyid, |
| 158 |
ofb64_printsub}, |
| 159 |
# endif |
| 160 |
/* ENCTYPE_DES_OFB64 */ |
| 161 |
# endif/* DES_ENCRYPTION */ |
| 162 |
{0,}, |
| 163 |
}; |
| 164 |
|
| 165 |
static unsigned char str_send[64] = |
| 166 |
{ IAC, SB, TELOPT_ENCRYPT, ENCRYPT_SUPPORT }; |
| 167 |
static unsigned char str_suplen = 0; |
| 168 |
static unsigned char str_start[72] = { IAC, SB, TELOPT_ENCRYPT }; |
| 169 |
static unsigned char str_end[] = { IAC, SB, TELOPT_ENCRYPT, 0, IAC, SE }; |
| 170 |
|
| 171 |
Encryptions * |
| 172 |
findencryption (int type) |
| 173 |
{ |
| 174 |
Encryptions *ep = encryptions; |
| 175 |
|
| 176 |
if (!(I_SUPPORT_ENCRYPT & remote_supports_decrypt & typemask (type))) |
| 177 |
return (0); |
| 178 |
while (ep->type && ep->type != type) |
| 179 |
++ep; |
| 180 |
return (ep->type ? ep : 0); |
| 181 |
} |
| 182 |
|
| 183 |
Encryptions * |
| 184 |
finddecryption (int type) |
| 185 |
{ |
| 186 |
Encryptions *ep = encryptions; |
| 187 |
|
| 188 |
if (!(I_SUPPORT_DECRYPT & remote_supports_encrypt & typemask (type))) |
| 189 |
return (0); |
| 190 |
while (ep->type && ep->type != type) |
| 191 |
++ep; |
| 192 |
return (ep->type ? ep : 0); |
| 193 |
} |
| 194 |
|
| 195 |
# define MAXKEYLEN 64 |
| 196 |
|
| 197 |
static struct key_info |
| 198 |
{ |
| 199 |
unsigned char keyid[MAXKEYLEN]; |
| 200 |
int keylen; |
| 201 |
int dir; |
| 202 |
int *modep; |
| 203 |
Encryptions *(*getcrypt) (int); |
| 204 |
} ki[2] = { |
| 205 |
{{0}, 0, DIR_ENCRYPT, &encrypt_mode, findencryption}, |
| 206 |
{{0}, 0, DIR_DECRYPT, &decrypt_mode, finddecryption}, |
| 207 |
}; |
| 208 |
|
| 209 |
void |
| 210 |
encrypt_init (char *name, int server) |
| 211 |
{ |
| 212 |
Encryptions *ep = encryptions; |
| 213 |
|
| 214 |
Name = name; |
| 215 |
Server = server; |
| 216 |
i_support_encrypt = i_support_decrypt = 0; |
| 217 |
remote_supports_encrypt = remote_supports_decrypt = 0; |
| 218 |
encrypt_mode = 0; |
| 219 |
decrypt_mode = 0; |
| 220 |
encrypt_output = 0; |
| 221 |
decrypt_input = 0; |
| 222 |
str_suplen = 4; |
| 223 |
|
| 224 |
while (ep->type) |
| 225 |
{ |
| 226 |
if (encrypt_debug_mode) |
| 227 |
printf (">>>%s: I will support %s\r\n", |
| 228 |
Name, ENCTYPE_NAME (ep->type)); |
| 229 |
i_support_encrypt |= typemask (ep->type); |
| 230 |
i_support_decrypt |= typemask (ep->type); |
| 231 |
if ((i_wont_support_decrypt & typemask (ep->type)) == 0) |
| 232 |
if ((str_send[str_suplen++] = ep->type) == IAC) |
| 233 |
str_send[str_suplen++] = IAC; |
| 234 |
if (ep->init) |
| 235 |
(*ep->init) (Server); |
| 236 |
++ep; |
| 237 |
} |
| 238 |
str_send[str_suplen++] = IAC; |
| 239 |
str_send[str_suplen++] = SE; |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
encrypt_list_types (void) |
| 244 |
{ |
| 245 |
Encryptions *ep = encryptions; |
| 246 |
|
| 247 |
printf ("Valid encryption types:\n"); |
| 248 |
while (ep->type) |
| 249 |
{ |
| 250 |
printf ("\t%s (%d)\r\n", ENCTYPE_NAME (ep->type), ep->type); |
| 251 |
++ep; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
int |
| 256 |
EncryptEnable (char *type, char *mode) |
| 257 |
{ |
| 258 |
if (isprefix (type, "help") || isprefix (type, "?")) |
| 259 |
{ |
| 260 |
printf ("Usage: encrypt enable <type> [input|output]\n"); |
| 261 |
encrypt_list_types (); |
| 262 |
return (0); |
| 263 |
} |
| 264 |
if (EncryptType (type, mode)) |
| 265 |
return (EncryptStart (mode)); |
| 266 |
return (0); |
| 267 |
} |
| 268 |
|
| 269 |
int |
| 270 |
EncryptDisable (char *type, char *mode) |
| 271 |
{ |
| 272 |
Encryptions *ep; |
| 273 |
int ret = 0; |
| 274 |
|
| 275 |
if (isprefix (type, "help") || isprefix (type, "?")) |
| 276 |
{ |
| 277 |
printf ("Usage: encrypt disable <type> [input|output]\n"); |
| 278 |
encrypt_list_types (); |
| 279 |
} |
| 280 |
else if ((ep = (Encryptions *) genget (type, (char **) encryptions, |
| 281 |
sizeof (Encryptions))) == 0) |
| 282 |
printf ("%s: invalid encryption type\n", type); |
| 283 |
else if (Ambiguous ((char *) ep)) |
| 284 |
printf ("Ambiguous type '%s'\n", type); |
| 285 |
else |
| 286 |
{ |
| 287 |
if ((mode == 0) || (isprefix (mode, "input") ? 1 : 0)) |
| 288 |
{ |
| 289 |
if (decrypt_mode == ep->type) |
| 290 |
EncryptStopInput (); |
| 291 |
i_wont_support_decrypt |= typemask (ep->type); |
| 292 |
ret = 1; |
| 293 |
} |
| 294 |
if ((mode == 0) || (isprefix (mode, "output"))) |
| 295 |
{ |
| 296 |
if (encrypt_mode == ep->type) |
| 297 |
EncryptStopOutput (); |
| 298 |
i_wont_support_encrypt |= typemask (ep->type); |
| 299 |
ret = 1; |
| 300 |
} |
| 301 |
if (ret == 0) |
| 302 |
printf ("%s: invalid encryption mode\n", mode); |
| 303 |
} |
| 304 |
return (ret); |
| 305 |
} |
| 306 |
|
| 307 |
int |
| 308 |
EncryptType (char *type, char *mode) |
| 309 |
{ |
| 310 |
Encryptions *ep; |
| 311 |
int ret = 0; |
| 312 |
|
| 313 |
if (isprefix (type, "help") || isprefix (type, "?")) |
| 314 |
{ |
| 315 |
printf ("Usage: encrypt type <type> [input|output]\n"); |
| 316 |
encrypt_list_types (); |
| 317 |
} |
| 318 |
else if ((ep = (Encryptions *) genget (type, (char **) encryptions, |
| 319 |
sizeof (Encryptions))) == 0) |
| 320 |
printf ("%s: invalid encryption type\n", type); |
| 321 |
else if (Ambiguous ((char *) ep)) |
| 322 |
printf ("Ambiguous type '%s'\n", type); |
| 323 |
else |
| 324 |
{ |
| 325 |
if ((mode == 0) || isprefix (mode, "input")) |
| 326 |
{ |
| 327 |
decrypt_mode = ep->type; |
| 328 |
i_wont_support_decrypt &= ~typemask (ep->type); |
| 329 |
ret = 1; |
| 330 |
} |
| 331 |
if ((mode == 0) || isprefix (mode, "output")) |
| 332 |
{ |
| 333 |
encrypt_mode = ep->type; |
| 334 |
i_wont_support_encrypt &= ~typemask (ep->type); |
| 335 |
ret = 1; |
| 336 |
} |
| 337 |
if (ret == 0) |
| 338 |
printf ("%s: invalid encryption mode\n", mode); |
| 339 |
} |
| 340 |
return (ret); |
| 341 |
} |
| 342 |
|
| 343 |
int |
| 344 |
EncryptStart (char *mode) |
| 345 |
{ |
| 346 |
int ret = 0; |
| 347 |
if (mode) |
| 348 |
{ |
| 349 |
if (isprefix (mode, "input")) |
| 350 |
return (EncryptStartInput ()); |
| 351 |
if (isprefix (mode, "output")) |
| 352 |
return (EncryptStartOutput ()); |
| 353 |
if (isprefix (mode, "help") || isprefix (mode, "?")) |
| 354 |
{ |
| 355 |
printf ("Usage: encrypt start [input|output]\n"); |
| 356 |
return (0); |
| 357 |
} |
| 358 |
printf ("%s: invalid encryption mode 'encrypt start ?' for help\n", |
| 359 |
mode); |
| 360 |
return (0); |
| 361 |
} |
| 362 |
ret += EncryptStartInput (); |
| 363 |
ret += EncryptStartOutput (); |
| 364 |
return (ret); |
| 365 |
} |
| 366 |
|
| 367 |
int |
| 368 |
EncryptStartInput (void) |
| 369 |
{ |
| 370 |
if (decrypt_mode) |
| 371 |
{ |
| 372 |
encrypt_send_request_start (); |
| 373 |
return (1); |
| 374 |
} |
| 375 |
printf ("No previous decryption mode, decryption not enabled\r\n"); |
| 376 |
return (0); |
| 377 |
} |
| 378 |
|
| 379 |
int |
| 380 |
EncryptStartOutput (void) |
| 381 |
{ |
| 382 |
if (encrypt_mode) |
| 383 |
{ |
| 384 |
encrypt_start_output (encrypt_mode); |
| 385 |
return (1); |
| 386 |
} |
| 387 |
printf ("No previous encryption mode, encryption not enabled\r\n"); |
| 388 |
return (0); |
| 389 |
} |
| 390 |
|
| 391 |
int |
| 392 |
EncryptStop (char *mode) |
| 393 |
{ |
| 394 |
int ret = 0; |
| 395 |
if (mode) |
| 396 |
{ |
| 397 |
if (isprefix (mode, "input")) |
| 398 |
return (EncryptStopInput ()); |
| 399 |
if (isprefix (mode, "output")) |
| 400 |
return (EncryptStopOutput ()); |
| 401 |
if (isprefix (mode, "help") || isprefix (mode, "?")) |
| 402 |
{ |
| 403 |
printf ("Usage: encrypt stop [input|output]\n"); |
| 404 |
return (0); |
| 405 |
} |
| 406 |
printf ("%s: invalid encryption mode 'encrypt stop ?' for help\n", |
| 407 |
mode); |
| 408 |
return (0); |
| 409 |
} |
| 410 |
ret += EncryptStopInput (); |
| 411 |
ret += EncryptStopOutput (); |
| 412 |
return (ret); |
| 413 |
} |
| 414 |
|
| 415 |
int |
| 416 |
EncryptStopInput (void) |
| 417 |
{ |
| 418 |
encrypt_send_request_end (); |
| 419 |
return (1); |
| 420 |
} |
| 421 |
|
| 422 |
int |
| 423 |
EncryptStopOutput (void) |
| 424 |
{ |
| 425 |
encrypt_send_end (); |
| 426 |
return (1); |
| 427 |
} |
| 428 |
|
| 429 |
void |
| 430 |
encrypt_display (void) |
| 431 |
{ |
| 432 |
if (encrypt_output) |
| 433 |
printf ("Currently encrypting output with %s\r\n", |
| 434 |
ENCTYPE_NAME (encrypt_mode)); |
| 435 |
if (decrypt_input) |
| 436 |
printf ("Currently decrypting input with %s\r\n", |
| 437 |
ENCTYPE_NAME (decrypt_mode)); |
| 438 |
} |
| 439 |
|
| 440 |
int |
| 441 |
EncryptStatus (void) |
| 442 |
{ |
| 443 |
if (encrypt_output) |
| 444 |
printf ("Currently encrypting output with %s\r\n", |
| 445 |
ENCTYPE_NAME (encrypt_mode)); |
| 446 |
else if (encrypt_mode) |
| 447 |
{ |
| 448 |
printf ("Currently output is clear text.\r\n"); |
| 449 |
printf ("Last encryption mode was %s\r\n", ENCTYPE_NAME (encrypt_mode)); |
| 450 |
} |
| 451 |
if (decrypt_input) |
| 452 |
printf ("Currently decrypting input with %s\r\n", |
| 453 |
ENCTYPE_NAME (decrypt_mode)); |
| 454 |
else if (decrypt_mode) |
| 455 |
{ |
| 456 |
printf ("Currently input is clear text.\r\n"); |
| 457 |
printf ("Last decryption mode was %s\r\n", ENCTYPE_NAME (decrypt_mode)); |
| 458 |
} |
| 459 |
return 1; |
| 460 |
} |
| 461 |
|
| 462 |
void |
| 463 |
encrypt_send_support (void) |
| 464 |
{ |
| 465 |
if (str_suplen) |
| 466 |
{ |
| 467 |
/* |
| 468 |
* If the user has requested that decryption start |
| 469 |
* immediately, then send a "REQUEST START" before |
| 470 |
* we negotiate the type. |
| 471 |
*/ |
| 472 |
if (!Server && autodecrypt) |
| 473 |
encrypt_send_request_start (); |
| 474 |
net_write (str_send, str_suplen); |
| 475 |
str_suplen = 0; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
int |
| 480 |
EncryptDebug (int on) |
| 481 |
{ |
| 482 |
if (on < 0) |
| 483 |
encrypt_debug_mode ^= 1; |
| 484 |
else |
| 485 |
encrypt_debug_mode = on; |
| 486 |
printf ("Encryption debugging %s\r\n", |
| 487 |
encrypt_debug_mode ? "enabled" : "disabled"); |
| 488 |
return (1); |
| 489 |
} |
| 490 |
|
| 491 |
int |
| 492 |
EncryptVerbose (int on) |
| 493 |
{ |
| 494 |
if (on < 0) |
| 495 |
encrypt_verbose ^= 1; |
| 496 |
else |
| 497 |
encrypt_verbose = on; |
| 498 |
printf ("Encryption %s verbose\r\n", encrypt_verbose ? "is" : "is not"); |
| 499 |
return (1); |
| 500 |
} |
| 501 |
|
| 502 |
int |
| 503 |
EncryptAutoEnc (int on) |
| 504 |
{ |
| 505 |
encrypt_auto (on); |
| 506 |
printf ("Automatic encryption of output is %s\r\n", |
| 507 |
autoencrypt ? "enabled" : "disabled"); |
| 508 |
return (1); |
| 509 |
} |
| 510 |
|
| 511 |
int |
| 512 |
EncryptAutoDec (int on) |
| 513 |
{ |
| 514 |
decrypt_auto (on); |
| 515 |
printf ("Automatic decryption of input is %s\r\n", |
| 516 |
autodecrypt ? "enabled" : "disabled"); |
| 517 |
return (1); |
| 518 |
} |
| 519 |
|
| 520 |
/* |
| 521 |
* Called when ENCRYPT SUPPORT is received. |
| 522 |
*/ |
| 523 |
void |
| 524 |
encrypt_support (unsigned char *typelist, int cnt) |
| 525 |
{ |
| 526 |
int type, use_type = 0; |
| 527 |
Encryptions *ep; |
| 528 |
|
| 529 |
/* |
| 530 |
* Forget anything the other side has previously told us. |
| 531 |
*/ |
| 532 |
remote_supports_decrypt = 0; |
| 533 |
|
| 534 |
while (cnt-- > 0) |
| 535 |
{ |
| 536 |
type = *typelist++; |
| 537 |
if (encrypt_debug_mode) |
| 538 |
printf (">>>%s: He is supporting %s (%d)\r\n", |
| 539 |
Name, ENCTYPE_NAME (type), type); |
| 540 |
if ((type < ENCTYPE_CNT) && (I_SUPPORT_ENCRYPT & typemask (type))) |
| 541 |
{ |
| 542 |
remote_supports_decrypt |= typemask (type); |
| 543 |
if (use_type == 0) |
| 544 |
use_type = type; |
| 545 |
} |
| 546 |
} |
| 547 |
if (use_type) |
| 548 |
{ |
| 549 |
ep = findencryption (use_type); |
| 550 |
if (!ep) |
| 551 |
return; |
| 552 |
type = ep->start ? (*ep->start) (DIR_ENCRYPT, Server) : 0; |
| 553 |
if (encrypt_debug_mode) |
| 554 |
printf (">>>%s: (*ep->start)() returned %d\r\n", Name, type); |
| 555 |
if (type < 0) |
| 556 |
return; |
| 557 |
encrypt_mode = use_type; |
| 558 |
if (type == 0) |
| 559 |
encrypt_start_output (use_type); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
void |
| 564 |
encrypt_is (unsigned char *data, int cnt) |
| 565 |
{ |
| 566 |
Encryptions *ep; |
| 567 |
int type, ret; |
| 568 |
|
| 569 |
if (--cnt < 0) |
| 570 |
return; |
| 571 |
type = *data++; |
| 572 |
if (type < ENCTYPE_CNT) |
| 573 |
remote_supports_encrypt |= typemask (type); |
| 574 |
|
| 575 |
ep = finddecryption (type); |
| 576 |
if (!ep) |
| 577 |
{ |
| 578 |
if (encrypt_debug_mode) |
| 579 |
printf (">>>%s: Can't find type %s (%d) for initial negotiation\r\n", |
| 580 |
Name, |
| 581 |
ENCTYPE_NAME_OK (type) && ENCTYPE_NAME (type) |
| 582 |
? ENCTYPE_NAME (type) : "(unknown)", type); |
| 583 |
return; |
| 584 |
} |
| 585 |
if (!ep->is) |
| 586 |
{ |
| 587 |
if (encrypt_debug_mode) |
| 588 |
printf (">>>%s: No initial negotiation needed for type %s (%d)\r\n", |
| 589 |
Name, |
| 590 |
ENCTYPE_NAME_OK (type) && ENCTYPE_NAME (type) |
| 591 |
? ENCTYPE_NAME (type) : "(unknown)", type); |
| 592 |
ret = 0; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
ret = (*ep->is) (data, cnt); |
| 597 |
if (encrypt_debug_mode) |
| 598 |
printf ("(*ep->is)(%p, %d) returned %s (%d).\r\n", |
| 599 |
data, cnt, |
| 600 |
(ret < 0) ? "FAIL " |
| 601 |
: ((ret == 0) ? "SUCCESS " : "MORE_TO_DO "), ret); |
| 602 |
} |
| 603 |
if (ret < 0) |
| 604 |
autodecrypt = 0; |
| 605 |
else |
| 606 |
{ |
| 607 |
decrypt_mode = type; |
| 608 |
if (ret == 0 && autodecrypt) |
| 609 |
encrypt_send_request_start (); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
void |
| 614 |
encrypt_reply (unsigned char *data, int cnt) |
| 615 |
{ |
| 616 |
Encryptions *ep; |
| 617 |
int ret, type; |
| 618 |
|
| 619 |
if (--cnt < 0) |
| 620 |
return; |
| 621 |
type = *data++; |
| 622 |
|
| 623 |
ep = findencryption (type); |
| 624 |
if (!ep) |
| 625 |
{ |
| 626 |
if (encrypt_debug_mode) |
| 627 |
printf (">>>%s: Can't find type %s (%d) for initial negotiation\r\n", |
| 628 |
Name, |
| 629 |
ENCTYPE_NAME_OK (type) && ENCTYPE_NAME (type) |
| 630 |
? ENCTYPE_NAME (type) : "(unknown)", type); |
| 631 |
return; |
| 632 |
} |
| 633 |
if (!ep->reply) |
| 634 |
{ |
| 635 |
if (encrypt_debug_mode) |
| 636 |
printf (">>>%s: No initial negotiation needed for type %s (%d)\r\n", |
| 637 |
Name, |
| 638 |
ENCTYPE_NAME_OK (type) && ENCTYPE_NAME (type) |
| 639 |
? ENCTYPE_NAME (type) : "(unknown)", type); |
| 640 |
ret = 0; |
| 641 |
} |
| 642 |
else |
| 643 |
{ |
| 644 |
ret = (*ep->reply) (data, cnt); |
| 645 |
if (encrypt_debug_mode) |
| 646 |
printf ("(*ep->reply)(%p, %d) returned %s (%d).\r\n", |
| 647 |
data, cnt, |
| 648 |
(ret < 0) ? "FAIL " |
| 649 |
: ((ret == 0) ? "SUCCESS " : "MORE_TO_DO "), ret); |
| 650 |
} |
| 651 |
if (encrypt_debug_mode) |
| 652 |
printf (">>>%s: encrypt_reply returned %d\n", Name, ret); |
| 653 |
if (ret < 0) |
| 654 |
autoencrypt = 0; |
| 655 |
else |
| 656 |
{ |
| 657 |
encrypt_mode = type; |
| 658 |
if (ret == 0 && autoencrypt) |
| 659 |
encrypt_start_output (type); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
/* |
| 664 |
* Called when a ENCRYPT START command is received. |
| 665 |
*/ |
| 666 |
void |
| 667 |
encrypt_start (unsigned char *data MAYBE_UNUSED, int cnt MAYBE_UNUSED) |
| 668 |
{ |
| 669 |
Encryptions *ep; |
| 670 |
|
| 671 |
if (!decrypt_mode) |
| 672 |
{ |
| 673 |
/* |
| 674 |
* Something is wrong. We should not get a START |
| 675 |
* command without having already picked our |
| 676 |
* decryption scheme. Send a REQUEST-END to |
| 677 |
* attempt to clear the channel... |
| 678 |
*/ |
| 679 |
printf ("%s: Warning, Cannot decrypt input stream!!!\r\n", Name); |
| 680 |
encrypt_send_request_end (); |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
ep = finddecryption (decrypt_mode); |
| 685 |
if (ep) |
| 686 |
{ |
| 687 |
decrypt_input = ep->input; |
| 688 |
if (encrypt_verbose) |
| 689 |
printf ("[ Input is now decrypted with type %s ]\r\n", |
| 690 |
ENCTYPE_NAME (decrypt_mode)); |
| 691 |
if (encrypt_debug_mode) |
| 692 |
printf (">>>%s: Start to decrypt input with type %s\r\n", |
| 693 |
Name, ENCTYPE_NAME (decrypt_mode)); |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
printf ("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n", |
| 698 |
Name, |
| 699 |
ENCTYPE_NAME_OK (decrypt_mode) && ENCTYPE_NAME (decrypt_mode) |
| 700 |
? ENCTYPE_NAME (decrypt_mode) : "(unknown)", decrypt_mode); |
| 701 |
encrypt_send_request_end (); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
void |
| 706 |
encrypt_session_key (Session_Key *key, int server) |
| 707 |
{ |
| 708 |
Encryptions *ep = encryptions; |
| 709 |
|
| 710 |
havesessionkey = 1; |
| 711 |
|
| 712 |
while (ep->type) |
| 713 |
{ |
| 714 |
if (ep->session) |
| 715 |
(*ep->session) (key, server); |
| 716 |
++ep; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
/* |
| 721 |
* Called when ENCRYPT END is received. |
| 722 |
*/ |
| 723 |
void |
| 724 |
encrypt_end (void) |
| 725 |
{ |
| 726 |
decrypt_input = 0; |
| 727 |
if (encrypt_debug_mode) |
| 728 |
printf (">>>%s: Input is back to clear text\r\n", Name); |
| 729 |
if (encrypt_verbose) |
| 730 |
printf ("[ Input is now clear text ]\r\n"); |
| 731 |
} |
| 732 |
|
| 733 |
/* |
| 734 |
* Called when ENCRYPT REQUEST-END is received. |
| 735 |
*/ |
| 736 |
void |
| 737 |
encrypt_request_end (void) |
| 738 |
{ |
| 739 |
encrypt_send_end (); |
| 740 |
} |
| 741 |
|
| 742 |
/* |
| 743 |
* Called when ENCRYPT REQUEST-START is received. If we receive |
| 744 |
* this before a type is picked, then that indicates that the |
| 745 |
* other side wants us to start encrypting data as soon as we |
| 746 |
* can. |
| 747 |
*/ |
| 748 |
void |
| 749 |
encrypt_request_start (unsigned char *data MAYBE_UNUSED, int cnt MAYBE_UNUSED) |
| 750 |
{ |
| 751 |
if (encrypt_mode == 0) |
| 752 |
{ |
| 753 |
if (Server) |
| 754 |
autoencrypt = 1; |
| 755 |
return; |
| 756 |
} |
| 757 |
encrypt_start_output (encrypt_mode); |
| 758 |
} |
| 759 |
|
| 760 |
static unsigned char str_keyid[(MAXKEYLEN * 2) + 5] = |
| 761 |
{ IAC, SB, TELOPT_ENCRYPT }; |
| 762 |
|
| 763 |
static void |
| 764 |
encrypt_keyid (struct key_info *kp, unsigned char *keyid, int len) |
| 765 |
{ |
| 766 |
Encryptions *ep; |
| 767 |
int dir = kp->dir; |
| 768 |
int ret = 0; |
| 769 |
|
| 770 |
if (len > MAXKEYLEN) |
| 771 |
len = MAXKEYLEN; |
| 772 |
|
| 773 |
ep = (*kp->getcrypt) (*kp->modep); |
| 774 |
if (!ep) |
| 775 |
{ |
| 776 |
if (len == 0) |
| 777 |
return; |
| 778 |
kp->keylen = 0; |
| 779 |
} |
| 780 |
else if (len == 0) |
| 781 |
{ |
| 782 |
/* |
| 783 |
* Empty option, indicates a failure. |
| 784 |
*/ |
| 785 |
if (kp->keylen == 0) |
| 786 |
return; |
| 787 |
kp->keylen = 0; |
| 788 |
if (ep->keyid) |
| 789 |
(*ep->keyid) (dir, kp->keyid, &kp->keylen); |
| 790 |
|
| 791 |
} |
| 792 |
else if ((len != kp->keylen) || (memcmp (keyid, kp->keyid, len) != 0)) |
| 793 |
{ |
| 794 |
/* |
| 795 |
* Length or contents are different |
| 796 |
*/ |
| 797 |
kp->keylen = len; |
| 798 |
memmove (kp->keyid, keyid, len); |
| 799 |
if (ep->keyid) |
| 800 |
(*ep->keyid) (dir, kp->keyid, &kp->keylen); |
| 801 |
} |
| 802 |
else |
| 803 |
{ |
| 804 |
if (ep->keyid) |
| 805 |
ret = (*ep->keyid) (dir, kp->keyid, &kp->keylen); |
| 806 |
if ((ret == 0) && (dir == DIR_ENCRYPT) && autoencrypt) |
| 807 |
encrypt_start_output (*kp->modep); |
| 808 |
return; |
| 809 |
} |
| 810 |
|
| 811 |
encrypt_send_keyid (dir, kp->keyid, kp->keylen, 0); |
| 812 |
} |
| 813 |
|
| 814 |
void |
| 815 |
encrypt_enc_keyid (unsigned char *keyid, int len) |
| 816 |
{ |
| 817 |
encrypt_keyid (&ki[1], keyid, len); |
| 818 |
} |
| 819 |
|
| 820 |
void |
| 821 |
encrypt_dec_keyid (unsigned char *keyid, int len) |
| 822 |
{ |
| 823 |
encrypt_keyid (&ki[0], keyid, len); |
| 824 |
} |
| 825 |
|
| 826 |
void |
| 827 |
encrypt_send_keyid (int dir, unsigned char *keyid, int keylen, int saveit) |
| 828 |
{ |
| 829 |
unsigned char *strp; |
| 830 |
|
| 831 |
str_keyid[3] = (dir == DIR_ENCRYPT) ? ENCRYPT_ENC_KEYID : ENCRYPT_DEC_KEYID; |
| 832 |
if (saveit) |
| 833 |
{ |
| 834 |
struct key_info *kp = &ki[(dir == DIR_ENCRYPT) ? 0 : 1]; |
| 835 |
memmove (kp->keyid, keyid, keylen); |
| 836 |
kp->keylen = keylen; |
| 837 |
} |
| 838 |
|
| 839 |
for (strp = &str_keyid[4]; keylen > 0; --keylen) |
| 840 |
{ |
| 841 |
if ((*strp++ = *keyid++) == IAC) |
| 842 |
*strp++ = IAC; |
| 843 |
} |
| 844 |
*strp++ = IAC; |
| 845 |
*strp++ = SE; |
| 846 |
net_write (str_keyid, strp - str_keyid); |
| 847 |
} |
| 848 |
|
| 849 |
void |
| 850 |
encrypt_auto (int on) |
| 851 |
{ |
| 852 |
if (on < 0) |
| 853 |
autoencrypt ^= 1; |
| 854 |
else |
| 855 |
autoencrypt = on ? 1 : 0; |
| 856 |
} |
| 857 |
|
| 858 |
void |
| 859 |
decrypt_auto (int on) |
| 860 |
{ |
| 861 |
if (on < 0) |
| 862 |
autodecrypt ^= 1; |
| 863 |
else |
| 864 |
autodecrypt = on ? 1 : 0; |
| 865 |
} |
| 866 |
|
| 867 |
void |
| 868 |
encrypt_start_output (int type) |
| 869 |
{ |
| 870 |
Encryptions *ep; |
| 871 |
unsigned char *p; |
| 872 |
int i; |
| 873 |
|
| 874 |
ep = findencryption (type); |
| 875 |
if (!ep) |
| 876 |
{ |
| 877 |
if (encrypt_debug_mode) |
| 878 |
{ |
| 879 |
printf (">>>%s: Can't encrypt with type %s (%d)\r\n", |
| 880 |
Name, |
| 881 |
ENCTYPE_NAME_OK (type) && ENCTYPE_NAME (type) |
| 882 |
? ENCTYPE_NAME (type) : "(unknown)", type); |
| 883 |
} |
| 884 |
return; |
| 885 |
} |
| 886 |
if (ep->start) |
| 887 |
{ |
| 888 |
i = (*ep->start) (DIR_ENCRYPT, Server); |
| 889 |
if (encrypt_debug_mode) |
| 890 |
{ |
| 891 |
printf (">>>%s: Encrypt start: %s (%d) %s\r\n", |
| 892 |
Name, |
| 893 |
(i < 0) ? "failed" : |
| 894 |
"initial negotiation in progress", i, ENCTYPE_NAME (type)); |
| 895 |
} |
| 896 |
if (i) |
| 897 |
return; |
| 898 |
} |
| 899 |
p = str_start + 3; |
| 900 |
*p++ = ENCRYPT_START; |
| 901 |
for (i = 0; i < ki[0].keylen; ++i) |
| 902 |
{ |
| 903 |
if ((*p++ = ki[0].keyid[i]) == IAC) |
| 904 |
*p++ = IAC; |
| 905 |
} |
| 906 |
*p++ = IAC; |
| 907 |
*p++ = SE; |
| 908 |
net_write (str_start, p - str_start); |
| 909 |
net_encrypt (); |
| 910 |
/* |
| 911 |
* If we are already encrypting in some mode, then |
| 912 |
* encrypt the ring (which includes our request) in |
| 913 |
* the old mode, mark it all as "clear text" and then |
| 914 |
* switch to the new mode. |
| 915 |
*/ |
| 916 |
encrypt_output = ep->output; |
| 917 |
encrypt_mode = type; |
| 918 |
if (encrypt_debug_mode) |
| 919 |
printf (">>>%s: Started to encrypt output with type %s\r\n", |
| 920 |
Name, ENCTYPE_NAME (type)); |
| 921 |
if (encrypt_verbose) |
| 922 |
printf ("[ Output is now encrypted with type %s ]\r\n", |
| 923 |
ENCTYPE_NAME (type)); |
| 924 |
} |
| 925 |
|
| 926 |
void |
| 927 |
encrypt_send_end (void) |
| 928 |
{ |
| 929 |
if (!encrypt_output) |
| 930 |
return; |
| 931 |
|
| 932 |
str_end[3] = ENCRYPT_END; |
| 933 |
net_write (str_end, sizeof (str_end)); |
| 934 |
net_encrypt (); |
| 935 |
/* |
| 936 |
* Encrypt the output buffer now because it will not be done by |
| 937 |
* netflush... |
| 938 |
*/ |
| 939 |
encrypt_output = 0; |
| 940 |
if (encrypt_debug_mode) |
| 941 |
printf (">>>%s: Output is back to clear text\r\n", Name); |
| 942 |
if (encrypt_verbose) |
| 943 |
printf ("[ Output is now clear text ]\r\n"); |
| 944 |
} |
| 945 |
|
| 946 |
void |
| 947 |
encrypt_send_request_start (void) |
| 948 |
{ |
| 949 |
unsigned char *p; |
| 950 |
int i; |
| 951 |
|
| 952 |
p = &str_start[3]; |
| 953 |
*p++ = ENCRYPT_REQSTART; |
| 954 |
for (i = 0; i < ki[1].keylen; ++i) |
| 955 |
{ |
| 956 |
if ((*p++ = ki[1].keyid[i]) == IAC) |
| 957 |
*p++ = IAC; |
| 958 |
} |
| 959 |
*p++ = IAC; |
| 960 |
*p++ = SE; |
| 961 |
net_write (str_start, p - str_start); |
| 962 |
if (encrypt_debug_mode) |
| 963 |
printf (">>>%s: Request input to be encrypted\r\n", Name); |
| 964 |
} |
| 965 |
|
| 966 |
void |
| 967 |
encrypt_send_request_end (void) |
| 968 |
{ |
| 969 |
str_end[3] = ENCRYPT_REQEND; |
| 970 |
net_write (str_end, sizeof (str_end)); |
| 971 |
|
| 972 |
if (encrypt_debug_mode) |
| 973 |
printf (">>>%s: Request input to be clear text\r\n", Name); |
| 974 |
} |
| 975 |
|
| 976 |
void |
| 977 |
encrypt_wait (void) |
| 978 |
{ |
| 979 |
if (encrypt_debug_mode) |
| 980 |
printf (">>>%s: in encrypt_wait\r\n", Name); |
| 981 |
if (!havesessionkey || !(I_SUPPORT_ENCRYPT & remote_supports_decrypt)) |
| 982 |
return; |
| 983 |
while (autoencrypt && !encrypt_output) |
| 984 |
if (telnet_spin ()) |
| 985 |
return; |
| 986 |
} |
| 987 |
|
| 988 |
void |
| 989 |
encrypt_debug (int mode) |
| 990 |
{ |
| 991 |
encrypt_debug_mode = mode; |
| 992 |
} |
| 993 |
|
| 994 |
static void |
| 995 |
encrypt_gen_printsub (unsigned char *data, int cnt, char *buf, int buflen) |
| 996 |
{ |
| 997 |
char tbuf[16], *cp; |
| 998 |
|
| 999 |
cnt -= 2; |
| 1000 |
data += 2; |
| 1001 |
buf[buflen - 1] = '\0'; |
| 1002 |
buf[buflen - 2] = '*'; |
| 1003 |
buflen -= 2; |
| 1004 |
for (; cnt > 0; cnt--, data++) |
| 1005 |
{ |
| 1006 |
sprintf (tbuf, " %d", *data); |
| 1007 |
for (cp = tbuf; *cp && buflen > 0; --buflen) |
| 1008 |
*buf++ = *cp++; |
| 1009 |
if (buflen <= 0) |
| 1010 |
return; |
| 1011 |
} |
| 1012 |
*buf = '\0'; |
| 1013 |
} |
| 1014 |
|
| 1015 |
void |
| 1016 |
encrypt_printsub (unsigned char *data, int cnt, char *buf, int buflen) |
| 1017 |
{ |
| 1018 |
Encryptions *ep; |
| 1019 |
int type = data[1]; |
| 1020 |
|
| 1021 |
for (ep = encryptions; ep->type && ep->type != type; ep++) |
| 1022 |
; |
| 1023 |
|
| 1024 |
if (ep->printsub) |
| 1025 |
(*ep->printsub) (data, cnt, buf, buflen); |
| 1026 |
else |
| 1027 |
encrypt_gen_printsub (data, cnt, buf, buflen); |
| 1028 |
} |
| 1029 |
#endif /* ENCRYPTION */ |