]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
7c673cae FG |
3 | |
4 | #ifndef CEPH_RGW_AUTH_S3_H | |
5 | #define CEPH_RGW_AUTH_S3_H | |
6 | ||
31f18b77 FG |
7 | #include <array> |
8 | #include <memory> | |
7c673cae | 9 | #include <string> |
f67539c2 | 10 | #include <string_view> |
7c673cae FG |
11 | #include <tuple> |
12 | ||
31f18b77 FG |
13 | #include <boost/algorithm/string.hpp> |
14 | #include <boost/container/static_vector.hpp> | |
31f18b77 FG |
15 | |
16 | #include "common/sstring.hh" | |
7c673cae FG |
17 | #include "rgw_common.h" |
18 | #include "rgw_rest_s3.h" | |
7c673cae FG |
19 | #include "rgw_auth.h" |
20 | #include "rgw_auth_filters.h" | |
21 | #include "rgw_auth_keystone.h" | |
22 | ||
23 | ||
24 | namespace rgw { | |
25 | namespace auth { | |
26 | namespace s3 { | |
27 | ||
94b18763 FG |
28 | static constexpr auto RGW_AUTH_GRACE = std::chrono::minutes{15}; |
29 | ||
30 | // returns true if the request time is within RGW_AUTH_GRACE of the current time | |
31 | bool is_time_skew_ok(time_t t); | |
32 | ||
11fdf7f2 TL |
33 | class STSAuthStrategy : public rgw::auth::Strategy, |
34 | public rgw::auth::RemoteApplier::Factory, | |
35 | public rgw::auth::LocalApplier::Factory, | |
36 | public rgw::auth::RoleApplier::Factory { | |
37 | typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t; | |
20effc67 | 38 | rgw::sal::Store* store; |
9f95a23c | 39 | rgw::auth::ImplicitTenants& implicit_tenant_context; |
11fdf7f2 TL |
40 | |
41 | STSEngine sts_engine; | |
42 | ||
43 | aplptr_t create_apl_remote(CephContext* const cct, | |
44 | const req_state* const s, | |
45 | rgw::auth::RemoteApplier::acl_strategy_t&& acl_alg, | |
2a845540 | 46 | const rgw::auth::RemoteApplier::AuthInfo &info) const override { |
20effc67 TL |
47 | auto apl = rgw::auth::add_sysreq(cct, store, s, |
48 | rgw::auth::RemoteApplier(cct, store, std::move(acl_alg), info, | |
9f95a23c TL |
49 | implicit_tenant_context, |
50 | rgw::auth::ImplicitTenants::IMPLICIT_TENANTS_S3)); | |
11fdf7f2 TL |
51 | return aplptr_t(new decltype(apl)(std::move(apl))); |
52 | } | |
53 | ||
54 | aplptr_t create_apl_local(CephContext* const cct, | |
55 | const req_state* const s, | |
56 | const RGWUserInfo& user_info, | |
57 | const std::string& subuser, | |
2a845540 TL |
58 | const std::optional<uint32_t>& perm_mask, |
59 | const std::string& access_key_id) const override { | |
20effc67 | 60 | auto apl = rgw::auth::add_sysreq(cct, store, s, |
2a845540 | 61 | rgw::auth::LocalApplier(cct, user_info, subuser, perm_mask, access_key_id)); |
11fdf7f2 TL |
62 | return aplptr_t(new decltype(apl)(std::move(apl))); |
63 | } | |
64 | ||
65 | aplptr_t create_apl_role(CephContext* const cct, | |
66 | const req_state* const s, | |
f91f0fd5 | 67 | const rgw::auth::RoleApplier::Role& role, |
20effc67 TL |
68 | const rgw::auth::RoleApplier::TokenAttrs& token_attrs) const override { |
69 | auto apl = rgw::auth::add_sysreq(cct, store, s, | |
70 | rgw::auth::RoleApplier(cct, role, token_attrs)); | |
11fdf7f2 TL |
71 | return aplptr_t(new decltype(apl)(std::move(apl))); |
72 | } | |
73 | ||
74 | public: | |
75 | STSAuthStrategy(CephContext* const cct, | |
20effc67 | 76 | rgw::sal::Store* store, |
9f95a23c | 77 | rgw::auth::ImplicitTenants& implicit_tenant_context, |
11fdf7f2 | 78 | AWSEngine::VersionAbstractor* const ver_abstractor) |
20effc67 | 79 | : store(store), |
9f95a23c | 80 | implicit_tenant_context(implicit_tenant_context), |
20effc67 | 81 | sts_engine(cct, store, *ver_abstractor, |
11fdf7f2 TL |
82 | static_cast<rgw::auth::LocalApplier::Factory*>(this), |
83 | static_cast<rgw::auth::RemoteApplier::Factory*>(this), | |
84 | static_cast<rgw::auth::RoleApplier::Factory*>(this)) { | |
85 | if (cct->_conf->rgw_s3_auth_use_sts) { | |
86 | add_engine(Control::SUFFICIENT, sts_engine); | |
87 | } | |
88 | } | |
89 | ||
90 | const char* get_name() const noexcept override { | |
91 | return "rgw::auth::s3::STSAuthStrategy"; | |
92 | } | |
93 | }; | |
94 | ||
7c673cae FG |
95 | class ExternalAuthStrategy : public rgw::auth::Strategy, |
96 | public rgw::auth::RemoteApplier::Factory { | |
97 | typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t; | |
20effc67 | 98 | rgw::sal::Store* store; |
9f95a23c | 99 | rgw::auth::ImplicitTenants& implicit_tenant_context; |
7c673cae FG |
100 | |
101 | using keystone_config_t = rgw::keystone::CephCtxConfig; | |
102 | using keystone_cache_t = rgw::keystone::TokenCache; | |
9f95a23c | 103 | using secret_cache_t = rgw::auth::keystone::SecretCache; |
7c673cae FG |
104 | using EC2Engine = rgw::auth::keystone::EC2Engine; |
105 | ||
3efd9988 | 106 | boost::optional <EC2Engine> keystone_engine; |
7c673cae FG |
107 | LDAPEngine ldap_engine; |
108 | ||
109 | aplptr_t create_apl_remote(CephContext* const cct, | |
110 | const req_state* const s, | |
111 | rgw::auth::RemoteApplier::acl_strategy_t&& acl_alg, | |
2a845540 | 112 | const rgw::auth::RemoteApplier::AuthInfo &info) const override { |
20effc67 TL |
113 | auto apl = rgw::auth::add_sysreq(cct, store, s, |
114 | rgw::auth::RemoteApplier(cct, store, std::move(acl_alg), info, | |
9f95a23c TL |
115 | implicit_tenant_context, |
116 | rgw::auth::ImplicitTenants::IMPLICIT_TENANTS_S3)); | |
7c673cae FG |
117 | /* TODO(rzarzynski): replace with static_ptr. */ |
118 | return aplptr_t(new decltype(apl)(std::move(apl))); | |
119 | } | |
120 | ||
121 | public: | |
122 | ExternalAuthStrategy(CephContext* const cct, | |
20effc67 | 123 | rgw::sal::Store* store, |
9f95a23c | 124 | rgw::auth::ImplicitTenants& implicit_tenant_context, |
31f18b77 | 125 | AWSEngine::VersionAbstractor* const ver_abstractor) |
20effc67 | 126 | : store(store), |
9f95a23c | 127 | implicit_tenant_context(implicit_tenant_context), |
20effc67 | 128 | ldap_engine(cct, store, *ver_abstractor, |
7c673cae FG |
129 | static_cast<rgw::auth::RemoteApplier::Factory*>(this)) { |
130 | ||
131 | if (cct->_conf->rgw_s3_auth_use_keystone && | |
132 | ! cct->_conf->rgw_keystone_url.empty()) { | |
3efd9988 FG |
133 | |
134 | keystone_engine.emplace(cct, ver_abstractor, | |
135 | static_cast<rgw::auth::RemoteApplier::Factory*>(this), | |
136 | keystone_config_t::get_instance(), | |
9f95a23c TL |
137 | keystone_cache_t::get_instance<keystone_config_t>(), |
138 | secret_cache_t::get_instance()); | |
3efd9988 FG |
139 | add_engine(Control::SUFFICIENT, *keystone_engine); |
140 | ||
7c673cae FG |
141 | } |
142 | ||
a8e16298 | 143 | if (ldap_engine.valid()) { |
7c673cae FG |
144 | add_engine(Control::SUFFICIENT, ldap_engine); |
145 | } | |
146 | } | |
147 | ||
148 | const char* get_name() const noexcept override { | |
149 | return "rgw::auth::s3::AWSv2ExternalAuthStrategy"; | |
150 | } | |
151 | }; | |
152 | ||
153 | ||
d2e6a577 FG |
154 | template <class AbstractorT, |
155 | bool AllowAnonAccessT = false> | |
31f18b77 FG |
156 | class AWSAuthStrategy : public rgw::auth::Strategy, |
157 | public rgw::auth::LocalApplier::Factory { | |
7c673cae FG |
158 | typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t; |
159 | ||
31f18b77 FG |
160 | static_assert(std::is_base_of<rgw::auth::s3::AWSEngine::VersionAbstractor, |
161 | AbstractorT>::value, | |
162 | "AbstractorT must be a subclass of rgw::auth::s3::VersionAbstractor"); | |
7c673cae | 163 | |
20effc67 | 164 | rgw::sal::Store* store; |
31f18b77 | 165 | AbstractorT ver_abstractor; |
7c673cae | 166 | |
d2e6a577 | 167 | S3AnonymousEngine anonymous_engine; |
7c673cae | 168 | ExternalAuthStrategy external_engines; |
11fdf7f2 | 169 | STSAuthStrategy sts_engine; |
31f18b77 | 170 | LocalEngine local_engine; |
7c673cae FG |
171 | |
172 | aplptr_t create_apl_local(CephContext* const cct, | |
173 | const req_state* const s, | |
174 | const RGWUserInfo& user_info, | |
11fdf7f2 | 175 | const std::string& subuser, |
2a845540 TL |
176 | const std::optional<uint32_t>& perm_mask, |
177 | const std::string& access_key_id) const override { | |
20effc67 | 178 | auto apl = rgw::auth::add_sysreq(cct, store, s, |
2a845540 | 179 | rgw::auth::LocalApplier(cct, user_info, subuser, perm_mask, access_key_id)); |
7c673cae FG |
180 | /* TODO(rzarzynski): replace with static_ptr. */ |
181 | return aplptr_t(new decltype(apl)(std::move(apl))); | |
182 | } | |
183 | ||
184 | public: | |
1adf2230 AA |
185 | using engine_map_t = std::map <std::string, std::reference_wrapper<const Engine>>; |
186 | void add_engines(const std::vector <std::string>& auth_order, | |
187 | engine_map_t eng_map) | |
188 | { | |
189 | auto ctrl_flag = Control::SUFFICIENT; | |
190 | for (const auto &eng : auth_order) { | |
191 | // fallback to the last engine, in case of multiple engines, since ctrl | |
192 | // flag is sufficient for others, error from earlier engine is returned | |
193 | if (&eng == &auth_order.back() && eng_map.size() > 1) { | |
194 | ctrl_flag = Control::FALLBACK; | |
195 | } | |
11fdf7f2 TL |
196 | if (const auto kv = eng_map.find(eng); |
197 | kv != eng_map.end()) { | |
1adf2230 AA |
198 | add_engine(ctrl_flag, kv->second); |
199 | } | |
200 | } | |
201 | } | |
202 | ||
11fdf7f2 | 203 | auto parse_auth_order(CephContext* const cct) |
1adf2230 AA |
204 | { |
205 | std::vector <std::string> result; | |
206 | ||
11fdf7f2 TL |
207 | const std::set <std::string_view> allowed_auth = { "sts", "external", "local" }; |
208 | std::vector <std::string> default_order = { "sts", "external", "local" }; | |
1adf2230 AA |
209 | // supplied strings may contain a space, so let's bypass that |
210 | boost::split(result, cct->_conf->rgw_s3_auth_order, | |
211 | boost::is_any_of(", "), boost::token_compress_on); | |
212 | ||
213 | if (std::any_of(result.begin(), result.end(), | |
11fdf7f2 | 214 | [allowed_auth](std::string_view s) |
1adf2230 AA |
215 | { return allowed_auth.find(s) == allowed_auth.end();})){ |
216 | return default_order; | |
217 | } | |
218 | return result; | |
219 | } | |
220 | ||
31f18b77 | 221 | AWSAuthStrategy(CephContext* const cct, |
9f95a23c | 222 | rgw::auth::ImplicitTenants& implicit_tenant_context, |
20effc67 TL |
223 | rgw::sal::Store* store) |
224 | : store(store), | |
31f18b77 | 225 | ver_abstractor(cct), |
d2e6a577 FG |
226 | anonymous_engine(cct, |
227 | static_cast<rgw::auth::LocalApplier::Factory*>(this)), | |
20effc67 TL |
228 | external_engines(cct, store, implicit_tenant_context, &ver_abstractor), |
229 | sts_engine(cct, store, implicit_tenant_context, &ver_abstractor), | |
230 | local_engine(cct, store, ver_abstractor, | |
7c673cae | 231 | static_cast<rgw::auth::LocalApplier::Factory*>(this)) { |
11fdf7f2 | 232 | /* The anonymous auth. */ |
d2e6a577 FG |
233 | if (AllowAnonAccessT) { |
234 | add_engine(Control::SUFFICIENT, anonymous_engine); | |
235 | } | |
7c673cae | 236 | |
1adf2230 AA |
237 | auto auth_order = parse_auth_order(cct); |
238 | engine_map_t engine_map; | |
11fdf7f2 TL |
239 | |
240 | /* STS Auth*/ | |
241 | if (! sts_engine.is_empty()) { | |
242 | engine_map.insert(std::make_pair("sts", std::cref(sts_engine))); | |
243 | } | |
244 | ||
d2e6a577 | 245 | /* The external auth. */ |
7c673cae | 246 | if (! external_engines.is_empty()) { |
1adf2230 | 247 | engine_map.insert(std::make_pair("external", std::cref(external_engines))); |
7c673cae | 248 | } |
d2e6a577 | 249 | /* The local auth. */ |
7c673cae | 250 | if (cct->_conf->rgw_s3_auth_use_rados) { |
1adf2230 | 251 | engine_map.insert(std::make_pair("local", std::cref(local_engine))); |
7c673cae | 252 | } |
11fdf7f2 | 253 | |
1adf2230 | 254 | add_engines(auth_order, engine_map); |
7c673cae FG |
255 | } |
256 | ||
257 | const char* get_name() const noexcept override { | |
31f18b77 | 258 | return "rgw::auth::s3::AWSAuthStrategy"; |
7c673cae FG |
259 | } |
260 | }; | |
261 | ||
31f18b77 FG |
262 | |
263 | class AWSv4ComplMulti : public rgw::auth::Completer, | |
264 | public rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>, | |
265 | public std::enable_shared_from_this<AWSv4ComplMulti> { | |
266 | using io_base_t = rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>; | |
11fdf7f2 | 267 | using signing_key_t = sha256_digest_t; |
31f18b77 FG |
268 | |
269 | CephContext* const cct; | |
270 | ||
f67539c2 TL |
271 | const std::string_view date; |
272 | const std::string_view credential_scope; | |
31f18b77 FG |
273 | const signing_key_t signing_key; |
274 | ||
275 | class ChunkMeta { | |
276 | size_t data_offset_in_stream = 0; | |
277 | size_t data_length = 0; | |
278 | std::string signature; | |
279 | ||
280 | ChunkMeta(const size_t data_starts_in_stream, | |
281 | const size_t data_length, | |
f67539c2 | 282 | const std::string_view signature) |
31f18b77 FG |
283 | : data_offset_in_stream(data_starts_in_stream), |
284 | data_length(data_length), | |
f67539c2 | 285 | signature(std::string(signature)) { |
31f18b77 FG |
286 | } |
287 | ||
f67539c2 TL |
288 | explicit ChunkMeta(const std::string_view& signature) |
289 | : signature(std::string(signature)) { | |
31f18b77 FG |
290 | } |
291 | ||
292 | public: | |
293 | static constexpr size_t SIG_SIZE = 64; | |
294 | ||
295 | /* Let's suppose the data length fields can't exceed uint64_t. */ | |
296 | static constexpr size_t META_MAX_SIZE = \ | |
297 | sarrlen("\r\nffffffffffffffff;chunk-signature=") + SIG_SIZE + sarrlen("\r\n"); | |
298 | ||
299 | /* The metadata size of for the last, empty chunk. */ | |
300 | static constexpr size_t META_MIN_SIZE = \ | |
301 | sarrlen("0;chunk-signature=") + SIG_SIZE + sarrlen("\r\n"); | |
302 | ||
303 | /* Detect whether a given stream_pos fits in boundaries of a chunk. */ | |
304 | bool is_new_chunk_in_stream(size_t stream_pos) const; | |
305 | ||
306 | /* Get the remaining data size. */ | |
307 | size_t get_data_size(size_t stream_pos) const; | |
308 | ||
309 | const std::string& get_signature() const { | |
310 | return signature; | |
311 | } | |
312 | ||
313 | /* Factory: create an object representing metadata of first, initial chunk | |
314 | * in a stream. */ | |
f67539c2 | 315 | static ChunkMeta create_first(const std::string_view& seed_signature) { |
31f18b77 FG |
316 | return ChunkMeta(seed_signature); |
317 | } | |
318 | ||
319 | /* Factory: parse a block of META_MAX_SIZE bytes and creates an object | |
320 | * representing non-first chunk in a stream. As the process is sequential | |
321 | * and depends on the previous chunk, caller must pass it. */ | |
322 | static std::pair<ChunkMeta, size_t> create_next(CephContext* cct, | |
323 | ChunkMeta&& prev, | |
324 | const char* metabuf, | |
325 | size_t metabuf_len); | |
326 | } chunk_meta; | |
327 | ||
328 | size_t stream_pos; | |
329 | boost::container::static_vector<char, ChunkMeta::META_MAX_SIZE> parsing_buf; | |
330 | ceph::crypto::SHA256* sha256_hash; | |
331 | std::string prev_chunk_signature; | |
332 | ||
333 | bool is_signature_mismatched(); | |
334 | std::string calc_chunk_signature(const std::string& payload_hash) const; | |
335 | ||
336 | public: | |
337 | /* We need the constructor to be public because of the std::make_shared that | |
338 | * is employed by the create() method. */ | |
339 | AWSv4ComplMulti(const req_state* const s, | |
f67539c2 TL |
340 | std::string_view date, |
341 | std::string_view credential_scope, | |
342 | std::string_view seed_signature, | |
31f18b77 FG |
343 | const signing_key_t& signing_key) |
344 | : io_base_t(nullptr), | |
345 | cct(s->cct), | |
346 | date(std::move(date)), | |
347 | credential_scope(std::move(credential_scope)), | |
348 | signing_key(signing_key), | |
349 | ||
350 | /* The evolving state. */ | |
351 | chunk_meta(ChunkMeta::create_first(seed_signature)), | |
352 | stream_pos(0), | |
353 | sha256_hash(calc_hash_sha256_open_stream()), | |
354 | prev_chunk_signature(std::move(seed_signature)) { | |
355 | } | |
356 | ||
357 | ~AWSv4ComplMulti() { | |
358 | if (sha256_hash) { | |
359 | calc_hash_sha256_close_stream(&sha256_hash); | |
360 | } | |
361 | } | |
362 | ||
363 | /* rgw::io::DecoratedRestfulClient. */ | |
364 | size_t recv_body(char* buf, size_t max) override; | |
365 | ||
366 | /* rgw::auth::Completer. */ | |
11fdf7f2 | 367 | void modify_request_state(const DoutPrefixProvider* dpp, req_state* s_rw) override; |
31f18b77 FG |
368 | bool complete() override; |
369 | ||
370 | /* Factories. */ | |
371 | static cmplptr_t create(const req_state* s, | |
f67539c2 TL |
372 | std::string_view date, |
373 | std::string_view credential_scope, | |
374 | std::string_view seed_signature, | |
31f18b77 FG |
375 | const boost::optional<std::string>& secret_key); |
376 | ||
377 | }; | |
378 | ||
379 | class AWSv4ComplSingle : public rgw::auth::Completer, | |
380 | public rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>, | |
381 | public std::enable_shared_from_this<AWSv4ComplSingle> { | |
382 | using io_base_t = rgw::io::DecoratedRestfulClient<rgw::io::RestfulClient*>; | |
383 | ||
384 | CephContext* const cct; | |
385 | const char* const expected_request_payload_hash; | |
386 | ceph::crypto::SHA256* sha256_hash = nullptr; | |
387 | ||
388 | public: | |
389 | /* Defined in rgw_auth_s3.cc because of get_v4_exp_payload_hash(). We need | |
390 | * the constructor to be public because of the std::make_shared employed by | |
391 | * the create() method. */ | |
11fdf7f2 | 392 | explicit AWSv4ComplSingle(const req_state* const s); |
31f18b77 FG |
393 | |
394 | ~AWSv4ComplSingle() { | |
395 | if (sha256_hash) { | |
396 | calc_hash_sha256_close_stream(&sha256_hash); | |
397 | } | |
398 | } | |
399 | ||
400 | /* rgw::io::DecoratedRestfulClient. */ | |
401 | size_t recv_body(char* buf, size_t max) override; | |
402 | ||
403 | /* rgw::auth::Completer. */ | |
11fdf7f2 | 404 | void modify_request_state(const DoutPrefixProvider* dpp, req_state* s_rw) override; |
31f18b77 FG |
405 | bool complete() override; |
406 | ||
407 | /* Factories. */ | |
408 | static cmplptr_t create(const req_state* s, | |
409 | const boost::optional<std::string>&); | |
410 | ||
411 | }; | |
412 | ||
7c673cae FG |
413 | } /* namespace s3 */ |
414 | } /* namespace auth */ | |
415 | } /* namespace rgw */ | |
416 | ||
417 | void rgw_create_s3_canonical_header( | |
b3b6e05e | 418 | const DoutPrefixProvider *dpp, |
7c673cae FG |
419 | const char *method, |
420 | const char *content_md5, | |
421 | const char *content_type, | |
422 | const char *date, | |
9f95a23c TL |
423 | const meta_map_t& meta_map, |
424 | const meta_map_t& qs_map, | |
7c673cae FG |
425 | const char *request_uri, |
426 | const std::map<std::string, std::string>& sub_resources, | |
427 | std::string& dest_str); | |
b3b6e05e TL |
428 | bool rgw_create_s3_canonical_header(const DoutPrefixProvider *dpp, |
429 | const req_info& info, | |
7c673cae FG |
430 | utime_t *header_time, /* out */ |
431 | std::string& dest, /* out */ | |
432 | bool qsr); | |
433 | static inline std::tuple<bool, std::string, utime_t> | |
b3b6e05e | 434 | rgw_create_s3_canonical_header(const DoutPrefixProvider *dpp, const req_info& info, const bool qsr) { |
7c673cae FG |
435 | std::string dest; |
436 | utime_t header_time; | |
437 | ||
b3b6e05e | 438 | const bool ok = rgw_create_s3_canonical_header(dpp, info, &header_time, dest, qsr); |
7c673cae FG |
439 | return std::make_tuple(ok, dest, header_time); |
440 | } | |
441 | ||
31f18b77 FG |
442 | namespace rgw { |
443 | namespace auth { | |
444 | namespace s3 { | |
445 | ||
446 | static constexpr char AWS4_HMAC_SHA256_STR[] = "AWS4-HMAC-SHA256"; | |
224ce89b | 447 | static constexpr char AWS4_HMAC_SHA256_PAYLOAD_STR[] = "AWS4-HMAC-SHA256-PAYLOAD"; |
31f18b77 FG |
448 | |
449 | static constexpr char AWS4_EMPTY_PAYLOAD_HASH[] = \ | |
450 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; | |
451 | ||
452 | static constexpr char AWS4_UNSIGNED_PAYLOAD_HASH[] = "UNSIGNED-PAYLOAD"; | |
453 | ||
454 | static constexpr char AWS4_STREAMING_PAYLOAD_HASH[] = \ | |
455 | "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"; | |
456 | ||
20effc67 TL |
457 | bool is_non_s3_op(RGWOpType op_type); |
458 | ||
a8e16298 | 459 | int parse_v4_credentials(const req_info& info, /* in */ |
f67539c2 TL |
460 | std::string_view& access_key_id, /* out */ |
461 | std::string_view& credential_scope, /* out */ | |
462 | std::string_view& signedheaders, /* out */ | |
463 | std::string_view& signature, /* out */ | |
464 | std::string_view& date, /* out */ | |
465 | std::string_view& session_token, /* out */ | |
b3b6e05e TL |
466 | const bool using_qs, /* in */ |
467 | const DoutPrefixProvider *dpp); /* in */ | |
11fdf7f2 | 468 | |
20effc67 TL |
469 | string gen_v4_scope(const ceph::real_time& timestamp, |
470 | const string& region, | |
471 | const string& service); | |
472 | ||
11fdf7f2 TL |
473 | static inline bool char_needs_aws4_escaping(const char c, bool encode_slash) |
474 | { | |
475 | if ((c >= 'a' && c <= 'z') || | |
476 | (c >= 'A' && c <= 'Z') || | |
477 | (c >= '0' && c <= '9')) { | |
478 | return false; | |
479 | } | |
480 | ||
481 | switch (c) { | |
482 | case '-': | |
483 | case '_': | |
484 | case '.': | |
485 | case '~': | |
486 | return false; | |
487 | } | |
488 | ||
489 | if (c == '/' && !encode_slash) | |
490 | return false; | |
491 | ||
492 | return true; | |
493 | } | |
494 | ||
495 | static inline std::string aws4_uri_encode(const std::string& src, bool encode_slash) | |
496 | { | |
497 | std::string result; | |
498 | ||
499 | for (const std::string::value_type c : src) { | |
500 | if (char_needs_aws4_escaping(c, encode_slash)) { | |
501 | rgw_uri_escape_char(c, result); | |
502 | } else { | |
503 | result.push_back(c); | |
504 | } | |
505 | } | |
506 | ||
507 | return result; | |
508 | } | |
509 | ||
f67539c2 | 510 | static inline std::string aws4_uri_recode(const std::string_view& src, bool encode_slash) |
11fdf7f2 TL |
511 | { |
512 | std::string decoded = url_decode(src); | |
513 | return aws4_uri_encode(decoded, encode_slash); | |
514 | } | |
31f18b77 FG |
515 | |
516 | static inline std::string get_v4_canonical_uri(const req_info& info) { | |
517 | /* The code should normalize according to RFC 3986 but S3 does NOT do path | |
518 | * normalization that SigV4 typically does. This code follows the same | |
519 | * approach that boto library. See auth.py:canonical_uri(...). */ | |
520 | ||
11fdf7f2 | 521 | std::string canonical_uri = aws4_uri_recode(info.request_uri_aws4, false); |
31f18b77 FG |
522 | |
523 | if (canonical_uri.empty()) { | |
524 | canonical_uri = "/"; | |
525 | } else { | |
526 | boost::replace_all(canonical_uri, "+", "%20"); | |
527 | } | |
528 | ||
529 | return canonical_uri; | |
530 | } | |
531 | ||
20effc67 TL |
532 | static inline std::string gen_v4_canonical_uri(const req_info& info) { |
533 | /* The code should normalize according to RFC 3986 but S3 does NOT do path | |
534 | * normalization that SigV4 typically does. This code follows the same | |
535 | * approach that boto library. See auth.py:canonical_uri(...). */ | |
536 | ||
537 | std::string canonical_uri = aws4_uri_recode(info.request_uri, false); | |
538 | ||
539 | if (canonical_uri.empty()) { | |
540 | canonical_uri = "/"; | |
541 | } else { | |
542 | boost::replace_all(canonical_uri, "+", "%20"); | |
543 | } | |
544 | ||
545 | return canonical_uri; | |
546 | } | |
547 | ||
11fdf7f2 TL |
548 | static inline const string calc_v4_payload_hash(const string& payload) |
549 | { | |
550 | ceph::crypto::SHA256* sha256_hash = calc_hash_sha256_open_stream(); | |
551 | calc_hash_sha256_update_stream(sha256_hash, payload.c_str(), payload.length()); | |
552 | const auto payload_hash = calc_hash_sha256_close_stream(&sha256_hash); | |
553 | return payload_hash; | |
554 | } | |
555 | ||
31f18b77 FG |
556 | static inline const char* get_v4_exp_payload_hash(const req_info& info) |
557 | { | |
11fdf7f2 | 558 | /* In AWSv4 the hash of real, transferred payload IS NOT necessary to form |
31f18b77 FG |
559 | * a Canonical Request, and thus verify a Signature. x-amz-content-sha256 |
560 | * header lets get the information very early -- before seeing first byte | |
561 | * of HTTP body. As a consequence, we can decouple Signature verification | |
562 | * from payload's fingerprint check. */ | |
563 | const char *expected_request_payload_hash = \ | |
564 | info.env->get("HTTP_X_AMZ_CONTENT_SHA256"); | |
565 | ||
566 | if (!expected_request_payload_hash) { | |
567 | /* An HTTP client MUST send x-amz-content-sha256. The single exception | |
568 | * is the case of using the Query Parameters where "UNSIGNED-PAYLOAD" | |
569 | * literals are used for crafting Canonical Request: | |
570 | * | |
571 | * You don't include a payload hash in the Canonical Request, because | |
572 | * when you create a presigned URL, you don't know the payload content | |
573 | * because the URL is used to upload an arbitrary payload. Instead, you | |
574 | * use a constant string UNSIGNED-PAYLOAD. */ | |
575 | expected_request_payload_hash = AWS4_UNSIGNED_PAYLOAD_HASH; | |
576 | } | |
577 | ||
578 | return expected_request_payload_hash; | |
579 | } | |
580 | ||
581 | static inline bool is_v4_payload_unsigned(const char* const exp_payload_hash) | |
582 | { | |
583 | return boost::equals(exp_payload_hash, AWS4_UNSIGNED_PAYLOAD_HASH); | |
584 | } | |
585 | ||
586 | static inline bool is_v4_payload_empty(const req_state* const s) | |
587 | { | |
588 | /* from rfc2616 - 4.3 Message Body | |
589 | * | |
590 | * "The presence of a message-body in a request is signaled by the inclusion | |
591 | * of a Content-Length or Transfer-Encoding header field in the request's | |
592 | * message-headers." */ | |
593 | return s->content_length == 0 && | |
594 | s->info.env->get("HTTP_TRANSFER_ENCODING") == nullptr; | |
595 | } | |
596 | ||
597 | static inline bool is_v4_payload_streamed(const char* const exp_payload_hash) | |
598 | { | |
599 | return boost::equals(exp_payload_hash, AWS4_STREAMING_PAYLOAD_HASH); | |
600 | } | |
601 | ||
602 | std::string get_v4_canonical_qs(const req_info& info, bool using_qs); | |
603 | ||
20effc67 TL |
604 | std::string gen_v4_canonical_qs(const req_info& info); |
605 | ||
31f18b77 FG |
606 | boost::optional<std::string> |
607 | get_v4_canonical_headers(const req_info& info, | |
f67539c2 | 608 | const std::string_view& signedheaders, |
31f18b77 FG |
609 | bool using_qs, |
610 | bool force_boto2_compat); | |
611 | ||
20effc67 TL |
612 | std::string gen_v4_canonical_headers(const req_info& info, |
613 | const std::map<std::string, std::string>& extra_headers, | |
614 | string *signed_hdrs); | |
615 | ||
31f18b77 FG |
616 | extern sha256_digest_t |
617 | get_v4_canon_req_hash(CephContext* cct, | |
f67539c2 | 618 | const std::string_view& http_verb, |
31f18b77 FG |
619 | const std::string& canonical_uri, |
620 | const std::string& canonical_qs, | |
621 | const std::string& canonical_hdrs, | |
f67539c2 | 622 | const std::string_view& signed_hdrs, |
b3b6e05e TL |
623 | const std::string_view& request_payload_hash, |
624 | const DoutPrefixProvider *dpp); | |
31f18b77 FG |
625 | |
626 | AWSEngine::VersionAbstractor::string_to_sign_t | |
627 | get_v4_string_to_sign(CephContext* cct, | |
f67539c2 TL |
628 | const std::string_view& algorithm, |
629 | const std::string_view& request_date, | |
630 | const std::string_view& credential_scope, | |
b3b6e05e TL |
631 | const sha256_digest_t& canonreq_hash, |
632 | const DoutPrefixProvider *dpp); | |
31f18b77 FG |
633 | |
634 | extern AWSEngine::VersionAbstractor::server_signature_t | |
f67539c2 | 635 | get_v4_signature(const std::string_view& credential_scope, |
31f18b77 | 636 | CephContext* const cct, |
f67539c2 | 637 | const std::string_view& secret_key, |
b3b6e05e TL |
638 | const AWSEngine::VersionAbstractor::string_to_sign_t& string_to_sign, |
639 | const DoutPrefixProvider *dpp); | |
31f18b77 FG |
640 | |
641 | extern AWSEngine::VersionAbstractor::server_signature_t | |
642 | get_v2_signature(CephContext*, | |
643 | const std::string& secret_key, | |
644 | const AWSEngine::VersionAbstractor::string_to_sign_t& string_to_sign); | |
31f18b77 FG |
645 | } /* namespace s3 */ |
646 | } /* namespace auth */ | |
647 | } /* namespace rgw */ | |
7c673cae FG |
648 | |
649 | #endif |