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:
24
src/legacy.c
24
src/legacy.c
@@ -194,8 +194,6 @@ ssh_private_key privatekey_from_file(ssh_session session, const char *filename,
|
|||||||
void publickey_free(ssh_public_key key);
|
void publickey_free(ssh_public_key key);
|
||||||
int ssh_publickey_to_file(ssh_session session, const char *file,
|
int ssh_publickey_to_file(ssh_session session, const char *file,
|
||||||
ssh_string pubkey, int type);
|
ssh_string pubkey, int type);
|
||||||
ssh_string publickey_from_file(ssh_session session, const char *filename,
|
|
||||||
int *type);
|
|
||||||
ssh_public_key publickey_from_privatekey(ssh_private_key prv);
|
ssh_public_key publickey_from_privatekey(ssh_private_key prv);
|
||||||
ssh_string publickey_to_string(ssh_public_key key);
|
ssh_string publickey_to_string(ssh_public_key key);
|
||||||
*
|
*
|
||||||
@@ -319,6 +317,28 @@ void privatekey_free(ssh_private_key prv) {
|
|||||||
SAFE_FREE(prv);
|
SAFE_FREE(prv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssh_string publickey_from_file(ssh_session session, const char *filename,
|
||||||
|
int *type) {
|
||||||
|
ssh_key key;
|
||||||
|
ssh_string key_str;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = ssh_pki_import_pubkey_file(session, filename, &key);
|
||||||
|
if (rc < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
key_str = ssh_pki_publickey_to_blob(key);
|
||||||
|
if (key_str == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*type = key->type;
|
||||||
|
ssh_key_free(key);
|
||||||
|
|
||||||
|
return key_str;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* SERVER SUPPORT
|
* SERVER SUPPORT
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@@ -1298,91 +1298,6 @@ int ssh_publickey_to_file(ssh_session session, const char *file,
|
|||||||
return SSH_OK;
|
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.
|
* @brief Try to read the public key from a given file.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user