Skip to content

Commit

Permalink
Fix bugs related to files being overwritten during batch signature
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlimitedPugWorks committed Oct 8, 2024
1 parent 61fd61e commit d64c3a2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
40 changes: 38 additions & 2 deletions pteid-mw-pt/_src/eidmw/applayer/MiscUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,27 @@ void CPathUtil::generate_unique_filenames(const char *folder, std::vector<std::s
std::vector<std::pair<std::string, int>> filenames_counter;
std::vector<std::string> unique_filenames;

// If the folder isn't empty
if (std::string(folder).size() != 0) {
// Add files in current folder
for (const auto &entry : std::filesystem::directory_iterator(folder)) {
std::string current_path = entry.path().string();
char *basename = Basename((char *)current_path.c_str());
filenames_counter.push_back(std::make_pair(basename, 0));
}
}

for (int i = 0; i < filenames.size(); i++) {

std::string basename = std::string(Basename((char *)(filenames[i]->c_str())));
std::string clean_filename = CPathUtil::remove_ext_from_basename(basename.c_str());
std::string extension = std::string(basename.c_str()).erase(0, clean_filename.length());
std::string final_name = clean_filename + suffix + extension;

int equal_filename_count = 0;
for (unsigned int j = 0; j < filenames_counter.size(); j++) {
std::string current_file_name = filenames_counter.at(j).first;
if (basename.compare(current_file_name) == 0) {
if (final_name.compare(current_file_name) == 0) {
// filenames_counter contains clean_filename
equal_filename_count = ++filenames_counter.at(j).second;
break;
Expand All @@ -832,7 +843,7 @@ void CPathUtil::generate_unique_filenames(const char *folder, std::vector<std::s

if (equal_filename_count == 0) {
// clean_filename is not part of the vector, make sure it's added to it
filenames_counter.push_back(std::make_pair(basename, equal_filename_count));
filenames_counter.push_back(std::make_pair(final_name, equal_filename_count));
}

std::string final_path = "";
Expand All @@ -841,7 +852,32 @@ void CPathUtil::generate_unique_filenames(const char *folder, std::vector<std::s

final_path += clean_filename;
if (equal_filename_count > 0) {
while (true) {
// Variable that stores if a match for the new name has been found
bool found = false;
// This is the file_name that will be tested
std::string tested_file_name =
clean_filename + "_" + std::to_string(equal_filename_count) + suffix + extension;
// For each file in the unique_filenames vector
for (unsigned int i = 0; i < filenames_counter.size(); i++) {
std::string current_file_name = filenames_counter.at(i).first;
// Sees if the current_file_name matches the test file
if (tested_file_name.compare(current_file_name) == 0) {
// If it does, it increments the equal_filename:count
equal_filename_count = ++filenames_counter.at(i).second;
// Sets found to true, since a match has been found
found = true;
// Breaks and will test with the next name
break;
}
}
// If a match hasn't been found then the name can be used.
if (!found)
break;
}
final_path += "_" + std::to_string(equal_filename_count);
std::string added_pair = clean_filename + "_" + std::to_string(equal_filename_count) + suffix + extension;
filenames_counter.push_back(std::make_pair(added_pair, 0));
}
final_path += suffix + extension;

Expand Down
1 change: 1 addition & 0 deletions pteid-mw-pt/_src/eidmw/applayer/MiscUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <time.h>
#include "TLVBuffer.h"
#include <openssl/x509.h>
#include <filesystem>

namespace eIDMW {

Expand Down
47 changes: 43 additions & 4 deletions pteid-mw-pt/_src/eidmw/applayer/PDFSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,28 +375,55 @@ std::string PDFSignature::generateFinalPath(const char *output_dir, const char *
fprintf(stderr, "%s Outdir: %s path: %s\n", __FUNCTION__, output_dir, path);
char *pdf_filename = Basename((char *)path);
std::string clean_filename = CPathUtil::remove_ext_from_basename(pdf_filename);
std::string clean_signed_filename = clean_filename + "_signed";

int equal_filename_count = 0;

for (unsigned int i = 0; i < unique_filenames.size(); i++) {
std::string current_file_name = unique_filenames.at(i).first;
if (clean_filename.compare(current_file_name) == 0) {
if (clean_signed_filename.compare(current_file_name) == 0) {
// unique_filenames contains clean_filename
equal_filename_count = ++unique_filenames.at(i).second;
break;
}
}

if (equal_filename_count == 0) {
// clean_filename is not part of the vector, make sure it's added to it
unique_filenames.push_back(std::make_pair(clean_filename, equal_filename_count));
// clean_signed_filename is not part of the vector, make sure it's added to it
unique_filenames.push_back(std::make_pair(clean_signed_filename, 0));
}

std::string final_path = std::string(output_dir) + PATH_SEP + clean_filename;

// If there was a duplicate path
if (equal_filename_count > 0) {
// Will see if the new path is also unique
while (true) {
// Variable that stores if a match for the new name has been found
bool found = false;
// This is the file_name that will be tested
std::string tested_file_name = clean_filename + "_" + std::to_string(equal_filename_count) + "_signed";
// For each file in the unique_filenames vector
for (unsigned int i = 0; i < unique_filenames.size(); i++) {
std::string current_file_name = unique_filenames.at(i).first;
// Sees if the current_file_name matches the test file
if (tested_file_name.compare(current_file_name) == 0) {
// If it does, it increments the equal_filename:count
equal_filename_count = ++unique_filenames.at(i).second;
// Sets found to true, since a match has been found
found = true;
// Breaks and will test with the next name
break;
}
}
// If a match hasn't been found then the name can be used.
if (!found)
break;
}
final_path += "_" + std::to_string(equal_filename_count);
std::string added_pair = clean_filename + "_" + std::to_string(equal_filename_count) + "_signed";
unique_filenames.push_back(std::make_pair(added_pair, 0));
}

final_path += "_signed.pdf";

return final_path;
Expand Down Expand Up @@ -447,6 +474,18 @@ int PDFSignature::signFiles(const char *location, const char *reason, const char
bool throwTimestampError = false;
bool throwLTVError = false;
bool cachedPin = false;

MWLOG(LEV_ERROR, MOD_APL, "outfile_path: %s", outfile_path);

// Add files in current dir
for (const auto &entry : std::filesystem::directory_iterator(outfile_path)) {
std::string current_path = entry.path().string();
char *current_filename = Basename((char *)current_path.c_str());
std::string clean_filename = CPathUtil::remove_ext_from_basename(current_filename);
MWLOG(LEV_ERROR, MOD_APL, "current_path: %s", clean_filename);
unique_filenames.push_back(std::make_pair(clean_filename, 0));
}

for (unsigned int i = 0; i < m_files_to_sign.size(); i++) {
try {
char *current_file = m_files_to_sign.at(i).first;
Expand Down
1 change: 1 addition & 0 deletions pteid-mw-pt/_src/eidmw/applayer/PDFSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Export.h"
#include <vector>
#include <utility>
#include <filesystem>

#include "ByteArray.h"
#include "APLCard.h"
Expand Down

0 comments on commit d64c3a2

Please sign in to comment.