~ubuntu-branches/ubuntu/oneiric/likewise-open/oneiric

« back to all changes in this revision

Viewing changes to krb5/src/windows/wintel/encrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Salley
  • Date: 2010-11-22 12:06:00 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122120600-8lba1fpceot71wlb
Tags: 6.0.0.53010-1
Likewise Open 6.0

Show diffs side-by-side

added added

removed removed

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