@@ -82,7 +82,7 @@ void TempURLEngine::get_owner_info(const DoutPrefixProvider* dpp, const req_stat
8282 const string& bucket_name = s->init_state .url_bucket ;
8383
8484 /* TempURL requires that bucket and object names are specified. */
85- if (bucket_name.empty () || s->object -> empty ( )) {
85+ if (bucket_name.empty () || rgw::sal::Object::empty ( s->object )) {
8686 throw -EPERM;
8787 }
8888
@@ -190,90 +190,132 @@ std::string extract_swift_subuser(const std::string& swift_user_name)
190190 }
191191}
192192
193- class TempURLEngine ::SignatureHelper
194- {
195- private:
196- static constexpr uint32_t output_size =
197- CEPH_CRYPTO_HMACSHA1_DIGESTSIZE * 2 + 1 ;
198-
199- unsigned char dest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; // 20
200- char dest_str[output_size];
201-
193+ template <class HASHFLAVOR , SignatureFlavor SIGNATUREFLAVOR>
194+ class TempURLSignatureT : public rgw ::auth::swift::FormatSignature<HASHFLAVOR,SIGNATUREFLAVOR> {
195+ using UCHARPTR = const unsigned char *;
196+ using base_t = SignatureHelperT<HASHFLAVOR>;
197+ using format_signature_t = rgw::auth::swift::FormatSignature<HASHFLAVOR,SIGNATUREFLAVOR>;
202198public:
203- SignatureHelper () = default ;
204-
205199 const char * calc (const std::string& key,
206200 const std::string_view& method,
207201 const std::string_view& path,
208202 const std::string& expires) {
203+ HASHFLAVOR hmac ((UCHARPTR) key.data (), key.size ());
209204
210- using ceph::crypto::HMACSHA1;
211- using UCHARPTR = const unsigned char *;
212-
213- HMACSHA1 hmac ((UCHARPTR) key.c_str (), key.size ());
214205 hmac.Update ((UCHARPTR) method.data (), method.size ());
215206 hmac.Update ((UCHARPTR) " \n " , 1 );
216207 hmac.Update ((UCHARPTR) expires.c_str (), expires.size ());
217208 hmac.Update ((UCHARPTR) " \n " , 1 );
218209 hmac.Update ((UCHARPTR) path.data (), path.size ());
219- hmac.Final (dest);
210+ hmac.Final (base_t :: dest);
220211
221- buf_to_hex ((UCHARPTR) dest, sizeof (dest), dest_str);
222-
223- return dest_str;
212+ return format_signature_t::result ();
224213 }
225-
226- bool is_equal_to (const std::string& rhs) const {
227- /* never allow out-of-range exception */
228- if (rhs.size () < (output_size - 1 )) {
229- return false ;
214+ }; /* TempURLSignatureT */
215+ class TempURLEngine ::SignatureHelper {
216+ public:
217+ SignatureHelper () {};
218+ virtual ~SignatureHelper () {};
219+ virtual const char * calc (const std::string& key,
220+ const std::string_view& method,
221+ const std::string_view& path,
222+ const std::string& expires) {
223+ return nullptr ;
224+ }
225+ virtual bool is_equal_to (const std::string& rhs) {
226+ return false ;
227+ };
228+ static std::unique_ptr<SignatureHelper> get_sig_helper (std::string_view x);
229+ };
230+ class TempURLSignature {
231+ friend TempURLEngine;
232+ using BadSignatureHelper = TempURLEngine::SignatureHelper;
233+ template <typename HASHFLAVOR, SignatureFlavor SIGNATUREFLAVOR>
234+ class SignatureHelper_x : public TempURLEngine ::SignatureHelper
235+ {
236+ friend TempURLEngine;
237+ TempURLSignatureT<HASHFLAVOR,SIGNATUREFLAVOR> d;
238+ public:
239+ SignatureHelper_x () {};
240+ ~SignatureHelper_x () { };
241+ virtual const char * calc (const std::string& key,
242+ const std::string_view& method,
243+ const std::string_view& path,
244+ const std::string& expires) {
245+ return d.calc (key,method,path,expires);
230246 }
231- return rhs.compare (0 /* pos */ , output_size, dest_str) == 0 ;
247+ virtual bool is_equal_to (const std::string& rhs) {
248+ return d.is_equal_to (rhs);
249+ };
250+ };
251+ };
252+
253+ std::unique_ptr<TempURLEngine::SignatureHelper> TempURLEngine::SignatureHelper::get_sig_helper (std::string_view x) {
254+ size_t pos = x.find (' :' );
255+ if (pos == x.npos || pos <= 0 ) {
256+ switch (x.length ()) {
257+ case CEPH_CRYPTO_HMACSHA1_DIGESTSIZE*2 :
258+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA1,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
259+ case CEPH_CRYPTO_HMACSHA256_DIGESTSIZE*2 :
260+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA256,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
261+ case CEPH_CRYPTO_HMACSHA512_DIGESTSIZE*2 :
262+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA512,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
263+ }
264+ return std::make_unique<TempURLSignature::BadSignatureHelper>();
232265 }
266+ std::string_view type { x.substr (0 ,pos) };
267+ if (type == " sha1" ) {
268+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA1,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
269+ } else if (type == " sha256" ) {
270+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA256,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
271+ } else if (type == " sha512" ) {
272+ return std::make_unique<TempURLSignature::SignatureHelper_x<ceph::crypto::HMACSHA512,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
273+ }
274+ return std::make_unique<TempURLSignature::BadSignatureHelper>();
275+ };
233276
234- }; /* TempURLEngine::SignatureHelper */
235-
236- class TempURLEngine ::PrefixableSignatureHelper
237- : private TempURLEngine::SignatureHelper {
238- using base_t = SignatureHelper;
277+ class TempURLEngine ::PrefixableSignatureHelper {
239278
240279 const std::string_view decoded_uri;
241280 const std::string_view object_name;
242281 std::string_view no_obj_uri;
243282
244283 const boost::optional<const std::string&> prefix;
284+ std::unique_ptr<SignatureHelper> base_sig_helper;
245285
246286public:
247- PrefixableSignatureHelper (const std::string& _decoded_uri,
287+ PrefixableSignatureHelper (const std::string_view sig,
288+ const std::string& _decoded_uri,
248289 const std::string& object_name,
249290 const boost::optional<const std::string&> prefix)
250291 : decoded_uri(_decoded_uri),
251292 object_name (object_name),
252- prefix(prefix) {
293+ prefix(prefix),
294+ base_sig_helper(TempURLEngine::SignatureHelper::get_sig_helper(sig)) {
253295 /* Transform: v1/acct/cont/obj - > v1/acct/cont/
254296 *
255297 * NOTE(rzarzynski): we really want to substr() on std::string_view,
256298 * not std::string. Otherwise we would end with no_obj_uri referencing
257299 * a temporary. */
258300 no_obj_uri = \
259301 decoded_uri.substr (0 , decoded_uri.length () - object_name.length ());
260- }
302+ };
261303
262304 const char * calc (const std::string& key,
263305 const std::string_view& method,
264306 const std::string_view& path,
265307 const std::string& expires) {
266308 if (!prefix) {
267- return base_t:: calc (key, method, path, expires);
309+ return base_sig_helper-> calc (key, method, path, expires);
268310 } else {
269311 const auto prefixed_path = \
270312 string_cat_reserve (" prefix:" , no_obj_uri, *prefix);
271- return base_t:: calc (key, method, prefixed_path, expires);
313+ return base_sig_helper-> calc (key, method, prefixed_path, expires);
272314 }
273315 }
274316
275317 bool is_equal_to (const std::string& rhs) const {
276- bool is_auth_ok = base_t:: is_equal_to (rhs);
318+ bool is_auth_ok = base_sig_helper-> is_equal_to (rhs);
277319
278320 if (prefix && is_auth_ok) {
279321 const auto prefix_uri = string_cat_reserve (no_obj_uri, *prefix);
@@ -360,6 +402,7 @@ TempURLEngine::authenticate(const DoutPrefixProvider* dpp, const req_state* cons
360402
361403 /* Need to try each combination of keys, allowed path and methods. */
362404 PrefixableSignatureHelper sig_helper {
405+ temp_url_sig,
363406 s->decoded_uri ,
364407 s->object ->get_name (),
365408 temp_url_prefix
@@ -772,4 +815,3 @@ RGWOp *RGWHandler_SWIFT_Auth::op_get()
772815{
773816 return new RGW_SWIFT_Auth_Get;
774817}
775-
0 commit comments