bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1 | /* |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2 | Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 19 | extern "C" |
| 20 | { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 21 | #include "digest/md5.h" |
| 22 | #include "libcrecovery/common.h" |
| 23 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 24 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 25 | #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 bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 36 | #include <sys/mman.h> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 37 | #include "twcommon.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 38 | #include "data.hpp" |
| 39 | #include "variables.h" |
| 40 | #include "twrp-functions.hpp" |
| 41 | #include "twrpDigest.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 42 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 43 | #include "gui/gui.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 44 | |
| 45 | using namespace std; |
| 46 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 47 | void twrpDigest::setfn(const string& fn) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 48 | md5fn = fn; |
| 49 | } |
| 50 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 51 | void twrpDigest::initMD5(void) { |
| 52 | MD5Init(&md5c); |
| 53 | md5string = ""; |
| 54 | } |
| 55 | |
| 56 | int 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 | |
| 66 | void twrpDigest::finalizeMD5stream() { |
| 67 | MD5Final(md5sum, &md5c); |
| 68 | } |
| 69 | |
| 70 | string 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 bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 86 | int twrpDigest::computeMD5(void) { |
| 87 | string line; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 88 | FILE *file; |
| 89 | int len; |
| 90 | unsigned char buf[1024]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 91 | initMD5(); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 92 | file = fopen(md5fn.c_str(), "rb"); |
| 93 | if (file == NULL) |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 94 | return MD5_NOT_FOUND; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 95 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 96 | MD5Update(&md5c, buf, len); |
| 97 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 98 | fclose(file); |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 99 | MD5Final(md5sum, &md5c); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int twrpDigest::write_md5digest(void) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 104 | string md5file, md5str; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 105 | md5file = md5fn + ".md5"; |
| 106 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 107 | md5str = createMD5string(); |
| 108 | TWFunc::write_file(md5file, md5str); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 109 | tw_set_default_metadata(md5file.c_str()); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 110 | LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5str.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int twrpDigest::read_md5digest(void) { |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 115 | size_t i = 0; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 116 | 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 Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 131 | if (!foundMd5File) |
| 132 | return MD5_NOT_FOUND; |
| 133 | if (TWFunc::read_file(md5file, line) != 0) |
| 134 | return MD5_FILE_UNREADABLE; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 135 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int twrpDigest::verify_md5digest(void) { |
| 140 | string buf; |
| 141 | char hex[3]; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 142 | int i, ret; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 143 | string md5str; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 144 | |
| 145 | ret = read_md5digest(); |
| 146 | if (ret != 0) |
| 147 | return ret; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 148 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 149 | 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]); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 155 | md5str += hex; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 156 | } |
that | d1afb52 | 2017-02-23 23:05:59 +0100 | [diff] [blame] | 157 | if (tokens.empty() || tokens.at(0) != md5str) |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 158 | return MD5_MATCH_FAIL; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 159 | |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 160 | return MD5_OK; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 161 | } |