ssh_key_clone.c 869 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Make a copy of an existing ssh_key object, e.g. to survive after
  3. * the original is freed.
  4. */
  5. #include "misc.h"
  6. #include "ssh.h"
  7. ssh_key *ssh_key_clone(ssh_key *key)
  8. {
  9. /*
  10. * To avoid having to add a special method in the vtable API, we
  11. * clone by round-tripping through public and private blobs.
  12. */
  13. strbuf *pub = strbuf_new_nm();
  14. ssh_key_public_blob(key, BinarySink_UPCAST(pub));
  15. ssh_key *copy;
  16. if (ssh_key_has_private(key)) {
  17. strbuf *priv = strbuf_new_nm();
  18. ssh_key_private_blob(key, BinarySink_UPCAST(priv));
  19. copy = ssh_key_new_priv(ssh_key_alg(key), ptrlen_from_strbuf(pub),
  20. ptrlen_from_strbuf(priv));
  21. strbuf_free(priv);
  22. } else {
  23. copy = ssh_key_new_pub(ssh_key_alg(key), ptrlen_from_strbuf(pub));
  24. }
  25. strbuf_free(pub);
  26. return copy;
  27. }