blob: ab8f456f539d788f4c8575755d8fc34cfe3b3c16 [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)
94 return -1;
95 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
131 if (!foundMd5File) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500132 gui_msg("no_md5=Skipping MD5 check: no MD5 file found");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500133 return -1;
Matt Mower0251abc2014-04-08 21:33:08 -0500134 } else if (TWFunc::read_file(md5file, line) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500135 LOGERR("Skipping MD5 check: MD5 file unreadable %s\n", strerror(errno));
Matt Mowerd5c1a922014-04-15 12:50:58 -0500136 return 1;
Matt Mower0251abc2014-04-08 21:33:08 -0500137 }
138
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500139 return 0;
140}
141
Matt Mowerd5c1a922014-04-15 12:50:58 -0500142/* verify_md5digest return codes:
143 -2: md5 did not match
144 -1: no md5 file found
145 0: md5 matches
146 1: md5 file unreadable
147*/
148
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500149int twrpDigest::verify_md5digest(void) {
150 string buf;
151 char hex[3];
Matt Mowerd5c1a922014-04-15 12:50:58 -0500152 int i, ret;
bigbiffce8f83c2015-12-12 18:30:21 -0500153 string md5str;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500154
155 ret = read_md5digest();
156 if (ret != 0)
157 return ret;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400158 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500159 vector<string> tokens;
160 while (ss >> buf)
161 tokens.push_back(buf);
162 computeMD5();
163 for (i = 0; i < 16; ++i) {
164 snprintf(hex, 3, "%02x", md5sum[i]);
bigbiffce8f83c2015-12-12 18:30:21 -0500165 md5str += hex;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500166 }
bigbiffce8f83c2015-12-12 18:30:21 -0500167 if (tokens.at(0) != md5str) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500168 gui_err("md5_fail=MD5 does not match");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500169 return -2;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500170 }
171
Ethan Yonker74db1572015-10-28 12:44:49 -0500172 gui_msg("md5_match=MD5 matched");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500173 return 0;
174}