blob: 0693c550088a0a55c06a1fd4bfd309a839b1a38f [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"
43
44using namespace std;
45
46void twrpDigest::setfn(string fn) {
47 md5fn = fn;
48}
49
50int twrpDigest::computeMD5(void) {
51 string line;
52 struct MD5Context md5c;
53 FILE *file;
54 int len;
55 unsigned char buf[1024];
56 MD5Init(&md5c);
57 file = fopen(md5fn.c_str(), "rb");
58 if (file == NULL)
59 return -1;
60 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
61 MD5Update(&md5c, buf, len);
62 }
Dees_Troy2673cec2013-04-02 20:22:16 +000063 fclose(file);
Matt Mower0251abc2014-04-08 21:33:08 -050064 MD5Final(md5sum, &md5c);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050065 return 0;
66}
67
68int twrpDigest::write_md5digest(void) {
69 int i;
70 string md5string, md5file;
71 char hex[3];
72 md5file = md5fn + ".md5";
73
74 for (i = 0; i < 16; ++i) {
Matt Mower0251abc2014-04-08 21:33:08 -050075 snprintf(hex, 3, "%02x", md5sum[i]);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050076 md5string += hex;
77 }
78 md5string += " ";
79 md5string += basename((char*) md5fn.c_str());
80 md5string += + "\n";
81 TWFunc::write_file(md5file, md5string);
bigbiff bigbiff6b059542013-09-10 10:28:26 -040082 LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050083 return 0;
84}
85
86int twrpDigest::read_md5digest(void) {
Matt Mower0251abc2014-04-08 21:33:08 -050087 int i = 0;
88 bool foundMd5File = false;
89 string md5file = "";
90 vector<string> md5ext;
91 md5ext.push_back(".md5");
92 md5ext.push_back(".md5sum");
93
94 while (i < md5ext.size()) {
95 md5file = md5fn + md5ext[i];
96 if (TWFunc::Path_Exists(md5file)) {
97 foundMd5File = true;
98 break;
99 }
100 i++;
101 }
102
103 if (!foundMd5File) {
Matt Mowerd5c1a922014-04-15 12:50:58 -0500104 gui_print("Skipping MD5 check: no MD5 file found\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500105 return -1;
Matt Mower0251abc2014-04-08 21:33:08 -0500106 } else if (TWFunc::read_file(md5file, line) != 0) {
Matt Mowerd5c1a922014-04-15 12:50:58 -0500107 gui_print("Skipping MD5 check: MD5 file unreadable\n");
108 return 1;
Matt Mower0251abc2014-04-08 21:33:08 -0500109 }
110
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500111 return 0;
112}
113
Matt Mowerd5c1a922014-04-15 12:50:58 -0500114/* verify_md5digest return codes:
115 -2: md5 did not match
116 -1: no md5 file found
117 0: md5 matches
118 1: md5 file unreadable
119*/
120
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500121int twrpDigest::verify_md5digest(void) {
122 string buf;
123 char hex[3];
Matt Mowerd5c1a922014-04-15 12:50:58 -0500124 int i, ret;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500125 string md5string;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500126
127 ret = read_md5digest();
128 if (ret != 0)
129 return ret;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400130 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500131 vector<string> tokens;
132 while (ss >> buf)
133 tokens.push_back(buf);
134 computeMD5();
135 for (i = 0; i < 16; ++i) {
136 snprintf(hex, 3, "%02x", md5sum[i]);
137 md5string += hex;
138 }
Matt Mowerd5c1a922014-04-15 12:50:58 -0500139 if (tokens.at(0) != md5string) {
140 LOGERR("MD5 does not match\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500141 return -2;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500142 }
143
144 gui_print("MD5 matched\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500145 return 0;
146}