1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-08 19:02:06 +03:00

pki: Make publickey_from_file() a legacy function.

This commit is contained in:
Andreas Schneider
2011-08-16 00:46:34 +02:00
parent 9dfef44fd8
commit 19a3f5a61d
2 changed files with 22 additions and 87 deletions

View File

@@ -1298,91 +1298,6 @@ int ssh_publickey_to_file(ssh_session session, const char *file,
return SSH_OK;
}
/**
* @brief Retrieve a public key from a file.
*
* @param[in] session The SSH session to use.
*
* @param[in] filename The filename of the public key.
*
* @param[out] type The Pointer to a integer. If it is not NULL, it will
* contain the type of the key after execution.
*
* @return A SSH String containing the public key, or NULL if it
* failed.
*
* @see string_free()
* @see publickey_from_privatekey()
*/
ssh_string publickey_from_file(ssh_session session, const char *filename,
int *type) {
ssh_buffer buffer = NULL;
char buf[4096] = {0};
ssh_string str = NULL;
char *ptr = NULL;
int key_type;
int fd = -1;
int r;
fd = open(filename, O_RDONLY);
if (fd < 0) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Public key file doesn't exist");
return NULL;
}
if (read(fd, buf, 8) != 8) {
close(fd);
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL;
}
buf[7] = '\0';
key_type = ssh_type_from_name(buf);
if (key_type == -1) {
close(fd);
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL;
}
r = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (r <= 0) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL;
}
buf[r] = 0;
ptr = strchr(buf, ' ');
/* eliminate the garbage at end of file */
if (ptr) {
*ptr = '\0';
}
buffer = base64_to_bin(buf);
if (buffer == NULL) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Invalid public key file");
return NULL;
}
str = ssh_string_new(buffer_get_rest_len(buffer));
if (str == NULL) {
ssh_set_error(session, SSH_FATAL, "Not enough space");
ssh_buffer_free(buffer);
return NULL;
}
ssh_string_fill(str, buffer_get_rest(buffer), buffer_get_rest_len(buffer));
ssh_buffer_free(buffer);
if (type) {
*type = key_type;
}
return str;
}
/**
* @brief Try to read the public key from a given file.
*