|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
|
|
Packit |
9c64f8 |
*
|
|
Packit |
9c64f8 |
* This is free software; you can redistribute it and/or modify
|
|
Packit |
9c64f8 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
9c64f8 |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
9c64f8 |
* (at your option) any later version.
|
|
Packit |
9c64f8 |
*
|
|
Packit |
9c64f8 |
* This software is distributed in the hope that it will be useful,
|
|
Packit |
9c64f8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
9c64f8 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
9c64f8 |
* GNU General Public License for more details.
|
|
Packit |
9c64f8 |
*
|
|
Packit |
9c64f8 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
9c64f8 |
* along with this program; if not, write to the Free Software
|
|
Packit |
9c64f8 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
Packit |
9c64f8 |
* USA.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* vncauth.c - Functions for VNC password management and authentication.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#ifdef __STRICT_ANSI__
|
|
Packit |
9c64f8 |
#define _BSD_SOURCE
|
|
Packit |
9c64f8 |
#define _POSIX_SOURCE
|
|
Packit |
9c64f8 |
#define _XOPEN_SOURCE 600
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
#ifdef LIBVNCSERVER_HAVE_SYS_TYPES_H
|
|
Packit |
9c64f8 |
#include <sys/types.h>
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
#include <stdio.h>
|
|
Packit |
9c64f8 |
#include <stdlib.h>
|
|
Packit |
9c64f8 |
#include <unistd.h>
|
|
Packit |
9c64f8 |
#include <rfb/rfbproto.h>
|
|
Packit |
9c64f8 |
#include "d3des.h"
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#include <string.h>
|
|
Packit |
9c64f8 |
#include <math.h>
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#ifdef LIBVNCSERVER_HAVE_SYS_STAT_H
|
|
Packit |
9c64f8 |
#include <sys/stat.h>
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#include <time.h>
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#ifdef WIN32
|
|
Packit |
9c64f8 |
#define srandom srand
|
|
Packit |
9c64f8 |
#define random rand
|
|
Packit |
9c64f8 |
#else
|
|
Packit |
9c64f8 |
#include <sys/time.h>
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/* libvncclient does not need this */
|
|
Packit |
9c64f8 |
#ifndef rfbEncryptBytes
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* We use a fixed key to store passwords, since we assume that our local
|
|
Packit |
9c64f8 |
* file system is secure but nonetheless don't want to store passwords
|
|
Packit |
9c64f8 |
* as plaintext.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
static unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* Encrypt a password and store it in a file. Returns 0 if successful,
|
|
Packit |
9c64f8 |
* 1 if the file could not be written.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
int
|
|
Packit |
9c64f8 |
rfbEncryptAndStorePasswd(char *passwd, char *fname)
|
|
Packit |
9c64f8 |
{
|
|
Packit |
9c64f8 |
FILE *fp;
|
|
Packit |
9c64f8 |
unsigned int i;
|
|
Packit |
9c64f8 |
unsigned char encryptedPasswd[8];
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
if ((fp = fopen(fname,"w")) == NULL) return 1;
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/* windows security sux */
|
|
Packit |
9c64f8 |
#ifndef WIN32
|
|
Packit |
9c64f8 |
fchmod(fileno(fp), S_IRUSR|S_IWUSR);
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/* pad password with nulls */
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < 8; i++) {
|
|
Packit |
9c64f8 |
if (i < strlen(passwd)) {
|
|
Packit |
9c64f8 |
encryptedPasswd[i] = passwd[i];
|
|
Packit |
9c64f8 |
} else {
|
|
Packit |
9c64f8 |
encryptedPasswd[i] = 0;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/* Do encryption in-place - this way we overwrite our copy of the plaintext
|
|
Packit |
9c64f8 |
password */
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
rfbDesKey(fixedkey, EN0);
|
|
Packit |
9c64f8 |
rfbDes(encryptedPasswd, encryptedPasswd);
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < 8; i++) {
|
|
Packit |
9c64f8 |
putc(encryptedPasswd[i], fp);
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
fclose(fp);
|
|
Packit |
9c64f8 |
return 0;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* Decrypt a password from a file. Returns a pointer to a newly allocated
|
|
Packit |
9c64f8 |
* string containing the password or a null pointer if the password could
|
|
Packit |
9c64f8 |
* not be retrieved for some reason.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
char *
|
|
Packit |
9c64f8 |
rfbDecryptPasswdFromFile(char *fname)
|
|
Packit |
9c64f8 |
{
|
|
Packit |
9c64f8 |
FILE *fp;
|
|
Packit |
9c64f8 |
int i, ch;
|
|
Packit |
9c64f8 |
unsigned char *passwd = (unsigned char *)malloc(9);
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
if ((fp = fopen(fname,"r")) == NULL) {
|
|
Packit |
9c64f8 |
free(passwd);
|
|
Packit |
9c64f8 |
return NULL;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < 8; i++) {
|
|
Packit |
9c64f8 |
ch = getc(fp);
|
|
Packit |
9c64f8 |
if (ch == EOF) {
|
|
Packit |
9c64f8 |
fclose(fp);
|
|
Packit |
9c64f8 |
free(passwd);
|
|
Packit |
9c64f8 |
return NULL;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
passwd[i] = ch;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
fclose(fp);
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
rfbDesKey(fixedkey, DE1);
|
|
Packit |
9c64f8 |
rfbDes(passwd, passwd);
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
passwd[8] = 0;
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
return (char *)passwd;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* Generate CHALLENGESIZE random bytes for use in challenge-response
|
|
Packit |
9c64f8 |
* authentication.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
void
|
|
Packit |
9c64f8 |
rfbRandomBytes(unsigned char *bytes)
|
|
Packit |
9c64f8 |
{
|
|
Packit |
9c64f8 |
int i;
|
|
Packit |
9c64f8 |
static rfbBool s_srandom_called = FALSE;
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
if (!s_srandom_called) {
|
|
Packit |
9c64f8 |
srandom((unsigned int)time(NULL) ^ (unsigned int)getpid());
|
|
Packit |
9c64f8 |
s_srandom_called = TRUE;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < CHALLENGESIZE; i++) {
|
|
Packit |
9c64f8 |
bytes[i] = (unsigned char)(random() & 255);
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
#endif
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/*
|
|
Packit |
9c64f8 |
* Encrypt CHALLENGESIZE bytes in memory using a password.
|
|
Packit |
9c64f8 |
*/
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
void
|
|
Packit |
9c64f8 |
rfbEncryptBytes(unsigned char *bytes, char *passwd)
|
|
Packit |
9c64f8 |
{
|
|
Packit |
9c64f8 |
unsigned char key[8];
|
|
Packit |
9c64f8 |
unsigned int i;
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
/* key is simply password padded with nulls */
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < 8; i++) {
|
|
Packit |
9c64f8 |
if (i < strlen(passwd)) {
|
|
Packit |
9c64f8 |
key[i] = passwd[i];
|
|
Packit |
9c64f8 |
} else {
|
|
Packit |
9c64f8 |
key[i] = 0;
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
rfbDesKey(key, EN0);
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
for (i = 0; i < CHALLENGESIZE; i += 8) {
|
|
Packit |
9c64f8 |
rfbDes(bytes+i, bytes+i);
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
|
|
Packit |
9c64f8 |
void
|
|
Packit |
9c64f8 |
rfbEncryptBytes2(unsigned char *where, const int length, unsigned char *key) {
|
|
Packit |
9c64f8 |
int i, j;
|
|
Packit |
9c64f8 |
rfbDesKey(key, EN0);
|
|
Packit |
9c64f8 |
for (i = 0; i< 8; i++)
|
|
Packit |
9c64f8 |
where[i] ^= key[i];
|
|
Packit |
9c64f8 |
rfbDes(where, where);
|
|
Packit |
9c64f8 |
for (i = 8; i < length; i += 8) {
|
|
Packit |
9c64f8 |
for (j = 0; j < 8; j++)
|
|
Packit |
9c64f8 |
where[i + j] ^= where[i + j - 8];
|
|
Packit |
9c64f8 |
rfbDes(where + i, where + i);
|
|
Packit |
9c64f8 |
}
|
|
Packit |
9c64f8 |
}
|