2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)encrypt.c 8.2 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/libtelnet/encrypt.c,v 1.3.2.2 2002/04/13 10:59:07 markm Exp $
34 * Copyright (C) 1990 by the Massachusetts Institute of Technology
36 * Export of this software from the United States of America is assumed
37 * to require a specific license from the United States Government.
38 * It is the responsibility of any person or organization contemplating
39 * export to obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of M.I.T. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. M.I.T. makes no representations about the suitability of
49 * this software for any purpose. It is provided "as is" without express
50 * or implied warranty.
56 #include <arpa/telnet.h>
65 * These functions pointers point to the current routines
66 * for encrypting and decrypting data.
68 void (*encrypt_output
)(unsigned char *, int);
69 int (*decrypt_input
)(int);
71 int EncryptType(char *type
, char *mode
);
72 int EncryptStart(char *mode
);
73 int EncryptStop(char *mode
);
74 int EncryptStartInput(void);
75 int EncryptStartOutput(void);
76 int EncryptStopInput(void);
77 int EncryptStopOutput(void);
79 int encrypt_debug_mode
= 0;
80 static int decrypt_mode
= 0;
81 static int encrypt_mode
= 0;
82 static int encrypt_verbose
= 0;
83 static int autoencrypt
= 0;
84 static int autodecrypt
= 0;
85 static int havesessionkey
= 0;
86 static int Server
= 0;
87 static const char *Name
= "Noname";
89 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
91 static long i_support_encrypt
= 0
92 | typemask(ENCTYPE_DES_CFB64
) | typemask(ENCTYPE_DES_OFB64
)
94 static long i_support_decrypt
= 0
95 | typemask(ENCTYPE_DES_CFB64
) | typemask(ENCTYPE_DES_OFB64
)
98 static long i_wont_support_encrypt
= 0;
99 static long i_wont_support_decrypt
= 0;
100 #define I_SUPPORT_ENCRYPT (i_support_encrypt & ~i_wont_support_encrypt)
101 #define I_SUPPORT_DECRYPT (i_support_decrypt & ~i_wont_support_decrypt)
103 static long remote_supports_encrypt
= 0;
104 static long remote_supports_decrypt
= 0;
106 static Encryptions encryptions
[] = {
107 { "DES_CFB64", ENCTYPE_DES_CFB64
,
117 { "DES_OFB64", ENCTYPE_DES_OFB64
,
127 { NULL
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
130 static unsigned char str_send
[64] = { IAC
, SB
, TELOPT_ENCRYPT
,
132 static unsigned char str_suplen
= 0;
133 static unsigned char str_start
[72] = { IAC
, SB
, TELOPT_ENCRYPT
};
134 static unsigned char str_end
[] = { IAC
, SB
, TELOPT_ENCRYPT
, 0, IAC
, SE
};
137 findencryption(int type
)
139 Encryptions
*ep
= encryptions
;
141 if (!(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
& (unsigned)typemask(type
)))
143 while (ep
->type
&& ep
->type
!= type
)
145 return(ep
->type
? ep
: 0);
149 finddecryption(int type
)
151 Encryptions
*ep
= encryptions
;
153 if (!(I_SUPPORT_DECRYPT
& remote_supports_encrypt
& (unsigned)typemask(type
)))
155 while (ep
->type
&& ep
->type
!= type
)
157 return(ep
->type
? ep
: 0);
162 static struct key_info
{
163 unsigned char keyid
[MAXKEYLEN
];
167 Encryptions
*(*getcrypt
)(int);
169 { { 0 }, 0, DIR_ENCRYPT
, &encrypt_mode
, findencryption
},
170 { { 0 }, 0, DIR_DECRYPT
, &decrypt_mode
, finddecryption
},
173 static void encrypt_keyid(struct key_info
*kp
, unsigned char *keyid
, int len
);
176 encrypt_init(const char *name
, int server
)
178 Encryptions
*ep
= encryptions
;
182 i_support_encrypt
= i_support_decrypt
= 0;
183 remote_supports_encrypt
= remote_supports_decrypt
= 0;
186 encrypt_output
= NULL
;
187 decrypt_input
= NULL
;
192 if (encrypt_debug_mode
)
193 printf(">>>%s: I will support %s\r\n",
194 Name
, ENCTYPE_NAME(ep
->type
));
195 i_support_encrypt
|= typemask(ep
->type
);
196 i_support_decrypt
|= typemask(ep
->type
);
197 if ((i_wont_support_decrypt
& typemask(ep
->type
)) == 0)
198 if ((str_send
[str_suplen
++] = ep
->type
) == IAC
)
199 str_send
[str_suplen
++] = IAC
;
204 str_send
[str_suplen
++] = IAC
;
205 str_send
[str_suplen
++] = SE
;
209 encrypt_list_types(void)
211 Encryptions
*ep
= encryptions
;
213 printf("Valid encryption types:\n");
215 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep
->type
), ep
->type
);
221 EncryptEnable(char *type
, char *mode
)
223 if (isprefix(type
, "help") || isprefix(type
, "?")) {
224 printf("Usage: encrypt enable <type> [input|output]\n");
225 encrypt_list_types();
228 if (EncryptType(type
, mode
))
229 return(EncryptStart(mode
));
234 EncryptDisable(char *type
, char *mode
)
239 if (isprefix(type
, "help") || isprefix(type
, "?")) {
240 printf("Usage: encrypt disable <type> [input|output]\n");
241 encrypt_list_types();
242 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
243 sizeof(Encryptions
))) == NULL
) {
244 printf("%s: invalid encryption type\n", type
);
245 } else if (Ambiguous((char **)ep
)) {
246 printf("Ambiguous type '%s'\n", type
);
248 if ((mode
== NULL
) || (isprefix(mode
, "input") ? 1 : 0)) {
249 if (decrypt_mode
== ep
->type
)
251 i_wont_support_decrypt
|= typemask(ep
->type
);
254 if ((mode
== NULL
) || (isprefix(mode
, "output"))) {
255 if (encrypt_mode
== ep
->type
)
257 i_wont_support_encrypt
|= typemask(ep
->type
);
261 printf("%s: invalid encryption mode\n", mode
);
267 EncryptType(char *type
, char *mode
)
272 if (isprefix(type
, "help") || isprefix(type
, "?")) {
273 printf("Usage: encrypt type <type> [input|output]\n");
274 encrypt_list_types();
275 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
276 sizeof(Encryptions
))) == NULL
) {
277 printf("%s: invalid encryption type\n", type
);
278 } else if (Ambiguous((char **)ep
)) {
279 printf("Ambiguous type '%s'\n", type
);
281 if ((mode
== NULL
) || isprefix(mode
, "input")) {
282 decrypt_mode
= ep
->type
;
283 i_wont_support_decrypt
&= ~typemask(ep
->type
);
286 if ((mode
== NULL
) || isprefix(mode
, "output")) {
287 encrypt_mode
= ep
->type
;
288 i_wont_support_encrypt
&= ~typemask(ep
->type
);
292 printf("%s: invalid encryption mode\n", mode
);
298 EncryptStart(char *mode
)
302 if (isprefix(mode
, "input"))
303 return(EncryptStartInput());
304 if (isprefix(mode
, "output"))
305 return(EncryptStartOutput());
306 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
307 printf("Usage: encrypt start [input|output]\n");
310 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode
);
313 ret
+= EncryptStartInput();
314 ret
+= EncryptStartOutput();
319 EncryptStartInput(void)
322 encrypt_send_request_start();
325 printf("No previous decryption mode, decryption not enabled\r\n");
330 EncryptStartOutput(void)
333 encrypt_start_output(encrypt_mode
);
336 printf("No previous encryption mode, encryption not enabled\r\n");
341 EncryptStop(char *mode
)
345 if (isprefix(mode
, "input"))
346 return(EncryptStopInput());
347 if (isprefix(mode
, "output"))
348 return(EncryptStopOutput());
349 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
350 printf("Usage: encrypt stop [input|output]\n");
353 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode
);
356 ret
+= EncryptStopInput();
357 ret
+= EncryptStopOutput();
362 EncryptStopInput(void)
364 encrypt_send_request_end();
369 EncryptStopOutput(void)
376 encrypt_display(void)
379 printf("Currently encrypting output with %s\r\n",
380 ENCTYPE_NAME(encrypt_mode
));
382 printf("Currently decrypting input with %s\r\n",
383 ENCTYPE_NAME(decrypt_mode
));
390 printf("Currently encrypting output with %s\r\n",
391 ENCTYPE_NAME(encrypt_mode
));
392 else if (encrypt_mode
) {
393 printf("Currently output is clear text.\r\n");
394 printf("Last encryption mode was %s\r\n",
395 ENCTYPE_NAME(encrypt_mode
));
398 printf("Currently decrypting input with %s\r\n",
399 ENCTYPE_NAME(decrypt_mode
));
400 } else if (decrypt_mode
) {
401 printf("Currently input is clear text.\r\n");
402 printf("Last decryption mode was %s\r\n",
403 ENCTYPE_NAME(decrypt_mode
));
409 encrypt_send_support(void)
413 * If the user has requested that decryption start
414 * immediatly, then send a "REQUEST START" before
415 * we negotiate the type.
417 if (!Server
&& autodecrypt
)
418 encrypt_send_request_start();
419 net_write(str_send
, str_suplen
);
420 printsub('>', &str_send
[2], str_suplen
- 2);
429 encrypt_debug_mode
^= 1;
431 encrypt_debug_mode
= on
;
432 printf("Encryption debugging %s\r\n",
433 encrypt_debug_mode
? "enabled" : "disabled");
438 EncryptVerbose(int on
)
441 encrypt_verbose
^= 1;
443 encrypt_verbose
= on
;
444 printf("Encryption %s verbose\r\n",
445 encrypt_verbose
? "is" : "is not");
450 EncryptAutoEnc(int on
)
453 printf("Automatic encryption of output is %s\r\n",
454 autoencrypt
? "enabled" : "disabled");
459 EncryptAutoDec(int on
)
462 printf("Automatic decryption of input is %s\r\n",
463 autodecrypt
? "enabled" : "disabled");
468 * Called when ENCRYPT SUPPORT is received.
471 encrypt_support(unsigned char *typelist
, int cnt
)
473 int type
, use_type
= 0;
477 * Forget anything the other side has previously told us.
479 remote_supports_decrypt
= 0;
483 if (encrypt_debug_mode
)
484 printf(">>>%s: He is supporting %s (%d)\r\n",
486 ENCTYPE_NAME(type
), type
);
487 if ((type
< ENCTYPE_CNT
) &&
488 (I_SUPPORT_ENCRYPT
& typemask(type
))) {
489 remote_supports_decrypt
|= typemask(type
);
495 ep
= findencryption(use_type
);
498 type
= ep
->start
? (*ep
->start
)(DIR_ENCRYPT
, Server
) : 0;
499 if (encrypt_debug_mode
)
500 printf(">>>%s: (*ep->start)() returned %d\r\n",
504 encrypt_mode
= use_type
;
506 encrypt_start_output(use_type
);
511 encrypt_is(unsigned char *data
, int cnt
)
519 if (type
< ENCTYPE_CNT
)
520 remote_supports_encrypt
|= typemask(type
);
521 if (!(ep
= finddecryption(type
))) {
522 if (encrypt_debug_mode
)
523 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
525 ENCTYPE_NAME_OK(type
)
526 ? ENCTYPE_NAME(type
) : "(unknown)",
531 if (encrypt_debug_mode
)
532 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
534 ENCTYPE_NAME_OK(type
)
535 ? ENCTYPE_NAME(type
) : "(unknown)",
539 ret
= (*ep
->is
)(data
, cnt
);
540 if (encrypt_debug_mode
)
541 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data
, cnt
,
542 (ret
< 0) ? "FAIL " :
543 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
549 if (ret
== 0 && autodecrypt
)
550 encrypt_send_request_start();
555 encrypt_reply(unsigned char *data
, int cnt
)
563 if (!(ep
= findencryption(type
))) {
564 if (encrypt_debug_mode
)
565 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
567 ENCTYPE_NAME_OK(type
)
568 ? ENCTYPE_NAME(type
) : "(unknown)",
573 if (encrypt_debug_mode
)
574 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
576 ENCTYPE_NAME_OK(type
)
577 ? ENCTYPE_NAME(type
) : "(unknown)",
581 ret
= (*ep
->reply
)(data
, cnt
);
582 if (encrypt_debug_mode
)
583 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
585 (ret
< 0) ? "FAIL " :
586 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
588 if (encrypt_debug_mode
)
589 printf(">>>%s: encrypt_reply returned %d\n", Name
, ret
);
594 if (ret
== 0 && autoencrypt
)
595 encrypt_start_output(type
);
600 * Called when a ENCRYPT START command is received.
603 encrypt_start(unsigned char *data __unused
, int cnt __unused
)
609 * Something is wrong. We should not get a START
610 * command without having already picked our
611 * decryption scheme. Send a REQUEST-END to
612 * attempt to clear the channel...
614 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name
);
615 encrypt_send_request_end();
619 if ((ep
= finddecryption(decrypt_mode
))) {
620 decrypt_input
= ep
->input
;
622 printf("[ Input is now decrypted with type %s ]\r\n",
623 ENCTYPE_NAME(decrypt_mode
));
624 if (encrypt_debug_mode
)
625 printf(">>>%s: Start to decrypt input with type %s\r\n",
626 Name
, ENCTYPE_NAME(decrypt_mode
));
628 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
630 ENCTYPE_NAME_OK(decrypt_mode
)
631 ? ENCTYPE_NAME(decrypt_mode
)
634 encrypt_send_request_end();
639 encrypt_session_key( Session_Key
*key
, int server
)
641 Encryptions
*ep
= encryptions
;
647 (*ep
->session
)(key
, server
);
653 * Called when ENCRYPT END is received.
658 decrypt_input
= NULL
;
659 if (encrypt_debug_mode
)
660 printf(">>>%s: Input is back to clear text\r\n", Name
);
662 printf("[ Input is now clear text ]\r\n");
666 * Called when ENCRYPT REQUEST-END is received.
669 encrypt_request_end(void)
675 * Called when ENCRYPT REQUEST-START is received. If we receive
676 * this before a type is picked, then that indicates that the
677 * other side wants us to start encrypting data as soon as we
681 encrypt_request_start(unsigned char *data __unused
, int cnt __unused
)
683 if (encrypt_mode
== 0) {
688 encrypt_start_output(encrypt_mode
);
691 static unsigned char str_keyid
[(MAXKEYLEN
*2)+5] = { IAC
, SB
, TELOPT_ENCRYPT
};
694 encrypt_enc_keyid(unsigned char *keyid
, int len
)
696 encrypt_keyid(&ki
[1], keyid
, len
);
700 encrypt_dec_keyid(unsigned char *keyid
, int len
)
702 encrypt_keyid(&ki
[0], keyid
, len
);
706 encrypt_keyid(struct key_info
*kp
, unsigned char *keyid
, int len
)
715 if (!(ep
= (*kp
->getcrypt
)(*kp
->modep
))) {
719 } else if (len
== 0) {
721 * Empty option, indicates a failure.
727 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
729 } else if ((len
!= kp
->keylen
) ||
730 (memcmp(keyid
, kp
->keyid
, len
) != 0)) {
732 * Length or contents are different
735 memmove(kp
->keyid
, keyid
, len
);
737 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
740 ret
= (*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
741 if ((ret
== 0) && (dir
== DIR_ENCRYPT
) && autoencrypt
)
742 encrypt_start_output(*kp
->modep
);
746 encrypt_send_keyid(dir
, kp
->keyid
, kp
->keylen
, 0);
750 encrypt_send_keyid(int dir
, const char *keyid
, int keylen
, int saveit
)
754 str_keyid
[3] = (dir
== DIR_ENCRYPT
)
755 ? ENCRYPT_ENC_KEYID
: ENCRYPT_DEC_KEYID
;
757 struct key_info
*kp
= &ki
[(dir
== DIR_ENCRYPT
) ? 0 : 1];
758 memmove(kp
->keyid
, keyid
, keylen
);
762 for (strp
= &str_keyid
[4]; keylen
> 0; --keylen
) {
763 if ((*strp
++ = *keyid
++) == IAC
)
768 net_write(str_keyid
, strp
- str_keyid
);
769 printsub('>', &str_keyid
[2], strp
- str_keyid
- 2);
778 autoencrypt
= on
? 1 : 0;
787 autodecrypt
= on
? 1 : 0;
791 encrypt_start_output(int type
)
797 if (!(ep
= findencryption(type
))) {
798 if (encrypt_debug_mode
) {
799 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
801 ENCTYPE_NAME_OK(type
)
802 ? ENCTYPE_NAME(type
) : "(unknown)",
808 i
= (*ep
->start
)(DIR_ENCRYPT
, Server
);
809 if (encrypt_debug_mode
) {
810 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
813 "initial negotiation in progress",
814 i
, ENCTYPE_NAME(type
));
820 *p
++ = ENCRYPT_START
;
821 for (i
= 0; i
< ki
[0].keylen
; ++i
) {
822 if ((*p
++ = ki
[0].keyid
[i
]) == IAC
)
827 net_write(str_start
, p
- str_start
);
829 printsub('>', &str_start
[2], p
- &str_start
[2]);
831 * If we are already encrypting in some mode, then
832 * encrypt the ring (which includes our request) in
833 * the old mode, mark it all as "clear text" and then
834 * switch to the new mode.
836 encrypt_output
= ep
->output
;
838 if (encrypt_debug_mode
)
839 printf(">>>%s: Started to encrypt output with type %s\r\n",
840 Name
, ENCTYPE_NAME(type
));
842 printf("[ Output is now encrypted with type %s ]\r\n",
847 encrypt_send_end(void)
852 str_end
[3] = ENCRYPT_END
;
853 net_write(str_end
, sizeof(str_end
));
855 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
857 * Encrypt the output buffer now because it will not be done by
860 encrypt_output
= NULL
;
861 if (encrypt_debug_mode
)
862 printf(">>>%s: Output is back to clear text\r\n", Name
);
864 printf("[ Output is now clear text ]\r\n");
868 encrypt_send_request_start(void)
874 *p
++ = ENCRYPT_REQSTART
;
875 for (i
= 0; i
< ki
[1].keylen
; ++i
) {
876 if ((*p
++ = ki
[1].keyid
[i
]) == IAC
)
881 net_write(str_start
, p
- str_start
);
882 printsub('>', &str_start
[2], p
- &str_start
[2]);
883 if (encrypt_debug_mode
)
884 printf(">>>%s: Request input to be encrypted\r\n", Name
);
888 encrypt_send_request_end(void)
890 str_end
[3] = ENCRYPT_REQEND
;
891 net_write(str_end
, sizeof(str_end
));
892 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
894 if (encrypt_debug_mode
)
895 printf(">>>%s: Request input to be clear text\r\n", Name
);
901 if (encrypt_debug_mode
)
902 printf(">>>%s: in encrypt_wait\r\n", Name
);
903 if (!havesessionkey
|| !(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
))
905 while (autoencrypt
&& !encrypt_output
)
911 encrypt_gen_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
917 buf
[buflen
-1] = '\0';
920 for (; cnt
> 0; cnt
--, data
++) {
921 sprintf(tbuf
, " %d", *data
);
922 for (cp
= tbuf
; *cp
&& buflen
> 0; --buflen
)
931 encrypt_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
936 for (ep
= encryptions
; ep
->type
&& ep
->type
!= type
; ep
++)
940 (*ep
->printsub
)(data
, cnt
, buf
, buflen
);
942 encrypt_gen_printsub(data
, cnt
, buf
, buflen
);
944 #endif /* ENCRYPTION */