blob: 8380d2c8b06bdeeeb8aa30fae895aca8ca1f040c [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"
Matt Mowerd5c1a922014-04-15 12:50:58 -050022 #include "gui/gui.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050023 #include "libcrecovery/common.h"
24}
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020025
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050026#include <vector>
27#include <string>
28#include <sstream>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32#include <string.h>
33#include <libgen.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <fstream>
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050037#include <sys/mman.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "data.hpp"
40#include "variables.h"
41#include "twrp-functions.hpp"
42#include "twrpDigest.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060043#include "set_metadata.h"
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) {
Matt Mowerd5c1a922014-04-15 12:50:58 -0500106 gui_print("Skipping MD5 check: no MD5 file found\n");
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) {
Matt Mowerd5c1a922014-04-15 12:50:58 -0500109 gui_print("Skipping MD5 check: MD5 file unreadable\n");
110 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) {
142 LOGERR("MD5 does not match\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500143 return -2;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500144 }
145
146 gui_print("MD5 matched\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500147 return 0;
148}