Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signatureVerified flag for each signature #994

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Signer
/* naming - "Signature" was already taken by unpackers */
struct DigitalSignature
{
bool isValid;
std::string fileDigest;
std::string signedDigest;
std::string digestAlgorithm;
Expand Down
26 changes: 18 additions & 8 deletions src/fileformat/file_format/pe/authenticode/pkcs7_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ std::vector<DigitalSignature> Pkcs7Signature::getSignatures(const retdec::filefo
/* No signer would mean, we have pretty much nothing */
if (!signerInfo.has_value()) {
signatures.push_back(signature);
signature.isValid = false;
return signatures;
}

Expand All @@ -556,18 +557,27 @@ std::vector<DigitalSignature> Pkcs7Signature::getSignatures(const retdec::filefo

signature.signer = signer;

// If there are warnings, then the signature is invalid
// We don't use the countersignature warnings here as
// previous implementation did when verifying
if (signature.warnings.empty()) {
HoundThe marked this conversation as resolved.
Show resolved Hide resolved
signature.isValid = true;
} else {
signature.isValid = false;
}

for (auto&& counterSig : signInfo.counterSignatures) {
CertificateProcessor processor;
auto certChain = processor.getChain(counterSig.signerCert, certs);
auto fileformatCertChain = convertToFileformatCertChain(certChain);

Signer counterSigner;
counterSigner.chain = fileformatCertChain;
counterSigner.signingTime = counterSig.signTime,
counterSigner.signingTime = counterSig.signTime;
counterSigner.digest = bytesToHexString(counterSig.messageDigest.data(),
counterSig.messageDigest.size()),
counterSigner.digestAlgorithm = OBJ_nid2ln(counterSig.digestAlgorithm),
counterSigner.warnings = counterSig.verify(signerInfo->encryptDigest),
counterSig.messageDigest.size());
counterSigner.digestAlgorithm = OBJ_nid2ln(counterSig.digestAlgorithm);
counterSigner.warnings = counterSig.verify(signerInfo->encryptDigest);
counterSigner.counterSigners = std::vector<Signer>();

signature.signer.counterSigners.push_back(counterSigner);
Expand All @@ -580,11 +590,11 @@ std::vector<DigitalSignature> Pkcs7Signature::getSignatures(const retdec::filefo

Signer counterSigner;
counterSigner.chain = fileformatCertChain;
counterSigner.signingTime = counterSig.signTime,
counterSigner.signingTime = counterSig.signTime;
counterSigner.digest = bytesToHexString(counterSig.messageDigest.data(),
counterSig.messageDigest.size()),
counterSigner.digestAlgorithm = OBJ_nid2ln(counterSig.digestAlgorithm),
counterSigner.warnings = counterSig.verify(signerInfo->encryptDigest),
counterSig.messageDigest.size());
counterSigner.digestAlgorithm = OBJ_nid2ln(counterSig.digestAlgorithm);
counterSigner.warnings = counterSig.verify(signerInfo->encryptDigest);
counterSigner.counterSigners = std::vector<Signer>();

signature.signer.counterSigners.push_back(counterSigner);
Expand Down
2 changes: 1 addition & 1 deletion src/fileformat/file_format/pe/pe_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ void PeFormat::loadCertificates()
auto certBytes = securityDir.getCertificate(0);

authenticode::Authenticode authenticode(certBytes);

certificateTable = new CertificateTable(authenticode.getSignatures(this));
}

Expand Down
2 changes: 2 additions & 0 deletions src/fileinfo/file_presentation/json_presentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ void WriteSigner(JsonPresentation::Writer& writer, const Signer& signer)
void WriteSignature(JsonPresentation::Writer& writer, const DigitalSignature& signature)
{
writer.StartObject();
writer.String("signatureVerified");
writer.Bool(signature.isValid);
writer.String("warnings");
writer.StartArray();
for (auto&& warn : signature.warnings) {
Expand Down
1 change: 1 addition & 0 deletions src/fileinfo/file_presentation/plain_presentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ static void printSigner(const Signer& signer, int indent)

static void printSignature(const DigitalSignature& signature, int indent)
{
Log::info() << std::string(indent, ' ') << "Is Valid: " << signature.isValid << "\n";
Log::info() << std::string(indent, ' ') << "Digest Algorithm: " << signature.digestAlgorithm << "\n";
Log::info() << std::string(indent, ' ') << "Signed Digest: " << signature.signedDigest << "\n";
Log::info() << std::string(indent, ' ') << "File Digest: " << signature.fileDigest << "\n";
Expand Down