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