Skip to content

Commit 6f0b88a

Browse files
committed
Look at the key type to determine the expected algorithm.
It's not mandatory to have an `alg` claim in the JWKS; we can derive the information we need from the `kty` (key type) claim. The java library used by IAM does not advertise `alg`. There's potentially more work to do here if we want to support additional RSA signing algorithms.
1 parent 94f1e20 commit 6f0b88a

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/scitokens_internal.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,33 @@ Validator::get_public_key_pem(const std::string &issuer, const std::string &kid,
417417
auto key_obj = find_key_id(keys, kid);
418418

419419
auto iter = key_obj.find("alg");
420+
std::string alg;
420421
if (iter == key_obj.end() || (!iter->second.is<std::string>())) {
421-
throw JsonException("Key is missing algorithm name");
422-
}
423-
auto alg = iter->second.get<std::string>();
422+
auto iter2 = key_obj.find("kty");
423+
if (iter2 == key_obj.end() || !iter2->second.is<std::string>()) {
424+
throw JsonException("Key is missing key type");
425+
} else {
426+
auto kty = iter2->second.get<std::string>();
427+
if (kty == "RSA") {
428+
alg = "RS256";
429+
} else if (kty == "EC") {
430+
auto iter3 = key_obj.find("crv");
431+
if (iter3 == key_obj.end() || !iter3->second.is<std::string>()) {
432+
throw JsonException("EC key is missing curve name");
433+
}
434+
auto crv = iter2->second.get<std::string>();
435+
if (crv == "P-256") {
436+
alg = "EC256";
437+
} else {
438+
throw JsonException("Unsupported EC curve in public key");
439+
}
440+
} else {
441+
throw JsonException("Unknown public key type");
442+
}
443+
}
444+
} else {
445+
alg = iter->second.get<std::string>();
446+
}
424447
if (alg != "RS256" and alg != "ES256") {
425448
throw UnsupportedKeyException("Issuer is using an unsupported algorithm");
426449
}

0 commit comments

Comments
 (0)