From 60b92e458e1cf16f0029d9251e0f117ff27a02d0 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 30 Aug 2011 10:16:53 +0200 Subject: [PATCH] pki: Use consistent API for ssh_pki_export_pubkey_blob(). --- include/libssh/pki.h | 3 ++- src/agent.c | 4 ++-- src/auth.c | 12 ++++++------ src/legacy.c | 10 +++++++--- src/messages.c | 4 ++-- src/pki.c | 26 +++++++++++++++++++++----- src/server.c | 9 +++++---- 7 files changed, 45 insertions(+), 23 deletions(-) diff --git a/include/libssh/pki.h b/include/libssh/pki.h index 0d898217..77106b43 100644 --- a/include/libssh/pki.h +++ b/include/libssh/pki.h @@ -75,7 +75,8 @@ int ssh_pki_signature_verify_blob(ssh_session session, size_t dlen); /* SSH Public Key Functions */ -ssh_string ssh_pki_export_pubkey_blob(const ssh_key key); +int ssh_pki_export_pubkey_blob(const ssh_key key, + ssh_string *pblob); int ssh_pki_import_pubkey_blob(const ssh_string key_blob, ssh_key *pkey); diff --git a/src/agent.c b/src/agent.c index 548b958d..420b23d9 100644 --- a/src/agent.c +++ b/src/agent.c @@ -441,8 +441,8 @@ ssh_string ssh_agent_sign_data(ssh_session session, return NULL; } - key_blob = ssh_pki_export_pubkey_blob(pubkey); - if (key_blob == NULL) { + rc = ssh_pki_export_pubkey_blob(pubkey, &key_blob); + if (rc < 0) { ssh_buffer_free(request); return NULL; } diff --git a/src/auth.c b/src/auth.c index 853385d7..3792854a 100644 --- a/src/auth.c +++ b/src/auth.c @@ -601,8 +601,8 @@ int ssh_userauth_try_publickey(ssh_session session, } /* public key */ - str = ssh_pki_export_pubkey_blob(pubkey); - if (str == NULL) { + rc = ssh_pki_export_pubkey_blob(pubkey, &str); + if (rc < 0) { goto fail; } @@ -763,8 +763,8 @@ int ssh_userauth_publickey(ssh_session session, } /* public key */ - str = ssh_pki_export_pubkey_blob(privkey); - if (str == NULL) { + rc = ssh_pki_export_pubkey_blob(privkey, &str); + if (rc < 0) { goto fail; } @@ -899,8 +899,8 @@ static int ssh_userauth_agent_publickey(ssh_session session, } /* public key */ - str = ssh_pki_export_pubkey_blob(pubkey); - if (str == NULL) { + rc = ssh_pki_export_pubkey_blob(pubkey, &str); + if (rc < 0) { goto fail; } diff --git a/src/legacy.c b/src/legacy.c index f99551af..66528596 100644 --- a/src/legacy.c +++ b/src/legacy.c @@ -424,8 +424,8 @@ ssh_string publickey_from_file(ssh_session session, const char *filename, return NULL; } - key_str = ssh_pki_export_pubkey_blob(key); - if (key_str == NULL) { + rc = ssh_pki_export_pubkey_blob(key, &key_str); + if (rc < 0) { return NULL; } @@ -477,6 +477,7 @@ ssh_public_key publickey_from_string(ssh_session session, ssh_string pubkey_s) { ssh_string publickey_to_string(ssh_public_key pubkey) { ssh_key key; ssh_string key_blob; + int rc; key = ssh_key_new(); if (key == NULL) { @@ -489,7 +490,10 @@ ssh_string publickey_to_string(ssh_public_key pubkey) { key->dsa = pubkey->dsa_pub; key->rsa = pubkey->rsa_pub; - key_blob = ssh_pki_export_pubkey_blob(key); + rc = ssh_pki_export_pubkey_blob(key, &key_blob); + if (rc < 0) { + key_blob = NULL; + } key->dsa = NULL; key->rsa = NULL; diff --git a/src/messages.c b/src/messages.c index ca2df014..a9398bba 100644 --- a/src/messages.c +++ b/src/messages.c @@ -415,8 +415,8 @@ static ssh_buffer ssh_msg_userauth_build_digest(ssh_session session, } /* Add the publickey as blob */ - str = ssh_pki_export_pubkey_blob(msg->auth_request.pubkey); - if (str == NULL) { + rc = ssh_pki_export_pubkey_blob(msg->auth_request.pubkey, &str); + if (rc < 0) { ssh_buffer_free(buffer); return NULL; } diff --git a/src/pki.c b/src/pki.c index 7e86d307..7628bb4e 100644 --- a/src/pki.c +++ b/src/pki.c @@ -800,23 +800,39 @@ ssh_key ssh_pki_publickey_from_privatekey(const ssh_key privkey) { } /** + * @internal + * * @brief Create a key_blob from a public key. * * The "key_blob" is encoded as per RFC 4253 section 6.6 "Public Key * Algorithms" for any of the supported protocol 2 key types. * - * @param[in] key A public or private key to create the public ssh_string + * @param[in] key A public or private key to create the public ssh_string * from. * - * @return The key_blob or NULL on error. + * @param[out] pblob A pointer to store the newly allocated key blob. You + * NEED to free it. + * + * @return SSH_OK on success, SSH_ERROR otherwise. + * + * @see ssh_string_free() */ -ssh_string ssh_pki_export_pubkey_blob(const ssh_key key) +int ssh_pki_export_pubkey_blob(const ssh_key key, + ssh_string *pblob) { + ssh_string blob; + if (key == NULL) { - return NULL; + return SSH_OK; } - return pki_publickey_to_blob(key); + blob = pki_publickey_to_blob(key); + if (blob == NULL) { + return SSH_ERROR; + } + + *pblob = blob; + return SSH_OK; } /** diff --git a/src/server.c b/src/server.c index 9de93adc..7f3618a4 100644 --- a/src/server.c +++ b/src/server.c @@ -159,6 +159,7 @@ static int dh_handshake_server(ssh_session session) { ssh_string pubkey_blob; ssh_string sig_blob; ssh_string f; + int rc; if (dh_generate_y(session) < 0) { ssh_set_error(session, SSH_FATAL, "Could not create y number"); @@ -196,9 +197,9 @@ static int dh_handshake_server(ssh_session session) { return -1; } - pubkey_blob = ssh_pki_export_pubkey_blob(pubkey); + rc = ssh_pki_export_pubkey_blob(pubkey, &pubkey_blob); ssh_key_free(pubkey); - if (pubkey_blob == NULL) { + if (rc < 0) { ssh_set_error_oom(session); ssh_string_free(f); return -1; @@ -960,8 +961,8 @@ int ssh_message_auth_reply_pk_ok_simple(ssh_message msg) { return SSH_ERROR; } - pubkey_blob = ssh_pki_export_pubkey_blob(msg->auth_request.pubkey); - if (pubkey_blob == NULL) { + ret = ssh_pki_export_pubkey_blob(msg->auth_request.pubkey, &pubkey_blob); + if (ret < 0) { ssh_string_free(algo); return SSH_ERROR; }