Skip to content

Commit d278b39

Browse files
committed
add interfaces to set pkeys directly to ECDSA and PSS algorithms
1 parent 2493902 commit d278b39

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/jwt-cpp/jwt.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,9 @@ namespace jwt {
10251025
throw ecdsa_exception(error::ecdsa_error::invalid_key_size);
10261026
}
10271027

1028+
ecdsa(std::shared_ptr<EVP_PKEY> pkey, const EVP_MD* (*md)(), std::string name, size_t siglen)
1029+
: pkey(pkey), md(md), alg_name(std::move(name)), signature_length(siglen) {}
1030+
10281031
/**
10291032
* Sign jwt data
10301033
* \param data The data to sign
@@ -1384,6 +1387,9 @@ namespace jwt {
13841387
throw rsa_exception(error::rsa_error::no_key_provided);
13851388
}
13861389

1390+
pss(std::shared_ptr<EVP_PKEY> pkey, const EVP_MD* (*md)(), std::string name)
1391+
: pkey(pkey), md(md), alg_name(std::move(name)) {}
1392+
13871393
/**
13881394
* Sign jwt data
13891395
* \param data The data to sign
@@ -1595,6 +1601,8 @@ namespace jwt {
15951601
explicit es256(const std::string& public_key, const std::string& private_key = "",
15961602
const std::string& public_key_password = "", const std::string& private_key_password = "")
15971603
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha256, "ES256", 64) {}
1604+
1605+
explicit es256(std::shared_ptr<EVP_PKEY> pkey) : ecdsa(pkey, EVP_sha256, "ES256", 64) {}
15981606
};
15991607
/**
16001608
* ES384 algorithm
@@ -1612,6 +1620,8 @@ namespace jwt {
16121620
explicit es384(const std::string& public_key, const std::string& private_key = "",
16131621
const std::string& public_key_password = "", const std::string& private_key_password = "")
16141622
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha384, "ES384", 96) {}
1623+
1624+
explicit es384(std::shared_ptr<EVP_PKEY> pkey) : ecdsa(pkey, EVP_sha384, "ES384", 96) {}
16151625
};
16161626
/**
16171627
* ES512 algorithm
@@ -1629,6 +1639,8 @@ namespace jwt {
16291639
explicit es512(const std::string& public_key, const std::string& private_key = "",
16301640
const std::string& public_key_password = "", const std::string& private_key_password = "")
16311641
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha512, "ES512", 132) {}
1642+
1643+
explicit es512(std::shared_ptr<EVP_PKEY> pkey) : ecdsa(pkey, EVP_sha512, "ES512", 132) {}
16321644
};
16331645
/**
16341646
* ES256K algorithm
@@ -1645,6 +1657,8 @@ namespace jwt {
16451657
explicit es256k(const std::string& public_key, const std::string& private_key = "",
16461658
const std::string& public_key_password = "", const std::string& private_key_password = "")
16471659
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha256, "ES256K", 64) {}
1660+
1661+
explicit es256k(std::shared_ptr<EVP_PKEY> pkey) : ecdsa(pkey, EVP_sha256, "ES256K", 64) {}
16481662
};
16491663

16501664
#if !defined(JWT_OPENSSL_1_0_0) && !defined(JWT_OPENSSL_1_1_0)
@@ -1707,6 +1721,8 @@ namespace jwt {
17071721
explicit ps256(const std::string& public_key, const std::string& private_key = "",
17081722
const std::string& public_key_password = "", const std::string& private_key_password = "")
17091723
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha256, "PS256") {}
1724+
1725+
explicit ps256(std::shared_ptr<EVP_PKEY> pkey) : pss(pkey, EVP_sha256, "PS256") {}
17101726
};
17111727
/**
17121728
* PS384 algorithm
@@ -1722,6 +1738,8 @@ namespace jwt {
17221738
explicit ps384(const std::string& public_key, const std::string& private_key = "",
17231739
const std::string& public_key_password = "", const std::string& private_key_password = "")
17241740
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha384, "PS384") {}
1741+
1742+
explicit ps384(std::shared_ptr<EVP_PKEY> pkey) : pss(pkey, EVP_sha384, "PS384") {}
17251743
};
17261744
/**
17271745
* PS512 algorithm
@@ -1737,6 +1755,8 @@ namespace jwt {
17371755
explicit ps512(const std::string& public_key, const std::string& private_key = "",
17381756
const std::string& public_key_password = "", const std::string& private_key_password = "")
17391757
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha512, "PS512") {}
1758+
1759+
explicit ps512(std::shared_ptr<EVP_PKEY> pkey) : pss(pkey, EVP_sha512, "PS512") {}
17401760
};
17411761
} // namespace algorithm
17421762

@@ -3480,6 +3500,8 @@ namespace jwt {
34803500
return std::make_unique<algo<jwt::algorithm::es384>>(jwt::algorithm::es384(key.get_pkey()));
34813501
} else if (alg_name == "ES512") {
34823502
return std::make_unique<algo<jwt::algorithm::es512>>(jwt::algorithm::es512(key.get_pkey()));
3503+
} else if (alg_name == "ES256K") {
3504+
return std::make_unique<algo<jwt::algorithm::es256k>>(jwt::algorithm::es256k(key.get_pkey()));
34833505
} else if (alg_name == "HS256") {
34843506
return std::make_unique<algo<jwt::algorithm::hs256>>(jwt::algorithm::hs256(key.get_oct_key()));
34853507
} else if (alg_name == "HS384") {

0 commit comments

Comments
 (0)