mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
fixing conversion warnings on windows
where sizeof(size_t) != sizeof(int) since x86 still exists
This commit is contained in:
@@ -138,7 +138,7 @@ namespace jwt {
|
||||
auto get_sextet = [&](size_t offset) {
|
||||
for (size_t i = 0; i < alphabet.size(); i++) {
|
||||
if (alphabet[i] == base[offset])
|
||||
return i;
|
||||
return static_cast<uint32_t>(i);
|
||||
}
|
||||
throw std::runtime_error("Invalid input");
|
||||
};
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace jwt {
|
||||
#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);
|
||||
#else
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> certbio(BIO_new_mem_buf(certstr.data(), certstr.size()), BIO_free_all);
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> certbio(BIO_new_mem_buf(certstr.data(), static_cast<int>(certstr.size())), BIO_free_all);
|
||||
#endif
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> keybio(BIO_new(BIO_s_mem()), BIO_free_all);
|
||||
|
||||
@@ -99,10 +99,12 @@ namespace jwt {
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> pubkey_bio(BIO_new(BIO_s_mem()), BIO_free_all);
|
||||
if(key.substr(0, 27) == "-----BEGIN CERTIFICATE-----") {
|
||||
auto epkey = helper::extract_pubkey_from_cert(key, password);
|
||||
if ((size_t)BIO_write(pubkey_bio.get(), epkey.data(), epkey.size()) != epkey.size())
|
||||
const int len = static_cast<int>(epkey.size());
|
||||
if (BIO_write(pubkey_bio.get(), epkey.data(), len) != len)
|
||||
throw rsa_exception("failed to load public key: bio_write failed");
|
||||
} else {
|
||||
if ((size_t)BIO_write(pubkey_bio.get(), key.data(), key.size()) != key.size())
|
||||
const int len = static_cast<int>(key.size());
|
||||
if (BIO_write(pubkey_bio.get(), key.data(), len) != len)
|
||||
throw rsa_exception("failed to load public key: bio_write failed");
|
||||
}
|
||||
|
||||
@@ -115,7 +117,8 @@ namespace jwt {
|
||||
inline
|
||||
std::shared_ptr<EVP_PKEY> load_private_key_from_string(const std::string& key, const std::string& password = "") {
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> privkey_bio(BIO_new(BIO_s_mem()), BIO_free_all);
|
||||
if ((size_t)BIO_write(privkey_bio.get(), key.data(), key.size()) != key.size())
|
||||
const int len = static_cast<int>(key.size());
|
||||
if ((size_t)BIO_write(privkey_bio.get(), key.data(), len) != len)
|
||||
throw rsa_exception("failed to load private key: bio_write failed");
|
||||
std::shared_ptr<EVP_PKEY> pkey(PEM_read_bio_PrivateKey(privkey_bio.get(), nullptr, nullptr, const_cast<char*>(password.c_str())), EVP_PKEY_free);
|
||||
if (!pkey)
|
||||
@@ -147,7 +150,7 @@ namespace jwt {
|
||||
*/
|
||||
inline
|
||||
static std::unique_ptr<BIGNUM, decltype(&BN_free)> raw2bn(const std::string& raw) {
|
||||
return std::unique_ptr<BIGNUM, decltype(&BN_free)>(BN_bin2bn((const unsigned char*)raw.data(), raw.size(), nullptr), BN_free);
|
||||
return std::unique_ptr<BIGNUM, decltype(&BN_free)>(BN_bin2bn((const unsigned char*)raw.data(), static_cast<int>(raw.size()), nullptr), BN_free);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,9 +196,9 @@ namespace jwt {
|
||||
*/
|
||||
std::string sign(const std::string& data) const {
|
||||
std::string res;
|
||||
res.resize(EVP_MAX_MD_SIZE);
|
||||
unsigned int len = res.size();
|
||||
if (HMAC(md(), secret.data(), secret.size(), (const unsigned char*)data.data(), data.size(), (unsigned char*)res.data(), &len) == nullptr)
|
||||
res.resize(static_cast<size_t>(EVP_MAX_MD_SIZE));
|
||||
unsigned int len = static_cast<unsigned int>(res.size());
|
||||
if (HMAC(md(), secret.data(), static_cast<int>(secret.size()), (const unsigned char*)data.data(), static_cast<int>(data.size()), (unsigned char*)res.data(), &len) == nullptr)
|
||||
throw signature_generation_exception();
|
||||
res.resize(len);
|
||||
return res;
|
||||
@@ -307,7 +310,7 @@ namespace jwt {
|
||||
throw signature_verification_exception("failed to verify signature: VerifyInit failed");
|
||||
if (!EVP_VerifyUpdate(ctx.get(), data.data(), data.size()))
|
||||
throw signature_verification_exception("failed to verify signature: VerifyUpdate failed");
|
||||
auto res = EVP_VerifyFinal(ctx.get(), (const unsigned char*)signature.data(), signature.size(), pkey.get());
|
||||
auto res = EVP_VerifyFinal(ctx.get(), (const unsigned char*)signature.data(), static_cast<unsigned int>(signature.size()), pkey.get());
|
||||
if (res != 1)
|
||||
throw signature_verification_exception("evp verify final failed: " + std::to_string(res) + " " + ERR_error_string(ERR_get_error(), NULL));
|
||||
}
|
||||
@@ -346,10 +349,12 @@ namespace jwt {
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> pubkey_bio(BIO_new(BIO_s_mem()), BIO_free_all);
|
||||
if(public_key.substr(0, 27) == "-----BEGIN CERTIFICATE-----") {
|
||||
auto epkey = helper::extract_pubkey_from_cert(public_key, public_key_password);
|
||||
if ((size_t)BIO_write(pubkey_bio.get(), epkey.data(), epkey.size()) != epkey.size())
|
||||
const int len = static_cast<int>(epkey.size());
|
||||
if (BIO_write(pubkey_bio.get(), epkey.data(), len) != len)
|
||||
throw ecdsa_exception("failed to load public key: bio_write failed");
|
||||
} else {
|
||||
if ((size_t)BIO_write(pubkey_bio.get(), public_key.data(), public_key.size()) != public_key.size())
|
||||
const int len = static_cast<int>(public_key.size());
|
||||
if (BIO_write(pubkey_bio.get(), public_key.data(), len) != len)
|
||||
throw ecdsa_exception("failed to load public key: bio_write failed");
|
||||
}
|
||||
|
||||
@@ -363,7 +368,8 @@ namespace jwt {
|
||||
|
||||
if (!private_key.empty()) {
|
||||
std::unique_ptr<BIO, decltype(&BIO_free_all)> privkey_bio(BIO_new(BIO_s_mem()), BIO_free_all);
|
||||
if ((size_t)BIO_write(privkey_bio.get(), private_key.data(), private_key.size()) != private_key.size())
|
||||
const int len = static_cast<int>(private_key.size());
|
||||
if (BIO_write(privkey_bio.get(), private_key.data(), len) != len)
|
||||
throw ecdsa_exception("failed to load private key: bio_write failed");
|
||||
pkey.reset(PEM_read_bio_ECPrivateKey(privkey_bio.get(), nullptr, nullptr, const_cast<char*>(private_key_password.c_str())), EC_KEY_free);
|
||||
if (!pkey)
|
||||
@@ -388,7 +394,7 @@ namespace jwt {
|
||||
const std::string hash = generate_hash(data);
|
||||
|
||||
std::unique_ptr<ECDSA_SIG, decltype(&ECDSA_SIG_free)>
|
||||
sig(ECDSA_do_sign((const unsigned char*)hash.data(), hash.size(), pkey.get()), ECDSA_SIG_free);
|
||||
sig(ECDSA_do_sign((const unsigned char*)hash.data(), static_cast<int>(hash.size()), pkey.get()), ECDSA_SIG_free);
|
||||
if(!sig)
|
||||
throw signature_generation_exception();
|
||||
#ifdef OPENSSL10
|
||||
@@ -432,7 +438,7 @@ namespace jwt {
|
||||
|
||||
ECDSA_SIG_set0(sig.get(), r.release(), s.release());
|
||||
|
||||
if(ECDSA_do_verify((const unsigned char*)hash.data(), hash.size(), sig.get(), pkey.get()) != 1)
|
||||
if(ECDSA_do_verify((const unsigned char*)hash.data(), static_cast<int>(hash.size()), sig.get(), pkey.get()) != 1)
|
||||
throw signature_verification_exception("Invalid signature");
|
||||
#endif
|
||||
}
|
||||
@@ -535,7 +541,7 @@ namespace jwt {
|
||||
const int size = RSA_size(key.get());
|
||||
|
||||
std::string sig(size, 0x00);
|
||||
if(!RSA_public_decrypt(signature.size(), (const unsigned char*)signature.data(), (unsigned char*)sig.data(), key.get(), RSA_NO_PADDING))
|
||||
if(!RSA_public_decrypt(static_cast<int>(signature.size()), (const unsigned char*)signature.data(), (unsigned char*)sig.data(), key.get(), RSA_NO_PADDING))
|
||||
throw signature_verification_exception("Invalid signature");
|
||||
|
||||
if(!RSA_verify_PKCS1_PSS_mgf1(key.get(), (const unsigned char*)hash.data(), md(), md(), (const unsigned char*)sig.data(), -1))
|
||||
|
||||
Reference in New Issue
Block a user