1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

Improve publickey_from_file().

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@567 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-20 09:14:35 +00:00
parent 9001a34cd3
commit e067061263
2 changed files with 67 additions and 50 deletions

View File

@@ -253,7 +253,8 @@ PRIVATE_KEY *privatekey_from_file(SSH_SESSION *session, const char *filename,
STRING *publickey_to_string(PUBLIC_KEY *key); STRING *publickey_to_string(PUBLIC_KEY *key);
PUBLIC_KEY *publickey_from_privatekey(PRIVATE_KEY *prv); PUBLIC_KEY *publickey_from_privatekey(PRIVATE_KEY *prv);
void privatekey_free(PRIVATE_KEY *prv); void privatekey_free(PRIVATE_KEY *prv);
STRING *publickey_from_file(SSH_SESSION *session, const char *filename,int *_type); STRING *publickey_from_file(SSH_SESSION *session, const char *filename,
int *type);
STRING *publickey_from_next_file(SSH_SESSION *session, const char **pub_keys_path, STRING *publickey_from_next_file(SSH_SESSION *session, const char **pub_keys_path,
const char **keys_path, char **privkeyfile, int *type, int *count); const char **keys_path, char **privkeyfile, int *type, int *count);
int ssh_is_server_known(SSH_SESSION *session); int ssh_is_server_known(SSH_SESSION *session);

View File

@@ -851,56 +851,72 @@ void privatekey_free(PRIVATE_KEY *prv) {
* \see publickey_from_privatekey() * \see publickey_from_privatekey()
*/ */
STRING *publickey_from_file(SSH_SESSION *session, const char *filename, STRING *publickey_from_file(SSH_SESSION *session, const char *filename,
int *_type) { int *type) {
BUFFER *buffer; BUFFER *buffer = NULL;
int type; char buf[4096] = {0};
STRING *str; STRING *str = NULL;
char buf[4096]; /* noone will have bigger keys that that */ char *ptr = NULL;
/* where have i head that again ? */ int key_type;
int fd=open(filename,O_RDONLY); int fd = -1;
int r; int r;
char *ptr;
fd = open(filename, O_RDONLY);
if (fd < 0) { if (fd < 0) {
ssh_set_error(session,SSH_REQUEST_DENIED,"nonexistent public key file"); ssh_set_error(session, SSH_REQUEST_DENIED, "Public key file doesn't exist");
return NULL; return NULL;
} }
if (read(fd, buf, 8) != 8) { if (read(fd, buf, 8) != 8) {
close(fd); close(fd);
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file"); ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL; return NULL;
} }
buf[7]=0;
if(!strcmp(buf,"ssh-dss")) buf[7] = '\0';
type=TYPE_DSS;
else if (!strcmp(buf,"ssh-rsa")) key_type = ssh_type_from_name(buf);
type=TYPE_RSA; if (key_type == -1) {
else {
close(fd); close(fd);
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file"); ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL; return NULL;
} }
r = read(fd, buf, sizeof(buf) - 1); r = read(fd, buf, sizeof(buf) - 1);
close(fd); close(fd);
if (r <= 0) { if (r <= 0) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file"); ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL; return NULL;
} }
buf[r] = 0; buf[r] = 0;
ptr = strchr(buf, ' '); ptr = strchr(buf, ' ');
if(ptr)
*ptr=0; /* eliminates the garbage at end of file */ /* eliminate the garbage at end of file */
if (ptr) {
*ptr = '\0';
}
buffer = base64_to_bin(buf); buffer = base64_to_bin(buf);
if(buffer){ if (buffer == NULL) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL;
}
str = string_new(buffer_get_len(buffer)); str = string_new(buffer_get_len(buffer));
if (str == NULL) {
ssh_set_error(session, SSH_FATAL, "Not enough space");
buffer_free(buffer);
return NULL;
}
string_fill(str, buffer_get(buffer), buffer_get_len(buffer)); string_fill(str, buffer_get(buffer), buffer_get_len(buffer));
buffer_free(buffer); buffer_free(buffer);
if(_type)
*_type=type; if (type) {
return str; *type = key_type;
} else {
ssh_set_error(session,SSH_REQUEST_DENIED,"Invalid public key file");
return NULL; /* invalid file */
} }
return str;
} }