Ticket #22 (closed defect)

Opened 10 years ago

Last modified 10 years ago

Public key can not be readed from file

Reported by: esanchezm Owned by: bagder
Priority: normal Milestone:
Component: Version:
Keywords: Cc: esanchezm, bagder, jehousley
Blocked By: Blocks:

Description

If the public key file does not finish with '\n' or
'\r' the pubic key is not readed and the connection can
not be established using
libssh2_userauth_publickey_fromfile()

This kind of files can be done by copy-pasting a public
key in an empty file using notepad in Windows. I attach
one of this files. You can try to reproduce with any
private key.

Attachments

id_dsa.pub (605 bytes) - added by esanchezm 10 years ago.
File that causes this bug
patch.diff (490 bytes) - added by esanchezm 10 years ago.
Patch to solve this bug

Download all attachments as: .zip

Change History

Changed 10 years ago by esanchezm

File that causes this bug

Changed 10 years ago by esanchezm

Patch to solve this bug

comment:1 Changed 10 years ago by esanchezm

I add a patch to solve this bug. This works on my system,
but may be buggy...

comment:2 Changed 10 years ago by jehousley

I think this would be a better fix. The cause you suggest is from cut-n-paste, I believe that could also put spaces at the end too.

Jim

RCS file: /cvsroot/libssh2/libssh2/src/userauth.c,v
retrieving revision 1.16
diff -u -r1.16 userauth.c
--- src/userauth.c 4 Nov 2006 19:30:32 -0000 1.16
+++ src/userauth.c 12 Nov 2006 17:25:51 -0000
@@ -37,6 +37,8 @@

#include "libssh2_priv.h"


+#include <ctype.h>
+

/* Needed for struct iovec on some platforms */
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>

@@ -252,7 +254,11 @@

}
while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n') pubkey_l

en++;

rewind(fd);

-
+ if (feof(fd)) {
+ /* the last character was EOF */
+ pubkey_len--;
+ }
+

if (pubkey_len <= 1) {

libssh2_error(session, LIBSSH2_ERROR_FILE, "Invalid data in public key file", 0);
fclose(fd);

@@ -272,7 +278,10 @@

return -1;

}
fclose(fd);

while (pubkey_len && (pubkey[pubkey_len-1] == '\r'
pubkey[pubkey_len-1] == '\n')) pubkey_len--;

+ /*
+ * Remove trailing whitespace
+ */
+ while (pubkey_len && isspace(pubkey[pubkey_len-1])) pubkey_len--;

if (!pubkey_len) {

libssh2_error(session, LIBSSH2_ERROR_FILE, "Missing public key data", 0);

comment:3 Changed 10 years ago by esanchezm

I have test your patch and it works fine. BTW, the problem
was from cut-n-paste, yes, but that's the way to generate
OpenSSH keys using PuttyGen? on Windows.

comment:4 Changed 10 years ago by esanchezm

I was a bit quick in the previous comment and I realize it
didn't work really good. When I looked the code you sent,
something looks strange to me. You rewind the file
descriptor and check if it was EOF, which will never be as
long as you have rewind it. This code will works ok.

while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n')
pubkey_len++;
if (feof(fd)) {

/* the last character was EOF */
pubkey_len--;

}
rewind(fd);

comment:5 Changed 10 years ago by bagder

It seems parts of what have been discussed here have been committed. Is this still a problem? If it is, can you please resubmit an updated patch against recent CVS or at least 0.15?

comment:6 Changed 10 years ago by bagder

No response

Note: See TracTickets for help on using tickets.