Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: refactor vncauth.c #562

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions src/common/vncauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@
#include <sys/time.h>
#endif

/*
* make a string 8 Bytes long.
* If longer, shorten it.
* If shorter, zeropad it.
*/

static
void
zeroPadString(unsigned char *out, const char *in)
{
int inLen = strlen(in);
if(inLen > MAXPWLEN) {
inLen = MAXPWLEN;
}

memcpy(out, in, inLen);
memset(out+inLen, 0, MAXPWLEN-inLen);
}


/* libvncclient does not need this */
#ifndef rfbEncryptBytes
Expand All @@ -67,8 +86,7 @@
* as plaintext.
*/

static unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};

static unsigned char fixedkey[MAXPWLEN] = {23,82,107,6,35,78,88,7};

/*
* Encrypt a password and store it in a file. Returns 0 if successful,
Expand All @@ -91,22 +109,13 @@ rfbEncryptAndStorePasswd(char *passwd, char *fname)
#endif

/* pad password with nulls */

for (i = 0; i < 8; i++) {
if (i < strlen(passwd)) {
encryptedPasswd[i] = passwd[i];
} else {
encryptedPasswd[i] = 0;
}
}
zeroPadString(encryptedPasswd, passwd);

/* Do encryption in-place - this way we overwrite our copy of the plaintext
password */
encrypt_rfbdes(encryptedPasswd, &out_len, fixedkey, encryptedPasswd, sizeof(encryptedPasswd));

for (i = 0; i < 8; i++) {
putc(encryptedPasswd[i], fp);
}
fwrite(encryptedPasswd, 1, sizeof(encryptedPasswd), fp);

Check failure

Code scanning / CodeQL

Cleartext storage of sensitive information in file

This write into file 'fp' may contain unencrypted data from [this source.](1).
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not changed that. Its storing the same bytes in the file as before.
The Ci is not wrong, but i am not making it worse.


fclose(fp);
return 0;
Expand All @@ -124,28 +133,26 @@ rfbDecryptPasswdFromFile(char *fname)
{
FILE *fp;
int i, ch;
unsigned char *passwd = (unsigned char *)malloc(9);
unsigned char *passwd = (unsigned char *)malloc(MAXPWLEN+1);
int out_len;

if (!passwd || (fp = fopen(fname,"r")) == NULL) {
free(passwd);
return NULL;
}

for (i = 0; i < 8; i++) {
ch = getc(fp);
if (ch == EOF) {
fclose(fp);
free(passwd);
return NULL;
}
passwd[i] = ch;
}

size_t read = fread(passwd, 1, MAXPWLEN, fp);
fclose(fp);

if(!decrypt_rfbdes(passwd, &out_len, fixedkey, passwd, 8))
return NULL;
if (read < MAXPWLEN) {
free(passwd);
return NULL;
}

if(!decrypt_rfbdes(passwd, &out_len, fixedkey, passwd, MAXPWLEN)) {
free(passwd);
return NULL;
}

passwd[8] = 0;

Expand Down Expand Up @@ -188,14 +195,7 @@ rfbEncryptBytes(unsigned char *bytes, char *passwd)
int out_len;

/* key is simply password padded with nulls */

for (i = 0; i < 8; i++) {
if (i < strlen(passwd)) {
key[i] = passwd[i];
} else {
key[i] = 0;
}
}
zeroPadString(key, passwd);

encrypt_rfbdes(bytes, &out_len, key, bytes, CHALLENGESIZE);
}
Expand Down