blob: fe0e06778b745fbbe47a6a4e08f3fe2c0f3e3ce7 [file] [log] [blame]
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 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
47void twrpDigest::setfn(string fn) {
48 md5fn = fn;
49}
50
51int twrpDigest::computeMD5(void) {
52 string line;
53 struct MD5Context md5c;
54 FILE *file;
55 int len;
56 unsigned char buf[1024];
57 MD5Init(&md5c);
58 file = fopen(md5fn.c_str(), "rb");
59 if (file == NULL)
60 return -1;
61 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
62 MD5Update(&md5c, buf, len);
63 }
Dees_Troy2673cec2013-04-02 20:22:16 +000064 fclose(file);
Matt Mower0251abc2014-04-08 21:33:08 -050065 MD5Final(md5sum, &md5c);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050066 return 0;
67}
68
69int twrpDigest::write_md5digest(void) {
70 int i;
71 string md5string, md5file;
72 char hex[3];
73 md5file = md5fn + ".md5";
74
75 for (i = 0; i < 16; ++i) {
Matt Mower0251abc2014-04-08 21:33:08 -050076 snprintf(hex, 3, "%02x", md5sum[i]);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050077 md5string += hex;
78 }
79 md5string += " ";
80 md5string += basename((char*) md5fn.c_str());
81 md5string += + "\n";
82 TWFunc::write_file(md5file, md5string);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060083 tw_set_default_metadata(md5file.c_str());
bigbiff bigbiff6b059542013-09-10 10:28:26 -040084 LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050085 return 0;
86}
87
88int twrpDigest::read_md5digest(void) {
Matt Mower2b18a532015-02-20 16:58:05 -060089 size_t i = 0;
Matt Mower0251abc2014-04-08 21:33:08 -050090 bool foundMd5File = false;
91 string md5file = "";
92 vector<string> md5ext;
93 md5ext.push_back(".md5");
94 md5ext.push_back(".md5sum");
95
96 while (i < md5ext.size()) {
97 md5file = md5fn + md5ext[i];
98 if (TWFunc::Path_Exists(md5file)) {
99 foundMd5File = true;
100 break;
101 }
102 i++;
103 }
104
105 if (!foundMd5File) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500106 gui_msg("no_md5=Skipping MD5 check: no MD5 file found");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500107 return -1;
Matt Mower0251abc2014-04-08 21:33:08 -0500108 } else if (TWFunc::read_file(md5file, line) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500109 LOGERR("Skipping MD5 check: MD5 file unreadable %s\n", strerror(errno));
Matt Mowerd5c1a922014-04-15 12:50:58 -0500110 return 1;
Matt Mower0251abc2014-04-08 21:33:08 -0500111 }
112
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500113 return 0;
114}
115
Matt Mowerd5c1a922014-04-15 12:50:58 -0500116/* verify_md5digest return codes:
117 -2: md5 did not match
118 -1: no md5 file found
119 0: md5 matches
120 1: md5 file unreadable
121*/
122
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500123int twrpDigest::verify_md5digest(void) {
124 string buf;
125 char hex[3];
Matt Mowerd5c1a922014-04-15 12:50:58 -0500126 int i, ret;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500127 string md5string;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500128
129 ret = read_md5digest();
130 if (ret != 0)
131 return ret;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400132 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500133 vector<string> tokens;
134 while (ss >> buf)
135 tokens.push_back(buf);
136 computeMD5();
137 for (i = 0; i < 16; ++i) {
138 snprintf(hex, 3, "%02x", md5sum[i]);
139 md5string += hex;
140 }
Matt Mowerd5c1a922014-04-15 12:50:58 -0500141 if (tokens.at(0) != md5string) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500142 gui_err("md5_fail=MD5 does not match");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500143 return -2;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500144 }
145
Ethan Yonker74db1572015-10-28 12:44:49 -0500146 gui_msg("md5_match=MD5 matched");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500147 return 0;
148}