Skip to content

Commit 4f2c6f4

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

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-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

+46
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,23 @@ 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 = jwt::verify()
352+
.allow_algorithm(jwt::algorithm::rsa(jwt::helper::load_private_key_from_string(rsa_priv_key),
353+
EVP_sha256, "RS256"))
354+
.with_issuer("auth0");
355+
356+
auto decoded_token = jwt::decode(token);
357+
358+
verify.verify(decoded_token);
359+
}
360+
332361
TEST(TokenTest, VerifyTokenRS256PublicOnly) {
333362
std::string token =
334363
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
@@ -343,6 +372,23 @@ TEST(TokenTest, VerifyTokenRS256PublicOnly) {
343372
verify.verify(decoded_token);
344373
}
345374

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

0 commit comments

Comments
 (0)