blob: a9ba20a384734466a8dde6b03d85636ca06e90e6 [file] [log] [blame]
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020019extern "C"
20{
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050021 #include "digest/md5.h"
22 #include "libcrecovery/common.h"
23}
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020024
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050025#include <vector>
26#include <string>
27#include <sstream>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/wait.h>
31#include <string.h>
32#include <libgen.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <fstream>
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050036#include <sys/mman.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050038#include "data.hpp"
39#include "variables.h"
40#include "twrp-functions.hpp"
41#include "twrpDigest.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060042#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050043#include "gui/gui.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044
45using namespace std;
46
Ethan Yonker472f5062016-02-25 13:47:30 -060047void twrpDigest::setfn(const string& fn) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050048 md5fn = fn;
49}
50
bigbiffce8f83c2015-12-12 18:30:21 -050051void twrpDigest::initMD5(void) {
52 MD5Init(&md5c);
53 md5string = "";
54}
55
56int twrpDigest::updateMD5stream(unsigned char* stream, int len) {
57 if (md5fn.empty()) {
58 MD5Update(&md5c, stream, len);
59 }
60 else {
61 return -1;
62 }
63 return 0;
64}
65
66void twrpDigest::finalizeMD5stream() {
67 MD5Final(md5sum, &md5c);
68}
69
70string twrpDigest::createMD5string() {
71 int i;
72 char hex[3];
73
74 for (i = 0; i < 16; ++i) {
75 snprintf(hex, 3, "%02x", md5sum[i]);
76 md5string += hex;
77 }
78 if (!md5fn.empty()) {
79 md5string += " ";
80 md5string += basename((char*) md5fn.c_str());
81 md5string += + "\n";
82 }
83 return md5string;
84}
85
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050086int twrpDigest::computeMD5(void) {
87 string line;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050088 FILE *file;
89 int len;
90 unsigned char buf[1024];
bigbiffce8f83c2015-12-12 18:30:21 -050091 initMD5();
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050092 file = fopen(md5fn.c_str(), "rb");
93 if (file == NULL)
James Christopher Adduono79ae0932016-10-25 02:18:32 -040094 return MD5_NOT_FOUND;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050095 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
96 MD5Update(&md5c, buf, len);
97 }
Dees_Troy2673cec2013-04-02 20:22:16 +000098 fclose(file);
Matt Mower0251abc2014-04-08 21:33:08 -050099 MD5Final(md5sum, &md5c);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500100 return 0;
101}
102
103int twrpDigest::write_md5digest(void) {
bigbiffce8f83c2015-12-12 18:30:21 -0500104 string md5file, md5str;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500105 md5file = md5fn + ".md5";
106
bigbiffce8f83c2015-12-12 18:30:21 -0500107 md5str = createMD5string();
108 TWFunc::write_file(md5file, md5str);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600109 tw_set_default_metadata(md5file.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -0500110 LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5str.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500111 return 0;
112}
113
114int twrpDigest::read_md5digest(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600115 size_t i = 0;
Matt Mower0251abc2014-04-08 21:33:08 -0500116 bool foundMd5File = false;
117 string md5file = "";
118 vector<string> md5ext;
119 md5ext.push_back(".md5");
120 md5ext.push_back(".md5sum");
121
122 while (i < md5ext.size()) {
123 md5file = md5fn + md5ext[i];
124 if (TWFunc::Path_Exists(md5file)) {
125 foundMd5File = true;
126 break;
127 }
128 i++;
129 }
130
James Christopher Adduono79ae0932016-10-25 02:18:32 -0400131 if (!foundMd5File)
132 return MD5_NOT_FOUND;
133 if (TWFunc::read_file(md5file, line) != 0)
134 return MD5_FILE_UNREADABLE;
Matt Mower0251abc2014-04-08 21:33:08 -0500135
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500136 return 0;
137}
138
139int twrpDigest::verify_md5digest(void) {
140 string buf;
141 char hex[3];
Matt Mowerd5c1a922014-04-15 12:50:58 -0500142 int i, ret;
bigbiffce8f83c2015-12-12 18:30:21 -0500143 string md5str;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500144
145 ret = read_md5digest();
146 if (ret != 0)
147 return ret;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400148 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500149 vector<string> tokens;
150 while (ss >> buf)
151 tokens.push_back(buf);
152 computeMD5();
153 for (i = 0; i < 16; ++i) {
154 snprintf(hex, 3, "%02x", md5sum[i]);
bigbiffce8f83c2015-12-12 18:30:21 -0500155 md5str += hex;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500156 }
thatd1afb522017-02-23 23:05:59 +0100157 if (tokens.empty() || tokens.at(0) != md5str)
James Christopher Adduono79ae0932016-10-25 02:18:32 -0400158 return MD5_MATCH_FAIL;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500159
James Christopher Adduono79ae0932016-10-25 02:18:32 -0400160 return MD5_OK;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500161}