/src/rnp/src/lib/key-provider.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017, [Ribose Inc](https://www.ribose.com). |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without modification, |
6 | | * are permitted provided that the following conditions are met: |
7 | | * |
8 | | * 1. Redistributions of source code must retain the above copyright notice, |
9 | | * this list of conditions and the following disclaimer. |
10 | | * |
11 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
12 | | * this list of conditions and the following disclaimer in the documentation |
13 | | * and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
17 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
18 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
21 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
22 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
23 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
24 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | #ifndef RNP_KEY_PROVIDER_H |
27 | | #define RNP_KEY_PROVIDER_H |
28 | | |
29 | | #include "types.h" |
30 | | #include "fingerprint.hpp" |
31 | | |
32 | | namespace rnp { |
33 | | class Key; |
34 | | } |
35 | | |
36 | | typedef struct pgp_key_request_ctx_t pgp_key_request_ctx_t; |
37 | | |
38 | | typedef rnp::Key *pgp_key_callback_t(const pgp_key_request_ctx_t *ctx, void *userdata); |
39 | | |
40 | | namespace rnp { |
41 | | |
42 | | class KeySearch { |
43 | | public: |
44 | | enum class Type { Unknown, KeyID, Fingerprint, Grip, UserID }; |
45 | | static Type find_type(const std::string &name); |
46 | | |
47 | | virtual Type |
48 | | type() const |
49 | 633k | { |
50 | 633k | return type_; |
51 | 633k | } |
52 | | virtual bool matches(const Key &key) const = 0; |
53 | | virtual const std::string name() const = 0; |
54 | | virtual std::string value() const = 0; |
55 | 367k | virtual ~KeySearch() = default; |
56 | | |
57 | | static std::unique_ptr<KeySearch> create(const pgp::KeyID &keyid); |
58 | | static std::unique_ptr<KeySearch> create(const pgp::Fingerprint &fp); |
59 | | static std::unique_ptr<KeySearch> create(const pgp::KeyGrip &grip); |
60 | | static std::unique_ptr<KeySearch> create(const std::string &uid); |
61 | | static std::unique_ptr<KeySearch> create(const std::string &name, |
62 | | const std::string &value); |
63 | | |
64 | | protected: |
65 | | Type type_; |
66 | | }; |
67 | | |
68 | | class KeyIDSearch : public KeySearch { |
69 | | pgp::KeyID keyid_; |
70 | | |
71 | | public: |
72 | | bool matches(const Key &key) const; |
73 | | const std::string name() const; |
74 | | std::string value() const; |
75 | | bool hidden() const; |
76 | | |
77 | | KeyIDSearch(const pgp::KeyID &keyid); |
78 | | }; |
79 | | |
80 | | class KeyFingerprintSearch : public KeySearch { |
81 | | pgp::Fingerprint fp_; |
82 | | |
83 | | public: |
84 | | bool matches(const Key &key) const; |
85 | | const std::string name() const; |
86 | | std::string value() const; |
87 | | |
88 | | KeyFingerprintSearch(const pgp::Fingerprint &fp); |
89 | | const pgp::Fingerprint &get_fp() const; |
90 | | }; |
91 | | |
92 | | class KeyGripSearch : public KeySearch { |
93 | | pgp::KeyGrip grip_; |
94 | | |
95 | | public: |
96 | | bool matches(const Key &key) const; |
97 | | const std::string name() const; |
98 | | std::string value() const; |
99 | | |
100 | | KeyGripSearch(const pgp::KeyGrip &grip); |
101 | | }; |
102 | | |
103 | | class KeyUIDSearch : public KeySearch { |
104 | | std::string uid_; |
105 | | |
106 | | public: |
107 | | bool matches(const Key &key) const; |
108 | | const std::string name() const; |
109 | | std::string value() const; |
110 | | |
111 | | KeyUIDSearch(const std::string &uid); |
112 | | }; |
113 | | |
114 | | class KeyProvider { |
115 | | public: |
116 | | pgp_key_callback_t *callback; |
117 | | void * userdata; |
118 | | |
119 | | KeyProvider(pgp_key_callback_t *cb = nullptr, void *ud = nullptr) |
120 | 145k | : callback(cb), userdata(ud){}; |
121 | | |
122 | | /** @brief request public or secret pgp key, according to parameters |
123 | | * @param search search object |
124 | | * @param op for which operation key is requested |
125 | | * @param secret whether secret key is requested |
126 | | * @return a key pointer on success, or nullptr if key was not found otherwise |
127 | | **/ |
128 | | Key *request_key(const KeySearch &search, |
129 | | pgp_op_t op = PGP_OP_UNKNOWN, |
130 | | bool secret = false) const; |
131 | | }; |
132 | | } // namespace rnp |
133 | | |
134 | | typedef struct pgp_key_request_ctx_t { |
135 | | pgp_op_t op; |
136 | | bool secret; |
137 | | const rnp::KeySearch &search; |
138 | | |
139 | | pgp_key_request_ctx_t(pgp_op_t anop, bool sec, const rnp::KeySearch &srch) |
140 | 267k | : op(anop), secret(sec), search(srch) |
141 | 267k | { |
142 | 267k | } |
143 | | } pgp_key_request_ctx_t; |
144 | | |
145 | | /** key provider callback that searches a list of rnp::Key pointers |
146 | | * |
147 | | * @param ctx |
148 | | * @param userdata must be a list of key rnp::Key** |
149 | | */ |
150 | | rnp::Key *rnp_key_provider_key_ptr_list(const pgp_key_request_ctx_t *ctx, void *userdata); |
151 | | |
152 | | /** key provider callback that searches a given store |
153 | | * |
154 | | * @param ctx |
155 | | * @param userdata must be a pointer to rnp::KeyStore |
156 | | */ |
157 | | rnp::Key *rnp_key_provider_store(const pgp_key_request_ctx_t *ctx, void *userdata); |
158 | | |
159 | | /** key provider that calls other key providers |
160 | | * |
161 | | * @param ctx |
162 | | * @param userdata must be an array rnp::KeyProvider pointers, |
163 | | * ending with a nullptr. |
164 | | */ |
165 | | rnp::Key *rnp_key_provider_chained(const pgp_key_request_ctx_t *ctx, void *userdata); |
166 | | |
167 | | #endif |