home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / pmvnc100.zip / vncauth.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  4KB  |  164 lines

  1. /*
  2.  *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
  3.  *
  4.  *  This is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This software is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this program; if not, write to the Free Software
  16.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  17.  *  USA.
  18.  */
  19.  
  20. /*
  21.  * vncauth.c - Functions for VNC password management and authentication.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <vncauth.h>
  30. #include <d3des.h>
  31.  
  32. #ifdef  __EMX__         /* PM-VNC, fix for EMX/GCC */
  33. #define srandom(seed)   srand(seed)
  34. #define random()        rand()
  35. #endif
  36.  
  37. /*
  38.  * We use a fixed key to store passwords, since we assume that our local
  39.  * file system is secure but nonetheless don't want to store passwords
  40.  * as plaintext.
  41.  */
  42.  
  43. unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
  44.  
  45.  
  46. /*
  47.  * Encrypt a password and store it in a file.  Returns 0 if successful,
  48.  * 1 if the file could not be written.
  49.  */
  50.  
  51. int
  52. vncEncryptAndStorePasswd(char *passwd, char *fname)
  53. {
  54.     FILE *fp;
  55.     int i;
  56.     unsigned char encryptedPasswd[8];
  57.  
  58.     if ((fp = fopen(fname,"wb")) == NULL) return 1;
  59.  
  60.     chmod(fname, S_IRUSR|S_IWUSR);
  61.  
  62.     /* pad password with nulls */
  63.  
  64.     for (i = 0; i < 8; i++) {
  65.     if (i < strlen(passwd)) {
  66.         encryptedPasswd[i] = passwd[i];
  67.     } else {
  68.         encryptedPasswd[i] = 0;
  69.     }
  70.     }
  71.  
  72.     /* Do encryption in-place - this way we overwrite our copy of the plaintext
  73.        password */
  74.  
  75.     deskey(fixedkey, EN0);
  76.     des(encryptedPasswd, encryptedPasswd);
  77.  
  78.     for (i = 0; i < 8; i++) {
  79.     putc(encryptedPasswd[i], fp);
  80.     }
  81.   
  82.     fclose(fp);
  83.     return 0;
  84. }
  85.  
  86.  
  87. /*
  88.  * Decrypt a password from a file.  Returns a pointer to a newly allocated
  89.  * string containing the password or a null pointer if the password could
  90.  * not be retrieved for some reason.
  91.  */
  92.  
  93. char *
  94. vncDecryptPasswdFromFile(char *fname)
  95. {
  96.     FILE *fp;
  97.     int i, ch;
  98.     unsigned char *passwd = (unsigned char *)malloc(9);
  99.  
  100.     if ((fp = fopen(fname,"rb")) == NULL) return NULL;
  101.  
  102.     for (i = 0; i < 8; i++) {
  103.     ch = getc(fp);
  104.     if (ch == EOF) {
  105.         fclose(fp);
  106.         return NULL;
  107.     }
  108.     passwd[i] = ch;
  109.     }
  110.  
  111.     deskey(fixedkey, DE1);
  112.     des(passwd, passwd);
  113.  
  114.     passwd[8] = 0;
  115.  
  116.     return (char *)passwd;
  117. }
  118.  
  119.  
  120. /*
  121.  * Generate CHALLENGESIZE random bytes for use in challenge-response
  122.  * authentication.
  123.  */
  124.  
  125. void
  126. vncRandomBytes(unsigned char *bytes)
  127. {
  128.     int i;
  129.     unsigned int seed = (unsigned int) time(0);
  130.  
  131.     srandom(seed);
  132.     for (i = 0; i < CHALLENGESIZE; i++) {
  133.     bytes[i] = (unsigned char)(random() & 255);    
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139.  * Encrypt CHALLENGESIZE bytes in memory using a password.
  140.  */
  141.  
  142. void
  143. vncEncryptBytes(unsigned char *bytes, char *passwd)
  144. {
  145.     unsigned char key[8];
  146.     int i;
  147.  
  148.     /* key is simply password padded with nulls */
  149.  
  150.     for (i = 0; i < 8; i++) {
  151.     if (i < strlen(passwd)) {
  152.         key[i] = passwd[i];
  153.     } else {
  154.         key[i] = 0;
  155.     }
  156.     }
  157.  
  158.     deskey(key, EN0);
  159.  
  160.     for (i = 0; i < CHALLENGESIZE; i += 8) {
  161.     des(bytes+i, bytes+i);
  162.     }
  163. }
  164.