bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1 | /* |
| 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 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 | |
| 47 | void twrpDigest::setfn(string fn) { |
| 48 | md5fn = fn; |
| 49 | } |
| 50 | |
| 51 | int 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 64 | fclose(file); |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 65 | MD5Final(md5sum, &md5c); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int 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 Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 76 | snprintf(hex, 3, "%02x", md5sum[i]); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 77 | md5string += hex; |
| 78 | } |
| 79 | md5string += " "; |
| 80 | md5string += basename((char*) md5fn.c_str()); |
| 81 | md5string += + "\n"; |
| 82 | TWFunc::write_file(md5file, md5string); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 83 | tw_set_default_metadata(md5file.c_str()); |
bigbiff bigbiff | 6b05954 | 2013-09-10 10:28:26 -0400 | [diff] [blame] | 84 | LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int twrpDigest::read_md5digest(void) { |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 89 | size_t i = 0; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 90 | 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 Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 106 | gui_msg("no_md5=Skipping MD5 check: no MD5 file found"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 107 | return -1; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 108 | } else if (TWFunc::read_file(md5file, line) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 109 | LOGERR("Skipping MD5 check: MD5 file unreadable %s\n", strerror(errno)); |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 110 | return 1; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 111 | } |
| 112 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 116 | /* 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 bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 123 | int twrpDigest::verify_md5digest(void) { |
| 124 | string buf; |
| 125 | char hex[3]; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 126 | int i, ret; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 127 | string md5string; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 128 | |
| 129 | ret = read_md5digest(); |
| 130 | if (ret != 0) |
| 131 | return ret; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 132 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 133 | 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 Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 141 | if (tokens.at(0) != md5string) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 142 | gui_err("md5_fail=MD5 does not match"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 143 | return -2; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 146 | gui_msg("md5_match=MD5 matched"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 147 | return 0; |
| 148 | } |