mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
Added EdDSA support (#95)
* Fixed a build error `jwt.h` previously depended on the nlohmann JSON library's inclusion of `iso646.h`, which defines the `not` operator. * Added EdDSA support https://github.com/Thalhammer/jwt-cpp/issues/79 Disabled EdDSA support when building with OpenSSL <1.1.1. Support for EdDSA came with OpenSSL version 1.1.1, incl. API extensions. * Fixed build generation for Microsoft Visual Studio * Fixed MSVC build warnings "hides previous local declaration" * Fixed narrowing warnings when building with OpenSSL 1.0.2 * Compacted the string buffer initialization code pattern As per review comment: https://github.com/Thalhammer/jwt-cpp/pull/95#discussion_r462985506 * adding eddsa openssl tests * adding on mock openssl call as a test * adding more mock openssl call to test failures Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
This commit is contained in:
@@ -11,7 +11,7 @@ A header only library for creating and validating [JSON Web Tokens](https://tool
|
||||
|
||||
## Signature algorithms
|
||||
|
||||
As of version 0.2.0 jwt-cpp supports all algorithms defined by the spec. The modular design of jwt-cpp allows one to add additional algorithms without any problems. If you need any feel free to open a pull request.
|
||||
jwt-cpp supports all algorithms defined by the spec. The modular design of jwt-cpp allows one to add additional algorithms without any problems. If you need any feel free to open a pull request.
|
||||
For the sake of completeness, here is a list of all supported algorithms:
|
||||
|
||||
* HS256
|
||||
@@ -26,6 +26,7 @@ For the sake of completeness, here is a list of all supported algorithms:
|
||||
* PS256
|
||||
* PS384
|
||||
* PS512
|
||||
* EdDSA (Ed25519, Ed448), since 0.5.0
|
||||
|
||||
## Overview
|
||||
|
||||
|
||||
@@ -39,6 +39,11 @@
|
||||
#define OPENSSL10
|
||||
#endif
|
||||
|
||||
//If openssl version less than 1.1.1
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10101000L
|
||||
#define OPENSSL110
|
||||
#endif
|
||||
|
||||
#ifndef JWT_CLAIM_EXPLICIT
|
||||
#define JWT_CLAIM_EXPLICIT explicit
|
||||
#endif
|
||||
@@ -342,7 +347,7 @@ namespace jwt {
|
||||
std::string extract_pubkey_from_cert(const std::string& certstr, const std::string& pw, std::error_code& ec) {
|
||||
ec.clear();
|
||||
#if OPENSSL_VERSION_NUMBER <= 0x10100003L
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> certbio(BIO_new_mem_buf(const_cast<char*>(certstr.data()), certstr.size()), BIO_free_all);
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> certbio(BIO_new_mem_buf(const_cast<char*>(certstr.data()), static_cast<int>(certstr.size())), BIO_free_all);
|
||||
#else
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> certbio(BIO_new_mem_buf(certstr.data(), static_cast<int>(certstr.size())), BIO_free_all);
|
||||
#endif
|
||||
@@ -503,8 +508,7 @@ namespace jwt {
|
||||
static std::string bn2raw(const BIGNUM* bn)
|
||||
#endif
|
||||
{
|
||||
std::string res;
|
||||
res.resize(BN_num_bytes(bn));
|
||||
std::string res(BN_num_bytes(bn), '\0');
|
||||
BN_bn2bin(bn, (unsigned char*)res.data()); // NOLINT(google-readability-casting) requires `const_cast`
|
||||
return res;
|
||||
}
|
||||
@@ -580,8 +584,7 @@ namespace jwt {
|
||||
*/
|
||||
std::string sign(const std::string& data, std::error_code& ec) const {
|
||||
ec.clear();
|
||||
std::string res;
|
||||
res.resize(static_cast<size_t>(EVP_MAX_MD_SIZE));
|
||||
std::string res(static_cast<size_t>(EVP_MAX_MD_SIZE), '\0');
|
||||
auto len = static_cast<unsigned int>(res.size());
|
||||
if (HMAC(md(), secret.data(), static_cast<int>(secret.size()), reinterpret_cast<const unsigned char*>(data.data()), static_cast<int>(data.size()), (unsigned char*)res.data(), &len) == nullptr) { // NOLINT(google-readability-casting) requires `const_cast`
|
||||
ec = error::signature_generation_error::hmac_failed;
|
||||
@@ -671,8 +674,7 @@ namespace jwt {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string res;
|
||||
res.resize(EVP_PKEY_size(pkey.get()));
|
||||
std::string res(EVP_PKEY_size(pkey.get()), '\0');
|
||||
unsigned int len = 0;
|
||||
|
||||
if (!EVP_SignUpdate(ctx.get(), data.data(), data.size())){
|
||||
@@ -845,7 +847,7 @@ namespace jwt {
|
||||
sig.r = r.get();
|
||||
sig.s = s.get();
|
||||
|
||||
if(ECDSA_do_verify((const unsigned char*)hash.data(), hash.size(), &sig, pkey.get()) != 1) {
|
||||
if(ECDSA_do_verify((const unsigned char*)hash.data(), static_cast<int>(hash.size()), &sig, pkey.get()) != 1) {
|
||||
ec = error::signature_verification_error::invalid_signature;
|
||||
return;
|
||||
}
|
||||
@@ -896,8 +898,7 @@ namespace jwt {
|
||||
return {};
|
||||
}
|
||||
unsigned int len = 0;
|
||||
std::string res;
|
||||
res.resize(EVP_MD_CTX_size(ctx.get()));
|
||||
std::string res(EVP_MD_CTX_size(ctx.get()), '\0');
|
||||
if(EVP_DigestFinal(ctx.get(), (unsigned char*)res.data(), &len) == 0) { // NOLINT(google-readability-casting) requires `const_cast`
|
||||
ec = error::signature_generation_error::digestfinal_failed;
|
||||
return {};
|
||||
@@ -916,6 +917,108 @@ namespace jwt {
|
||||
const size_t signature_length;
|
||||
};
|
||||
|
||||
#ifndef OPENSSL110
|
||||
/**
|
||||
* \brief Base class for EdDSA family of algorithms
|
||||
*
|
||||
* The EdDSA algorithms were introduced in [OpenSSL v1.1.1](https://www.openssl.org/news/openssl-1.1.1-notes.html),
|
||||
* so these algorithms are only available when building against this version or higher.
|
||||
*/
|
||||
struct eddsa {
|
||||
/**
|
||||
* Construct new eddsa algorithm
|
||||
* \param public_key EdDSA public key in PEM format
|
||||
* \param private_key EdDSA private key or empty string if not available. If empty, signing will always fail.
|
||||
* \param public_key_password Password to decrypt public key pem.
|
||||
* \param private_key_password Password to decrypt private key pem.
|
||||
* \param md Pointer to hash function
|
||||
* \param name Name of the algorithm
|
||||
*/
|
||||
eddsa(const std::string& public_key, const std::string& private_key, const std::string& public_key_password, const std::string& private_key_password, const EVP_MD* (*md)(), std::string name)
|
||||
: md(md), alg_name(std::move(name))
|
||||
{
|
||||
if (!private_key.empty()) {
|
||||
pkey = helper::load_private_key_from_string(private_key, private_key_password);
|
||||
}
|
||||
else if (!public_key.empty()) {
|
||||
pkey = helper::load_public_key_from_string(public_key, public_key_password);
|
||||
}
|
||||
else
|
||||
throw ecdsa_exception(error::ecdsa_error::load_key_bio_read);
|
||||
}
|
||||
/**
|
||||
* Sign jwt data
|
||||
* \param data The data to sign
|
||||
* \param ec error_code filled with details on error
|
||||
* \return EdDSA signature for the given data
|
||||
*/
|
||||
std::string sign(const std::string& data, std::error_code& ec) const {
|
||||
ec.clear();
|
||||
std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> ctx(EVP_MD_CTX_create(), EVP_MD_CTX_free);
|
||||
if (!ctx) {
|
||||
ec = error::signature_generation_error::create_context_failed;
|
||||
return {};
|
||||
}
|
||||
if (!EVP_DigestSignInit(ctx.get(), nullptr, nullptr, nullptr, pkey.get())) {
|
||||
ec = error::signature_generation_error::signinit_failed;
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t len = EVP_PKEY_size(pkey.get());
|
||||
std::string res(len, '\0');
|
||||
|
||||
if (EVP_DigestSign(ctx.get(),
|
||||
reinterpret_cast<unsigned char*>(&res[0]), &len,
|
||||
reinterpret_cast<const unsigned char*>(data.data()), data.size()) != 1) {
|
||||
ec = error::signature_generation_error::signfinal_failed;
|
||||
return {};
|
||||
}
|
||||
|
||||
res.resize(len);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if signature is valid
|
||||
* \param data The data to check signature against
|
||||
* \param signature Signature provided by the jwt
|
||||
* \param ec Filled with details on error
|
||||
*/
|
||||
void verify(const std::string& data, const std::string& signature, std::error_code& ec) const {
|
||||
ec.clear();
|
||||
std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> ctx(EVP_MD_CTX_create(), EVP_MD_CTX_free);
|
||||
if (!ctx) {
|
||||
ec = error::signature_verification_error::create_context_failed;
|
||||
return;
|
||||
}
|
||||
if (!EVP_DigestVerifyInit(ctx.get(), nullptr, nullptr, nullptr, pkey.get())) {
|
||||
ec = error::signature_verification_error::verifyinit_failed;
|
||||
return;
|
||||
}
|
||||
auto res = EVP_DigestVerify(ctx.get(),
|
||||
reinterpret_cast<const unsigned char*>(signature.data()), signature.size(),
|
||||
reinterpret_cast<const unsigned char*>(data.data()), data.size());
|
||||
if (res != 1) {
|
||||
ec = error::signature_verification_error::verifyfinal_failed;
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the algorithm name provided to the constructor
|
||||
* \return algorithm's name
|
||||
*/
|
||||
std::string name() const {
|
||||
return alg_name;
|
||||
}
|
||||
private:
|
||||
/// OpenSSL struct containing keys
|
||||
std::shared_ptr<EVP_PKEY> pkey;
|
||||
/// Hash generator
|
||||
const EVP_MD* (*md)();
|
||||
/// algorithm's name
|
||||
const std::string alg_name;
|
||||
};
|
||||
#endif
|
||||
/**
|
||||
* \brief Base class for PSS-RSA family of algorithms
|
||||
*/
|
||||
@@ -1033,8 +1136,7 @@ namespace jwt {
|
||||
return {};
|
||||
}
|
||||
unsigned int len = 0;
|
||||
std::string res;
|
||||
res.resize(EVP_MD_CTX_size(ctx.get()));
|
||||
std::string res(EVP_MD_CTX_size(ctx.get()), '\0');
|
||||
if(EVP_DigestFinal(ctx.get(), (unsigned char*)res.data(), &len) == 0) { // NOLINT(google-readability-casting) requires `const_cast`
|
||||
ec = error::signature_generation_error::digestfinal_failed;
|
||||
return {};
|
||||
@@ -1178,6 +1280,44 @@ namespace jwt {
|
||||
{}
|
||||
};
|
||||
|
||||
#ifndef OPENSSL110
|
||||
/**
|
||||
* Ed25519 algorithm
|
||||
*
|
||||
* Requires at least OpenSSL 1.1.1.
|
||||
*/
|
||||
struct ed25519 : public eddsa {
|
||||
/**
|
||||
* Construct new instance of algorithm
|
||||
* \param public_key Ed25519 public key in PEM format
|
||||
* \param private_key Ed25519 private key or empty string if not available. If empty, signing will always fail.
|
||||
* \param public_key_password Password to decrypt public key pem.
|
||||
* \param private_key_password Password to decrypt private key pem.
|
||||
*/
|
||||
explicit ed25519(const std::string& public_key, const std::string& private_key = "", const std::string& public_key_password = "", const std::string& private_key_password = "")
|
||||
: eddsa(public_key, private_key, public_key_password, private_key_password, EVP_sha512, "EdDSA")
|
||||
{}
|
||||
};
|
||||
|
||||
/**
|
||||
* Ed448 algorithm
|
||||
*
|
||||
* Requires at least OpenSSL 1.1.1.
|
||||
*/
|
||||
struct ed448 : public eddsa {
|
||||
/**
|
||||
* Construct new instance of algorithm
|
||||
* \param public_key Ed448 public key in PEM format
|
||||
* \param private_key Ed448 private key or empty string if not available. If empty, signing will always fail.
|
||||
* \param public_key_password Password to decrypt public key pem.
|
||||
* \param private_key_password Password to decrypt private key pem.
|
||||
*/
|
||||
explicit ed448(const std::string& public_key, const std::string& private_key = "", const std::string& public_key_password = "", const std::string& private_key_password = "")
|
||||
: eddsa(public_key, private_key, public_key_password, private_key_password, EVP_shake256, "EdDSA")
|
||||
{}
|
||||
};
|
||||
#endif
|
||||
|
||||
/**
|
||||
* PS256 algorithm
|
||||
*/
|
||||
|
||||
@@ -33,8 +33,13 @@ target_compile_options(
|
||||
-Wno-c++98-compat
|
||||
-Wno-global-constructors
|
||||
-Wno-weak-vtables>)
|
||||
target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp::jwt-cpp GTest::GTest
|
||||
GTest::Main pthread dl)
|
||||
|
||||
# NOTE: Don't use space inside a generator expression here, because the function
|
||||
# prematurely breaks the expression into multiple lines.
|
||||
target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp::jwt-cpp
|
||||
GTest::GTest GTest::Main
|
||||
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:pthread>
|
||||
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${CMAKE_DL_LIBS}> )
|
||||
|
||||
gtest_add_tests(TARGET jwt-cpp-test)
|
||||
|
||||
|
||||
@@ -138,6 +138,38 @@ VR0TAQH/BAUwAwEB/zAKBggqhkjOPQQDAgNIADBFAiEAv2rcDBD4p/UeJIEFOK9j
|
||||
o4XKKbXs6oqMpkEDUinf6DACIGPcShH4m5haelZleORWM068C3BSVToRMSAivE79
|
||||
8ZhX
|
||||
-----END CERTIFICATE-----)";
|
||||
std::string ed25519_priv_key = R"(-----BEGIN PRIVATE KEY-----
|
||||
MC4CAQAwBQYDK2VwBCIEIP7pYCi8Xipwjttw6ehj0PNrsg3K1Vc5RBqAG5JePqsm
|
||||
-----END PRIVATE KEY-----)";
|
||||
std::string ed25519_pub_key = R"(-----BEGIN PUBLIC KEY-----
|
||||
MCowBQYDK2VwAyEAJHsGwkQLdwqaqp2Ar39YtLCVHiWgUPcGwQfes2cUOEc=
|
||||
-----END PUBLIC KEY-----)";
|
||||
std::string ed25519_pub_key_invalid = R"(-----BEGIN PUBLIC KEY-----
|
||||
MCowBQYDK2VwAyEAJHsGwkQLdwqaqp2Ar39YtLCVHIwGupcGwQfes2cUOEc=
|
||||
-----END PUBLIC KEY-----)";
|
||||
std::string ed25519_certificate = R"(-----BEGIN CERTIFICATE-----
|
||||
MIIBjzCCAUECFCQlWQxMEMe4c3OOimH4/y+o/HpfMAUGAytlcDBqMQswCQYDVQQG
|
||||
EwJDQTEPMA0GA1UECAwGUXVlYmVjMREwDwYDVQQHDAhNb250cmVhbDEQMA4GA1UE
|
||||
CgwHand0LWNwcDEPMA0GA1UECwwGZ2l0aHViMRQwEgYDVQQDDAtleGFtcGxlLmNv
|
||||
bTAeFw0yMDA3MzAyMTIwMDBaFw0yMjA2MzAyMTIwMDBaMGoxCzAJBgNVBAYTAkNB
|
||||
MQ8wDQYDVQQIDAZRdWViZWMxETAPBgNVBAcMCE1vbnRyZWFsMRAwDgYDVQQKDAdq
|
||||
d3QtY3BwMQ8wDQYDVQQLDAZnaXRodWIxFDASBgNVBAMMC2V4YW1wbGUuY29tMCow
|
||||
BQYDK2VwAyEAUdLe1SUWxc/95f39pfmuwe1SLHpFXf5gcRQlMH2sjgwwBQYDK2Vw
|
||||
A0EAezYcLIUnyy86uUnAZdAMPn7wTruNKtG36GrTF3PF4dtdoGF1OV5DLnNK0Hbs
|
||||
3GyYtaZs6AEHwDXl/INXu2zoCQ==
|
||||
-----END CERTIFICATE-----)";
|
||||
std::string ed448_priv_key = R"(-----BEGIN PRIVATE KEY-----
|
||||
MEcCAQAwBQYDK2VxBDsEOZNyV4kIWehIWSsPCnDEZbBF+g2WoUgUwox8eQJTq8Hz
|
||||
y4okU+JZAV8RqQ270fJL/Safvvc1SbbF1A==
|
||||
-----END PRIVATE KEY-----)";
|
||||
std::string ed448_pub_key = R"(-----BEGIN PUBLIC KEY-----
|
||||
MEMwBQYDK2VxAzoAQomzAH8FXc0rhznaLR9DYwe5+DNhP0UwpZzS5y6iJ/iirrzI
|
||||
9/Fjbc/UvFxwFKFw8IJ9QUwFsE8A
|
||||
-----END PUBLIC KEY-----)";
|
||||
std::string ed448_pub_key_invalid = R"(-----BEGIN PUBLIC KEY-----
|
||||
MEMwBQYDK2VxAzoAQomzAH8FXc0rhznaLR9DYwe5+DNhP0UwpZzS5y6iJ/iirrzI
|
||||
9/Fjbc/UvFxwFKFW7ij8QUwFsE8A
|
||||
-----END PUBLIC KEY-----)";
|
||||
std::string sample_cert = R"(-----BEGIN CERTIFICATE-----
|
||||
MIIDHDCCAgSgAwIBAgIIGlbUz5cvweUwDQYJKoZIhvcNAQEFBQAwMTEvMC0GA1UE
|
||||
AxMmc2VjdXJldG9rZW4uc3lzdGVtLmdzZXJ2aWNlYWNjb3VudC5jb20wHhcNMTkw
|
||||
|
||||
@@ -33,6 +33,10 @@ static uint64_t fail_EC_KEY_check_key = 0;
|
||||
static uint64_t fail_ECDSA_SIG_new = 0;
|
||||
static uint64_t fail_ECDSA_do_sign = 0;
|
||||
static uint64_t fail_EVP_PKEY_get1_RSA = 0;
|
||||
static uint64_t fail_EVP_DigestSignInit = 0;
|
||||
static uint64_t fail_EVP_DigestSign = 0;
|
||||
static uint64_t fail_EVP_DigestVerifyInit = 0;
|
||||
static uint64_t fail_EVP_DigestVerify = 0;
|
||||
|
||||
BIO* BIO_new(const BIO_METHOD *type) {
|
||||
static BIO*(*origMethod)(const BIO_METHOD*) = nullptr;
|
||||
@@ -247,6 +251,63 @@ struct rsa_st *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
|
||||
if(fail) return nullptr;
|
||||
else return origMethod(pkey);
|
||||
}
|
||||
|
||||
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e,
|
||||
EVP_PKEY *pkey) {
|
||||
static int(*origMethod)(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e,
|
||||
EVP_PKEY *pkey) = nullptr;
|
||||
if (origMethod == nullptr)
|
||||
origMethod = (decltype(origMethod))dlsym(RTLD_NEXT, "EVP_DigestSignInit");
|
||||
bool fail = fail_EVP_DigestSignInit & 1;
|
||||
fail_EVP_DigestSignInit = fail_EVP_DigestSignInit >> 1;
|
||||
if(fail) return 0;
|
||||
else return origMethod(ctx,pctx, type, e,pkey);
|
||||
}
|
||||
|
||||
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
size_t *siglen, const unsigned char *tbs,
|
||||
size_t tbslen) {
|
||||
static int(*origMethod)(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
size_t *siglen, const unsigned char *tbs,
|
||||
size_t tbslen) = nullptr;
|
||||
if (origMethod == nullptr)
|
||||
origMethod = (decltype(origMethod))dlsym(RTLD_NEXT, "EVP_DigestSign");
|
||||
bool fail = fail_EVP_DigestSign & 1;
|
||||
fail_EVP_DigestSign = fail_EVP_DigestSign >> 1;
|
||||
if(fail) return 0;
|
||||
else return origMethod(ctx, sigret, siglen, tbs, tbslen);
|
||||
}
|
||||
|
||||
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e,
|
||||
EVP_PKEY *pkey) {
|
||||
static int(*origMethod)(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e,
|
||||
EVP_PKEY *pkey) = nullptr;
|
||||
if (origMethod == nullptr)
|
||||
origMethod = (decltype(origMethod))dlsym(RTLD_NEXT, "EVP_DigestVerifyInit");
|
||||
bool fail = fail_EVP_DigestVerifyInit & 1;
|
||||
fail_EVP_DigestVerifyInit = fail_EVP_DigestVerifyInit >> 1;
|
||||
if(fail) return 0;
|
||||
else return origMethod(ctx,pctx, type, e,pkey);
|
||||
}
|
||||
|
||||
int EVP_DigestVerify(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
size_t *siglen, const unsigned char *tbs,
|
||||
size_t tbslen) {
|
||||
static int(*origMethod)(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
size_t *siglen, const unsigned char *tbs,
|
||||
size_t tbslen) = nullptr;
|
||||
if (origMethod == nullptr)
|
||||
origMethod = (decltype(origMethod))dlsym(RTLD_NEXT, "EVP_DigestVerify");
|
||||
bool fail = fail_EVP_DigestVerify & 1;
|
||||
fail_EVP_DigestVerify = fail_EVP_DigestVerify >> 1;
|
||||
if(fail) return 0;
|
||||
else return origMethod(ctx, sigret, siglen, tbs, tbslen);
|
||||
}
|
||||
|
||||
/**
|
||||
* =========== End of black magic ============
|
||||
*/
|
||||
@@ -270,6 +331,13 @@ inline namespace test_keys {
|
||||
extern std::string ecdsa521_pub_key_invalid;
|
||||
extern std::string sample_cert;
|
||||
extern std::string sample_cert_pubkey;
|
||||
extern std::string ed25519_priv_key;
|
||||
extern std::string ed25519_pub_key;
|
||||
extern std::string ed25519_pub_key_invalid;
|
||||
extern std::string ed25519_certificate;
|
||||
extern std::string ed448_priv_key;
|
||||
extern std::string ed448_pub_key;
|
||||
extern std::string ed448_pub_key_invalid;
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, ExtractPubkeyFromCertReference) {
|
||||
@@ -507,10 +575,6 @@ TEST(OpenSSLErrorTest, RS256VerifyErrorCode) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, ECDSAKeyReference) {
|
||||
jwt::algorithm::es256 alg{ecdsa256_pub_key, ecdsa256_priv_key};
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, ECDSAKey) {
|
||||
std::vector<multitest_entry> mapping = {
|
||||
{ &fail_BIO_new, 1, jwt::error::ecdsa_error::create_mem_bio_failed },
|
||||
@@ -560,7 +624,6 @@ TEST(OpenSSLErrorTest, ES256Reference) {
|
||||
jwt::algorithm::es256 alg{ecdsa256_pub_key,ecdsa256_priv_key};
|
||||
std::error_code ec;
|
||||
auto res = alg.sign("testdata", ec);
|
||||
//ASSERT_EQ(jwt::base::encode<jwt::alphabet::base64>(res), "aC/NqyHfPw5FDA0yRAnrbkrAlXjsr0obRkCg/HgP+77QYJrAg6YKkKoJwMXjUX8fQrxXKTN7em5L9dtmOep37Q==");
|
||||
ASSERT_FALSE(!(!ec));
|
||||
|
||||
alg.verify("testdata", res, ec);
|
||||
@@ -645,5 +708,90 @@ TEST(OpenSSLErrorTest, PS256VerifyErrorCode) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, EdDSAKey) {
|
||||
std::vector<multitest_entry> mapping = {
|
||||
// load_private_key_from_string
|
||||
{ &fail_BIO_new, 1, jwt::error::rsa_error::create_mem_bio_failed },
|
||||
{ &fail_BIO_write, 1, jwt::error::rsa_error::load_key_bio_write },
|
||||
{ &fail_PEM_read_bio_PrivateKey, 1, jwt::error::rsa_error::load_key_bio_read },
|
||||
// load_public_key_from_string
|
||||
{ &fail_BIO_new, 1, jwt::error::rsa_error::create_mem_bio_failed },
|
||||
{ &fail_BIO_write, 1, jwt::error::rsa_error::load_key_bio_write },
|
||||
// { &fail_PEM_read_bio_PUBKEY, 1, jwt::error::rsa_error::load_key_bio_read }
|
||||
};
|
||||
|
||||
run_multitest(mapping, [](std::error_code& ec) {
|
||||
try {
|
||||
jwt::algorithm::ed25519 alg{ed25519_pub_key, ed25519_priv_key};
|
||||
FAIL(); // Should never reach this
|
||||
} catch(const std::system_error& e) {
|
||||
ec = e.code();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, EdDSACertificate) {
|
||||
std::vector<multitest_entry> mapping = {
|
||||
// load_public_key_from_string
|
||||
{ &fail_BIO_new, 1, jwt::error::rsa_error::create_mem_bio_failed },
|
||||
{ &fail_BIO_write, 1, jwt::error::rsa_error::load_key_bio_write },
|
||||
{ &fail_PEM_read_bio_PUBKEY, 1, jwt::error::rsa_error::load_key_bio_read },
|
||||
// extract_pubkey_from_cert
|
||||
{ &fail_BIO_new, 2, jwt::error::rsa_error::create_mem_bio_failed },
|
||||
{ &fail_PEM_read_bio_X509, 1, jwt::error::rsa_error::cert_load_failed },
|
||||
{ &fail_X509_get_pubkey, 1, jwt::error::rsa_error::get_key_failed },
|
||||
{ &fail_PEM_write_bio_PUBKEY, 1, jwt::error::rsa_error::write_key_failed },
|
||||
{ &fail_BIO_ctrl, 1, jwt::error::rsa_error::convert_to_pem_failed }
|
||||
};
|
||||
|
||||
run_multitest(mapping, [](std::error_code& ec) {
|
||||
try {
|
||||
jwt::algorithm::ed25519 alg{ed25519_certificate};
|
||||
FAIL(); // Should never reach this
|
||||
} catch(const std::system_error& e) {
|
||||
ec = e.code();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, Ed25519Reference) {
|
||||
jwt::algorithm::ed25519 alg{ed25519_pub_key, ed25519_priv_key};
|
||||
std::error_code ec;
|
||||
auto res = alg.sign("testdata", ec);
|
||||
ASSERT_FALSE(!(!ec));
|
||||
|
||||
alg.verify("testdata", res, ec);
|
||||
ASSERT_FALSE(!(!ec));
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, Ed25519SignErrorCode) {
|
||||
jwt::algorithm::ed25519 alg{ed25519_pub_key, ed25519_priv_key};
|
||||
std::vector<multitest_entry> mapping = {
|
||||
{ &fail_EVP_MD_CTX_new, 1, jwt::error::signature_generation_error::create_context_failed },
|
||||
{ &fail_EVP_DigestSignInit, 1, jwt::error::signature_generation_error::signinit_failed },
|
||||
{ &fail_EVP_DigestSign, 1, jwt::error::signature_generation_error::signfinal_failed }
|
||||
};
|
||||
|
||||
run_multitest(mapping, [&alg](std::error_code& ec){
|
||||
auto res = alg.sign("testdata", ec);
|
||||
ASSERT_EQ(res, "");
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OpenSSLErrorTest, Ed25519VerifyErrorCode) {
|
||||
jwt::algorithm::ed25519 alg{ed25519_pub_key, ed25519_priv_key};
|
||||
auto signature = jwt::base::decode<jwt::alphabet::base64>(
|
||||
"aC/NqyHfPw5FDA0yRAnrbkrAlXjsr0obRkCg/HgP+77QYJrAg6YKkKoJwMXjUX8fQrxXKTN7em5L9dtmOep37Q==");
|
||||
std::vector<multitest_entry> mapping = {
|
||||
{ &fail_EVP_MD_CTX_new, 1, jwt::error::signature_verification_error::create_context_failed },
|
||||
{ &fail_EVP_DigestVerifyInit, 1, jwt::error::signature_verification_error::verifyinit_failed },
|
||||
{ &fail_EVP_DigestVerify, 1, jwt::error::signature_verification_error::verifyfinal_failed }
|
||||
};
|
||||
|
||||
run_multitest(mapping, [&alg, &signature](std::error_code& ec){
|
||||
alg.verify("testdata", signature, ec);
|
||||
});
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -18,6 +18,12 @@ inline namespace test_keys {
|
||||
extern std::string ecdsa521_priv_key;
|
||||
extern std::string ecdsa521_pub_key;
|
||||
extern std::string ecdsa521_pub_key_invalid;
|
||||
extern std::string ed25519_priv_key;
|
||||
extern std::string ed25519_pub_key;
|
||||
extern std::string ed25519_pub_key_invalid;
|
||||
extern std::string ed448_priv_key;
|
||||
extern std::string ed448_pub_key;
|
||||
extern std::string ed448_pub_key_invalid;
|
||||
}
|
||||
|
||||
TEST(TokenTest, DecodeToken) {
|
||||
@@ -181,6 +187,34 @@ TEST(TokenTest, CreateTokenES512NoPrivate) {
|
||||
}(), jwt::signature_generation_exception);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL110
|
||||
TEST(TokenTest, CreateTokenEd25519) {
|
||||
|
||||
auto token = jwt::create()
|
||||
.set_issuer("auth0")
|
||||
.set_type("JWS")
|
||||
.sign(jwt::algorithm::ed25519("", ed25519_priv_key, "", ""));
|
||||
|
||||
auto decoded = jwt::decode(token);
|
||||
|
||||
ASSERT_THROW(jwt::verify().allow_algorithm(jwt::algorithm::ed25519(ed25519_pub_key_invalid, "", "", "")).verify(decoded), jwt::signature_verification_exception);
|
||||
ASSERT_NO_THROW(jwt::verify().allow_algorithm(jwt::algorithm::ed25519(ed25519_pub_key, "", "", "")).verify(decoded));
|
||||
}
|
||||
|
||||
TEST(TokenTest, CreateTokenEd448) {
|
||||
|
||||
auto token = jwt::create()
|
||||
.set_issuer("auth0")
|
||||
.set_type("JWS")
|
||||
.sign(jwt::algorithm::ed448("", ed448_priv_key, "", ""));
|
||||
|
||||
auto decoded = jwt::decode(token);
|
||||
|
||||
ASSERT_THROW(jwt::verify().allow_algorithm(jwt::algorithm::ed448(ed448_pub_key_invalid, "", "", "")).verify(decoded), jwt::signature_verification_exception);
|
||||
ASSERT_NO_THROW(jwt::verify().allow_algorithm(jwt::algorithm::ed448(ed448_pub_key, "", "", "")).verify(decoded));
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(TokenTest, VerifyTokenWrongAlgorithm) {
|
||||
std::string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.VA2i1ui1cnoD6I3wnji1WAVCf29EekysvevGrT2GXqK1dDMc8"
|
||||
"HAZCTQxa1Q8NppnpYV-hlqxh-X3Bb0JOePTGzjynpNZoJh2aHZD-GKpZt7OO1Zp8AFWPZ3p8Cahq8536fD8RiBES9jRsvChZvOqA7gMcFc4"
|
||||
@@ -366,69 +400,70 @@ TEST(TokenTest, VerifyTokenHS256FailSignatureLength) {
|
||||
}
|
||||
|
||||
TEST(TokenTest, VerifyFail) {
|
||||
auto token = jwt::create()
|
||||
.set_issuer("auth0")
|
||||
.set_type("JWS")
|
||||
.set_audience("random")
|
||||
.set_payload_claim("typetest", picojson::value(10.0))
|
||||
.sign(jwt::algorithm::none{});
|
||||
{
|
||||
auto token = jwt::create()
|
||||
.set_issuer("auth0")
|
||||
.set_type("JWS")
|
||||
.set_audience("random")
|
||||
.set_payload_claim("typetest", picojson::value(10.0))
|
||||
.sign(jwt::algorithm::none{});
|
||||
|
||||
auto decoded_token = jwt::decode(token);
|
||||
auto decoded_token = jwt::decode(token);
|
||||
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience(std::set<std::string>{ "test" });
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_subject("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("myclaim", jwt::claim(std::string("test")));
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("typetest", jwt::claim(picojson::value(true)));
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
jwt::claim object;
|
||||
std::istringstream iss{R"({ "test": null })"};
|
||||
iss >> object;
|
||||
ASSERT_EQ(object.get_type() , jwt::json::type::object);
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience(std::set<std::string>{ "test" });
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_subject("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("myclaim", jwt::claim(std::string("test")));
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("typetest", jwt::claim(picojson::value(true)));
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
{
|
||||
jwt::claim object;
|
||||
std::istringstream iss{ R"({ "test": null })" };
|
||||
iss >> object;
|
||||
ASSERT_EQ(object.get_type(), jwt::json::type::object);
|
||||
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("myclaim", object);
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_claim("myclaim", object);
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
auto token = jwt::create()
|
||||
.set_issuer("auth0")
|
||||
@@ -436,11 +471,14 @@ TEST(TokenTest, VerifyFail) {
|
||||
.sign(jwt::algorithm::none{});
|
||||
|
||||
auto decoded_token = jwt::decode(token);
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
|
||||
{
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth0")
|
||||
.with_audience("test");
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,6 +600,52 @@ TEST(TokenTest, VerifyTokenPS256FailNoKey) {
|
||||
}(), jwt::rsa_exception);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL110
|
||||
TEST(TokenTest, VerifyTokenEd25519) {
|
||||
const std::string token = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.OujgVcO8xQx5xLcAYWENCRU1SCGH5HcX4MX4o6wU3M4"
|
||||
"DOnKiNmc0O2AnvQlzr-9cgI4QGQzeC6gz_fgLoesADg";
|
||||
|
||||
auto verify = jwt::verify().allow_algorithm(jwt::algorithm::ed25519(ed25519_pub_key, "", "", ""));
|
||||
auto decoded_token = jwt::decode(token);
|
||||
|
||||
verify.verify(decoded_token);
|
||||
}
|
||||
|
||||
TEST(TokenTest, VerifyTokenEd25519Fail) {
|
||||
const std::string token = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.OujgVcO8xQx5xLcAYWENCRU1SCGH5HcX4MX4o6wU3M4"
|
||||
"DOnKiNmc0O2AnvQlzr-9cgI4QGQzeC6gz_fgLoesADg";
|
||||
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::ed25519(ed25519_pub_key_invalid, "", "", ""));
|
||||
auto decoded_token = jwt::decode(token);
|
||||
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::signature_verification_exception);
|
||||
}
|
||||
|
||||
TEST(TokenTest, VerifyTokenEd448) {
|
||||
const std::string token = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.Aldes9jrXZXxfNjuovqmIZ3r2WF4yVXVr2Q8B8SkAmv"
|
||||
"Bsw_3MHs8HtgKeXbqKFYWpHOCtmZJcH-AWMvoY6FCNdQqbESGTkv58O6tFbXDD_nLejWNAOuvcO2LPMySmkVNQUopmQf_HO62Mug1ngepUDE"
|
||||
"A";
|
||||
|
||||
auto verify = jwt::verify().allow_algorithm(jwt::algorithm::ed448(ed448_pub_key, "", "", ""));
|
||||
auto decoded_token = jwt::decode(token);
|
||||
|
||||
verify.verify(decoded_token);
|
||||
}
|
||||
|
||||
TEST(TokenTest, VerifyTokenEd448Fail) {
|
||||
const std::string token = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.Aldes9jrXZXxfNjuovqmIZ3r2WF4yVXVr2Q8B8SkAmv"
|
||||
"Bsw_3MHs8HtgKeXbqKFYWpHOCtmZJcH-AWMvoY6FCNdQqbESGTkv58O6tFbXDD_nLejWNAOuvcO2LPMySmkVNQUopmQf_HO62Mug1ngepUDE"
|
||||
"A";
|
||||
|
||||
auto verify = jwt::verify()
|
||||
.allow_algorithm(jwt::algorithm::ed448(ed448_pub_key_invalid, "", "", ""));
|
||||
auto decoded_token = jwt::decode(token);
|
||||
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::signature_verification_exception);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct test_clock {
|
||||
jwt::date n;
|
||||
jwt::date now() const {
|
||||
|
||||
Reference in New Issue
Block a user