Skip to content

Commit 94f1e20

Browse files
committed
Fix handling of base64 padding.
The contents of the public key JSON are base64 encoded using the standard JWT rules. This fixes the fact we did not internal pad the values inside the public keys, causing base64 decoding failures for specific issuers (such as our test IAM endpoint). This prevented us from verifying any SciToken from endpoints using the affected public keys.
1 parent 0a29b2f commit 94f1e20

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/scitokens_internal.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,28 @@ struct local_base64url : public jwt::alphabet::base64url {
189189
};
190190

191191

192+
// Assuming a padding, decode
193+
std::string b64url_decode_nopadding(const std::string &input)
194+
{
195+
std::string result = input;
196+
switch (result.size() % 4) {
197+
case 1:
198+
result += "="; // fallthrough
199+
case 2:
200+
result += "="; // fallthrough
201+
case 3:
202+
result += "="; // fallthrough
203+
default:
204+
break;
205+
}
206+
return jwt::base::decode<local_base64url>(result);
207+
}
208+
209+
192210
std::string
193211
es256_from_coords(const std::string &x_str, const std::string &y_str) {
194-
auto x_decode = jwt::base::decode<local_base64url>(x_str);
195-
auto y_decode = jwt::base::decode<local_base64url>(y_str);
212+
auto x_decode = b64url_decode_nopadding(x_str);
213+
auto y_decode = b64url_decode_nopadding(y_str);
196214

197215
std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)> ec(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
198216
if (!ec.get()) {
@@ -232,8 +250,8 @@ es256_from_coords(const std::string &x_str, const std::string &y_str) {
232250

233251
std::string
234252
rs256_from_coords(const std::string &e_str, const std::string &n_str) {
235-
auto e_decode = jwt::base::decode<local_base64url>(e_str);
236-
auto n_decode = jwt::base::decode<local_base64url>(n_str);
253+
auto e_decode = b64url_decode_nopadding(e_str);
254+
auto n_decode = b64url_decode_nopadding(n_str);
237255
std::unique_ptr<BIGNUM, decltype(&BN_free)> e_bignum(BN_bin2bn(reinterpret_cast<const unsigned char *>(e_decode.c_str()), e_decode.size(), nullptr), BN_free);
238256
std::unique_ptr<BIGNUM, decltype(&BN_free)> n_bignum(BN_bin2bn(reinterpret_cast<const unsigned char *>(n_decode.c_str()), n_decode.size(), nullptr), BN_free);
239257

0 commit comments

Comments
 (0)