diff --git a/src/PkiValidator.cpp b/src/PkiValidator.cpp index d608e90..e25902a 100644 --- a/src/PkiValidator.cpp +++ b/src/PkiValidator.cpp @@ -1,10 +1,11 @@ -#include -#include -#include -#include #include "PkiValidator.h" -nstool::PkiValidator::PkiValidator() +#include +#include +#include + +nstool::PkiValidator::PkiValidator() : + mModuleName("nstool::PkiValidator") { clearCertificates(); } @@ -48,7 +49,7 @@ void nstool::PkiValidator::addCertificate(const nn::pki::SignedDatasecond.key_type) { - sig_validate_res = fnd::rsa::pkcs::rsaVerify(rsa2048_pub, getCryptoHashAlgoFromEsSignHashAlgo(hash_algo), hash.data(), signature.data()); + throw tc::Exception(mModuleName, fmt::print("Public key for issuer \"{:s}\" cannot verify this signature.", issuer); } - else if (mKeyCfg.getPkiRootSignKey(issuer, ecdsa_pub) == true && sign_algo == nn::pki::sign::SIGN_ALGO_ECDSA240) + + if (sign_algo == nn::pki::sign::SIGN_ALGO_ECDSA240) { - throw tc::Exception(kModuleName, "ECDSA signatures are not supported"); - } - else - { - throw tc::Exception(kModuleName, "Public key for issuer \"" + issuer + "\" does not exist."); + throw tc::Exception(mModuleName, "ECDSA signatures are not supported"); } + + rsa_key = itr->second.rsa_key; } else { @@ -127,25 +129,65 @@ void nstool::PkiValidator::validateSignature(const std::string& issuer, nn::pki: if (issuer_pubk_type == nn::pki::cert::RSA4096 && sign_algo == nn::pki::sign::SIGN_ALGO_RSA4096) { - sig_validate_res = fnd::rsa::pkcs::rsaVerify(issuer_cert.getRsa4098PublicKey(), getCryptoHashAlgoFromEsSignHashAlgo(hash_algo), hash.data(), signature.data()); + rsa_key = issuer_cert.getRsa4098PublicKey(); } else if (issuer_pubk_type == nn::pki::cert::RSA2048 && sign_algo == nn::pki::sign::SIGN_ALGO_RSA2048) { - sig_validate_res = fnd::rsa::pkcs::rsaVerify(issuer_cert.getRsa2048PublicKey(), getCryptoHashAlgoFromEsSignHashAlgo(hash_algo), hash.data(), signature.data()); + rsa_key = issuer_cert.getRsa2048PublicKey(); } else if (issuer_pubk_type == nn::pki::cert::ECDSA240 && sign_algo == nn::pki::sign::SIGN_ALGO_ECDSA240) { - throw tc::Exception(kModuleName, "ECDSA signatures are not supported"); + // ecc_key = issuer_cert.getEcdsa240PublicKey(); + throw tc::Exception(mModuleName, "ECDSA signatures are not supported"); } else { - throw tc::Exception(kModuleName, "Mismatch between issuer public key and signature type"); + throw tc::Exception(mModuleName, "Mismatch between issuer public key and signature type"); } } - if (sig_validate_res != 0) + // verify signature + switch (signature_id) { + case (SIGN_ID_RSA4096_SHA1): + sig_validate_res = tc::crypto::VerifyRsa4096Pkcs1Sha1(signature.data(), hash.data(), rsa_key); + break; + case (SIGN_ID_RSA2048_SHA1): + sig_validate_res = tc::crypto::VerifyRsa2048Pkcs1Sha1(signature.data(), hash.data(), rsa_key); + break; + case (SIGN_ID_ECDSA240_SHA1): + sig_validate_res = false; + break; + case (SIGN_ID_RSA4096_SHA256): + sig_validate_res = tc::crypto::VerifyRsa4096Pkcs1Sha256(signature.data(), hash.data(), rsa_key); + break; + case (SIGN_ID_RSA2048_SHA256): + sig_validate_res = tc::crypto::VerifyRsa2048Pkcs1Sha256(signature.data(), hash.data(), rsa_key); + break; + case (SIGN_ID_ECDSA240_SHA256): + sig_validate_res = false; + break; + } + if (sign_algo == nn::pki::sign::SIGN_ALGO_RSA4096) { - throw tc::Exception(kModuleName, "Incorrect signature"); + sig_validate_res = fnd::rsa::pkcs::rsaVerify(issuer_cert.getRsa4098PublicKey(), getCryptoHashAlgoFromEsSignHashAlgo(hash_algo), hash.data(), signature.data()); + } + else if (sign_algo == nn::pki::sign::SIGN_ALGO_RSA2048) + { + sig_validate_res = fnd::rsa::pkcs::rsaVerify(issuer_cert.getRsa2048PublicKey(), getCryptoHashAlgoFromEsSignHashAlgo(hash_algo), hash.data(), signature.data()); + } + else if (sign_algo == nn::pki::sign::SIGN_ALGO_ECDSA240) + { + throw tc::Exception(mModuleName, "ECDSA signatures are not supported"); + } + else + { + throw tc::Exception(mModuleName, "Mismatch between issuer public key and signature type"); + } + + + if (sig_valid == false) + { + throw tc::Exception(mModuleName, "Incorrect signature"); } @@ -159,7 +201,7 @@ void nstool::PkiValidator::makeCertIdent(const nn::pki::SignedData(ident.length(),64)); } bool nstool::PkiValidator::doesCertExist(const std::string& ident) const @@ -191,22 +233,5 @@ const nn::pki::SignedData& nstool::PkiValidator::getCe } } - throw tc::Exception(kModuleName, "Issuer certificate does not exist"); -} - -fnd::sha::HashType nstool::PkiValidator::getCryptoHashAlgoFromEsSignHashAlgo(nn::pki::sign::HashAlgo hash_algo) const -{ - fnd::sha::HashType hash_type = fnd::sha::HASH_SHA1; - - switch (hash_algo) - { - case (nn::pki::sign::HASH_ALGO_SHA1): - hash_type = fnd::sha::HASH_SHA1; - break; - case (nn::pki::sign::HASH_ALGO_SHA256): - hash_type = fnd::sha::HASH_SHA256; - break; - }; - - return hash_type; + throw tc::Exception(mModuleName, "Issuer certificate does not exist"); } \ No newline at end of file diff --git a/src/PkiValidator.h b/src/PkiValidator.h index 6cc2006..27ebf57 100644 --- a/src/PkiValidator.h +++ b/src/PkiValidator.h @@ -20,7 +20,7 @@ public: void validateSignature(const std::string& issuer, nn::pki::sign::SignatureId signature_id, const tc::ByteData& signature, const tc::ByteData& hash) const; private: - const std::string kModuleName = "NNPkiValidator"; + std::string mModuleName; KeyBag mKeyCfg; std::vector> mCertificateBank; @@ -29,7 +29,6 @@ private: void makeCertIdent(const std::string& issuer, const std::string& subject, std::string& ident) const; bool doesCertExist(const std::string& ident) const; const nn::pki::SignedData& getCert(const std::string& ident) const; - fnd::sha::HashType getCryptoHashAlgoFromEsSignHashAlgo(nn::pki::sign::HashAlgo hash_algo) const; }; } \ No newline at end of file