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