1 /* $NetBSD: encrypt.c,v 1.17 2012/03/21 05:33:27 matt Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 static char sccsid
[] = "@(#)encrypt.c 8.2 (Berkeley) 5/30/95";
36 __RCSID("$NetBSD: encrypt.c,v 1.17 2012/03/21 05:33:27 matt Exp $");
40 * Copyright (C) 1990 by the Massachusetts Institute of Technology
42 * Export of this software from the United States of America is assumed
43 * to require a specific license from the United States Government.
44 * It is the responsibility of any person or organization contemplating
45 * export to obtain such a license before exporting.
47 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
48 * distribute this software and its documentation for any purpose and
49 * without fee is hereby granted, provided that the above copyright
50 * notice appear in all copies and that both that copyright notice and
51 * this permission notice appear in supporting documentation, and that
52 * the name of M.I.T. not be used in advertising or publicity pertaining
53 * to distribution of the software without specific, written prior
54 * permission. M.I.T. makes no representations about the suitability of
55 * this software for any purpose. It is provided "as is" without express
56 * or implied warranty.
63 #include <arpa/telnet.h>
76 * These functions pointers point to the current routines
77 * for encrypting and decrypting data.
79 void (*encrypt_output
)(unsigned char *, int);
80 int (*decrypt_input
)(int);
82 int encrypt_debug_mode
= 0;
83 static int decrypt_mode
= 0;
84 static int encrypt_mode
= 0;
85 static int encrypt_verbose
= 0;
86 static int autoencrypt
= 0;
87 static int autodecrypt
= 0;
88 static int havesessionkey
= 0;
89 static int Server
= 0;
90 static const char *Name
= "Noname";
92 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
94 static long i_support_encrypt
= typemask(ENCTYPE_DES_CFB64
)
95 | typemask(ENCTYPE_DES_OFB64
);
96 static long i_support_decrypt
= typemask(ENCTYPE_DES_CFB64
)
97 | 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 #ifdef DES_ENCRYPTION
108 { "DES_CFB64", ENCTYPE_DES_CFB64
,
118 { "DES_OFB64", ENCTYPE_DES_OFB64
,
128 #endif /* DES_ENCRYPTION */
132 static unsigned char str_send
[64] = { IAC
, SB
, TELOPT_ENCRYPT
,
134 static unsigned char str_suplen
= 0;
135 static unsigned char str_start
[72] = { IAC
, SB
, TELOPT_ENCRYPT
};
136 static unsigned char str_end
[] = { IAC
, SB
, TELOPT_ENCRYPT
, 0, IAC
, SE
};
139 findencryption(int type
)
141 Encryptions
*ep
= encryptions
;
143 if (!(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
& typemask(type
)))
145 while (ep
->type
&& ep
->type
!= type
)
147 return(ep
->type
? ep
: 0);
151 finddecryption(int type
)
153 Encryptions
*ep
= encryptions
;
155 if (!(I_SUPPORT_DECRYPT
& remote_supports_encrypt
& typemask(type
)))
157 while (ep
->type
&& ep
->type
!= type
)
159 return(ep
->type
? ep
: 0);
164 static struct key_info
{
165 unsigned char keyid
[MAXKEYLEN
];
169 Encryptions
*(*getcrypt
)(int);
171 { { 0 }, 0, DIR_ENCRYPT
, &encrypt_mode
, findencryption
},
172 { { 0 }, 0, DIR_DECRYPT
, &decrypt_mode
, finddecryption
},
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;
189 encrypt_verbose
= !server
;
195 if (encrypt_debug_mode
)
196 printf(">>>%s: I will support %s\r\n",
197 Name
, ENCTYPE_NAME(ep
->type
));
198 i_support_encrypt
|= typemask(ep
->type
);
199 i_support_decrypt
|= typemask(ep
->type
);
200 if ((i_wont_support_decrypt
& typemask(ep
->type
)) == 0)
201 if ((str_send
[str_suplen
++] = ep
->type
) == IAC
)
202 str_send
[str_suplen
++] = IAC
;
207 str_send
[str_suplen
++] = IAC
;
208 str_send
[str_suplen
++] = SE
;
212 encrypt_list_types(void)
214 Encryptions
*ep
= encryptions
;
216 printf("Valid encryption types:\n");
218 printf("\t%s (%d)\r\n", ENCTYPE_NAME(ep
->type
), ep
->type
);
224 EncryptEnable(char *type
, char *mode
)
226 if (isprefix(type
, "help") || isprefix(type
, "?")) {
227 printf("Usage: encrypt enable <type> [input|output]\n");
228 encrypt_list_types();
231 if (EncryptType(type
, mode
))
232 return(EncryptStart(mode
));
237 EncryptDisable(char *type
, char *mode
)
239 register Encryptions
*ep
;
242 if (isprefix(type
, "help") || isprefix(type
, "?")) {
243 printf("Usage: encrypt disable <type> [input|output]\n");
244 encrypt_list_types();
245 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
246 sizeof(Encryptions
))) == 0) {
247 printf("%s: invalid encryption type\n", type
);
248 } else if (Ambiguous(ep
)) {
249 printf("Ambiguous type '%s'\n", type
);
251 if ((mode
== 0) || (isprefix(mode
, "input") ? 1 : 0)) {
252 if (decrypt_mode
== ep
->type
)
254 i_wont_support_decrypt
|= typemask(ep
->type
);
257 if ((mode
== 0) || (isprefix(mode
, "output"))) {
258 if (encrypt_mode
== ep
->type
)
260 i_wont_support_encrypt
|= typemask(ep
->type
);
264 printf("%s: invalid encryption mode\n", mode
);
270 EncryptType(char *type
, char *mode
)
272 register Encryptions
*ep
;
275 if (isprefix(type
, "help") || isprefix(type
, "?")) {
276 printf("Usage: encrypt type <type> [input|output]\n");
277 encrypt_list_types();
278 } else if ((ep
= (Encryptions
*)genget(type
, (char **)encryptions
,
279 sizeof(Encryptions
))) == 0) {
280 printf("%s: invalid encryption type\n", type
);
281 } else if (Ambiguous(ep
)) {
282 printf("Ambiguous type '%s'\n", type
);
284 if ((mode
== 0) || isprefix(mode
, "input")) {
285 decrypt_mode
= ep
->type
;
286 i_wont_support_decrypt
&= ~typemask(ep
->type
);
289 if ((mode
== 0) || isprefix(mode
, "output")) {
290 encrypt_mode
= ep
->type
;
291 i_wont_support_encrypt
&= ~typemask(ep
->type
);
295 printf("%s: invalid encryption mode\n", mode
);
301 EncryptStart(char *mode
)
303 register int ret
= 0;
305 if (isprefix(mode
, "input"))
306 return(EncryptStartInput());
307 if (isprefix(mode
, "output"))
308 return(EncryptStartOutput());
309 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
310 printf("Usage: encrypt start [input|output]\n");
313 printf("%s: invalid encryption mode 'encrypt start ?' for help\n", mode
);
316 ret
+= EncryptStartInput();
317 ret
+= EncryptStartOutput();
322 EncryptStartInput(void)
325 encrypt_send_request_start();
328 printf("No previous decryption mode, decryption not enabled\r\n");
333 EncryptStartOutput(void)
336 encrypt_start_output(encrypt_mode
);
339 printf("No previous encryption mode, encryption not enabled\r\n");
344 EncryptStop(char *mode
)
348 if (isprefix(mode
, "input"))
349 return(EncryptStopInput());
350 if (isprefix(mode
, "output"))
351 return(EncryptStopOutput());
352 if (isprefix(mode
, "help") || isprefix(mode
, "?")) {
353 printf("Usage: encrypt stop [input|output]\n");
356 printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode
);
359 ret
+= EncryptStopInput();
360 ret
+= EncryptStopOutput();
365 EncryptStopInput(void)
367 encrypt_send_request_end();
372 EncryptStopOutput(void)
379 encrypt_display(void)
382 printf("Currently encrypting output with %s\r\n",
383 ENCTYPE_NAME(encrypt_mode
));
385 printf("Currently decrypting input with %s\r\n",
386 ENCTYPE_NAME(decrypt_mode
));
393 printf("Currently encrypting output with %s\r\n",
394 ENCTYPE_NAME(encrypt_mode
));
395 else if (encrypt_mode
) {
396 printf("Currently output is clear text.\r\n");
397 printf("Last encryption mode was %s\r\n",
398 ENCTYPE_NAME(encrypt_mode
));
401 printf("Currently decrypting input with %s\r\n",
402 ENCTYPE_NAME(decrypt_mode
));
403 } else if (decrypt_mode
) {
404 printf("Currently input is clear text.\r\n");
405 printf("Last decryption mode was %s\r\n",
406 ENCTYPE_NAME(decrypt_mode
));
412 encrypt_send_support(void)
416 * If the user has requested that decryption start
417 * immediatly, then send a "REQUEST START" before
418 * we negotiate the type.
420 if (!Server
&& autodecrypt
)
421 encrypt_send_request_start();
422 telnet_net_write(str_send
, str_suplen
);
423 printsub('>', &str_send
[2], str_suplen
- 2);
432 encrypt_debug_mode
^= 1;
434 encrypt_debug_mode
= on
;
435 printf("Encryption debugging %s\r\n",
436 encrypt_debug_mode
? "enabled" : "disabled");
441 EncryptVerbose(int on
)
444 encrypt_verbose
^= 1;
446 encrypt_verbose
= on
;
447 printf("Encryption %s verbose\r\n",
448 encrypt_verbose
? "is" : "is not");
453 EncryptAutoEnc(int on
)
456 printf("Automatic encryption of output is %s\r\n",
457 autoencrypt
? "enabled" : "disabled");
462 EncryptAutoDec(int on
)
465 printf("Automatic decryption of input is %s\r\n",
466 autodecrypt
? "enabled" : "disabled");
471 * Called when ENCRYPT SUPPORT is received.
474 encrypt_support(unsigned char *typelist
, int cnt
)
476 register int type
, use_type
= 0;
480 * Forget anything the other side has previously told us.
482 remote_supports_decrypt
= 0;
486 if (encrypt_debug_mode
)
487 printf(">>>%s: He is supporting %s (%d)\r\n",
489 ENCTYPE_NAME(type
), type
);
490 if ((type
< ENCTYPE_CNT
) &&
491 (I_SUPPORT_ENCRYPT
& typemask(type
))) {
492 remote_supports_decrypt
|= typemask(type
);
498 ep
= findencryption(use_type
);
501 type
= ep
->start
? (*ep
->start
)(DIR_ENCRYPT
, Server
) : 0;
502 if (encrypt_debug_mode
)
503 printf(">>>%s: (*ep->start)() returned %d\r\n",
507 encrypt_mode
= use_type
;
509 encrypt_start_output(use_type
);
514 encrypt_is(unsigned char *data
, int cnt
)
517 register int type
, ret
;
522 if (type
< ENCTYPE_CNT
)
523 remote_supports_encrypt
|= typemask(type
);
524 if (!(ep
= finddecryption(type
))) {
525 if (encrypt_debug_mode
)
526 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
528 ENCTYPE_NAME_OK(type
)
529 ? ENCTYPE_NAME(type
) : "(unknown)",
534 if (encrypt_debug_mode
)
535 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
537 ENCTYPE_NAME_OK(type
)
538 ? ENCTYPE_NAME(type
) : "(unknown)",
542 ret
= (*ep
->is
)(data
, cnt
);
543 if (encrypt_debug_mode
)
544 printf("(*ep->is)(%p, %d) returned %s(%d)\n", data
, cnt
,
545 (ret
< 0) ? "FAIL " :
546 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
552 if (ret
== 0 && autodecrypt
)
553 encrypt_send_request_start();
558 encrypt_reply(unsigned char *data
, int cnt
)
561 register int ret
, type
;
566 if (!(ep
= findencryption(type
))) {
567 if (encrypt_debug_mode
)
568 printf(">>>%s: Can't find type %s (%d) for initial negotiation\r\n",
570 ENCTYPE_NAME_OK(type
)
571 ? ENCTYPE_NAME(type
) : "(unknown)",
576 if (encrypt_debug_mode
)
577 printf(">>>%s: No initial negotiation needed for type %s (%d)\r\n",
579 ENCTYPE_NAME_OK(type
)
580 ? ENCTYPE_NAME(type
) : "(unknown)",
584 ret
= (*ep
->reply
)(data
, cnt
);
585 if (encrypt_debug_mode
)
586 printf("(*ep->reply)(%p, %d) returned %s(%d)\n",
588 (ret
< 0) ? "FAIL " :
589 (ret
== 0) ? "SUCCESS " : "MORE_TO_DO ", ret
);
591 if (encrypt_debug_mode
)
592 printf(">>>%s: encrypt_reply returned %d\n", Name
, ret
);
597 if (ret
== 0 && autoencrypt
)
598 encrypt_start_output(type
);
603 * Called when a ENCRYPT START command is received.
606 encrypt_start(unsigned char *data
, int cnt
)
612 * Something is wrong. We should not get a START
613 * command without having already picked our
614 * decryption scheme. Send a REQUEST-END to
615 * attempt to clear the channel...
617 printf("%s: Warning, Cannot decrypt input stream!!!\r\n", Name
);
618 encrypt_send_request_end();
622 if ((ep
= finddecryption(decrypt_mode
)) != NULL
) {
623 decrypt_input
= ep
->input
;
625 printf("[ Input is now decrypted with type %s ]\r\n",
626 ENCTYPE_NAME(decrypt_mode
));
627 if (encrypt_debug_mode
)
628 printf(">>>%s: Start to decrypt input with type %s\r\n",
629 Name
, ENCTYPE_NAME(decrypt_mode
));
631 printf("%s: Warning, Cannot decrypt type %s (%d)!!!\r\n",
633 ENCTYPE_NAME_OK(decrypt_mode
)
634 ? ENCTYPE_NAME(decrypt_mode
)
637 encrypt_send_request_end();
642 encrypt_session_key(Session_Key
*key
, int server
)
644 Encryptions
*ep
= encryptions
;
650 (*ep
->session
)(key
, server
);
652 if (!encrypt_output
&& autoencrypt
&& !server
)
653 encrypt_start_output(ep
->type
);
654 if (!decrypt_input
&& autodecrypt
&& !server
)
655 encrypt_send_request_start();
662 * Called when ENCRYPT END is received.
668 if (encrypt_debug_mode
)
669 printf(">>>%s: Input is back to clear text\r\n", Name
);
671 printf("[ Input is now clear text ]\r\n");
675 * Called when ENCRYPT REQUEST-END is received.
678 encrypt_request_end(void)
684 * Called when ENCRYPT REQUEST-START is received. If we receive
685 * this before a type is picked, then that indicates that the
686 * other side wants us to start encrypting data as soon as we
690 encrypt_request_start(unsigned char *data
, int cnt
)
692 if (encrypt_mode
== 0) {
697 encrypt_start_output(encrypt_mode
);
700 static unsigned char str_keyid
[(MAXKEYLEN
*2)+5] = { IAC
, SB
, TELOPT_ENCRYPT
};
703 encrypt_enc_keyid(unsigned char *keyid
, int len
)
705 encrypt_keyid(&ki
[1], keyid
, len
);
709 encrypt_dec_keyid(unsigned char *keyid
, int len
)
711 encrypt_keyid(&ki
[0], keyid
, len
);
715 encrypt_keyid(struct key_info
*kp
, unsigned char *keyid
, int len
)
719 register int ret
= 0;
721 if (!(ep
= (*kp
->getcrypt
)(*kp
->modep
))) {
725 } else if (len
== 0) {
727 * Empty option, indicates a failure.
733 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
735 } else if ((size_t)len
> sizeof(kp
->keyid
)) {
737 } else if ((len
!= kp
->keylen
) ||
738 (memcmp(keyid
, kp
->keyid
, len
) != 0)) {
740 * Length or contents are different
743 memmove(kp
->keyid
, keyid
, len
);
745 (void)(*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
748 ret
= (*ep
->keyid
)(dir
, kp
->keyid
, &kp
->keylen
);
749 if ((ret
== 0) && (dir
== DIR_ENCRYPT
) && autoencrypt
)
750 encrypt_start_output(*kp
->modep
);
754 encrypt_send_keyid(dir
, kp
->keyid
, kp
->keylen
, 0);
758 encrypt_send_keyid(int dir
, const unsigned char *keyid
, int keylen
, int saveit
)
762 str_keyid
[3] = (dir
== DIR_ENCRYPT
)
763 ? ENCRYPT_ENC_KEYID
: ENCRYPT_DEC_KEYID
;
765 struct key_info
*kp
= &ki
[(dir
== DIR_ENCRYPT
) ? 0 : 1];
766 memmove(kp
->keyid
, keyid
, keylen
);
770 for (strp
= &str_keyid
[4]; keylen
> 0; --keylen
) {
771 if ((*strp
++ = *keyid
++) == IAC
)
776 telnet_net_write(str_keyid
, strp
- str_keyid
);
777 printsub('>', &str_keyid
[2], strp
- str_keyid
- 2);
786 autoencrypt
= on
? 1 : 0;
795 autodecrypt
= on
? 1 : 0;
799 encrypt_start_output(int type
)
802 register unsigned char *p
;
805 if (!(ep
= findencryption(type
))) {
806 if (encrypt_debug_mode
) {
807 printf(">>>%s: Can't encrypt with type %s (%d)\r\n",
809 ENCTYPE_NAME_OK(type
)
810 ? ENCTYPE_NAME(type
) : "(unknown)",
816 i
= (*ep
->start
)(DIR_ENCRYPT
, Server
);
817 if (encrypt_debug_mode
) {
818 printf(">>>%s: Encrypt start: %s (%d) %s\r\n",
821 "initial negotiation in progress",
822 i
, ENCTYPE_NAME(type
));
828 *p
++ = ENCRYPT_START
;
829 for (i
= 0; i
< ki
[0].keylen
; ++i
) {
830 if ((*p
++ = ki
[0].keyid
[i
]) == IAC
)
835 telnet_net_write(str_start
, p
- str_start
);
837 printsub('>', &str_start
[2], p
- &str_start
[2]);
839 * If we are already encrypting in some mode, then
840 * encrypt the ring (which includes our request) in
841 * the old mode, mark it all as "clear text" and then
842 * switch to the new mode.
844 encrypt_output
= ep
->output
;
846 if (encrypt_debug_mode
)
847 printf(">>>%s: Started to encrypt output with type %s\r\n",
848 Name
, ENCTYPE_NAME(type
));
850 printf("[ Output is now encrypted with type %s ]\r\n",
855 encrypt_send_end(void)
860 str_end
[3] = ENCRYPT_END
;
861 telnet_net_write(str_end
, sizeof(str_end
));
863 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
865 * Encrypt the output buffer now because it will not be done by
869 if (encrypt_debug_mode
)
870 printf(">>>%s: Output is back to clear text\r\n", Name
);
872 printf("[ Output is now clear text ]\r\n");
876 encrypt_send_request_start(void)
878 register unsigned char *p
;
882 *p
++ = ENCRYPT_REQSTART
;
883 for (i
= 0; i
< ki
[1].keylen
; ++i
) {
884 if ((*p
++ = ki
[1].keyid
[i
]) == IAC
)
889 telnet_net_write(str_start
, p
- str_start
);
890 printsub('>', &str_start
[2], p
- &str_start
[2]);
891 if (encrypt_debug_mode
)
892 printf(">>>%s: Request input to be encrypted\r\n", Name
);
896 encrypt_send_request_end(void)
898 str_end
[3] = ENCRYPT_REQEND
;
899 telnet_net_write(str_end
, sizeof(str_end
));
900 printsub('>', &str_end
[2], sizeof(str_end
) - 2);
902 if (encrypt_debug_mode
)
903 printf(">>>%s: Request input to be clear text\r\n", Name
);
909 if (encrypt_debug_mode
)
910 printf(">>>%s: in encrypt_wait\r\n", Name
);
911 if (!havesessionkey
|| !(I_SUPPORT_ENCRYPT
& remote_supports_decrypt
))
913 while (autoencrypt
&& !encrypt_output
)
919 encrypt_debug(int mode
)
921 encrypt_debug_mode
= mode
;
925 encrypt_gen_printsub(unsigned char *data
, int cnt
,
926 unsigned char *buf
, int buflen
)
932 buf
[buflen
-1] = '\0';
935 for (; cnt
> 0; cnt
--, data
++) {
936 snprintf(tbuf
, sizeof(tbuf
), " %d", *data
);
937 for (cp
= tbuf
; *cp
&& buflen
> 0; --buflen
)
946 encrypt_printsub(unsigned char *data
, int cnt
,
947 unsigned char *buf
, int buflen
)
950 register int type
= data
[1];
952 for (ep
= encryptions
; ep
->type
&& ep
->type
!= type
; ep
++)
956 (*ep
->printsub
)(data
, cnt
, buf
, buflen
);
958 encrypt_gen_printsub(data
, cnt
, buf
, buflen
);
960 #endif /* ENCRYPTION */