4242
4343using namespace std ;
4444
45+ template <class HASHFLAVOR , rgw::auth::swift::SignatureFlavor SIGNATUREFLAVOR>
46+ class FormPostSignatureT : public rgw ::auth::swift::FormatSignature<HASHFLAVOR,SIGNATUREFLAVOR>
47+ {
48+ using UCHARPTR = const unsigned char *;
49+ using base_t = rgw::auth::swift::SignatureHelperT<HASHFLAVOR>;
50+ using format_signature_t = rgw::auth::swift::FormatSignature<HASHFLAVOR,SIGNATUREFLAVOR>;
51+ public:
52+ const char * calc (const std::string& key,
53+ const std::string_view& path_info,
54+ const std::string_view& redirect,
55+ const std::string_view& max_file_size,
56+ const std::string_view& max_file_count,
57+ const std::string_view& expires) {
58+ HASHFLAVOR hmac ((UCHARPTR) key.data (), key.size ());
59+
60+ hmac.Update ((UCHARPTR) path_info.data (), path_info.size ());
61+ hmac.Update ((UCHARPTR) " \n " , 1 );
62+
63+ hmac.Update ((UCHARPTR) redirect.data (), redirect.size ());
64+ hmac.Update ((UCHARPTR) " \n " , 1 );
65+
66+ hmac.Update ((UCHARPTR) max_file_size.data (), max_file_size.size ());
67+ hmac.Update ((UCHARPTR) " \n " , 1 );
68+
69+ hmac.Update ((UCHARPTR) max_file_count.data (), max_file_count.size ());
70+ hmac.Update ((UCHARPTR) " \n " , 1 );
71+
72+ hmac.Update ((UCHARPTR) expires.data (), expires.size ());
73+
74+ hmac.Final (base_t ::dest);
75+
76+ return format_signature_t::result ();
77+ }
78+ };
79+ class RGWFormPost ::SignatureHelper {
80+ public:
81+ virtual ~SignatureHelper () {};
82+ virtual const char * calc (const std::string& key,
83+ const std::string_view& path_info,
84+ const std::string_view& redirect,
85+ const std::string_view& max_file_size,
86+ const std::string_view& max_file_count,
87+ const std::string_view& expires) {
88+ return nullptr ;
89+ };
90+ virtual const char * get_signature () const {
91+ return nullptr ;
92+ };
93+ virtual bool is_equal_to (const std::string& rhs) {
94+ return false ;
95+ };
96+ static std::unique_ptr<SignatureHelper> get_sig_helper (std::string_view x);
97+ };
98+ template <typename HASHFLAVOR, rgw::auth::swift::SignatureFlavor SIGNATUREFLAVOR>
99+ class RGWFormPost ::SignatureHelper_x : public RGWFormPost::SignatureHelper
100+ {
101+ friend RGWFormPost;
102+ private:
103+ FormPostSignatureT<HASHFLAVOR,SIGNATUREFLAVOR> d;
104+ public:
105+ ~SignatureHelper_x () { };
106+ SignatureHelper_x () {};
107+ virtual const char * calc (const std::string& key,
108+ const std::string_view& path_info,
109+ const std::string_view& redirect,
110+ const std::string_view& max_file_size,
111+ const std::string_view& max_file_count,
112+ const std::string_view& expires) {
113+ return d.calc (key,path_info,redirect,
114+ max_file_size,max_file_count,expires) ;
115+ };
116+ virtual const char * get_signature () const {
117+ return d.get_signature ();
118+ };
119+ virtual bool is_equal_to (const std::string& rhs) {
120+ return d.is_equal_to (rhs);
121+ };
122+ };
123+
124+ std::unique_ptr<RGWFormPost::SignatureHelper> RGWFormPost::SignatureHelper::get_sig_helper (std::string_view x) {
125+ size_t pos = x.find (' :' );
126+ if (pos == x.npos || pos <= 0 ) {
127+ switch (x.length ()) {
128+ case CEPH_CRYPTO_HMACSHA1_DIGESTSIZE*2 :
129+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA1,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
130+ case CEPH_CRYPTO_HMACSHA256_DIGESTSIZE*2 :
131+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA256,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
132+ case CEPH_CRYPTO_HMACSHA512_DIGESTSIZE*2 :
133+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA512,rgw::auth::swift::SignatureFlavor::BARE_HEX>>();
134+ }
135+ return std::make_unique<BadSignatureHelper>();
136+ }
137+ std::string_view type { x.substr (0 ,pos) };
138+ if (type == " sha1" ) {
139+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA1,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
140+ } else if (type == " sha256" ) {
141+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA256,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
142+ } else if (type == " sha512" ) {
143+ return std::make_unique<SignatureHelper_x<ceph::crypto::HMACSHA512,rgw::auth::swift::SignatureFlavor::NAMED_BASE64>>();
144+ }
145+ return std::make_unique<BadSignatureHelper>();
146+ };
147+
45148int RGWListBuckets_ObjStore_SWIFT::get_params (optional_yield y)
46149{
47150 prefix = s->info .args .get (" prefix" );
@@ -2034,6 +2137,7 @@ bool RGWFormPost::is_non_expired()
20342137bool RGWFormPost::is_integral ()
20352138{
20362139 const std::string form_signature = get_part_str (ctrl_parts, " signature" );
2140+ bool r = false ;
20372141
20382142 try {
20392143 get_owner_info (s, s->user ->get_info ());
@@ -2051,28 +2155,31 @@ bool RGWFormPost::is_integral()
20512155 continue ;
20522156 }
20532157
2054- SignatureHelper sig_helper;
2055- sig_helper. calc (temp_url_key,
2158+ auto sig_helper{ RGWFormPost::SignatureHelper::get_sig_helper (form_signature) } ;
2159+ sig_helper-> calc (temp_url_key,
20562160 s->info .request_uri ,
20572161 get_part_str (ctrl_parts, " redirect" ),
20582162 get_part_str (ctrl_parts, " max_file_size" , " 0" ),
20592163 get_part_str (ctrl_parts, " max_file_count" , " 0" ),
20602164 get_part_str (ctrl_parts, " expires" , " 0" ));
20612165
2062- const auto local_sig = sig_helper.get_signature ();
2166+ const char * local_sig = sig_helper->get_signature ();
2167+ if (!local_sig) local_sig = " ???" ;
20632168
20642169 ldpp_dout (this , 20 ) << " FormPost signature [" << temp_url_key_num << " ]"
20652170 << " (calculated): " << local_sig << dendl;
20662171
2067- if (sig_helper.is_equal_to (form_signature)) {
2068- return true ;
2069- } else {
2172+ r = sig_helper->is_equal_to (form_signature);
2173+ if (!r) {
20702174 ldpp_dout (this , 5 ) << " FormPost's signature mismatch: "
20712175 << local_sig << " != " << form_signature << dendl;
20722176 }
2177+ if (r) {
2178+ break ;
2179+ }
20732180 }
20742181
2075- return false ;
2182+ return r ;
20762183}
20772184
20782185void RGWFormPost::get_owner_info (const req_state* const s,
0 commit comments