Skip to content

Commit

Permalink
Zip install: this will allow TWRP to check md5 and sha256 checksums
Browse files Browse the repository at this point in the history
for zip installs.

Change-Id: I1fb6af777e08aea29bedaf2e6a9b385ae3b03fac
  • Loading branch information
bigbiff authored and Dees-Troy committed Apr 2, 2019
1 parent abc299c commit 718ab39
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
33 changes: 2 additions & 31 deletions twinstall.cpp 100644 → 100755
Expand Up @@ -332,41 +332,12 @@ int TWinstall_zip(const char* path, int* wipe_cache) {
if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
string digest_str;
string Full_Filename = path;
string digest_file = path;
string defmd5file = digest_file + ".md5sum";

if (TWFunc::Path_Exists(defmd5file)) {
digest_file += ".md5sum";
}
else {
digest_file += ".md5";
}

gui_msg("check_for_digest=Checking for Digest file...");
if (!TWFunc::Path_Exists(digest_file)) {
gui_msg("no_digest=Skipping Digest check: no Digest file found");
}
else {
if (TWFunc::read_file(digest_file, digest_str) != 0) {
LOGERR("Skipping MD5 check: MD5 file unreadable\n");
}
else {
twrpDigest *digest = new twrpMD5();
if (!twrpDigestDriver::stream_file_to_digest(Full_Filename, digest)) {
delete digest;
return INSTALL_CORRUPT;
}
string digest_check = digest->return_digest_string();
if (digest_str == digest_check) {
gui_msg(Msg("digest_matched=Digest matched for '{1}'.")(path));
}
else {

if (!twrpDigestDriver::Check_File_Digest(Full_Filename)) {
LOGERR("Aborting zip install: Digest verification failed\n");
delete digest;
return INSTALL_CORRUPT;
}
delete digest;
}
}
}

Expand Down
29 changes: 21 additions & 8 deletions twrpDigestDriver.cpp 100644 → 100755
Expand Up @@ -33,7 +33,7 @@
#include "twrpDigest/twrpSHA.hpp"


bool twrpDigestDriver::Check_Restore_File_Digest(const string& Filename) {
bool twrpDigestDriver::Check_File_Digest(const string& Filename) {
twrpDigest *digest;
string digestfile = Filename, file_name = Filename;
string digest_str;
Expand All @@ -47,18 +47,31 @@ bool twrpDigestDriver::Check_Restore_File_Digest(const string& Filename) {
use_sha2 = true;
}
else {
digest = new twrpMD5();
digestfile = Filename + ".md5";

digestfile = Filename + ".sha256";
if (TWFunc::Path_Exists(digestfile)) {
digest = new twrpSHA256();
use_sha2 = true;
} else {
digest = new twrpMD5();
digestfile = Filename + ".md5";
if (!TWFunc::Path_Exists(digestfile)) {
digestfile = Filename + ".md5sum";
}
}
}
#else
digest = new twrpMD5();
digestfile = Filename + ".md5";
if (!TWFunc::Path_Exists(digestfile)) {
digestfile = Filename + ".md5sum";
}

#endif

if (!TWFunc::Path_Exists(digestfile)) {
gui_msg(Msg(msg::kError, "no_digest_found=No digest file found for '{1}'. Please unselect Enable Digest verification to restore.")(Filename));
if (Filename.find(".zip") == std::string::npos) {
gui_msg(Msg(msg::kError, "no_digest_found=No digest file found for '{1}'. Please unselect Enable Digest verification to restore.")(Filename));
}
delete digest;
return false;
}
Expand All @@ -80,14 +93,14 @@ bool twrpDigestDriver::Check_Restore_File_Digest(const string& Filename) {
LOGINFO("SHA2 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Filename).c_str());
else
LOGINFO("MD5 Digest: %s %s\n", digest_str.c_str(), TWFunc::Get_Filename(Filename).c_str());
gui_msg(Msg("digest_matched=Digest matched for '{1}'.")(Filename));
delete digest;
return true;
}

gui_msg(Msg(msg::kError, "digest_fail_match=Digest failed to match on '{1}'.")(Filename));
delete digest;
return false;

}

bool twrpDigestDriver::Check_Digest(string Full_Filename) {
Expand All @@ -103,13 +116,13 @@ bool twrpDigestDriver::Check_Digest(string Full_Filename) {
if (!TWFunc::Path_Exists(split_filename))
break;
LOGINFO("split_filename: %s\n", split_filename);
if (!Check_Restore_File_Digest(split_filename))
if (!Check_File_Digest(split_filename))
return false;
index++;
}
return true;
}
return Check_Restore_File_Digest(Full_Filename); // Single file archive
return Check_File_Digest(Full_Filename); // Single file archive
}

bool twrpDigestDriver::Write_Digest(string Full_Filename) {
Expand Down
2 changes: 1 addition & 1 deletion twrpDigestDriver.hpp 100644 → 100755
Expand Up @@ -24,7 +24,7 @@
class twrpDigestDriver {
public:

static bool Check_Restore_File_Digest(const string& Filename); //Check the digest of a TWRP partition backup
static bool Check_File_Digest(const string& Filename); //Check the digest of a TWRP partition backup
static bool Check_Digest(string Full_Filename); //Check to make sure the digest is correct
static bool Write_Digest(string Full_Filename); //Write the digest to a file
static bool Make_Digest(string Full_Filename); //Create the digest for a partition backup
Expand Down

0 comments on commit 718ab39

Please sign in to comment.