Skip to content

Commit f2c1084

Browse files
committed
Support passing ssl library rsa keys
1 parent 3286105 commit f2c1084

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

include/jwt-cpp/jwt.h

+11
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,17 @@ namespace jwt {
14601460
} else
14611461
throw error::rsa_exception(error::rsa_error::no_key_provided);
14621462
}
1463+
/**
1464+
* Construct new rsa algorithm
1465+
*
1466+
* \param key_pair openssl EVP_PKEY structure containing RSA key pair. The private part is optional.
1467+
* \param md Pointer to hash function
1468+
* \param name Name of the algorithm
1469+
*/
1470+
rsa(helper::evp_pkey_handle key_pair, const EVP_MD* (*md)(), std::string name)
1471+
: pkey(std::move(key_pair)), md(md), alg_name(std::move(name)) {
1472+
if (!pkey) { throw error::rsa_exception(error::rsa_error::no_key_provided); }
1473+
}
14631474
/**
14641475
* Sign jwt data
14651476
* \param data The data to sign

tests/TokenTest.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ TEST(TokenTest, CreateTokenRS256) {
7171
token);
7272
}
7373

74+
TEST(TokenTest, CreateTokenEvpPkeyRS256) {
75+
auto token = jwt::create().set_issuer("auth0").set_type("JWS").sign(
76+
jwt::algorithm::rsa(jwt::helper::load_private_key_from_string(rsa_priv_key), EVP_sha256, "RS256"));
77+
78+
ASSERT_EQ(
79+
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
80+
"HAZCTQxa1Q8NppnpYV-hlqxh-X3Bb0JOePTGzjynpNZoJh2aHZD-GKpZt7OO1Zp8AFWPZ3p8Cahq8536fD8RiBES9jRsvChZvOqA7gMcFc4"
81+
"YD0iZhNIcI7a654u5yPYyTlf5kjR97prCf_OXWRn-bYY74zna4p_bP9oWCL4BkaoRcMxi-IR7kmVcCnvbYqyIrKloXP2qPO442RBGqU7Ov9"
82+
"sGQxiVqtRHKXZR9RbfvjrErY1KGiCp9M5i2bsUHadZEY44FE2jiOmx-uc2z5c05CCXqVSpfCjWbh9gQ",
83+
token);
84+
}
85+
7486
#if !defined(JWT_OPENSSL_1_0_0)
7587
TEST(TokenTest, CreateTokenRS256Encrypted) {
7688
// openssl genrsa -aes256 -out private.pem 2048
@@ -329,6 +341,21 @@ TEST(TokenTest, VerifyTokenRS256) {
329341
verify.verify(decoded_token);
330342
}
331343

344+
TEST(TokenTest, VerifyTokenEvpPkeyRS256) {
345+
std::string token =
346+
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
347+
"HAZCTQxa1Q8NppnpYV-hlqxh-X3Bb0JOePTGzjynpNZoJh2aHZD-GKpZt7OO1Zp8AFWPZ3p8Cahq8536fD8RiBES9jRsvChZvOqA7gMcFc4"
348+
"YD0iZhNIcI7a654u5yPYyTlf5kjR97prCf_OXWRn-bYY74zna4p_bP9oWCL4BkaoRcMxi-IR7kmVcCnvbYqyIrKloXP2qPO442RBGqU7Ov9"
349+
"sGQxiVqtRHKXZR9RbfvjrErY1KGiCp9M5i2bsUHadZEY44FE2jiOmx-uc2z5c05CCXqVSpfCjWbh9gQ";
350+
351+
auto verify =
352+
jwt::verify().allow_algorithm(jwt::algorithm::rsa(jwt::helper::load_private_key_from_string(rsa_priv_key), EVP_sha256, "RS256")).with_issuer("auth0");
353+
354+
auto decoded_token = jwt::decode(token);
355+
356+
verify.verify(decoded_token);
357+
}
358+
332359
TEST(TokenTest, VerifyTokenRS256PublicOnly) {
333360
std::string token =
334361
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
@@ -343,6 +370,20 @@ TEST(TokenTest, VerifyTokenRS256PublicOnly) {
343370
verify.verify(decoded_token);
344371
}
345372

373+
TEST(TokenTest, VerifyTokenEvpPkeyRS256PublicOnly) {
374+
std::string token =
375+
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
376+
"HAZCTQxa1Q8NppnpYV-hlqxh-X3Bb0JOePTGzjynpNZoJh2aHZD-GKpZt7OO1Zp8AFWPZ3p8Cahq8536fD8RiBES9jRsvChZvOqA7gMcFc4"
377+
"YD0iZhNIcI7a654u5yPYyTlf5kjR97prCf_OXWRn-bYY74zna4p_bP9oWCL4BkaoRcMxi-IR7kmVcCnvbYqyIrKloXP2qPO442RBGqU7Ov9"
378+
"sGQxiVqtRHKXZR9RbfvjrErY1KGiCp9M5i2bsUHadZEY44FE2jiOmx-uc2z5c05CCXqVSpfCjWbh9gQ";
379+
380+
auto verify = jwt::verify().allow_algorithm(jwt::algorithm::rsa(jwt::helper::load_public_key_from_string(rsa_pub_key),EVP_sha256, "RS256")).with_issuer("auth0");
381+
382+
auto decoded_token = jwt::decode(token);
383+
384+
verify.verify(decoded_token);
385+
}
386+
346387
TEST(TokenTest, VerifyTokenRS256PublicOnlyEncrypted) {
347388
// openssl genrsa -aes256 -out private.pem 2048
348389
// openssl rsa -in private.pem -pubout -out public.pem

0 commit comments

Comments
 (0)