mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-08 19:02:06 +03:00
pki: Remove session from ssh_pki_import_pubkey_* functions.
This commit is contained in:
@@ -452,17 +452,11 @@ LIBSSH_API int ssh_pki_import_privkey_file(const char *filename,
|
|||||||
void *auth_data,
|
void *auth_data,
|
||||||
ssh_key *pkey);
|
ssh_key *pkey);
|
||||||
|
|
||||||
LIBSSH_API int ssh_pki_import_pubkey_base64(ssh_session session,
|
LIBSSH_API int ssh_pki_import_pubkey_base64(const char *b64_key,
|
||||||
const char *b64_key,
|
|
||||||
enum ssh_keytypes_e type,
|
enum ssh_keytypes_e type,
|
||||||
ssh_key *pkey);
|
ssh_key *pkey);
|
||||||
LIBSSH_API int ssh_pki_import_pubkey_blob(ssh_session session,
|
LIBSSH_API int ssh_pki_import_pubkey_file(const char *filename,
|
||||||
const ssh_string key_blob,
|
|
||||||
ssh_key *pkey);
|
ssh_key *pkey);
|
||||||
LIBSSH_API int ssh_pki_import_pubkey_file(ssh_session session,
|
|
||||||
const char *filename,
|
|
||||||
ssh_key *pkey);
|
|
||||||
LIBSSH_API ssh_string ssh_pki_publickey_to_blob(const ssh_key key);
|
|
||||||
LIBSSH_API int ssh_pki_publickey_to_base64(const ssh_key key,
|
LIBSSH_API int ssh_pki_publickey_to_base64(const ssh_key key,
|
||||||
unsigned char **b64_key,
|
unsigned char **b64_key,
|
||||||
enum ssh_keytypes_e *ptype);
|
enum ssh_keytypes_e *ptype);
|
||||||
|
@@ -68,6 +68,12 @@ ssh_key ssh_key_dup(const ssh_key key);
|
|||||||
void ssh_key_clean (ssh_key key);
|
void ssh_key_clean (ssh_key key);
|
||||||
|
|
||||||
ssh_key ssh_pki_publickey_from_privatekey(const ssh_key privkey);
|
ssh_key ssh_pki_publickey_from_privatekey(const ssh_key privkey);
|
||||||
|
|
||||||
|
int ssh_pki_import_pubkey_blob(const ssh_string key_blob,
|
||||||
|
ssh_key *pkey);
|
||||||
|
|
||||||
|
ssh_string ssh_pki_publickey_to_blob(const ssh_key key);
|
||||||
|
|
||||||
ssh_string ssh_pki_do_sign(ssh_session session, ssh_buffer sigbuf,
|
ssh_string ssh_pki_do_sign(ssh_session session, ssh_buffer sigbuf,
|
||||||
ssh_key privatekey);
|
ssh_key privatekey);
|
||||||
|
|
||||||
|
@@ -387,7 +387,7 @@ struct ssh_public_key_struct *agent_get_next_ident(struct ssh_session_struct *se
|
|||||||
ssh_string_free(tmp);
|
ssh_string_free(tmp);
|
||||||
|
|
||||||
/* get key from blob */
|
/* get key from blob */
|
||||||
rc = ssh_pki_import_pubkey_blob(session, blob, &key);
|
rc = ssh_pki_import_pubkey_blob(blob, &key);
|
||||||
ssh_string_free(blob);
|
ssh_string_free(blob);
|
||||||
if (rc == SSH_ERROR) {
|
if (rc == SSH_ERROR) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -348,7 +348,9 @@ ssh_string publickey_from_file(ssh_session session, const char *filename,
|
|||||||
ssh_string key_str;
|
ssh_string key_str;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
rc = ssh_pki_import_pubkey_file(session, filename, &key);
|
(void) session; /* unused */
|
||||||
|
|
||||||
|
rc = ssh_pki_import_pubkey_file(filename, &key);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -377,7 +379,9 @@ ssh_public_key publickey_from_string(ssh_session session, ssh_string pubkey_s) {
|
|||||||
ssh_key key;
|
ssh_key key;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
rc = ssh_pki_import_pubkey_blob(session, pubkey_s, &key);
|
(void) session; /* unused */
|
||||||
|
|
||||||
|
rc = ssh_pki_import_pubkey_blob(pubkey_s, &key);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
60
src/pki.c
60
src/pki.c
@@ -389,8 +389,7 @@ ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key) {
|
|||||||
return privkey;
|
return privkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pki_import_pubkey_buffer(ssh_session session,
|
static int pki_import_pubkey_buffer(ssh_buffer buffer,
|
||||||
ssh_buffer buffer,
|
|
||||||
enum ssh_keytypes_e type,
|
enum ssh_keytypes_e type,
|
||||||
ssh_key *pkey) {
|
ssh_key *pkey) {
|
||||||
ssh_key key;
|
ssh_key key;
|
||||||
@@ -498,9 +497,7 @@ static int pki_import_pubkey_buffer(ssh_session session,
|
|||||||
break;
|
break;
|
||||||
case SSH_KEYTYPE_ECDSA:
|
case SSH_KEYTYPE_ECDSA:
|
||||||
case SSH_KEYTYPE_UNKNOWN:
|
case SSH_KEYTYPE_UNKNOWN:
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_pki_log("Unknown public key protocol %d", type);
|
||||||
"Unknown public key protocol %d",
|
|
||||||
type);
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,8 +512,6 @@ fail:
|
|||||||
/**
|
/**
|
||||||
* @brief Import a base64 formated public key from a memory c-string.
|
* @brief Import a base64 formated public key from a memory c-string.
|
||||||
*
|
*
|
||||||
* @param[in] session The ssh session to use.
|
|
||||||
*
|
|
||||||
* @param[in] b64_key The base64 key to format.
|
* @param[in] b64_key The base64 key to format.
|
||||||
*
|
*
|
||||||
* @param[in] type The type of the key to format.
|
* @param[in] type The type of the key to format.
|
||||||
@@ -528,15 +523,14 @@ fail:
|
|||||||
*
|
*
|
||||||
* @see ssh_key_free()
|
* @see ssh_key_free()
|
||||||
*/
|
*/
|
||||||
int ssh_pki_import_pubkey_base64(ssh_session session,
|
int ssh_pki_import_pubkey_base64(const char *b64_key,
|
||||||
const char *b64_key,
|
|
||||||
enum ssh_keytypes_e type,
|
enum ssh_keytypes_e type,
|
||||||
ssh_key *pkey) {
|
ssh_key *pkey) {
|
||||||
ssh_buffer buffer;
|
ssh_buffer buffer;
|
||||||
ssh_string type_s;
|
ssh_string type_s;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (session == NULL || b64_key == NULL || pkey == NULL) {
|
if (b64_key == NULL || pkey == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -552,7 +546,7 @@ int ssh_pki_import_pubkey_base64(ssh_session session,
|
|||||||
}
|
}
|
||||||
ssh_string_free(type_s);
|
ssh_string_free(type_s);
|
||||||
|
|
||||||
rc = pki_import_pubkey_buffer(session, buffer, type, pkey);
|
rc = pki_import_pubkey_buffer(buffer, type, pkey);
|
||||||
ssh_buffer_free(buffer);
|
ssh_buffer_free(buffer);
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
@@ -563,8 +557,6 @@ int ssh_pki_import_pubkey_base64(ssh_session session,
|
|||||||
*
|
*
|
||||||
* @brief Import a public key from a ssh string.
|
* @brief Import a public key from a ssh string.
|
||||||
*
|
*
|
||||||
* @param[in] session The ssh session to use.
|
|
||||||
*
|
|
||||||
* @param[in] key_blob The key blob to import as specified in RFC 4253 section
|
* @param[in] key_blob The key blob to import as specified in RFC 4253 section
|
||||||
* 6.6 "Public Key Algorithms".
|
* 6.6 "Public Key Algorithms".
|
||||||
*
|
*
|
||||||
@@ -575,8 +567,7 @@ int ssh_pki_import_pubkey_base64(ssh_session session,
|
|||||||
*
|
*
|
||||||
* @see ssh_key_free()
|
* @see ssh_key_free()
|
||||||
*/
|
*/
|
||||||
int ssh_pki_import_pubkey_blob(ssh_session session,
|
int ssh_pki_import_pubkey_blob(const ssh_string key_blob,
|
||||||
const ssh_string key_blob,
|
|
||||||
ssh_key *pkey) {
|
ssh_key *pkey) {
|
||||||
ssh_buffer buffer;
|
ssh_buffer buffer;
|
||||||
ssh_string type_s = NULL;
|
ssh_string type_s = NULL;
|
||||||
@@ -589,32 +580,31 @@ int ssh_pki_import_pubkey_blob(ssh_session session,
|
|||||||
|
|
||||||
buffer = ssh_buffer_new();
|
buffer = ssh_buffer_new();
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_pki_log("Out of memory!");
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = buffer_add_data(buffer, ssh_string_data(key_blob),
|
rc = buffer_add_data(buffer, ssh_string_data(key_blob),
|
||||||
ssh_string_len(key_blob));
|
ssh_string_len(key_blob));
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
ssh_set_error_oom(session);
|
ssh_pki_log("Out of memory!");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
type_s = buffer_get_ssh_string(buffer);
|
type_s = buffer_get_ssh_string(buffer);
|
||||||
if (type_s == NULL) {
|
if (type_s == NULL) {
|
||||||
ssh_set_error(session, SSH_FATAL, "Invalid public key format");
|
ssh_pki_log("Out of memory!");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
type_c = ssh_string_to_char(type_s);
|
type_c = ssh_string_to_char(type_s);
|
||||||
if (type_c == NULL) {
|
if (type_c == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_pki_log("Out of memory!");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
ssh_string_free(type_s);
|
ssh_string_free(type_s);
|
||||||
|
|
||||||
rc = pki_import_pubkey_buffer(session, buffer,
|
rc = pki_import_pubkey_buffer(buffer, ssh_key_type_from_name(type_c), pkey);
|
||||||
ssh_key_type_from_name(type_c), pkey);
|
|
||||||
|
|
||||||
ssh_buffer_free(buffer);
|
ssh_buffer_free(buffer);
|
||||||
free(type_c);
|
free(type_c);
|
||||||
@@ -628,8 +618,7 @@ fail:
|
|||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ssh_pki_import_pubkey_file(ssh_session session, const char *filename,
|
int ssh_pki_import_pubkey_file(const char *filename, ssh_key *pkey)
|
||||||
ssh_key *pkey)
|
|
||||||
{
|
{
|
||||||
enum ssh_keytypes_e type;
|
enum ssh_keytypes_e type;
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
@@ -639,34 +628,28 @@ int ssh_pki_import_pubkey_file(ssh_session session, const char *filename,
|
|||||||
off_t size;
|
off_t size;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (session == NULL || pkey == NULL) {
|
if (pkey == NULL || filename == NULL || *filename == '\0') {
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filename == NULL || *filename == '\0') {
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = stat(filename, &sb);
|
rc = stat(filename, &sb);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
ssh_set_error(session, SSH_REQUEST_DENIED,
|
ssh_pki_log("Error gettint stat of %s: %s",
|
||||||
"Error gettint stat of %s: %s",
|
filename, strerror(errno));
|
||||||
filename, strerror(errno));
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
file = fopen(filename, "r");
|
file = fopen(filename, "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
ssh_set_error(session, SSH_REQUEST_DENIED,
|
ssh_pki_log("Error opening %s: %s",
|
||||||
"Error opening %s: %s",
|
filename, strerror(errno));
|
||||||
filename, strerror(errno));
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
key_buf = malloc(sb.st_size + 1);
|
key_buf = malloc(sb.st_size + 1);
|
||||||
if (key_buf == NULL) {
|
if (key_buf == NULL) {
|
||||||
fclose(file);
|
fclose(file);
|
||||||
ssh_set_error_oom(session);
|
ssh_pki_log("Out of memory!");
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -675,9 +658,8 @@ int ssh_pki_import_pubkey_file(ssh_session session, const char *filename,
|
|||||||
|
|
||||||
if (size != sb.st_size) {
|
if (size != sb.st_size) {
|
||||||
SAFE_FREE(key_buf);
|
SAFE_FREE(key_buf);
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_pki_log("Error reading %s: %s",
|
||||||
"Error reading %s: %s",
|
filename, strerror(errno));
|
||||||
filename, strerror(errno));
|
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -694,7 +676,7 @@ int ssh_pki_import_pubkey_file(ssh_session session, const char *filename,
|
|||||||
while (!isspace((int)*p)) p++;
|
while (!isspace((int)*p)) p++;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
rc = ssh_pki_import_pubkey_base64(session, q, type, pkey);
|
rc = ssh_pki_import_pubkey_base64(q, type, pkey);
|
||||||
SAFE_FREE(key_buf);
|
SAFE_FREE(key_buf);
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
|
@@ -284,7 +284,7 @@ static void torture_pki_publickey_dsa_base64(void **state)
|
|||||||
while (*p != ' ') p++;
|
while (*p != ' ') p++;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
rc = ssh_pki_import_pubkey_base64(session, q, type, &key);
|
rc = ssh_pki_import_pubkey_base64(q, type, &key);
|
||||||
assert_true(rc == 0);
|
assert_true(rc == 0);
|
||||||
|
|
||||||
rc = ssh_pki_publickey_to_base64(key, &b64_key, &type);
|
rc = ssh_pki_publickey_to_base64(key, &b64_key, &type);
|
||||||
@@ -322,7 +322,7 @@ static void torture_pki_publickey_rsa_base64(void **state)
|
|||||||
while (*p != ' ') p++;
|
while (*p != ' ') p++;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
rc = ssh_pki_import_pubkey_base64(session, q, type, &key);
|
rc = ssh_pki_import_pubkey_base64(q, type, &key);
|
||||||
assert_true(rc == 0);
|
assert_true(rc == 0);
|
||||||
|
|
||||||
rc = ssh_pki_publickey_to_base64(key, &b64_key, &type);
|
rc = ssh_pki_publickey_to_base64(key, &b64_key, &type);
|
||||||
|
Reference in New Issue
Block a user