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