libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
key.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/config.hpp>
5
7#include <sshpp/export.hpp>
8#include <sshpp/result.hpp>
9#include <sshpp/types.hpp>
10
11#include <filesystem>
12#include <functional>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace sshpp {
19
26
27enum class HashType { md5, sha1, sha256 };
28
31 std::string key_path;
32 std::string comment;
33 int attempt = 0;
34};
35using PassphraseCallback = std::function<Result<SecureString>(const PassphraseRequest&)>;
36using PasswordCallback = std::function<Result<SecureString>()>;
37
38class SSHPP_API Fingerprint {
39public:
40 Fingerprint() = default;
41 Fingerprint(HashType type, std::vector<std::byte> bytes) : type_(type), bytes_(std::move(bytes)) {}
42
43 HashType type() const noexcept { return type_; }
44 ByteView bytes() const noexcept { return bytes_; }
45
46 std::string to_string() const;
47 std::string to_hex() const;
48
49 bool operator==(const Fingerprint& other) const noexcept;
50 bool operator!=(const Fingerprint& other) const noexcept { return !(*this == other); }
51
52private:
53 HashType type_ = HashType::sha256;
54 std::vector<std::byte> bytes_;
55};
56
59class SSHPP_API Key {
60public:
61 Key() = default;
62 ~Key();
63 Key(Key&&) noexcept;
64 Key& operator=(Key&&) noexcept;
65 Key(const Key&) = delete;
66 Key& operator=(const Key&) = delete;
67
68 explicit operator bool() const noexcept { return native_ != nullptr; }
69 native_key native_handle() const noexcept { return native_; }
70 static Key from_native(native_key, Ownership);
71
72 static Result<Key> from_private_file(const std::filesystem::path&, PassphraseCallback = {});
73 static Result<Key> from_public_file(const std::filesystem::path&);
74 static Result<Key> from_public_base64(std::string_view b64, KeyType type);
75 static Result<Key> from_authorized_keys_line(std::string_view line);
76
77 Result<std::string> to_public_base64() const;
78 Result<std::string> to_authorized_keys_line(std::string_view comment = {}) const;
79 Result<void> write_public_file(const std::filesystem::path&) const;
80 Result<Key> public_part() const;
81
82 KeyType type() const noexcept;
83 std::string type_name() const;
84 int bits() const noexcept;
85 bool is_private() const noexcept;
86 bool is_public() const noexcept;
87 Result<Fingerprint> fingerprint(HashType = HashType::sha256) const;
88 bool equals(const Key& other, bool compare_private = false) const noexcept;
89
90 static Result<Key> generate(KeyType, int bits = 0);
91
92private:
93 explicit Key(native_key n, bool owning) : native_(n), owning_(owning) {}
94 native_key native_ = nullptr;
95 bool owning_ = true;
96};
97
98using PublicKey = Key;
99
100} // namespace sshpp
101
102#if SSHPP_HEADER_ONLY
103#include <sshpp/detail/key.ipp>
104#endif
Input byte range. Implicitly constructible from std::string_view / std::vector<std::byte>.
Definition types.hpp:54
Definition key.hpp:38
bool operator!=(const Fingerprint &other) const noexcept
Definition key.hpp:50
Fingerprint(HashType type, std::vector< std::byte > bytes)
Definition key.hpp:41
ByteView bytes() const noexcept
Definition key.hpp:44
HashType type() const noexcept
Definition key.hpp:43
Fingerprint()=default
Definition key.hpp:59
Key()=default
native_key native_handle() const noexcept
Definition key.hpp:69
Definition result.hpp:16
Definition auth.hpp:18
::ssh_key_struct * native_key
Definition native_fwd.hpp:27
Ownership
Definition types.hpp:16
Key PublicKey
Definition fwd.hpp:11
HashType
Definition key.hpp:27
std::function< Result< SecureString >(const PassphraseRequest &)> PassphraseCallback
Definition key.hpp:35
std::function< Result< SecureString >()> PasswordCallback
Definition key.hpp:36
KeyType
Definition key.hpp:20
Definition error.hpp:110
Request passed to a passphrase/password prompt callback.
Definition key.hpp:30
int attempt
Definition key.hpp:33
std::string comment
Definition key.hpp:32
std::string key_path
Definition key.hpp:31