1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Utility function ssh_key_clone().

This makes a second independent copy of an existing ssh_key, for
situations where one piece of code is going to want to keep it after
its current owner frees it.

In order to have it work on an arbitrary ssh_key, whether public-only
or a full public+private key pair, I've had to add an ssh_key query
method to ask whether a private key is known. I'm surprised I haven't
found a need for that before! But I suppose in most situations in an
SSH client you statically know which kind of key you're dealing with.
This commit is contained in:
Simon Tatham
2022-04-20 13:51:28 +01:00
parent 180d1b78de
commit c2f1a563a5
6 changed files with 70 additions and 0 deletions

View File

@ -49,6 +49,7 @@ add_sources_from_current_dir(utils
smemclr.c
smemeq.c
spr_get_error_message.c
ssh_key_clone.c
ssh2_pick_fingerprint.c
sshutils.c
strbuf.c

32
utils/ssh_key_clone.c Normal file
View File

@ -0,0 +1,32 @@
/*
* Make a copy of an existing ssh_key object, e.g. to survive after
* the original is freed.
*/
#include "misc.h"
#include "ssh.h"
ssh_key *ssh_key_clone(ssh_key *key)
{
/*
* To avoid having to add a special method in the vtable API, we
* clone by round-tripping through public and private blobs.
*/
strbuf *pub = strbuf_new_nm();
ssh_key_public_blob(key, BinarySink_UPCAST(pub));
ssh_key *copy;
if (ssh_key_has_private(key)) {
strbuf *priv = strbuf_new_nm();
ssh_key_private_blob(key, BinarySink_UPCAST(priv));
copy = ssh_key_new_priv(ssh_key_alg(key), ptrlen_from_strbuf(pub),
ptrlen_from_strbuf(priv));
strbuf_free(priv);
} else {
copy = ssh_key_new_pub(ssh_key_alg(key), ptrlen_from_strbuf(pub));
}
strbuf_free(pub);
return copy;
}