Skip to content

Commit e37e259

Browse files
committed
Support passing public keys too for ssl library key handles
1 parent 174a8e2 commit e37e259

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

include/jwt-cpp/jwt.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -1570,11 +1570,9 @@ namespace jwt {
15701570
throw error::ecdsa_exception(error::ecdsa_error::invalid_key_size);
15711571
}
15721572

1573-
ecdsa(helper::evp_pkey_handle private_key, const EVP_MD* (*md)(), std::string name, size_t siglen)
1574-
: pkey(std::move(private_key)), md(md), alg_name(std::move(name)), signature_length(siglen) {
1575-
if (pkey) {
1576-
check_private_key(pkey.get());
1577-
} else {
1573+
ecdsa(helper::evp_pkey_handle key_pair, const EVP_MD* (*md)(), std::string name, size_t siglen)
1574+
: pkey(std::move(key_pair)), md(md), alg_name(std::move(name)), signature_length(siglen) {
1575+
if (!pkey) {
15781576
throw error::ecdsa_exception(error::ecdsa_error::no_key_provided);
15791577
}
15801578
size_t keysize = EVP_PKEY_bits(pkey.get());
@@ -1773,6 +1771,16 @@ namespace jwt {
17731771
const size_t signature_length;
17741772
};
17751773

1774+
// enum class ecdsa_algorithm { es384 };
1775+
//
1776+
// struct ecdsa_algorithm_builder {
1777+
// ecdsa_algorithm_builder(ecdsa_algorithm algorithm) {}
1778+
//
1779+
// ecdsa build() { return ecdsa(); }
1780+
//
1781+
// private:
1782+
// };
1783+
17761784
#if !defined(JWT_OPENSSL_1_0_0) && !defined(JWT_OPENSSL_1_1_0)
17771785
/**
17781786
* \brief Base class for EdDSA family of algorithms

tests/TokenTest.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,28 @@ TEST(TokenTest, CreateTokenES256) {
175175
ASSERT_NO_THROW(jwt::verify().allow_algorithm(jwt::algorithm::es256(ecdsa256_pub_key, "", "", "")).verify(decoded));
176176
}
177177

178+
TEST(TokenTest, CreateTokenEvpPkeyES256) {
179+
180+
auto token = jwt::create().set_issuer("auth0").set_type("JWS").sign(jwt::algorithm::ecdsa(
181+
jwt::helper::load_private_ec_key_from_string(ecdsa256_priv_key), EVP_sha256, "ES256", 64));
182+
183+
auto decoded = jwt::decode(token);
184+
185+
ASSERT_THROW(
186+
jwt::verify().allow_algorithm(jwt::algorithm::es256(ecdsa256_pub_key_invalid, "", "", "")).verify(decoded),
187+
jwt::error::signature_verification_exception);
188+
ASSERT_NO_THROW(jwt::verify().allow_algorithm(jwt::algorithm::es256(ecdsa256_pub_key, "", "", "")).verify(decoded));
189+
}
190+
191+
TEST(TokenTest, CreateTokenEvpPkeyES256NoPrivate) {
192+
ASSERT_THROW(
193+
[]() {
194+
auto token = jwt::create().set_issuer("auth0").set_type("JWS").sign(jwt::algorithm::ecdsa(
195+
jwt::helper::load_public_ec_key_from_string(ecdsa256_pub_key), EVP_sha256, "ES256", 64));
196+
}(),
197+
jwt::error::signature_generation_exception);
198+
}
199+
178200
TEST(TokenTest, CreateTokenES256NoPrivate) {
179201
ASSERT_THROW(
180202
[]() {
@@ -548,6 +570,17 @@ TEST(TokenTest, VerifyTokenES256FailNoKey) {
548570
jwt::error::ecdsa_exception);
549571
}
550572

573+
TEST(TokenTest, VerifyTokenEvpPkeyES256FailNoKey) {
574+
ASSERT_THROW(
575+
[]() {
576+
auto verify = jwt::verify()
577+
.allow_algorithm(
578+
jwt::algorithm::ecdsa(jwt::helper::evp_pkey_handle{nullptr}, EVP_sha256, "ES256", 64))
579+
.with_issuer("auth0");
580+
}(),
581+
jwt::error::ecdsa_exception);
582+
}
583+
551584
TEST(TokenTest, VerifyTokenES256) {
552585
const std::string token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_"
553586
"4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
@@ -558,6 +591,17 @@ TEST(TokenTest, VerifyTokenES256) {
558591
verify.verify(decoded_token);
559592
}
560593

594+
TEST(TokenTest, VerifyTokenEvpPkeyES256) {
595+
const std::string token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_"
596+
"4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
597+
598+
auto verify = jwt::verify().allow_algorithm(
599+
jwt::algorithm::ecdsa(jwt::helper::load_public_ec_key_from_string(ecdsa256_pub_key), EVP_sha256, "ES256", 64));
600+
auto decoded_token = jwt::decode(token);
601+
602+
verify.verify(decoded_token);
603+
}
604+
561605
TEST(TokenTest, VerifyTokenES256Fail) {
562606
const std::string token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_"
563607
"4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";

0 commit comments

Comments
 (0)