diff --git a/include/jwt-cpp/base.h b/include/jwt-cpp/base.h index 7f07be1..4ff7b20 100644 --- a/include/jwt-cpp/base.h +++ b/include/jwt-cpp/base.h @@ -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(i); } throw std::runtime_error("Invalid input"); }; diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 10fe011..b769202 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -78,7 +78,7 @@ namespace jwt { #if OPENSSL_VERSION_NUMBER <= 0x10100003L std::unique_ptr certbio(BIO_new_mem_buf(const_cast(certstr.data()), certstr.size()), BIO_free_all); #else - std::unique_ptr certbio(BIO_new_mem_buf(certstr.data(), certstr.size()), BIO_free_all); + std::unique_ptr certbio(BIO_new_mem_buf(certstr.data(), static_cast(certstr.size())), BIO_free_all); #endif std::unique_ptr keybio(BIO_new(BIO_s_mem()), BIO_free_all); @@ -99,10 +99,12 @@ namespace jwt { std::unique_ptr 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(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(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 load_private_key_from_string(const std::string& key, const std::string& password = "") { std::unique_ptr 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(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 pkey(PEM_read_bio_PrivateKey(privkey_bio.get(), nullptr, nullptr, const_cast(password.c_str())), EVP_PKEY_free); if (!pkey) @@ -147,7 +150,7 @@ namespace jwt { */ inline static std::unique_ptr raw2bn(const std::string& raw) { - return std::unique_ptr(BN_bin2bn((const unsigned char*)raw.data(), raw.size(), nullptr), BN_free); + return std::unique_ptr(BN_bin2bn((const unsigned char*)raw.data(), static_cast(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(EVP_MAX_MD_SIZE)); + unsigned int len = static_cast(res.size()); + if (HMAC(md(), secret.data(), static_cast(secret.size()), (const unsigned char*)data.data(), static_cast(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(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 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(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(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 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(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(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 - 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(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(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(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))