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