1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/utils/ssh_key_clone.c
Simon Tatham c2f1a563a5 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.
2022-04-24 08:39:04 +01:00

33 lines
869 B
C

/*
* 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;
}